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  DISTI_DEPRECATED( "This identifier is forbidden by the C++ standard. Use BaseClass instead." )
101  typedef GLPolygon _BaseClass;
102  typedef GLPolygon BaseClass;
103  friend class GLTriMeshEditor;
104 
105 #ifdef GLES
106  GlsIndexArray _indices;
107 #else
108  /** Stores object in display list for faster drawing */
110 #endif
111 
112  /* Override base class since a mesh can have multiple textures */
113  GLS_EXPORT int TextureIndex( void ) DISTI_METHOD_OVERRIDE;
114 
115  /* Override base class since a mesh can have multiple textures */
116  GLS_EXPORT void TextureIndex( int index ) DISTI_METHOD_OVERRIDE;
117 
118  /* Override base class since a mesh can have multiple materials */
119  GLS_EXPORT void MaterialIndices( DynamicArray<int> index ) DISTI_METHOD_OVERRIDE;
120 
121  /** Gets a vector of material indices. */
122  GLS_EXPORT DynamicArray<int>& MaterialIndices() DISTI_METHOD_OVERRIDE;
123 
124  /* Override base class since a mesh can have multiple materials */
125  GLS_EXPORT void MaterialIndex( unsigned int index );
126 
127  /* Override base class since a mesh can have multiple materials */
128  GLS_EXPORT int MaterialIndex() DISTI_METHOD_OVERRIDE;
129 
130  /** Provides access to the faces stored in this mesh */
131  GLS_EXPORT GLMeshFace* Faces( void ) { return _faces; }
132 
133  /* See base class */
134  virtual GLS_EXPORT void CopyGeometry( DisplayObject* src ) DISTI_METHOD_OVERRIDE;
135 
136  /* See base class */
137  virtual GLS_EXPORT DisplayObject* CloneObject( bool generateNames = false ) DISTI_METHOD_OVERRIDE;
138 
139  /* See base class */
140  virtual GLS_EXPORT void CopyProperties( DisplayObject* src ) DISTI_METHOD_OVERRIDE;
141 
142  /* See base class*/
143  virtual GLS_EXPORT void SetValue( int spec, va_list& args ) DISTI_METHOD_OVERRIDE;
144 
145  /* Copies the attributes from an object (possibly a group) to this one. Used by the
146  * mesh creator to copy properties from the objectrs that were converted into this mesh.
147  * \param src The object to copy properties from */
148  GLS_EXPORT void AssignPropertiesFromGroup( DisplayObject* src );
149 
150  /** Sets the number of faces in the mesh and allocates the face array
151  * \param n The new number of faces for the mesh
152  */
153  GLS_EXPORT void NumberOfFaces( unsigned int n );
154 
155  /** \return The number of faces in this mesh */
156  GLS_EXPORT unsigned int NumberOfFaces( void ) { return _nFaces; }
157 
158  /** For all faces of the mesh that have the old material, replace that material
159  * with the new material.
160  * \param oldIndex Old material index
161  * \param newIndex New material index
162  */
163  GLS_EXPORT void ReplaceMaterial( unsigned int oldIndex, unsigned int newIndex );
164 
165  /** Sets the vertices and texture index of a specific face
166  * \param face
167  * \param a The vertex index for the first vertex of the triangle
168  * \param b The vertex index for the second vertex of the triangle
169  * \param c The vertex index for the third vertex of the triangle
170  * \param tindex The texture index for triangle
171  * \param materialIndex The material index for triangle
172  */
173  GLS_EXPORT void SetFace( unsigned int face, int a, int b, int c, int tindex, unsigned int materialIndex = 0 );
174 
175  /** Sets all the faces of the mesh. Used in runtime.
176  * \param numFaces The number of faces that will be passed in
177  * \param ... A variable argument field which will be numFaces instances of a,b,c,tindex,materialIndex
178  */
179  GLS_EXPORT void VaSetFaces( unsigned int numFaces, ... );
180 
181  /** Sets all the faces of the mesh. Used in parser
182  * \param nFaces The number of faces that will be set
183  * \param faces An array of GLMeshFaces with nFaces elements
184  */
185  GLS_EXPORT void SetFaces( unsigned int nFaces, GLMeshFace* faces );
186 
187  /** Sets all the faces and vertices of the mesh. Used in generated code
188  * \param nVertices Number of vertices
189  * \param vertices Array of initialized vertices
190  * \param nFaces Number of faces
191  * \param faces Array of initialized faces
192  */
193  GLS_EXPORT void SetVerticesAndFaces( unsigned int nVertices, const GLMeshVertex* vertices, unsigned int nFaces, const GLMeshFace* faces );
194 
195  /** Creates a GLTriMesh with the vertices specified
196  * \param nVertices The number of vertices in the new polygon
197  * \param vertices The array of vertices for the polygon
198  */
199  GLS_EXPORT GLTriMesh( unsigned int nVertices, Vertex* vertices );
200 
201  /** Creates a GLTriMesh with the vertices specified
202  * \param nVertices The number of vertices in the new polygon
203  * \param vertices The array of vertices for the polygon
204  * \param location The location point of the object
205  */
206  GLS_EXPORT GLTriMesh( unsigned int nVertices, Vertex* vertices, Vertex& location );
207 
208  /** Allocate a (blank) GLTriMesh object */
209  GLS_EXPORT GLTriMesh( void );
210 
211  GLS_EXPORT GLTriMesh( const GLTriMesh& that, const bool generateNames );
212 
213  /** Destroy a GLTriMesh object */
214  virtual GLS_EXPORT ~GLTriMesh( void );
215 
216  static GLS_EXPORT DisplayObject* CreateInstance();
217 
218  virtual GLS_EXPORT void SetAvailableAttributes( unsigned int value ) DISTI_METHOD_OVERRIDE;
219 
220 #ifdef GLES
221  /* See base class */
222  virtual GLS_EXPORT void PreDraw( const OpenGLMatrices& current, Culler& culler ) DISTI_METHOD_OVERRIDE;
223 #endif
224 
225  /* See base class */
226  virtual GLS_EXPORT void Draw( void ) DISTI_METHOD_OVERRIDE;
227 
228  /* See base class */
229  virtual GLS_EXPORT bool Hit( float x, float y, float z, float scale, const Vector& directionVector, Vector* collisionPoint ) DISTI_METHOD_OVERRIDE;
230 
231 #ifndef GLES
232  /* See base class */
233  virtual GLS_EXPORT InterfaceListType* GetCppInterfaceDescription( InterfaceListType* addToThisList = NULL ) DISTI_METHOD_OVERRIDE;
234 
235  /* See base class */
236  virtual GLS_EXPORT void GetCppInterfaceDescriptionFree( InterfaceListType* array ) DISTI_METHOD_OVERRIDE;
237 
238  virtual GLS_EXPORT void UseDisplayList( const bool& flag );
239  virtual GLS_EXPORT bool UseDisplayList( void );
240 #endif
241 
242  /** Causes the display list for this object to be recomputed */
243  GLS_EXPORT void InvalidateGeometry( void )
244  {
245 #ifndef GLES
246  _displayList.Invalidate();
247 #endif
248  }
249 
250 #ifdef GLES
251  GLS_EXPORT void DirtyTextureData( void );
252 #endif
253 
254 protected:
255 #ifndef GLES
256  /** Contains the actual OpenGL drawing commands to draw the object */
257  virtual GLS_EXPORT void DrawGeometry( void );
258 #endif
259  /* A mesh can have multiple textures */
260  virtual GLS_EXPORT bool SetupTexture( const int textureIndex );
261 
262 #ifdef GLES
263  /** Set a single attribute from the GLO file.
264  * \param data The attribute to set and its associated data.
265  */
266  virtual GLS_EXPORT void SetFromGloData( GlsGloFileAttribute& data ) DISTI_METHOD_OVERRIDE;
267 #endif
268 
269 private:
270  unsigned int _nFaces; /** Number of faces (triangles) in this mesh */
271 
272  GLMeshFace* _faces; /** Array of vertex indices for triangles */
273 
274  DynamicArray<int> _newMaterialIndices; /** Intermediate data storage to make the setting of new material indices through the Faces simpler. */
275 
276 #ifdef GLES
277  /* Resizable array of indices into the faces array where the active texture is different from
278  * the previous face.
279  */
280  DynamicArray<unsigned int> _textureChangeIndices;
281  bool _textureChangesDirty; /** true if _textureSwitchIndices needs to be recomputed */
282 
283  /* Utility routine used by Draw. Should only be called when _textureSwitchDirty is true.
284  * Rebuilds the _textureSwitchIndices array by scanning the data in the face array
285  * and taking note when the texture index changes between faces.
286  */
287  void RecalculateTextureChanges();
288 #endif
289  /* Utility routine used by Hit. Performs a "FirstHit" test, which performs a hit test on the object
290  * but does not guarantee that the collision point returned by Hit is the closest collision point
291  * to the observer
292  * \param point The logical coordinate of the click, relative to the object
293  * \param directionVector The direction of the pick vector
294  * \param collisionPoint Set to the point on the object hit by directionVector
295  * \return boolean indicating if the object was hit by the mouse
296  */
297  bool FirstHit( const Vector& point, const Vector& directionVector, Vector* collisionPoint );
298 
299  /* Utility routine used by Hit. Performs a "BestHit" test, which performs a hit test on the object
300  * and guarantees that the collision point returned by Hit is the closest collision point to the observer.
301  * \param point The logical coordinate of the click, relative to the object
302  * \param directionVector The direction of the pick vector
303  * \param collisionPoint Set to the point on the object hit by directionVector
304  * \return boolean indicating if the object was hit by the mouse
305  */
306  bool BestHit( const Vector& point, const Vector& directionVector, Vector* collisionPoint );
307 
308  /* Utility routines used to detect when the material indices has changed */
309  void OnMatChanged();
310  void OnMatChanged( DynamicArray<int>& a, DynamicArray<int>& b );
311 
312  GLTriMesh& operator=( const GLTriMesh& ) DISTI_SPECIAL_MEM_FUN_DELETE;
313  GLTriMesh( const GLTriMesh& ) DISTI_SPECIAL_MEM_FUN_DELETE;
314 
315  void CopyGeometryInternal( GLTriMesh* src );
316  void CopyPropertiesInternal( GLTriMesh* src );
317 };
318 
319 /** The DistiAttributeMeshFaceArray class. Defines metadata for the GlMeshFace type.
320  */
322 {
323  GLMeshFace** _attribPtr;
324  unsigned int* _numFaces;
325  bool _inRuntimeMode;
326 
327 public:
328  GLS_EXPORT DistiAttributeMeshFaceArray( CallbackMethodCallerBase* callback, const AttributeName& name, GLMeshFace** attribPtr, unsigned int* numFaces, bool inRuntimeMode );
329 
330  virtual GLS_EXPORT ~DistiAttributeMeshFaceArray();
331 
332  virtual GLS_EXPORT bool OkToWrite() const;
333 
334  virtual GLS_EXPORT std::ostream& WriteValue( std::ostream& );
335  virtual GLS_EXPORT std::istream& ReadValue( std::istream& );
336 };
337 
338 } // namespace disti
339 
340 #endif
Definition: cull.h:49
Definition: vertex.h:409
The disti metadata.
#define DISTI_DEPRECATED(msg)
Defines whether this compiler supports the C++14 deprecated attribute.
Definition: gls_cpp_lang_support.h:436
int MaterialIndex() override
void Invalidate(void)
Definition: gls_display_list.h:82
Definition: dynamic_array.h:66
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:473
virtual void PreDraw(const OpenGLMatrices &current, Culler &culler)
void ReplaceMaterial(unsigned int oldIndex, unsigned int newIndex)
virtual void CopyGeometry(DisplayObject *src) override
virtual void SetAvailableAttributes(unsigned int value) override
The Polygon class. Implements Polygons.
Definition: glpolygon.h:55
virtual void Draw(void) override
Definition: display.h:98
The disti::GLPolygon class. Implements Polygons.
int texture
Definition: gltrimesh.h:75
void SetFaces(unsigned int nFaces, GLMeshFace *faces)
unsigned int NumberOfFaces(void)
Definition: gltrimesh.h:156
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:988
The disti::DisplayObject class and global enumerations.
void VaSetFaces(unsigned int numFaces,...)
unsigned int material
Definition: gltrimesh.h:76
virtual void SetValue(int spec, va_list &args) override
The glTriMesh class. Implements Triangle Meshes.
Definition: gltrimesh.h:92
virtual DisplayObject * CloneObject(bool generateNames=false) override
virtual void CopyProperties(DisplayObject *src) override
int TextureIndex(void) override
Definition: disti_metadata.h:186
void InvalidateGeometry(void)
Definition: gltrimesh.h:243
bool SetupTexture(void)
Definition: gltrimesh.h:81
virtual void GetCppInterfaceDescriptionFree(InterfaceListType *array) override
Structure for holding mesh vertex indices.
Definition: gltrimesh.h:72
GlsDisplayList _displayList
Definition: gltrimesh.h:109
Definition: callback_caller_base.h:55
virtual bool Hit(float x, float y, float z, float scale, const Vector &directionVector, Vector *collisionPoint) override
Definition: gltrimesh.h:321
Definition: vertex.h:84
void SetVerticesAndFaces(unsigned int nVertices, const GLMeshVertex *vertices, unsigned int nFaces, const GLMeshFace *faces)
virtual InterfaceListType * GetCppInterfaceDescription(InterfaceListType *addToThisList=NULL) override
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:85
Definition: bmpimage.h:46
virtual ~GLTriMesh(void)
GLMeshFace * Faces(void)
Definition: gltrimesh.h:131
virtual void DrawGeometry(void)
The disti::GlsDisplayList class.
DynamicArray< int > & MaterialIndices() override