Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GeometricMultiTexcoordPrimitive.Sphere.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 
5 using SiliconStudio.Core.Mathematics;
6 
7 namespace SiliconStudio.Paradox.Graphics
8 {
9  public partial class GeometricMultiTexcoordPrimitive
10  {
11 
12  /// <summary>
13  /// A sphere primitive.
14  /// </summary>
15  public struct Sphere
16  {
17  /// <summary>
18  /// Creates a sphere primitive.
19  /// </summary>
20  /// <param name="device">The device.</param>
21  /// <param name="diameter">The diameter.</param>
22  /// <param name="tessellation">The tessellation.</param>
23  /// <param name="toLeftHanded">if set to <c>true</c> vertices and indices will be transformed to left handed. Default is false.</param>
24  /// <returns>A sphere primitive.</returns>
25  /// <exception cref="System.ArgumentOutOfRangeException">tessellation;Must be >= 3</exception>
26  public static GeometricMultiTexcoordPrimitive New(GraphicsDevice device, float diameter = 1.0f, int tessellation = 16, bool toLeftHanded = false)
27  {
28  return new GeometricMultiTexcoordPrimitive(device, New(diameter, tessellation, toLeftHanded));
29  }
30 
31  /// <summary>
32  /// Creates a sphere primitive.
33  /// </summary>
34  /// <param name="diameter">The diameter.</param>
35  /// <param name="tessellation">The tessellation.</param>
36  /// <param name="toLeftHanded">if set to <c>true</c> vertices and indices will be transformed to left handed. Default is false.</param>
37  /// <returns>A sphere primitive.</returns>
38  /// <exception cref="System.ArgumentOutOfRangeException">tessellation;Must be >= 3</exception>
39  public static GeometricMeshData<VertexPositionNormalTangentMultiTexture> New(float diameter = 1.0f, int tessellation = 16, bool toLeftHanded = false)
40  {
41  if (tessellation < 3) throw new ArgumentOutOfRangeException("tessellation", "Must be >= 3");
42 
43  int verticalSegments = tessellation;
44  int horizontalSegments = tessellation * 2;
45 
46  var vertices = new VertexPositionNormalTangentMultiTexture[(verticalSegments + 1) * (horizontalSegments + 1)];
47  var indices = new int[(verticalSegments) * (horizontalSegments + 1) * 6];
48 
49  float radius = diameter / 2;
50 
51  int vertexCount = 0;
52  // Create rings of vertices at progressively higher latitudes.
53  for (int i = 0; i <= verticalSegments; i++)
54  {
55  float v = 1.0f - (float)i / verticalSegments;
56 
57  var latitude = (float)((i * Math.PI / verticalSegments) - Math.PI / 2.0);
58  var dy = (float)Math.Sin(latitude);
59  var dxz = (float)Math.Cos(latitude);
60 
61  // Create a single ring of vertices at this latitude.
62  for (int j = 0; j <= horizontalSegments; j++)
63  {
64  float u = (float)j / horizontalSegments;
65 
66  var longitude = (float)(j * 2.0 * Math.PI / horizontalSegments);
67  var dx = (float)Math.Sin(longitude);
68  var dz = (float)Math.Cos(longitude);
69 
70  dx *= dxz;
71  dz *= dxz;
72 
73  var normal = new Vector3(dx, dy, dz);
74  var textureCoordinate = new Vector2(u, v);
75 
76  var tangent = new Vector4(normal.Z, 0, -normal.X, 0); // Y ^ normal
77  vertices[vertexCount++] = new VertexPositionNormalTangentMultiTexture(normal * radius, normal, tangent, textureCoordinate);
78  }
79  }
80 
81  // Fill the index buffer with triangles joining each pair of latitude rings.
82  int stride = horizontalSegments + 1;
83 
84  int indexCount = 0;
85  for (int i = 0; i < verticalSegments; i++)
86  {
87  for (int j = 0; j <= horizontalSegments; j++)
88  {
89  int nextI = i + 1;
90  int nextJ = (j + 1) % stride;
91 
92  indices[indexCount++] = (i * stride + j);
93  indices[indexCount++] = (nextI * stride + j);
94  indices[indexCount++] = (i * stride + nextJ);
95 
96  indices[indexCount++] = (i * stride + nextJ);
97  indices[indexCount++] = (nextI * stride + j);
98  indices[indexCount++] = (nextI * stride + nextJ);
99  }
100  }
101 
102  // Create the primitive object.
103  // Create the primitive object.
104  return new GeometricMeshData<VertexPositionNormalTangentMultiTexture>(vertices, indices, toLeftHanded) { Name = "Sphere" };
105  }
106  }
107  }
108 }
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
static GeometricMultiTexcoordPrimitive New(GraphicsDevice device, float diameter=1.0f, int tessellation=16, bool toLeftHanded=false)
Creates a sphere primitive.
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...
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, int tessellation=16, bool toLeftHanded=false)
Creates a sphere primitive.
SiliconStudio.Core.Mathematics.Vector3 Vector3