GL Studio C++ Runtime API
gltrimesh.h
Go to the documentation of this file.
1 /*! \file
2  \brief The disti::GLTriMesh class. Implements Triangle Meshes.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2017 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 
41 #ifndef _GL_TRI_MESH_H
42 #define _GL_TRI_MESH_H
43 
44 #include "display.h"
45 #include "gls_cpp_lang_support.h"
46 #ifndef GLES
47 # include "gls_display_list.h"
48 #endif
49 #include "disti_metadata.h"
50 #include "glpolygon.h"
51 #ifdef GLES
52 # include "dynamic_array.h"
53 # include "gls_index_array.h"
54 #endif
55 
56 namespace disti
57 {
58 // Forward Declaration
59 class GlsGloFileAttribute;
60 
61 // SetValue enumerations
62 enum
63 {
64  GLS_TRIMESH_FIRST_VALUE = GLS_LAST_INITIALIZER + 1,
65  GLS_TRIMESH_USE_DISPLAY_LIST, // whether or not to use a display list
66  GLS_TRIMESH_FACES
67 };
68 
69 /**
70  \brief Structure for holding mesh vertex indices
71  */
72 typedef struct
73 {
74  int a, b, c; /** Vertex indices of the face */
75  int texture; /** Texture index of face */
76  unsigned int material; /** Material index of face */
77 } GLMeshFace;
78 
79 /** The GLMeshVertex structure. Defines all of the attributes of a TriMesh vertex. Used in generated code.
80  */
81 typedef struct
82 {
83  float vertex[ 3 ]; /**< The vertex coordinate */
84  unsigned char color[ 4 ]; /**< The vertex color */
85  float texCoord[ 2 ]; /**< The texture coordinate */
86  float normal[ 3 ]; /**< The vertex normal */
87 } GLMeshVertex;
88 
89 /**
90  \brief The glTriMesh class. Implements Triangle Meshes
91 */
92 class GLTriMesh : public GLPolygon
93 {
94 protected:
95  void DrawSolidMesh( unsigned int faceOffset, unsigned int faceCount, bool textureObject, bool gouraudShading, bool perVertexLighting );
96  void DrawOutlineMesh();
97  //void DrawPointsMesh();
98 
99 public:
100  typedef GLPolygon _BaseClass;
101  friend class GLTriMeshEditor;
102 
103 #ifdef GLES
104  GlsIndexArray _indices;
105 #else
106  /** Stores object in display list for faster drawing */
108 #endif
109 
110  /* Override base class since a mesh can have multiple textures */
111  GLS_EXPORT int TextureIndex( void );
112 
113  /* Override base class since a mesh can have multiple textures */
114  GLS_EXPORT void TextureIndex( int index );
115 
116  /* Override base class since a mesh can have multiple materials */
117  GLS_EXPORT void MaterialIndices( DynamicArray<int> index );
118 
119  /** Gets a vector of material indices. */
120  GLS_EXPORT DynamicArray<int>& MaterialIndices();
121 
122  /* Override base class since a mesh can have multiple materials */
123  GLS_EXPORT void MaterialIndex( unsigned int index );
124 
125  /* Override base class since a mesh can have multiple materials */
126  GLS_EXPORT int MaterialIndex();
127 
128  /** Provides access to the faces stored in this mesh */
129  GLS_EXPORT GLMeshFace* Faces( void ) { return _faces; }
130 
131  /* See base class */
132  virtual GLS_EXPORT void CopyGeometry( DisplayObject* src );
133 
134  /* See base class */
135  virtual GLS_EXPORT DisplayObject* CloneObject( bool generateNames = false );
136 
137  /* See base class */
138  virtual GLS_EXPORT void CopyProperties( DisplayObject* src );
139 
140  /* See base class*/
141  virtual GLS_EXPORT void SetValue( int spec, va_list& args );
142 
143  /* Copies the attributes from an object (possibly a group) to this one. Used by the
144  * mesh creator to copy properties from the objectrs that were converted into this mesh.
145  * \param src The object to copy properties from */
146  GLS_EXPORT void AssignPropertiesFromGroup( DisplayObject* src );
147 
148  /** Sets the number of faces in the mesh and allocates the face array
149  * \param n The new number of faces for the mesh
150  */
151  GLS_EXPORT void NumberOfFaces( unsigned int n );
152 
153  /** \return The number of faces in this mesh */
154  GLS_EXPORT unsigned int NumberOfFaces( void ) { return _nFaces; }
155 
156  /** For all faces of the mesh that have the old material, replace that material
157  * with the new material.
158  * \param oldIndex Old material index
159  * \param newIndex New material index
160  */
161  GLS_EXPORT void ReplaceMaterial( unsigned int oldIndex, unsigned int newIndex );
162 
163  /** Sets the vertices and texture index of a specific face
164  * \param face
165  * \param a The vertex index for the first vertex of the triangle
166  * \param b The vertex index for the second vertex of the triangle
167  * \param c The vertex index for the third vertex of the triangle
168  * \param tindex The texture index for triangle
169  * \param materialIndex The material index for triangle
170  */
171  GLS_EXPORT void SetFace( unsigned int face, int a, int b, int c, int tindex, unsigned int materialIndex = 0 );
172 
173  /** Sets all the faces of the mesh. Used in runtime.
174  * \param numFaces The number of faces that will be passed in
175  * \param ... A variable argument field which will be numFaces instances of a,b,c,tindex,materialIndex
176  */
177  GLS_EXPORT void VaSetFaces( unsigned int numFaces, ... );
178 
179  /** Sets all the faces of the mesh. Used in parser
180  * \param nFaces The number of faces that will be set
181  * \param faces An array of GLMeshFaces with nFaces elements
182  */
183  GLS_EXPORT void SetFaces( unsigned int nFaces, GLMeshFace* faces );
184 
185  /** Sets all the faces and vertices of the mesh. Used in generated code
186  * \param nVertices Number of vertices
187  * \param vertices Array of initialized vertices
188  * \param nFaces Number of faces
189  * \param faces Array of initialized faces
190  */
191  GLS_EXPORT void SetVerticesAndFaces( unsigned int nVertices, const GLMeshVertex* vertices, unsigned int nFaces, const GLMeshFace* faces );
192 
193  /** Creates a GLTriMesh with the vertices specified
194  * \param nVertices The number of vertices in the new polygon
195  * \param vertices The array of vertices for the polygon
196  */
197  GLS_EXPORT GLTriMesh( unsigned int nVertices, Vertex* vertices );
198 
199  /** Creates a GLTriMesh with the vertices specified
200  * \param nVertices The number of vertices in the new polygon
201  * \param vertices The array of vertices for the polygon
202  * \param location The location point of the object
203  */
204  GLS_EXPORT GLTriMesh( unsigned int nVertices, Vertex* vertices, Vertex& location );
205 
206  /** Allocate a (blank) GLTriMesh object */
207  GLS_EXPORT GLTriMesh( void );
208 
209  GLS_EXPORT GLTriMesh( const GLTriMesh& that, const bool generateNames );
210 
211  /** Destroy a GLTriMesh object */
212  virtual GLS_EXPORT ~GLTriMesh( void );
213 
214  static GLS_EXPORT DisplayObject* CreateInstance();
215 
216  virtual GLS_EXPORT void SetAvailableAttributes( unsigned int value );
217 
218 #ifdef GLES
219  /* See base class */
220  virtual GLS_EXPORT void PreDraw( const OpenGLMatrices& current, Culler& culler );
221 #endif
222 
223  /* See base class */
224  virtual GLS_EXPORT void Draw( void );
225 
226  /* See base class */
227  virtual GLS_EXPORT bool Hit( float x, float y, float z, float scale, const Vector& directionVector, Vector* collisionPoint );
228 
229 #ifndef GLES
230  /* See base class */
231  virtual GLS_EXPORT InterfaceListType* GetCppInterfaceDescription( InterfaceListType* addToThisList = NULL );
232 
233  /* See base class */
234  virtual GLS_EXPORT void GetCppInterfaceDescriptionFree( InterfaceListType* array );
235 
236  virtual GLS_EXPORT void UseDisplayList( const bool& flag );
237  virtual GLS_EXPORT bool UseDisplayList( void );
238 #endif
239 
240  /** Causes the display list for this object to be recomputed */
241  GLS_EXPORT void InvalidateGeometry( void )
242  {
243 #ifndef GLES
244  _displayList.Invalidate();
245 #endif
246  }
247 
248 #ifdef GLES
249  GLS_EXPORT void DirtyTextureData( void );
250 #endif
251 
252 protected:
253 #ifndef GLES
254  /** Contains the actual OpenGL drawing commands to draw the object */
255  virtual GLS_EXPORT void DrawGeometry( void );
256 #endif
257  /* A mesh can have multiple textures */
258  virtual GLS_EXPORT bool SetupTexture( const int textureIndex );
259 
260 #ifdef GLES
261  /** Set a single attribute from the GLO file.
262  * \param data The attribute to set and its associated data.
263  */
264  virtual GLS_EXPORT void SetFromGloData( GlsGloFileAttribute& data );
265 #endif
266 
267 private:
268  unsigned int _nFaces; /** Number of faces (triangles) in this mesh */
269 
270  GLMeshFace* _faces; /** Array of vertex indices for triangles */
271 
272  DynamicArray<int> _newMaterialIndices; /** Intermediate data storage to make the setting of new material indices through the Faces simpler. */
273 
274 #ifdef GLES
275  /* Resizable array of indices into the faces array where the active texture is different from
276  * the previous face.
277  */
278  DynamicArray<unsigned int> _textureChangeIndices;
279  bool _textureChangesDirty; /** true if _textureSwitchIndices needs to be recomputed */
280 
281  /* Utility routine used by Draw. Should only be called when _textureSwitchDirty is true.
282  * Rebuilds the _textureSwitchIndices array by scanning the data in the face array
283  * and taking note when the texture index changes between faces.
284  */
285  void RecalculateTextureChanges();
286 #endif
287  /* Utility routine used by Hit. Performs a "FirstHit" test, which performs a hit test on the object
288  * but does not guarantee that the collision point returned by Hit is the closest collision point
289  * to the observer
290  * \param point The logical coordinate of the click, relative to the object
291  * \param directionVector The direction of the pick vector
292  * \param collisionPoint Set to the point on the object hit by directionVector
293  * \return boolean indicating if the object was hit by the mouse
294  */
295  bool FirstHit( const Vector& point, const Vector& directionVector, Vector* collisionPoint );
296 
297  /* Utility routine used by Hit. Performs a "BestHit" test, which performs a hit test on the object
298  * and guarantees that the collision point returned by Hit is the closest collision point to the observer.
299  * \param point The logical coordinate of the click, relative to the object
300  * \param directionVector The direction of the pick vector
301  * \param collisionPoint Set to the point on the object hit by directionVector
302  * \return boolean indicating if the object was hit by the mouse
303  */
304  bool BestHit( const Vector& point, const Vector& directionVector, Vector* collisionPoint );
305 
306  /* Utility routines used to detect when the material indices has changed */
307  void OnMatChanged();
308  void OnMatChanged( DynamicArray<int>& a, DynamicArray<int>& b );
309 
310  GLTriMesh& operator=( const GLTriMesh& ) DISTI_SPECIAL_MEM_FUN_DELETE;
311  GLTriMesh( const GLTriMesh& ) DISTI_SPECIAL_MEM_FUN_DELETE;
312 
313  void CopyGeometryInternal( GLTriMesh* src );
314  void CopyPropertiesInternal( GLTriMesh* src );
315 };
316 
317 /** The DistiAttributeMeshFaceArray class. Defines metadata for the GlMeshFace type.
318  */
320 {
321  GLMeshFace** _attribPtr;
322  unsigned int* _numFaces;
323  bool _inRuntimeMode;
324 
325 public:
326  GLS_EXPORT DistiAttributeMeshFaceArray( CallbackMethodCallerBase* callback, const AttributeName& name, GLMeshFace** attribPtr, unsigned int* numFaces, bool inRuntimeMode );
327 
328  virtual GLS_EXPORT ~DistiAttributeMeshFaceArray();
329 
330  virtual GLS_EXPORT bool OkToWrite() const;
331 
332  virtual GLS_EXPORT std::ostream& WriteValue( std::ostream& );
333  virtual GLS_EXPORT std::istream& ReadValue( std::istream& );
334 };
335 
336 } // namespace disti
337 
338 #endif
Definition: cull.h:49
virtual void SetAvailableAttributes(unsigned int value)
virtual void GetCppInterfaceDescriptionFree(InterfaceListType *array)
Definition: vertex.h:408
virtual InterfaceListType * GetCppInterfaceDescription(InterfaceListType *addToThisList=NULL)
The disti metadata.
int TextureIndex(void)
virtual void CopyProperties(DisplayObject *src)
void Invalidate(void)
Definition: gls_display_list.h:82
The disti::DynamicArray class. A templated array of objects capable of dynamically growing...
Class to contain current OpenGL view, projection and draw matrices.
Definition: util.h:278
virtual void PreDraw(const OpenGLMatrices &current, Culler &culler)
void ReplaceMaterial(unsigned int oldIndex, unsigned int newIndex)
virtual void Draw(void)
The Polygon class. Implements Polygons.
Definition: glpolygon.h:55
Definition: display.h:97
The disti::GLPolygon class. Implements Polygons.
int texture
Definition: gltrimesh.h:75
void SetFaces(unsigned int nFaces, GLMeshFace *faces)
virtual void CopyGeometry(DisplayObject *src)
unsigned int NumberOfFaces(void)
Definition: gltrimesh.h:154
Definition: gls_display_list.h:50
void SetFace(unsigned int face, int a, int b, int c, int tindex, unsigned int materialIndex=0)
Definition: gls_glo_file.h:835
The disti::DisplayObject class and global enumerations.
void VaSetFaces(unsigned int numFaces,...)
unsigned int material
Definition: gltrimesh.h:76
The glTriMesh class. Implements Triangle Meshes.
Definition: gltrimesh.h:92
Definition: disti_metadata.h:174
DynamicArray< int > & MaterialIndices()
void InvalidateGeometry(void)
Definition: gltrimesh.h:241
bool SetupTexture(void)
Definition: gltrimesh.h:81
Structure for holding mesh vertex indices.
Definition: gltrimesh.h:72
GlsDisplayList _displayList
Definition: gltrimesh.h:107
Definition: callback_caller_base.h:55
virtual void SetValue(int spec, va_list &args)
virtual bool Hit(float x, float y, float z, float scale, const Vector &directionVector, Vector *collisionPoint)
Definition: gltrimesh.h:319
Definition: vertex.h:83
void SetVerticesAndFaces(unsigned int nVertices, const GLMeshVertex *vertices, unsigned int nFaces, const GLMeshFace *faces)
Definition: gls_index_array.h:52
Macros and helper code to determine what subset of C++11/14/17 is available.
The disti::GlsIndexArray class, for managing index buffers.
Definition: disti_metadata.h:79
Definition: bmpimage.h:46
virtual ~GLTriMesh(void)
GLMeshFace * Faces(void)
Definition: gltrimesh.h:129
virtual DisplayObject * CloneObject(bool generateNames=false)
virtual void DrawGeometry(void)
The disti::GlsDisplayList class.