Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GeometricPrimitive.Plane.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 // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 
24 using System;
25 using SiliconStudio.Core.Mathematics;
26 
27 namespace SiliconStudio.Paradox.Graphics
28 {
29  public partial class GeometricPrimitive
30  {
31  /// <summary>
32  /// A plane primitive.
33  /// </summary>
34  public struct Plane
35  {
36  /// <summary>
37  /// Creates a Plane primitive on the X/Y plane with a normal equal to -<see cref="Vector3.UnitZ"/>.
38  /// </summary>
39  /// <param name="device">The device.</param>
40  /// <param name="sizeX">The size X.</param>
41  /// <param name="sizeY">The size Y.</param>
42  /// <param name="tessellation">The tessellation, as the number of quads per axis.</param>
43  /// <param name="toLeftHanded">if set to <c>true</c> vertices and indices will be transformed to left handed. Default is false.</param>
44  /// <param name="uvFactors">Scale UVs between 0 and the values of this parameter.</param>
45  /// <returns>A Plane primitive.</returns>
46  /// <exception cref="System.ArgumentOutOfRangeException">tessellation;tessellation must be > 0</exception>
47  public static GeometricPrimitive New(GraphicsDevice device, float sizeX = 1.0f, float sizeY = 1.0f, int tessellation = 1, bool toLeftHanded = false, Vector2? uvFactors = null)
48  {
49  return new GeometricPrimitive(device, New(sizeX, sizeY, tessellation, toLeftHanded, uvFactors));
50  }
51 
52  /// <summary>
53  /// Creates a Plane primitive on the X/Y plane with a normal equal to -<see cref="Vector3.UnitZ"/>.
54  /// </summary>
55  /// <param name="sizeX">The size X.</param>
56  /// <param name="sizeY">The size Y.</param>
57  /// <param name="tessellation">The tessellation, as the number of quads per axis.</param>
58  /// <param name="toLeftHanded">if set to <c>true</c> vertices and indices will be transformed to left handed. Default is false.</param>
59  /// <param name="uvFactors">Scale UVs between 0 and the values of this parameter.</param>
60  /// <returns>A Plane primitive.</returns>
61  /// <exception cref="System.ArgumentOutOfRangeException">tessellation;tessellation must be > 0</exception>
62  public static GeometricMeshData<VertexPositionNormalTexture> New(float sizeX = 1.0f, float sizeY = 1.0f, int tessellation = 1, bool toLeftHanded = false, Vector2? uvFactors = null, bool generateBackFace = false)
63  {
64  if (tessellation < 1)
65  {
66  throw new ArgumentOutOfRangeException("tessellation", "tessellation must be > 0");
67  }
68 
69  var lineWidth = tessellation + 1;
70  var vertices = new VertexPositionNormalTexture[lineWidth * lineWidth];
71  var indices = new int[tessellation * tessellation * 6 * (generateBackFace? 2: 1)];
72 
73  var deltaX = sizeX / tessellation;
74  var deltaY = sizeY / tessellation;
75 
76  sizeX /= 2.0f;
77  sizeY /= 2.0f;
78 
79  int vertexCount = 0;
80  int indexCount = 0;
81  var normal = Vector3.UnitZ;
82 
83  var uv = uvFactors.HasValue ? uvFactors.Value : new Vector2(1, 1);
84 
85  // Create vertices
86  for (int y = 0; y < (tessellation + 1); y++)
87  {
88  for (int x = 0; x < (tessellation + 1); x++)
89  {
90  var position = new Vector3(-sizeX + deltaX * x, sizeY - deltaY * y, 0);
91  var texCoord = new Vector2(uv.X * x / tessellation, uv.Y * y / tessellation);
92  vertices[vertexCount++] = new VertexPositionNormalTexture(position, normal, texCoord);
93  }
94  }
95 
96  // Create indices
97  for (int y = 0; y < tessellation; y++)
98  {
99  for (int x = 0; x < tessellation; x++)
100  {
101  // Six indices (two triangles) per face.
102  int vbase = lineWidth * y + x;
103  indices[indexCount++] = (vbase + 1);
104  indices[indexCount++] = (vbase + 1 + lineWidth);
105  indices[indexCount++] = (vbase + lineWidth);
106 
107  indices[indexCount++] = (vbase + 1);
108  indices[indexCount++] = (vbase + lineWidth);
109  indices[indexCount++] = (vbase);
110  }
111  }
112  if(generateBackFace)
113  {
114  // Create indices
115  for (int y = 0; y < tessellation; y++)
116  {
117  for (int x = 0; x < tessellation; x++)
118  {
119  // Six indices (two triangles) per face.
120  int vbase = lineWidth * y + x;
121  indices[indexCount++] = (vbase + 1);
122  indices[indexCount++] = (vbase + lineWidth);
123  indices[indexCount++] = (vbase + 1 + lineWidth);
124 
125  indices[indexCount++] = (vbase + 1);
126  indices[indexCount++] = (vbase);
127  indices[indexCount++] = (vbase + lineWidth);
128  }
129  }
130  }
131 
132  // Create the primitive object.
133  return new GeometricMeshData<VertexPositionNormalTexture>(vertices, indices, toLeftHanded) { Name = "Plane" };
134  }
135  }
136  }
137 }
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
static GeometricMeshData< VertexPositionNormalTexture > New(float sizeX=1.0f, float sizeY=1.0f, int tessellation=1, bool toLeftHanded=false, Vector2?uvFactors=null, bool generateBackFace=false)
Creates a Plane primitive on the X/Y plane with a normal equal to -Vector3.UnitZ. ...
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 texture information...
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.
static GeometricPrimitive New(GraphicsDevice device, float sizeX=1.0f, float sizeY=1.0f, int tessellation=1, bool toLeftHanded=false, Vector2?uvFactors=null)
Creates a Plane primitive on the X/Y plane with a normal equal to -Vector3.UnitZ. ...
SiliconStudio.Core.Mathematics.Vector3 Vector3