GteMeshFactory.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.2 (2016/08/29)
7 
8 #pragma once
9 
10 #include <Mathematics/GteMesh.h>
13 #include <Graphics/GteVisual.h>
14 
15 namespace gte
16 {
17 // This class is a factory for Visual objects corresponding to common
18 // geometric primitives. Triangle mesh primitives are generated. Each mesh
19 // is centered at (0,0,0) and has an up-axis of (0,0,1). The other axes
20 // forming the coordinate system are (1,0,0) and (0,1,0).
21 //
22 // The factory always generates 3-tuple positions. If normals, tangents, or
23 // binormals are requested, they are also generated as 3-tuples. They are
24 // stored in the vertex buffer as 3-tuples or 4-tuples as requested (w = 1 for
25 // positions, w = 0 for the others). The factory also generates 2-tuple
26 // texture coordinates. These are stored in the vertex buffer for 2-tuple
27 // units. All other attribute types are unassigned by the factory.
28 
30 {
31 public:
32  // Construction and destruction.
33  ~MeshFactory();
34  MeshFactory();
35 
36  // Specify the vertex format.
37  inline void SetVertexFormat(VertexFormat const& format);
38 
39  // Specify the usage for the vertex buffer data. The default is
40  // Resource::IMMUTABLE.
41  inline void SetVertexBufferUsage(Resource::Usage usage);
42 
43  // Specify the type of indices and where the index buffer data should be
44  // stored. For 'unsigned int' indices, set 'use32Bit' to 'true'; for
45  // 'unsigned short' indices, set 'use32Bit' to false. The default is
46  // 'unsigned int'.
47  inline void SetIndexFormat(bool use32Bit);
48 
49  // Specify the usage for the index buffer data. The default is
50  // Resource::IMMUTABLE.
51  inline void SetIndexBufferUsage(Resource::Usage usage);
52 
53  // For the geometric primitives that have an inside and an outside, you
54  // may specify where the observer is expected to see the object. If the
55  // observer must see the primitive from the outside, pass 'true' to this
56  // function. If the observer must see the primitive from the inside, pass
57  // 'false'. This Boolean flag simply controls the triangle face order
58  // for face culling. The default is 'true' (observer view object from the
59  // outside).
60  inline void SetOutside(bool outside);
61 
62 
63  // The rectangle is in the plane z = 0 and is visible to an observer who
64  // is on the side of the plane to which the normal (0,0,1) points. It has
65  // corners (-xExtent, -yExtent, 0), (+xExtent, -yExtent, 0),
66  // (-xExtent, +yExtent, 0), and (+xExtent, +yExtent, 0). The mesh has
67  // numXSamples vertices in the x-direction and numYSamples vertices in the
68  // y-direction for a total of numXSamples*numYSamples vertices.
69  std::shared_ptr<Visual> CreateRectangle(unsigned int numXSamples,
70  unsigned int numYSamples, float xExtent, float yExtent);
71 
72  // The triangle is in the plane z = 0 and is visible to an observer who is
73  // on the side of the plane to which the normal (0,0,1) points. It has
74  // vertices (0, 0, 0), (xExtent, 0, 0), and (0, yExtent, 0). The mesh
75  // has numSamples vertices along each of the x- and y-axes for a total
76  // of numSamples*(numSamples+1)/2 vertices.
77  std::shared_ptr<Visual> CreateTriangle(unsigned int numSamples,
78  float xExtent, float yExtent);
79 
80  // The circular disk is in the plane z = 0 and is visible to an observer
81  // who is on the side of the plane to which the normal (0,0,1) points.
82  // It has center (0,0,0) and the specified radius. The mesh has its
83  // first vertex at the center. Samples are placed along rays whose
84  // common origin is the center. There are numRadialSamples rays. Along
85  // each ray the mesh has numShellSamples vertices.
86  std::shared_ptr<Visual> CreateDisk(unsigned int numShellSamples,
87  unsigned int numRadialSamples, float radius);
88 
89  // The box has center (0,0,0); unit-length axes (1,0,0), (0,1,0), and
90  // (0,0,1); and extents (half-lengths) xExtent, yExtent, and zExtent. The
91  // mesh has 8 vertices and 12 triangles. For example, the box corner in
92  // the first octant is (xExtent, yExtent, zExtent).
93  std::shared_ptr<Visual> CreateBox(float xExtent, float yExtent,
94  float zExtent);
95 
96  // The cylinder has center (0,0,0), the specified radius, and the
97  // specified height. The cylinder axis is a line segment of the form
98  // (0,0,0) + t*(0,0,1) for |t| <= height/2. The cylinder wall is
99  // implicitly defined by x^2+y^2 = radius^2. CreateCylinderOpen leads
100  // to a cylinder whose end-disks are omitted; you have an open tube.
101  // CreateCylinderClosed leads to a cylinder with end-disks. Each end-disk
102  // is a regular polygon that is tessellated by including a vertex at the
103  // center of the polygon and decomposing the polygon into triangles that
104  // all share the center vertex and each triangle containing an edge of the
105  // polygon.
106  std::shared_ptr<Visual> CreateCylinderOpen(unsigned int numAxisSamples,
107  unsigned int numRadialSamples, float radius, float height);
108  std::shared_ptr<Visual> CreateCylinderClosed(unsigned int numAxisSamples,
109  unsigned int numRadialSamples, float radius, float height);
110 
111  // The sphere has center (0,0,0) and the specified radius. The north pole
112  // is at (0,0,radius) and the south pole is at (0,0,-radius). The mesh has
113  // the topology of an open cylinder (which is also the topology of a
114  // rectangle with wrap-around for one pair of parallel edges) and is then
115  // stitched to the north and south poles. The triangles are unevenly
116  // distributed. If you want a more even distribution, create an
117  // icosahedron and subdivide it.
118  std::shared_ptr<Visual> CreateSphere(unsigned int numZSamples,
119  unsigned int numRadialSamples, float radius);
120 
121  // The torus has center (0,0,0). If you observe the torus along the
122  // line with direction (0,0,1), you will see an annulus. The circle
123  // that is the center of the annulus has radius 'outerRadius'. The
124  // distance from this circle to the boundaries of the annulus is the
125  // 'inner radius'.
126  std::shared_ptr<Visual> CreateTorus(unsigned int numCircleSamples,
127  unsigned int numRadialSamples, float outerRadius, float innerRadius);
128 
129  // Platonic solids, all inscribed in a unit sphere centered at (0,0,0).
130  std::shared_ptr<Visual> CreateTetrahedron();
131  std::shared_ptr<Visual> CreateHexahedron();
132  std::shared_ptr<Visual> CreateOctahedron();
133  std::shared_ptr<Visual> CreateDodecahedron();
134  std::shared_ptr<Visual> CreateIcosahedron();
135 
136 private:
137  // Support for creating vertex and index buffers.
138  std::shared_ptr<VertexBuffer> CreateVBuffer(unsigned int numVertices);
139  std::shared_ptr<IndexBuffer> CreateIBuffer(unsigned int numTriangles);
140 
141  // Support for vertex buffers.
142  char* GetGeometricChannel(std::shared_ptr<VertexBuffer> const& vbuffer,
143  VASemantic semantic, float w);
144  inline Vector3<float>& Position(unsigned int i);
145  inline Vector3<float>& Normal(unsigned int i);
146  inline Vector3<float>& Tangent(unsigned int i);
147  inline Vector3<float>& Bitangent(unsigned int i);
148  inline Vector2<float>& TCoord(unsigned int unit, unsigned int i);
149  void SetPosition(unsigned int i, Vector3<float> const& pos);
150  void SetNormal(unsigned int i, Vector3<float> const& nor);
151  void SetTangent(unsigned int i, Vector3<float> const& tan);
152  void SetBinormal(unsigned int i, Vector3<float> const& bin);
153  void SetTCoord(unsigned int i, Vector2<float> const& tcd);
154  void SetPlatonicTCoord(unsigned int i, Vector3<float> const& pos);
155 
156  // Support for index buffers.
157  void ReverseTriangleOrder(IndexBuffer* ibuffer);
158 
160  size_t mIndexSize;
161  Resource::Usage mVBUsage, mIBUsage;
162  bool mOutside;
163  bool mAssignTCoords[VA_MAX_TCOORD_UNITS];
164 
165  char* mPositions;
166  char* mNormals;
167  char* mTangents;
168  char* mBitangents;
169  char* mTCoords[VA_MAX_TCOORD_UNITS];
170 };
171 
172 
174 {
175  mVFormat = format;
176 }
177 
178 inline void MeshFactory::SetVertexBufferUsage(Resource::Usage usage)
179 {
180  mVBUsage = usage;
181 }
182 
183 inline void MeshFactory::SetIndexFormat(bool use32Bit)
184 {
185  mIndexSize = (use32Bit ? sizeof(unsigned int) : sizeof(unsigned short));
186 }
187 
188 inline void MeshFactory::SetIndexBufferUsage(Resource::Usage usage)
189 {
190  mIBUsage = usage;
191 }
192 
193 inline void MeshFactory::SetOutside(bool outside)
194 {
195  mOutside = outside;
196 }
197 
198 inline Vector3<float>& MeshFactory::Position(unsigned int i)
199 {
200  return *reinterpret_cast<Vector3<float>*>(
201  mPositions + i*mVFormat.GetVertexSize());
202 }
203 
204 inline Vector3<float>& MeshFactory::Normal(unsigned int i)
205 {
206  return *reinterpret_cast<Vector3<float>*>(
207  mNormals + i*mVFormat.GetVertexSize());
208 }
209 
210 inline Vector3<float>& MeshFactory::Tangent(unsigned int i)
211 {
212  return *reinterpret_cast<Vector3<float>*>(
213  mTangents + i*mVFormat.GetVertexSize());
214 }
215 
216 inline Vector3<float>& MeshFactory::Bitangent(unsigned int i)
217 {
218  return *reinterpret_cast<Vector3<float>*>(
219  mBitangents + i*mVFormat.GetVertexSize());
220 }
221 
222 inline Vector2<float>& MeshFactory::TCoord(unsigned int unit, unsigned int i)
223 {
224  return *reinterpret_cast<Vector2<float>*>(
225  mTCoords[unit] + i*mVFormat.GetVertexSize());
226 }
227 
228 }
void SetVertexBufferUsage(Resource::Usage usage)
void SetVertexFormat(VertexFormat const &format)
Vector3< float > & Normal(unsigned int i)
Vector3< float > & Bitangent(unsigned int i)
VA_MAX_TCOORD_UNITS
Vector3< float > & Position(unsigned int i)
Vector2< float > & TCoord(unsigned int unit, unsigned int i)
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:852
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
Vector3< float > & Tangent(unsigned int i)
void SetIndexFormat(bool use32Bit)
void SetOutside(bool outside)
VertexFormat mVFormat
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:103
GLint GLsizei GLsizei height
Definition: glcorearb.h:98
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:659
void SetIndexBufferUsage(Resource::Usage usage)
#define GTE_IMPEXP
Definition: GTEngineDEF.h:63
Resource::Usage mVBUsage


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01