Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GeometricMultiTexcoordPrimitive.Torus.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 
6 using SiliconStudio.Core.Mathematics;
7 
8 namespace SiliconStudio.Paradox.Graphics
9 {
10  public partial class GeometricMultiTexcoordPrimitive
11  {
12  /// <summary>
13  /// A Torus primitive.
14  /// </summary>
15  public struct Torus
16  {
17  /// <summary>
18  /// Creates a torus primitive.
19  /// </summary>
20  /// <param name="device">The device.</param>
21  /// <param name="diameter">The diameter.</param>
22  /// <param name="thickness">The thickness.</param>
23  /// <param name="tessellation">The tessellation.</param>
24  /// <param name="toLeftHanded">if set to <c>true</c> vertices and indices will be transformed to left handed. Default is false.</param>
25  /// <returns>A Torus primitive.</returns>
26  /// <exception cref="System.ArgumentOutOfRangeException">tessellation;tessellation parameter out of range</exception>
27  public static GeometricMultiTexcoordPrimitive New(GraphicsDevice device, float diameter = 1.0f, float thickness = 0.33333f, int tessellation = 32, bool toLeftHanded = false)
28  {
29  return new GeometricMultiTexcoordPrimitive(device, New(diameter, thickness, tessellation, toLeftHanded));
30  }
31 
32  /// <summary>
33  /// Creates a torus primitive.
34  /// </summary>
35  /// <param name="diameter">The diameter.</param>
36  /// <param name="thickness">The thickness.</param>
37  /// <param name="tessellation">The tessellation.</param>
38  /// <param name="toLeftHanded">if set to <c>true</c> vertices and indices will be transformed to left handed. Default is false.</param>
39  /// <returns>A Torus primitive.</returns>
40  /// <exception cref="System.ArgumentOutOfRangeException">tessellation;tessellation parameter out of range</exception>
41  public static GeometricMeshData<VertexPositionNormalTangentMultiTexture> New(float diameter = 1.0f, float thickness = 0.33333f, int tessellation = 32, bool toLeftHanded = false)
42  {
43  var vertices = new List<VertexPositionNormalTangentMultiTexture>();
44  var indices = new List<int>();
45 
46  if (tessellation < 3)
47  throw new ArgumentOutOfRangeException("tessellation");
48 
49  int stride = tessellation + 1;
50 
51  // First we loop around the main ring of the torus.
52  for (int i = 0; i <= tessellation; i++)
53  {
54  float u = (float)i / tessellation;
55 
56  float outerAngle = i * MathUtil.TwoPi / tessellation - MathUtil.PiOverTwo;
57 
58  // Create a transform matrix that will align geometry to
59  // slice perpendicularly though the current ring position.
60  var transform = Matrix.Translation(diameter / 2, 0, 0) * Matrix.RotationY(outerAngle);
61 
62  // Now we loop along the other axis, around the side of the tube.
63  for (int j = 0; j <= tessellation; j++)
64  {
65  float v = 1 - (float)j / tessellation;
66 
67  float innerAngle = j * MathUtil.TwoPi / tessellation + MathUtil.Pi;
68  float dx = (float)Math.Cos(innerAngle), dy = (float)Math.Sin(innerAngle);
69 
70  // Create a vertex.
71  var normal = new Vector3(dx, dy, 0);
72  var position = normal * thickness / 2;
73  var textureCoordinate = new Vector2(u, v);
74 
75  Vector3.TransformCoordinate(ref position, ref transform, out position);
76  Vector3.TransformNormal(ref normal, ref transform, out normal);
77 
78  var tangent = new Vector4((float)Math.Sin(outerAngle), 0, -(float)Math.Cos(outerAngle), 0); // Y ^ (cos, 0, sin)
79  vertices.Add(new VertexPositionNormalTangentMultiTexture(position, normal, tangent, textureCoordinate));
80 
81  // And create indices for two triangles.
82  int nextI = (i + 1) % stride;
83  int nextJ = (j + 1) % stride;
84 
85  indices.Add(i * stride + j);
86  indices.Add(i * stride + nextJ);
87  indices.Add(nextI * stride + j);
88 
89  indices.Add(i * stride + nextJ);
90  indices.Add(nextI * stride + nextJ);
91  indices.Add(nextI * stride + j);
92  }
93  }
94 
95  // Create the primitive object.
96  return new GeometricMeshData<VertexPositionNormalTangentMultiTexture>(vertices.ToArray(), indices.ToArray(), toLeftHanded) { Name = "Torus" };
97  }
98  }
99  }
100 }
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
A geometric primitive. Use Sphere to learn how to use it.
Describes a custom vertex format structure that contains position, color and 10 texture coordinates i...
static void RotationY(float angle, out Matrix result)
Creates a matrix that rotates around the y-axis.
Definition: Matrix.cs:2500
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
static GeometricMeshData< VertexPositionNormalTangentMultiTexture > New(float diameter=1.0f, float thickness=0.33333f, int tessellation=32, bool toLeftHanded=false)
Creates a torus primitive.
SiliconStudio.Core.Mathematics.Vector3 Vector3
static GeometricMultiTexcoordPrimitive New(GraphicsDevice device, float diameter=1.0f, float thickness=0.33333f, int tessellation=32, bool toLeftHanded=false)
Creates a torus primitive.
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47