KiCad PCB EDA Suite
C_OGL_3DMODEL Class Reference

#include <c_ogl_3dmodel.h>

Public Member Functions

 C_OGL_3DMODEL (const S3DMODEL &a3DModel, MATERIAL_MODE aMaterialMode)
 C_OGL_3DMODEL - Load a 3d model. More...
 
 ~C_OGL_3DMODEL ()
 
void Draw_opaque () const
 Draw_opaque - render the model into the current context. More...
 
void Draw_transparent () const
 Draw_transparent - render the model into the current context. More...
 
bool Have_opaque () const
 Have_opaque - return true if have opaque meshs to render. More...
 
bool Have_transparent () const
 Have_transparent - return true if have transparent meshs to render. More...
 
void Draw_bbox () const
 Draw_bbox - draw main bounding box of the model. More...
 
void Draw_bboxes () const
 Draw_bboxes - draw individual bounding boxes of each mesh. More...
 
const CBBOXGetBBox () const
 GetBBox - Get main bbox. More...
 

Private Attributes

GLuint m_ogl_idx_list_opaque
 display list for rendering opaque meshes More...
 
GLuint m_ogl_idx_list_transparent
 display list for rendering transparent meshes More...
 
GLuint m_ogl_idx_list_meshes
 display lists for all meshes. More...
 
unsigned int m_nr_meshes
 number of meshes of this model More...
 
CBBOX m_model_bbox
 global bounding box for this model More...
 
CBBOXm_meshs_bbox
 individual bbox for each mesh More...
 

Detailed Description

Definition at line 39 of file c_ogl_3dmodel.h.

Constructor & Destructor Documentation

C_OGL_3DMODEL::C_OGL_3DMODEL ( const S3DMODEL a3DModel,
MATERIAL_MODE  aMaterialMode 
)

C_OGL_3DMODEL - Load a 3d model.

This must be called inside a gl context

Parameters
a3DModela 3d model data to load.
aMaterialModea mode to render the materials of the model

Definition at line 37 of file c_ogl_3dmodel.cpp.

References SMESH::m_Color, SMATERIAL::m_Diffuse, SMESH::m_FaceIdx, SMESH::m_FaceIdxSize, SMESH::m_MaterialIdx, S3DMODEL::m_Materials, S3DMODEL::m_MaterialsSize, S3DMODEL::m_Meshes, S3DMODEL::m_MeshesSize, m_meshs_bbox, m_model_bbox, SMESH::m_Normals, m_nr_meshes, m_ogl_idx_list_meshes, m_ogl_idx_list_opaque, m_ogl_idx_list_transparent, SMESH::m_Positions, SMESH::m_Texcoords, SMATERIAL::m_Transparency, SMESH::m_VertexSize, MATERIAL_MODE_CAD_MODE, MATERIAL_MODE_DIFFUSE_ONLY, MATERIAL_MODE_NORMAL, MaterialDiffuseToColorCAD(), OGL_SetDiffuseOnlyMaterial(), OGL_SetMaterial(), CBBOX::Reset(), and CBBOX::Union().

39 {
42  m_nr_meshes = 0;
43  m_meshs_bbox = NULL;
44 
45  // Validate a3DModel pointers
46  wxASSERT( a3DModel.m_Materials != NULL );
47  wxASSERT( a3DModel.m_Meshes != NULL );
48  wxASSERT( a3DModel.m_MaterialsSize > 0 );
49  wxASSERT( a3DModel.m_MeshesSize > 0 );
50 
51  if( (a3DModel.m_Materials != NULL) && (a3DModel.m_Meshes != NULL) &&
52  (a3DModel.m_MaterialsSize > 0) && (a3DModel.m_MeshesSize > 0) )
53  {
54  m_nr_meshes = a3DModel.m_MeshesSize;
55 
56  m_meshs_bbox = new CBBOX[a3DModel.m_MeshesSize];
57 
58  // Generate m_MeshesSize auxiliar lists to render the meshes
59  m_ogl_idx_list_meshes = glGenLists( a3DModel.m_MeshesSize );
60 
61  // Render each mesh of the model
62  // /////////////////////////////////////////////////////////////////////
63  for( unsigned int mesh_i = 0; mesh_i < a3DModel.m_MeshesSize; ++mesh_i )
64  {
65  if( glIsList( m_ogl_idx_list_meshes + mesh_i ) )
66  {
67  const SMESH &mesh = a3DModel.m_Meshes[mesh_i];
68 
69  // Validate the mesh pointers
70  wxASSERT( mesh.m_Positions != NULL );
71  wxASSERT( mesh.m_FaceIdx != NULL );
72  wxASSERT( mesh.m_Normals != NULL );
73 
74  if( (mesh.m_Positions != NULL) &&
75  (mesh.m_Normals != NULL) &&
76  (mesh.m_FaceIdx != NULL) &&
77  (mesh.m_FaceIdxSize > 0) && (mesh.m_VertexSize > 0) )
78  {
79  SFVEC4F *pColorRGBA = NULL;
80 
81  // Create the bbox for this mesh
82  // /////////////////////////////////////////////////////////
83  m_meshs_bbox[mesh_i].Reset();
84 
85  for( unsigned int vertex_i = 0;
86  vertex_i < mesh.m_VertexSize;
87  ++vertex_i )
88  {
89  m_meshs_bbox[mesh_i].Union( mesh.m_Positions[vertex_i] );
90  }
91 
92  // Make sure we start with client state disabled
93  // /////////////////////////////////////////////////////////
94  glDisableClientState( GL_TEXTURE_COORD_ARRAY );
95  glDisableClientState( GL_COLOR_ARRAY );
96 
97 
98  // Enable arrays client states
99  // /////////////////////////////////////////////////////////
100  glEnableClientState( GL_VERTEX_ARRAY );
101  glEnableClientState( GL_NORMAL_ARRAY );
102 
103  glVertexPointer( 3, GL_FLOAT, 0, mesh.m_Positions );
104  glNormalPointer( GL_FLOAT, 0, mesh.m_Normals );
105 
106  if( mesh.m_Color != NULL )
107  {
108  glEnableClientState( GL_COLOR_ARRAY );
109 
110  float transparency = 0.0f;
111 
112  if( mesh.m_MaterialIdx < a3DModel.m_MaterialsSize )
113  transparency = a3DModel.m_Materials[mesh.m_MaterialIdx].m_Transparency;
114 
115  if( (transparency > FLT_EPSILON) &&
116  (aMaterialMode == MATERIAL_MODE_NORMAL) )
117  {
118  // Create a new array of RGBA colors
119  pColorRGBA = new SFVEC4F[mesh.m_VertexSize];
120 
121  // Copy RGB array and add the Alpha value
122  for( unsigned int i = 0; i < mesh.m_VertexSize; ++i )
123  pColorRGBA[i] = SFVEC4F( mesh.m_Color[i],
124  1.0f - transparency );
125 
126  // Load an RGBA array
127  glColorPointer( 4, GL_FLOAT, 0, pColorRGBA );
128  }
129  else
130  {
131  switch( aMaterialMode )
132  {
135  // load the original RGB color array
136  glColorPointer( 3, GL_FLOAT, 0, mesh.m_Color );
137  break;
139  // Create a new array of RGBA colors
140  pColorRGBA = new SFVEC4F[mesh.m_VertexSize];
141 
142  // Copy RGB array and add the Alpha value
143  for( unsigned int i = 0; i < mesh.m_VertexSize; ++i )
144  {
145  pColorRGBA[i] =
147  1.0f );
148  }
149 
150  // Load an RGBA array
151  glColorPointer( 4, GL_FLOAT, 0, pColorRGBA );
152  break;
153  default:
154  break;
155  }
156  }
157  }
158 
159  if( mesh.m_Texcoords != NULL )
160  {
161  glEnableClientState( GL_TEXTURE_COORD_ARRAY );
162  glTexCoordPointer( 2, GL_FLOAT, 0, mesh.m_Texcoords );
163  }
164 
165  // Compile the display list to store triangles
166  // /////////////////////////////////////////////////////////
167  glNewList( m_ogl_idx_list_meshes + mesh_i, GL_COMPILE );
168 
169  // Set material properties
170  // /////////////////////////////////////////////////////////
171 
172  if( mesh.m_Color != NULL )
173  {
174  // This enables the use of the Color Pointer information
175  glEnable( GL_COLOR_MATERIAL );
176  glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
177  }
178  else
179  {
180  glDisable( GL_COLOR_MATERIAL );
181  }
182 
183  if( mesh.m_MaterialIdx < a3DModel.m_MaterialsSize )
184  {
185  switch( aMaterialMode )
186  {
188  OGL_SetMaterial( a3DModel.m_Materials[mesh.m_MaterialIdx] );
189  break;
192  a3DModel.m_Materials[mesh.m_MaterialIdx].m_Diffuse );
193  break;
197  a3DModel.m_Materials[mesh.m_MaterialIdx].m_Diffuse ) );
198  break;
199  default:
200  break;
201  }
202  }
203 
204  // Draw mesh
205  // /////////////////////////////////////////////////////////
206  glDrawElements( GL_TRIANGLES, mesh.m_FaceIdxSize,
207  GL_UNSIGNED_INT, mesh.m_FaceIdx );
208 
209  glDisable( GL_COLOR_MATERIAL );
210 
211  glEndList();
212 
213  // Disable arrays client states
214  // /////////////////////////////////////////////////////////
215  glDisableClientState( GL_TEXTURE_COORD_ARRAY );
216  glDisableClientState( GL_COLOR_ARRAY );
217  glDisableClientState( GL_NORMAL_ARRAY );
218  glDisableClientState( GL_VERTEX_ARRAY );
219 
220  glFinish();
221 
222  delete [] pColorRGBA;
223  }
224  }
225  }// for each mesh
226 
227 
228  m_ogl_idx_list_opaque = glGenLists( 1 );
229 
230  // Check if the generated list is valid
231  if( glIsList( m_ogl_idx_list_opaque ) )
232  {
233  bool have_opaque_meshes = false;
234  bool have_transparent_meshes = false;
235 
236  // Compile the model display list
237  glNewList( m_ogl_idx_list_opaque, GL_COMPILE );
238 
239  // Render each mesh display list (opaque first)
240  // /////////////////////////////////////////////////////////////////
241  for( unsigned int mesh_i = 0; mesh_i < a3DModel.m_MeshesSize; ++mesh_i )
242  {
243  const SMESH &mesh = a3DModel.m_Meshes[mesh_i];
244 
245  if( mesh.m_MaterialIdx < a3DModel.m_MaterialsSize )
246  {
247  const SMATERIAL &material = a3DModel.m_Materials[mesh.m_MaterialIdx];
248 
249  if( material.m_Transparency == 0.0f )
250  {
251  have_opaque_meshes = true; // Flag that we have at least one opaque mesh
252  glCallList( m_ogl_idx_list_meshes + mesh_i );
253  }
254  else
255  {
256  have_transparent_meshes = true; // Flag that we found a transparent mesh
257  }
258  }
259  }
260 
261  glEndList();
262 
263  if( !have_opaque_meshes )
264  {
265  // If we dont have opaque meshes, we can free the list
266  glDeleteLists( m_ogl_idx_list_opaque, 1 );
268  }
269 
270  if( have_transparent_meshes )
271  {
272  m_ogl_idx_list_transparent = glGenLists( 1 );
273 
274  // Check if the generated list is valid
275  if( glIsList( m_ogl_idx_list_transparent ) )
276  {
277  // Compile the model display list
278  glNewList( m_ogl_idx_list_transparent, GL_COMPILE );
279 
280  glEnable( GL_BLEND );
281  glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
282 
283  // Render each mesh display list
284  // /////////////////////////////////////////////////////////
285  for( unsigned mesh_i = 0; mesh_i < a3DModel.m_MeshesSize; ++mesh_i )
286  {
287  const SMESH &mesh = a3DModel.m_Meshes[mesh_i];
288 
289  if( mesh.m_MaterialIdx < a3DModel.m_MaterialsSize )
290  {
291  const SMATERIAL &material = a3DModel.m_Materials[mesh.m_MaterialIdx];
292 
293  // Render the transparent mesh if it have a transparency value
294  if( material.m_Transparency != 0.0f )
295  glCallList( m_ogl_idx_list_meshes + mesh_i );
296  }
297  }
298 
299  glDisable( GL_BLEND );
300 
301  glEndList();
302  }
303  else
304  {
306  }
307  }
308  }
309  else
310  {
312  }
313 
314  // Create the main bbox
315  // /////////////////////////////////////////////////////////////////////
317 
318  for( unsigned int mesh_i = 0; mesh_i < a3DModel.m_MeshesSize; ++mesh_i )
319  m_model_bbox.Union( m_meshs_bbox[mesh_i] );
320 
321  glFinish();
322  }
323 }
SFVEC3F * m_Normals
Vertex normals array.
Definition: c3dmodel.h:80
Use only diffuse material properties.
Definition: 3d_enums.h:94
unsigned int m_nr_meshes
number of meshes of this model
Definition: c_ogl_3dmodel.h:91
glm::vec4 SFVEC4F
Definition: xv3d_types.h:49
float m_Transparency
1.0 is completely transparent, 0.0 completely opaque
Definition: c3dmodel.h:44
SFVEC3F * m_Positions
Vertex position array.
Definition: c3dmodel.h:79
GLuint m_ogl_idx_list_transparent
display list for rendering transparent meshes
Definition: c_ogl_3dmodel.h:89
Per-vertex normal/color/texcoors structure.
Definition: c3dmodel.h:76
SMESH * m_Meshes
The meshes list of this model.
Definition: c3dmodel.h:93
CBBOX m_model_bbox
global bounding box for this model
Definition: c_ogl_3dmodel.h:93
void Union(const SFVEC3F &aPoint)
Function Union recalculate the bounding box adding a point.
Definition: cbbox.cpp:105
unsigned int m_FaceIdxSize
Number of elements of the m_FaceIdx array.
Definition: c3dmodel.h:83
void OGL_SetMaterial(const SMATERIAL &aMaterial)
OGL_SetMaterial - Set OpenGL materials.
Definition: ogl_utils.cpp:141
unsigned int m_VertexSize
Number of vertex in the arrays.
Definition: c3dmodel.h:78
SFVEC2F * m_Texcoords
Vertex texture coordinates array, can be NULL.
Definition: c3dmodel.h:81
SFVEC3F * m_Color
Vertex color array, can be NULL.
Definition: c3dmodel.h:82
Use a gray shading based on diffuse material.
Definition: 3d_enums.h:95
unsigned int m_MaterialsSize
Number of materials in the material array.
Definition: c3dmodel.h:95
GLuint m_ogl_idx_list_opaque
display list for rendering opaque meshes
Definition: c_ogl_3dmodel.h:88
Use all material properties from model file.
Definition: 3d_enums.h:93
GLuint m_ogl_idx_list_meshes
display lists for all meshes.
Definition: c_ogl_3dmodel.h:90
SFVEC3F MaterialDiffuseToColorCAD(const SFVEC3F &aDiffuseColor)
Definition: 3d_math.h:154
unsigned int m_MaterialIdx
Material Index to be used in this mesh (must be < m_MaterialsSize )
Definition: c3dmodel.h:85
SMATERIAL * m_Materials
The materials list of this model.
Definition: c3dmodel.h:96
SFVEC3F m_Diffuse
Default diffuse color if m_Color is NULL.
Definition: c3dmodel.h:40
CBBOX * m_meshs_bbox
individual bbox for each mesh
Definition: c_ogl_3dmodel.h:94
void OGL_SetDiffuseOnlyMaterial(const SFVEC3F &aMaterialDiffuse)
OGL_SetDiffuseOnlyMaterial - sets only the diffuse color and keep other parameters with default value...
Definition: ogl_utils.cpp:161
unsigned int * m_FaceIdx
Triangle Face Indexes.
Definition: c3dmodel.h:84
unsigned int m_MeshesSize
Number of meshes in the array.
Definition: c3dmodel.h:92
Class CBBOX manages a bounding box defined by two SFVEC3F min max points.
Definition: cbbox.h:40
void Reset()
Function Reset reset the bounding box to zero and de-initialized it.
Definition: cbbox.cpp:98
C_OGL_3DMODEL::~C_OGL_3DMODEL ( )

Definition at line 340 of file c_ogl_3dmodel.cpp.

References m_meshs_bbox, m_nr_meshes, m_ogl_idx_list_meshes, m_ogl_idx_list_opaque, and m_ogl_idx_list_transparent.

341 {
342  if( glIsList( m_ogl_idx_list_opaque ) )
343  glDeleteLists( m_ogl_idx_list_opaque, 1 );
344 
345  if( glIsList( m_ogl_idx_list_transparent ) )
346  glDeleteLists( m_ogl_idx_list_transparent, 1 );
347 
348  if( glIsList( m_ogl_idx_list_meshes ) )
349  glDeleteLists( m_ogl_idx_list_meshes, m_nr_meshes );
350 
354 
355  delete[] m_meshs_bbox;
356  m_meshs_bbox = NULL;
357 }
unsigned int m_nr_meshes
number of meshes of this model
Definition: c_ogl_3dmodel.h:91
GLuint m_ogl_idx_list_transparent
display list for rendering transparent meshes
Definition: c_ogl_3dmodel.h:89
GLuint m_ogl_idx_list_opaque
display list for rendering opaque meshes
Definition: c_ogl_3dmodel.h:88
GLuint m_ogl_idx_list_meshes
display lists for all meshes.
Definition: c_ogl_3dmodel.h:90
CBBOX * m_meshs_bbox
individual bbox for each mesh
Definition: c_ogl_3dmodel.h:94

Member Function Documentation

void C_OGL_3DMODEL::Draw_bbox ( ) const

Draw_bbox - draw main bounding box of the model.

Definition at line 360 of file c_ogl_3dmodel.cpp.

References m_model_bbox, and OGL_draw_bbox().

Referenced by C3D_RENDER_OGL_LEGACY::render_3D_module().

361 {
363 }
CBBOX m_model_bbox
global bounding box for this model
Definition: c_ogl_3dmodel.h:93
void OGL_draw_bbox(const CBBOX &aBBox)
OGL_draw_bbox - draw the bounding box lines.
void C_OGL_3DMODEL::Draw_bboxes ( ) const

Draw_bboxes - draw individual bounding boxes of each mesh.

Definition at line 366 of file c_ogl_3dmodel.cpp.

References m_meshs_bbox, m_nr_meshes, and OGL_draw_bbox().

Referenced by C3D_RENDER_OGL_LEGACY::render_3D_module().

367 {
368  for( unsigned int mesh_i = 0; mesh_i < m_nr_meshes; ++mesh_i )
369  OGL_draw_bbox( m_meshs_bbox[mesh_i] );
370 }
unsigned int m_nr_meshes
number of meshes of this model
Definition: c_ogl_3dmodel.h:91
void OGL_draw_bbox(const CBBOX &aBBox)
OGL_draw_bbox - draw the bounding box lines.
CBBOX * m_meshs_bbox
individual bbox for each mesh
Definition: c_ogl_3dmodel.h:94
void C_OGL_3DMODEL::Draw_opaque ( ) const

Draw_opaque - render the model into the current context.

Definition at line 326 of file c_ogl_3dmodel.cpp.

References m_ogl_idx_list_opaque.

Referenced by C3D_MODEL_VIEWER::OnPaint(), and C3D_RENDER_OGL_LEGACY::render_3D_module().

327 {
328  if( glIsList( m_ogl_idx_list_opaque ) )
329  glCallList( m_ogl_idx_list_opaque );
330 }
GLuint m_ogl_idx_list_opaque
display list for rendering opaque meshes
Definition: c_ogl_3dmodel.h:88
void C_OGL_3DMODEL::Draw_transparent ( ) const

Draw_transparent - render the model into the current context.

Definition at line 333 of file c_ogl_3dmodel.cpp.

References m_ogl_idx_list_transparent.

Referenced by C3D_MODEL_VIEWER::OnPaint(), and C3D_RENDER_OGL_LEGACY::render_3D_module().

334 {
335  if( glIsList( m_ogl_idx_list_transparent ) )
336  glCallList( m_ogl_idx_list_transparent );
337 }
GLuint m_ogl_idx_list_transparent
display list for rendering transparent meshes
Definition: c_ogl_3dmodel.h:89
const CBBOX& C_OGL_3DMODEL::GetBBox ( ) const
inline

GetBBox - Get main bbox.

Returns
the main model bbox

Definition at line 85 of file c_ogl_3dmodel.h.

References m_model_bbox.

Referenced by C3D_MODEL_VIEWER::OnPaint().

85 { return m_model_bbox; }
CBBOX m_model_bbox
global bounding box for this model
Definition: c_ogl_3dmodel.h:93
bool C_OGL_3DMODEL::Have_opaque ( ) const

Have_opaque - return true if have opaque meshs to render.

Definition at line 373 of file c_ogl_3dmodel.cpp.

References m_ogl_idx_list_opaque.

Referenced by C3D_RENDER_OGL_LEGACY::render_3D_module().

374 {
375  return glIsList( m_ogl_idx_list_opaque );
376 }
GLuint m_ogl_idx_list_opaque
display list for rendering opaque meshes
Definition: c_ogl_3dmodel.h:88
bool C_OGL_3DMODEL::Have_transparent ( ) const

Have_transparent - return true if have transparent meshs to render.

Definition at line 379 of file c_ogl_3dmodel.cpp.

References m_ogl_idx_list_transparent.

Referenced by C3D_RENDER_OGL_LEGACY::render_3D_module().

380 {
381  return glIsList( m_ogl_idx_list_transparent );
382 }
GLuint m_ogl_idx_list_transparent
display list for rendering transparent meshes
Definition: c_ogl_3dmodel.h:89

Member Data Documentation

CBBOX* C_OGL_3DMODEL::m_meshs_bbox
private

individual bbox for each mesh

Definition at line 94 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), Draw_bboxes(), and ~C_OGL_3DMODEL().

CBBOX C_OGL_3DMODEL::m_model_bbox
private

global bounding box for this model

Definition at line 93 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), Draw_bbox(), and GetBBox().

unsigned int C_OGL_3DMODEL::m_nr_meshes
private

number of meshes of this model

Definition at line 91 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), Draw_bboxes(), and ~C_OGL_3DMODEL().

GLuint C_OGL_3DMODEL::m_ogl_idx_list_meshes
private

display lists for all meshes.

Definition at line 90 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), and ~C_OGL_3DMODEL().

GLuint C_OGL_3DMODEL::m_ogl_idx_list_opaque
private

display list for rendering opaque meshes

Definition at line 88 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), Draw_opaque(), Have_opaque(), and ~C_OGL_3DMODEL().

GLuint C_OGL_3DMODEL::m_ogl_idx_list_transparent
private

display list for rendering transparent meshes

Definition at line 89 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), Draw_transparent(), Have_transparent(), and ~C_OGL_3DMODEL().


The documentation for this class was generated from the following files: