Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GeometricPrimitive.Cone.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 
4 using System;
5 
6 using SiliconStudio.Core.Mathematics;
7 
8 namespace SiliconStudio.Paradox.Graphics
9 {
10  public partial class GeometricPrimitive
11  {
12  /// <summary>
13  /// A cone with a circular base and rolled face.
14  /// </summary>
15  public struct Cone
16  {
17  /// <summary>
18  /// Creates a cone a circular base and a rolled face.
19  /// </summary>
20  /// <param name="device">The device.</param>
21  /// <param name="color">The color of the primitive</param>
22  /// <param name="radius">The radius or the base</param>
23  /// <param name="height">The height of the cone</param>
24  /// <param name="tessellation">The number of segments composing the base</param>
25  /// <param name="toLeftHanded">if set to <c>true</c> vertices and indices will be transformed to left handed. Default is false.</param>
26  /// <returns>A cone.</returns>
27  public static GeometricPrimitive<VertexPositionNormalColor> New(GraphicsDevice device, Color color, float radius = 0.5f, float height = 1.0f, int tessellation = 32, bool toLeftHanded = false)
28  {
29  // Create the primitive object.
30  return new GeometricPrimitive<VertexPositionNormalColor>(device, New(color, radius, height, tessellation, toLeftHanded));
31  }
32 
33  /// <summary>
34  /// Creates a cone a circular base and a rolled face.
35  /// </summary>
36  /// <param name="color">The color of the primitive</param>
37  /// <param name="radius">The radius or the base</param>
38  /// <param name="height">The height of the cone</param>
39  /// <param name="tessellation">The number of segments composing the base</param>
40  /// <param name="toLeftHanded">if set to <c>true</c> vertices and indices will be transformed to left handed. Default is false.</param>
41  /// <returns>A cone.</returns>
42  public static GeometricMeshData<VertexPositionNormalColor> New(Color color, float radius = 0.5f, float height = 1.0f, int tessellation = 32, bool toLeftHanded = false)
43  {
44  var indices = new int[6*tessellation];
45  var vertices = new VertexPositionNormalColor[3 * tessellation + 1];
46 
47  var slopeLength = Math.Sqrt(radius * radius + height * height);
48  var slopeCos = radius / slopeLength;
49  var slopeSin = height / slopeLength;
50 
51  var index = 0;
52  var vertice = 0;
53 
54  // Cone
55  for (int i = 0; i < tessellation; ++i)
56  {
57  var angle = i / (double)tessellation * 2.0 * Math.PI;
58  var angleTop = (i + 0.5) / tessellation * 2.0 * Math.PI;
59 
60  var position = new Vector3(0.0f, (float)Math.Cos(angle) * radius, (float)Math.Sin(angle) * radius);
61  var normal = new Vector3((float)slopeSin, (float)(Math.Cos(angle) * slopeCos), (float)(Math.Sin(angle) * slopeSin));
62  var normalTop = new Vector3((float)slopeSin, (float)(Math.Cos(angleTop) * slopeCos), (float)(Math.Sin(angleTop) * slopeSin));
63 
64  vertices[vertice++] = new VertexPositionNormalColor { Position = new Vector3(height, 0.0f, 0.0f), Normal = normalTop, Color = color };
65  vertices[vertice++] = new VertexPositionNormalColor { Position = position, Normal = normal, Color = color };
66 
67  indices[index++] = i * 2;
68  indices[index++] = (i * 2 + 3) % (tessellation * 2);
69  indices[index++] = i * 2 + 1;
70  }
71 
72  // End cap
73  vertices[vertice++] = new VertexPositionNormalColor { Position = new Vector3(), Normal = -Vector3.UnitX, Color = color };
74  for (int i = 0; i < tessellation; ++i)
75  {
76  var angle = i / (double)tessellation * 2 * Math.PI;
77  var position = new Vector3(0.0f, (float)Math.Cos(angle) * radius, (float)Math.Sin(angle) * radius);
78  vertices[vertice++] = (new VertexPositionNormalColor { Position = position, Normal = -Vector3.UnitX, Color = color });
79 
80  indices[index++] = tessellation * 2;
81  indices[index++] = tessellation * 2 + 1 + i;
82  indices[index++] = tessellation * 2 + 1 + ((i + 1) % tessellation);
83  }
84 
85  return new GeometricMeshData<VertexPositionNormalColor>(vertices, indices, toLeftHanded) { Name = "Cone"};
86  }
87  }
88  }
89 }
A geometric primitive. Use Cube, Cylinder, GeoSphere, Plane, Sphere, Teapot, Torus. See Draw+vertices to learn how to use it.
Describes a custom vertex format structure that contains position, normal and color information...
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
static GeometricPrimitive< VertexPositionNormalColor > New(GraphicsDevice device, Color color, float radius=0.5f, float height=1.0f, int tessellation=32, bool toLeftHanded=false)
Creates a cone a circular base and a rolled face.
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 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
Definition: Color.cs:16
SiliconStudio.Core.Mathematics.Vector3 Vector3
A cone with a circular base and rolled face.
static GeometricMeshData< VertexPositionNormalColor > New(Color color, float radius=0.5f, float height=1.0f, int tessellation=32, bool toLeftHanded=false)
Creates a cone a circular base and a rolled face.