Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GeometricMultiTexcoordPrimitive.Cube.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 SiliconStudio.Core.Mathematics;
4 
5 namespace SiliconStudio.Paradox.Graphics
6 {
7  public partial class GeometricMultiTexcoordPrimitive
8  {
9  /// <summary>
10  /// A cube has six faces, each one pointing in a different direction.
11  /// </summary>
12  public struct Cube
13  {
14  private const int CubeFaceCount = 6;
15 
16  private static readonly Vector3[] faceNormals =
17  {
18  new Vector3(0, 0, 1),
19  new Vector3(0, 0, -1),
20  new Vector3(1, 0, 0),
21  new Vector3(-1, 0, 0),
22  new Vector3(0, 1, 0),
23  new Vector3(0, -1, 0)
24  };
25 
26  private static readonly Vector2[] textureCoordinates =
27  {
28  new Vector2(1, 0),
29  new Vector2(1, 1),
30  new Vector2(0, 1),
31  new Vector2(0, 0)
32  };
33 
34  /// <summary>
35  /// Creates a cube with six faces each one pointing in a different direction.
36  /// </summary>
37  /// <param name="device">The device.</param>
38  /// <param name="size">The size.</param>
39  /// <param name="toLeftHanded">if set to <c>true</c> vertices and indices will be transformed to left handed. Default is false.</param>
40  /// <returns>A cube.</returns>
41  public static GeometricMultiTexcoordPrimitive New(GraphicsDevice device, float size = 1.0f, bool toLeftHanded = false)
42  {
43  // Create the primitive object.
44  return new GeometricMultiTexcoordPrimitive(device, New(size, toLeftHanded));
45  }
46 
47  /// <summary>
48  /// Creates a cube with six faces each one pointing in a different direction.
49  /// </summary>
50  /// <param name="size">The size.</param>
51  /// <param name="toLeftHanded">if set to <c>true</c> vertices and indices will be transformed to left handed. Default is false.</param>
52  /// <returns>A cube.</returns>
53  public static GeometricMeshData<VertexPositionNormalTangentMultiTexture> New(float size = 1.0f, bool toLeftHanded = false)
54  {
55  var vertices = new VertexPositionNormalTangentMultiTexture[CubeFaceCount * 4];
56  var indices = new int[CubeFaceCount * 6];
57 
58  size /= 2.0f;
59 
60  int vertexCount = 0;
61  int indexCount = 0;
62  // Create each face in turn.
63  for (int i = 0; i < CubeFaceCount; i++)
64  {
65  Vector3 normal = faceNormals[i];
66 
67  // Get two vectors perpendicular both to the face normal and to each other.
68  Vector3 basis = (i >= 4) ? Vector3.UnitZ : Vector3.UnitY;
69 
70  Vector3 side1;
71  Vector3.Cross(ref normal, ref basis, out side1);
72 
73  Vector3 side2;
74  Vector3.Cross(ref normal, ref side1, out side2);
75 
76  // Six indices (two triangles) per face.
77  int vbase = i * 4;
78  indices[indexCount++] = (vbase + 0);
79  indices[indexCount++] = (vbase + 1);
80  indices[indexCount++] = (vbase + 2);
81 
82  indices[indexCount++] = (vbase + 0);
83  indices[indexCount++] = (vbase + 2);
84  indices[indexCount++] = (vbase + 3);
85 
86  // Four vertices per face.
87  vertices[vertexCount++] = new VertexPositionNormalTangentMultiTexture((normal - side1 - side2) * size, normal, new Vector4(-side1, 0), textureCoordinates[0]);
88  vertices[vertexCount++] = new VertexPositionNormalTangentMultiTexture((normal - side1 + side2) * size, normal, new Vector4(-side1, 0), textureCoordinates[1]);
89  vertices[vertexCount++] = new VertexPositionNormalTangentMultiTexture((normal + side1 + side2) * size, normal, new Vector4(-side1, 0), textureCoordinates[2]);
90  vertices[vertexCount++] = new VertexPositionNormalTangentMultiTexture((normal + side1 - side2) * size, normal, new Vector4(-side1, 0), textureCoordinates[3]);
91  }
92 
93  // Create the primitive object.
94  return new GeometricMeshData<VertexPositionNormalTangentMultiTexture>(vertices, indices, toLeftHanded) { Name = "Cube" };
95  }
96  }
97  }
98 }
static void Cross(ref Vector3 left, ref Vector3 right, out Vector3 result)
Calculates the cross product of two vectors.
Definition: Vector3.cs:476
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
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 GeometricMeshData< VertexPositionNormalTangentMultiTexture > New(float size=1.0f, bool toLeftHanded=false)
Creates a cube with six faces each one pointing in a different direction.
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
static readonly Vector3 UnitZ
The Z unit SiliconStudio.Core.Mathematics.Vector3 (0, 0, 1).
Definition: Vector3.cs:67
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
A cube has six faces, each one pointing in a different direction.
static readonly Vector3 UnitY
The Y unit SiliconStudio.Core.Mathematics.Vector3 (0, 1, 0).
Definition: Vector3.cs:62
static GeometricMultiTexcoordPrimitive New(GraphicsDevice device, float size=1.0f, bool toLeftHanded=false)
Creates a cube with six faces each one pointing in a different direction.
SiliconStudio.Core.Mathematics.Vector3 Vector3
_In_ size_t _In_ size_t size
Definition: DirectXTexP.h:175