KiCad PCB EDA Suite
C_OGL_3DMODEL Class Reference

#include <c_ogl_3dmodel.h>

Classes

struct  MATERIAL
 
struct  VERTEX
 

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 (bool aUseSelectedMaterial, SFVEC3F aSelectionColor=SFVEC3F(0.0f)) const
 Draw_opaque - render the model into the current context. More...
 
void Draw_transparent (float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor=SFVEC3F(0.0f)) 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...
 

Static Public Member Functions

static void BeginDrawMulti (bool aUseColorInformation)
 BeginDrawMulti - set some basic render states before drawing multiple models. More...
 
static void EndDrawMulti ()
 EndDrawMulti - cleanup render states after drawing multiple models. More...
 

Private Member Functions

void Draw (bool aTransparent, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor) const
 

Static Private Member Functions

static void MakeBbox (const CBBOX &aBox, unsigned int aIdxOffset, VERTEX *aVtxOut, GLuint *aIdxOut, const glm::vec4 &aColor)
 

Private Attributes

MATERIAL_MODE m_material_mode
 
CBBOX m_model_bbox
 global bounding box for this model More...
 
std::vector< CBBOXm_meshes_bbox
 individual bbox for each mesh More...
 
GLuint m_vertex_buffer = 0
 
GLuint m_index_buffer = 0
 
GLenum m_index_buffer_type = GL_INVALID_ENUM
 
std::vector< MATERIALm_materials
 
bool m_have_opaque_meshes = false
 
bool m_have_transparent_meshes = false
 
GLuint m_bbox_vertex_buffer = 0
 
GLuint m_bbox_index_buffer = 0
 
GLenum m_bbox_index_buffer_type = GL_INVALID_ENUM
 

Static Private Attributes

static const wxChar * m_logTrace = wxT( "KI_TRACE_EDA_OGL_3DMODEL" )
 
static constexpr unsigned int bbox_vtx_count = 8
 
static constexpr unsigned int bbox_idx_count = 24
 

Detailed Description

Definition at line 41 of file c_ogl_3dmodel.h.

Constructor & Destructor Documentation

◆ C_OGL_3DMODEL()

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 81 of file c_ogl_3dmodel.cpp.

83 {
84  wxLogTrace( m_logTrace, wxT( "C_OGL_3DMODEL::C_OGL_3DMODEL %u meshes %u materials" ),
85  static_cast<unsigned int>( a3DModel.m_MeshesSize ),
86  static_cast<unsigned int>( a3DModel.m_MaterialsSize ) );
87 
88  auto start_time = std::chrono::high_resolution_clock::now();
89 
90  // Validate a3DModel pointers
91  wxASSERT( a3DModel.m_Materials != nullptr );
92  wxASSERT( a3DModel.m_Meshes != nullptr );
93  wxASSERT( a3DModel.m_MaterialsSize > 0 );
94  wxASSERT( a3DModel.m_MeshesSize > 0 );
95 
96  m_material_mode = aMaterialMode;
97 
98  if( a3DModel.m_Materials == nullptr || a3DModel.m_Meshes == nullptr
99  || a3DModel.m_MaterialsSize == 0 || a3DModel.m_MeshesSize == 0 )
100  return;
101 
102  // create empty bbox for each mesh. it will be updated when the vertices
103  // are copied.
104  m_meshes_bbox.resize( a3DModel.m_MeshesSize );
105 
106  // copy materials for later use during rendering.
107  m_materials.reserve( a3DModel.m_MaterialsSize );
108 
109  for( unsigned int i = 0; i < a3DModel.m_MaterialsSize; ++i )
110  m_materials.emplace_back( a3DModel.m_Materials[i] );
111 
112  // build temporary vertex and index buffers for bounding boxes.
113  // the first box is the outer box.
114  std::vector<VERTEX> bbox_tmp_vertices( ( m_meshes_bbox.size() + 1 ) * bbox_vtx_count );
115  std::vector<GLuint> bbox_tmp_indices( ( m_meshes_bbox.size() + 1 ) * bbox_idx_count );
116 
117  // group all meshes by material.
118  // for each material create a combined vertex and index buffer.
119  // some models might have many sub-meshes. so iterate over the
120  // input meshes only once.
121  struct MESH_GROUP
122  {
123  std::vector<VERTEX> m_vertices;
124  std::vector<GLuint> m_indices;
125  };
126 
127  std::vector<MESH_GROUP> mesh_groups( m_materials.size() );
128 
129  for( unsigned int mesh_i = 0; mesh_i < a3DModel.m_MeshesSize; ++mesh_i )
130  {
131  const auto& mesh = a3DModel.m_Meshes[mesh_i];
132 
133  // silently ignore meshes that have invalid material references
134  // or invalid geometry.
135  if( mesh.m_MaterialIdx >= m_materials.size()
136  || mesh.m_Positions == nullptr
137  || mesh.m_FaceIdx == nullptr
138  || mesh.m_Normals == nullptr
139  || mesh.m_FaceIdxSize == 0
140  || mesh.m_VertexSize == 0 )
141  continue;
142 
143  auto& mesh_group = mesh_groups[mesh.m_MaterialIdx];
144  auto& material = m_materials[mesh.m_MaterialIdx];
145 
146  if( material.IsTransparent() )
148  else
149  m_have_opaque_meshes = true;
150 
151  const unsigned int vtx_offset = mesh_group.m_vertices.size();
152  mesh_group.m_vertices.resize( mesh_group.m_vertices.size() + mesh.m_VertexSize );
153 
154  // copy vertex data and update the bounding box.
155  // use material color for mesh bounding box or some sort of average
156  // vertex color.
157  glm::vec3 avg_color = material.m_Diffuse;
158 
159  for( unsigned int vtx_i = 0; vtx_i < mesh.m_VertexSize; ++vtx_i )
160  {
161  m_meshes_bbox[mesh_i].Union( mesh.m_Positions[vtx_i] );
162 
163  auto& vtx_out = mesh_group.m_vertices[vtx_offset + vtx_i];
164 
165  vtx_out.m_pos = mesh.m_Positions[vtx_i];
166  vtx_out.m_nrm = glm::clamp( glm::vec4( mesh.m_Normals[vtx_i], 1.0f ) * 127.0f,
167  -127.0f, 127.0f );
168 
169  vtx_out.m_tex_uv = mesh.m_Texcoords != nullptr
170  ? mesh.m_Texcoords[vtx_i]
171  : glm::vec2 (0);
172 
173  if( mesh.m_Color != nullptr )
174  {
175  avg_color = ( avg_color + mesh.m_Color[vtx_i] ) * 0.5f;
176 
177  vtx_out.m_color =
178  glm::clamp( glm::vec4( mesh.m_Color[vtx_i],
179  1 - material.m_Transparency ) * 255.0f,
180  0.0f, 255.0f );
181 
182  vtx_out.m_cad_color =
183  glm::clamp( glm::vec4( MaterialDiffuseToColorCAD( mesh.m_Color[vtx_i] ),
184  1 ) * 255.0f,
185  0.0f, 255.0f );
186  }
187  else
188  {
189  // the mesh will be rendered with other meshes that might have
190  // vertex colors. thus, we can't enable/disable vertex colors
191  // for individual meshes during rendering.
192 
193  // if there are no vertex colors, use material color instead.
194  vtx_out.m_color =
195  glm::clamp( glm::vec4( material.m_Diffuse,
196  1 - material.m_Transparency ) * 255.0f,
197  0.0f, 255.0f );
198 
199  vtx_out.m_cad_color =
200  glm::clamp( glm::vec4 ( MaterialDiffuseToColorCAD( material.m_Diffuse ),
201  1 ) * 255.0f,
202  0.0f, 255.0f );
203  }
204  }
205 
206  if( m_meshes_bbox[mesh_i].IsInitialized() )
207  {
208  // generate geometry for the bounding box
209  MakeBbox( m_meshes_bbox[mesh_i], ( mesh_i + 1 ) * bbox_vtx_count,
210  &bbox_tmp_vertices[( mesh_i + 1 ) * bbox_vtx_count],
211  &bbox_tmp_indices[( mesh_i + 1 ) * bbox_idx_count],
212  { avg_color, 1.0f } );
213 
214  // bump the outer bounding box
215  m_model_bbox.Union( m_meshes_bbox[mesh_i] );
216  }
217 
218 
219  // append indices of this mesh to the mesh group.
220  const unsigned int idx_offset = mesh_group.m_indices.size();
221  unsigned int use_idx_count = mesh.m_FaceIdxSize;
222 
223  if( use_idx_count % 3 != 0 )
224  {
225  wxLogTrace( m_logTrace, wxT( " index count %u not multiple of 3, truncating" ),
226  static_cast<unsigned int>( use_idx_count ) );
227  use_idx_count = ( use_idx_count / 3 ) * 3;
228  }
229 
230  mesh_group.m_indices.resize( mesh_group.m_indices.size() + use_idx_count );
231 
232  for( unsigned int idx_i = 0; idx_i < use_idx_count; ++idx_i )
233  {
234  if( mesh.m_FaceIdx[idx_i] >= mesh.m_VertexSize )
235  {
236  wxLogTrace( m_logTrace, wxT( " index %u out of range (%u)" ),
237  static_cast<unsigned int>( mesh.m_FaceIdx[idx_i] ),
238  static_cast<unsigned int>( mesh.m_VertexSize ) );
239 
240  // FIXME: should skip this triangle
241  }
242 
243  mesh_group.m_indices[idx_offset + idx_i] = mesh.m_FaceIdx[idx_i] + vtx_offset;
244  }
245  }
246 
247  // generate geometry for the outer bounding box
249  MakeBbox( m_model_bbox, 0, &bbox_tmp_vertices[0], &bbox_tmp_indices[0],
250  { 0.0f, 1.0f, 0.0f, 1.0f } );
251 
252  // create bounding box buffers
253  glGenBuffers( 1, &m_bbox_vertex_buffer );
254  glBindBuffer( GL_ARRAY_BUFFER, m_bbox_vertex_buffer );
255  glBufferData( GL_ARRAY_BUFFER, sizeof( VERTEX ) * bbox_tmp_vertices.size(),
256  bbox_tmp_vertices.data(), GL_STATIC_DRAW );
257 
258  glGenBuffers( 1, &m_bbox_index_buffer );
259  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_bbox_index_buffer );
260 
261  if( bbox_tmp_vertices.size() <= std::numeric_limits<GLushort>::max() )
262  {
263  m_bbox_index_buffer_type = GL_UNSIGNED_SHORT;
264 
265  auto u16buf = std::make_unique<GLushort[]>( bbox_tmp_indices.size() );
266 
267  for( unsigned int i = 0; i < bbox_tmp_indices.size(); ++i )
268  u16buf[i] = static_cast<GLushort>( bbox_tmp_indices[i] );
269 
270  glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( GLushort ) * bbox_tmp_indices.size(),
271  u16buf.get(), GL_STATIC_DRAW );
272  }
273  else
274  {
275  m_bbox_index_buffer_type = GL_UNSIGNED_INT;
276  glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( GLuint ) * bbox_tmp_indices.size(),
277  bbox_tmp_indices.data(), GL_STATIC_DRAW );
278  }
279 
280 
281  // merge the mesh group geometry data.
282  unsigned int total_vertex_count = 0;
283  unsigned int total_index_count = 0;
284 
285  for( auto& mg : mesh_groups )
286  {
287  total_vertex_count += mg.m_vertices.size();
288  total_index_count += mg.m_indices.size();
289  }
290 
291  wxLogTrace( m_logTrace, wxT( " total %u vertices, %u indices" ),
292  total_vertex_count, total_index_count );
293 
294  glGenBuffers( 1, &m_vertex_buffer );
295  glBindBuffer( GL_ARRAY_BUFFER, m_vertex_buffer );
296  glBufferData( GL_ARRAY_BUFFER, sizeof( VERTEX ) * total_vertex_count,
297  nullptr, GL_STATIC_DRAW );
298 
299  unsigned int idx_size = 0;
300 
301  if( total_vertex_count <= std::numeric_limits<GLushort>::max() )
302  {
303  m_index_buffer_type = GL_UNSIGNED_SHORT;
304  idx_size = sizeof( GLushort );
305  }
306  else
307  {
308  m_index_buffer_type = GL_UNSIGNED_INT;
309  idx_size = sizeof( GLuint );
310  }
311 
312  // temporary index buffer which will contain either GLushort or GLuint
313  // type indices. allocate with a bit of meadow at the end.
314  auto tmp_idx = std::make_unique<GLuint[]>(
315  ( idx_size * total_index_count + 8 ) / sizeof( GLuint ) );
316 
317  unsigned int prev_vtx_count = 0;
318  unsigned int idx_offset = 0;
319  unsigned int vtx_offset = 0;
320 
321  for( unsigned int mg_i = 0; mg_i < mesh_groups.size (); ++mg_i )
322  {
323  auto& mg = mesh_groups[mg_i];
324  auto& mat = m_materials[mg_i];
325 
326  if( m_index_buffer_type == GL_UNSIGNED_SHORT )
327  {
328  auto idx_out = reinterpret_cast<GLushort*>(
329  reinterpret_cast<uintptr_t>( tmp_idx.get() ) + idx_offset );
330 
331  for( auto idx : mg.m_indices )
332  *idx_out++ = static_cast<GLushort>( idx + prev_vtx_count );
333  }
334  else if( m_index_buffer_type == GL_UNSIGNED_INT )
335  {
336  auto idx_out = reinterpret_cast<GLuint*>(
337  reinterpret_cast<uintptr_t>( tmp_idx.get() ) + idx_offset );
338 
339  for( auto idx : mg.m_indices )
340  *idx_out++ = static_cast<GLuint>( idx + prev_vtx_count );
341  }
342 
343  glBufferSubData( GL_ARRAY_BUFFER,
344  vtx_offset,
345  mg.m_vertices.size() * sizeof( VERTEX ),
346  mg.m_vertices.data() );
347 
348  mat.m_render_idx_buffer_offset = idx_offset;
349  mat.m_render_idx_count = mg.m_indices.size();
350 
351  prev_vtx_count += mg.m_vertices.size();
352  idx_offset += mg.m_indices.size() * idx_size;
353  vtx_offset += mg.m_vertices.size() * sizeof( VERTEX );
354  }
355 
356  glGenBuffers( 1, &m_index_buffer );
357  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_index_buffer );
358  glBufferData( GL_ELEMENT_ARRAY_BUFFER, idx_size * total_index_count,
359  tmp_idx.get(), GL_STATIC_DRAW );
360 
361  glBindBuffer( GL_ARRAY_BUFFER, 0 );
362  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
363 
364  auto end_time = std::chrono::high_resolution_clock::now();
365 
366  wxLogTrace( m_logTrace, wxT( " loaded in %u ms\n" ),
367  (unsigned int)std::chrono::duration_cast<std::chrono::milliseconds> (
368  end_time - start_time).count() );
369 }
static constexpr unsigned int bbox_vtx_count
static const wxChar * m_logTrace
MATERIAL_MODE m_material_mode
static constexpr unsigned int bbox_idx_count
SMESH * m_Meshes
The meshes list of this model.
Definition: c3dmodel.h:93
bool m_have_transparent_meshes
CBBOX m_model_bbox
global bounding box for this model
void Union(const SFVEC3F &aPoint)
Function Union recalculate the bounding box adding a point.
Definition: cbbox.cpp:106
GLenum m_bbox_index_buffer_type
GLuint m_bbox_vertex_buffer
unsigned int m_MaterialsSize
Number of materials in the material array.
Definition: c3dmodel.h:95
GLenum m_index_buffer_type
SFVEC3F MaterialDiffuseToColorCAD(const SFVEC3F &aDiffuseColor)
Definition: 3d_math.h:154
GLuint m_vertex_buffer
SMATERIAL * m_Materials
The materials list of this model.
Definition: c3dmodel.h:96
GLuint m_bbox_index_buffer
std::vector< MATERIAL > m_materials
static void MakeBbox(const CBBOX &aBox, unsigned int aIdxOffset, VERTEX *aVtxOut, GLuint *aIdxOut, const glm::vec4 &aColor)
GLuint m_index_buffer
unsigned int m_MeshesSize
Number of meshes in the array.
Definition: c3dmodel.h:92
std::vector< CBBOX > m_meshes_bbox
individual bbox for each mesh
bool m_have_opaque_meshes
bool IsInitialized() const
Function IsInitialized check if this bounding box is already initialized.
Definition: cbbox.cpp:88

References bbox_idx_count, bbox_vtx_count, CBBOX::IsInitialized(), m_bbox_index_buffer, m_bbox_index_buffer_type, m_bbox_vertex_buffer, m_have_opaque_meshes, m_have_transparent_meshes, m_index_buffer, m_index_buffer_type, m_logTrace, m_material_mode, S3DMODEL::m_Materials, m_materials, S3DMODEL::m_MaterialsSize, S3DMODEL::m_Meshes, m_meshes_bbox, S3DMODEL::m_MeshesSize, m_model_bbox, m_vertex_buffer, MakeBbox(), MaterialDiffuseToColorCAD(), and CBBOX::Union().

◆ ~C_OGL_3DMODEL()

C_OGL_3DMODEL::~C_OGL_3DMODEL ( )

Definition at line 463 of file c_ogl_3dmodel.cpp.

464 {
465  if( glDeleteBuffers )
466  {
467  glDeleteBuffers( 1, &m_vertex_buffer );
468  glDeleteBuffers( 1, &m_index_buffer );
469  glDeleteBuffers( 1, &m_bbox_vertex_buffer );
470  glDeleteBuffers( 1, &m_bbox_index_buffer );
471  }
472 }
GLuint m_bbox_vertex_buffer
GLuint m_vertex_buffer
GLuint m_bbox_index_buffer
GLuint m_index_buffer

References m_bbox_index_buffer, m_bbox_vertex_buffer, m_index_buffer, and m_vertex_buffer.

Member Function Documentation

◆ BeginDrawMulti()

void C_OGL_3DMODEL::BeginDrawMulti ( bool  aUseColorInformation)
static

BeginDrawMulti - set some basic render states before drawing multiple models.

Definition at line 371 of file c_ogl_3dmodel.cpp.

372 {
373  glEnableClientState( GL_VERTEX_ARRAY );
374  glEnableClientState( GL_NORMAL_ARRAY );
375 
376  if( aUseColorInformation )
377  {
378  glEnableClientState( GL_COLOR_ARRAY );
379  glEnableClientState( GL_TEXTURE_COORD_ARRAY );
380  glEnable( GL_COLOR_MATERIAL );
381  }
382 
383  glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
384 }

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

◆ Draw()

void C_OGL_3DMODEL::Draw ( bool  aTransparent,
float  aOpacity,
bool  aUseSelectedMaterial,
SFVEC3F  aSelectionColor 
) const
private

Definition at line 399 of file c_ogl_3dmodel.cpp.

400 {
401  if( aOpacity <= FLT_EPSILON )
402  return;
403 
404 
405  if( !glBindBuffer )
406  throw std::runtime_error( "The OpenGL context no longer exists: unable to draw" );
407 
408  glBindBuffer( GL_ARRAY_BUFFER, m_vertex_buffer );
409  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_index_buffer );
410 
411  glVertexPointer( 3, GL_FLOAT, sizeof( VERTEX ),
412  reinterpret_cast<const void*>( offsetof( VERTEX, m_pos ) ) );
413 
414  glNormalPointer( GL_BYTE, sizeof( VERTEX ),
415  reinterpret_cast<const void*>( offsetof( VERTEX, m_nrm ) ) );
416 
417  glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( VERTEX ),
418  reinterpret_cast<const void*>(
420  ? offsetof( VERTEX, m_cad_color )
421  : offsetof( VERTEX, m_color ) ) );
422 
423  glTexCoordPointer( 2, GL_FLOAT, sizeof( VERTEX ),
424  reinterpret_cast<const void*>( offsetof( VERTEX, m_tex_uv ) ) );
425 
426  const SFVEC4F param = SFVEC4F( 1.0f, 1.0f, 1.0f, aOpacity );
427 
428  glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (const float*)&param.x );
429 
430  // BeginDrawMulti();
431 
432  for( auto& mat : m_materials )
433  {
434  if( ( mat.IsTransparent() != aTransparent ) &&
435  ( aOpacity >= 1.0f ) )
436  continue;
437 
438  switch( m_material_mode )
439  {
441  OGL_SetMaterial( mat, aOpacity, aUseSelectedMaterial, aSelectionColor );
442  break;
443 
445  OGL_SetDiffuseOnlyMaterial( mat.m_Diffuse, aOpacity );
446  break;
447 
449  OGL_SetDiffuseOnlyMaterial( MaterialDiffuseToColorCAD( mat.m_Diffuse ), aOpacity );
450  break;
451 
452  default:
453  break;
454  }
455 
456  glDrawElements( GL_TRIANGLES, mat.m_render_idx_count, m_index_buffer_type,
457  reinterpret_cast<const void*>( mat.m_render_idx_buffer_offset ) );
458  }
459 
460  // EndDrawMulti();
461 }
Use a gray shading based on diffuse material.
glm::vec4 SFVEC4F
Definition: xv3d_types.h:49
void OGL_SetMaterial(const SMATERIAL &aMaterial, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor)
OGL_SetMaterial - Set OpenGL materials.
Definition: ogl_utils.cpp:145
MATERIAL_MODE m_material_mode
Use only diffuse material properties.
Use all material properties from model file.
GLenum m_index_buffer_type
SFVEC3F MaterialDiffuseToColorCAD(const SFVEC3F &aDiffuseColor)
Definition: 3d_math.h:154
GLuint m_vertex_buffer
std::vector< MATERIAL > m_materials
GLuint m_index_buffer
void OGL_SetDiffuseOnlyMaterial(const SFVEC3F &aMaterialDiffuse, float aOpacity)
OGL_SetDiffuseOnlyMaterial - sets only the diffuse color and keep other parameters with default value...
Definition: ogl_utils.cpp:169

References CAD_MODE, DIFFUSE_ONLY, m_index_buffer, m_index_buffer_type, m_material_mode, m_materials, m_vertex_buffer, MaterialDiffuseToColorCAD(), NORMAL, OGL_SetDiffuseOnlyMaterial(), and OGL_SetMaterial().

Referenced by Draw_opaque(), and Draw_transparent().

◆ Draw_bbox()

void C_OGL_3DMODEL::Draw_bbox ( ) const

Draw_bbox - draw main bounding box of the model.

Definition at line 475 of file c_ogl_3dmodel.cpp.

476 {
477  if( !glBindBuffer )
478  throw std::runtime_error( "The OpenGL context no longer exists: unable to draw bbox" );
479 
480  glBindBuffer( GL_ARRAY_BUFFER, m_bbox_vertex_buffer );
481  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_bbox_index_buffer );
482 
483  glVertexPointer( 3, GL_FLOAT, sizeof( VERTEX ),
484  reinterpret_cast<const void*>( offsetof( VERTEX, m_pos ) ) );
485 
486  glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( VERTEX ),
487  reinterpret_cast<const void*>( offsetof( VERTEX, m_color ) ) );
488 
489  glDrawElements( GL_LINES, bbox_idx_count, m_bbox_index_buffer_type,
490  reinterpret_cast<const void*>( 0 ) );
491 }
static constexpr unsigned int bbox_idx_count
GLenum m_bbox_index_buffer_type
GLuint m_bbox_vertex_buffer
GLuint m_bbox_index_buffer

References bbox_idx_count, m_bbox_index_buffer, m_bbox_index_buffer_type, and m_bbox_vertex_buffer.

◆ Draw_bboxes()

void C_OGL_3DMODEL::Draw_bboxes ( ) const

Draw_bboxes - draw individual bounding boxes of each mesh.

Definition at line 494 of file c_ogl_3dmodel.cpp.

495 {
496  if( !glBindBuffer )
497  throw std::runtime_error( "The OpenGL context no longer exists: unable to draw bboxes" );
498 
499  glBindBuffer( GL_ARRAY_BUFFER, m_bbox_vertex_buffer );
500  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_bbox_index_buffer );
501 
502  glVertexPointer( 3, GL_FLOAT, sizeof( VERTEX ),
503  reinterpret_cast<const void*>( offsetof( VERTEX, m_pos ) ) );
504 
505  glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( VERTEX ),
506  reinterpret_cast<const void*>( offsetof( VERTEX, m_color ) ) );
507 
508  unsigned int idx_size = m_bbox_index_buffer_type == GL_UNSIGNED_SHORT
509  ? sizeof( GLushort ) : sizeof( GLuint );
510 
511  glDrawElements( GL_LINES, bbox_idx_count * m_meshes_bbox.size(), m_bbox_index_buffer_type,
512  reinterpret_cast<const void*>( bbox_idx_count * idx_size ) );
513 }
static constexpr unsigned int bbox_idx_count
GLenum m_bbox_index_buffer_type
GLuint m_bbox_vertex_buffer
GLuint m_bbox_index_buffer
std::vector< CBBOX > m_meshes_bbox
individual bbox for each mesh

References bbox_idx_count, m_bbox_index_buffer, m_bbox_index_buffer_type, m_bbox_vertex_buffer, and m_meshes_bbox.

◆ Draw_opaque()

void C_OGL_3DMODEL::Draw_opaque ( bool  aUseSelectedMaterial,
SFVEC3F  aSelectionColor = SFVEC3F( 0.0f ) 
) const
inline

Draw_opaque - render the model into the current context.

Definition at line 56 of file c_ogl_3dmodel.h.

56 { Draw( false, 1.0f, aUseSelectedMaterial, aSelectionColor ); }
void Draw(bool aTransparent, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor) const

References Draw().

Referenced by C3D_MODEL_VIEWER::OnPaint().

◆ Draw_transparent()

void C_OGL_3DMODEL::Draw_transparent ( float  aOpacity,
bool  aUseSelectedMaterial,
SFVEC3F  aSelectionColor = SFVEC3F( 0.0f ) 
) const
inline

Draw_transparent - render the model into the current context.

Definition at line 61 of file c_ogl_3dmodel.h.

61 { Draw( true, aOpacity, aUseSelectedMaterial, aSelectionColor ); }
void Draw(bool aTransparent, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor) const

References Draw().

Referenced by C3D_MODEL_VIEWER::OnPaint().

◆ EndDrawMulti()

void C_OGL_3DMODEL::EndDrawMulti ( )
static

EndDrawMulti - cleanup render states after drawing multiple models.

Definition at line 386 of file c_ogl_3dmodel.cpp.

387 {
388  glDisable( GL_COLOR_MATERIAL );
389  glDisableClientState( GL_VERTEX_ARRAY );
390  glDisableClientState( GL_NORMAL_ARRAY );
391  glDisableClientState( GL_COLOR_ARRAY );
392  glDisableClientState( GL_TEXTURE_COORD_ARRAY );
393 
394  glBindBuffer( GL_ARRAY_BUFFER, 0 );
395  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
396 }

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

◆ GetBBox()

const CBBOX& C_OGL_3DMODEL::GetBBox ( ) const
inline

GetBBox - Get main bbox.

Returns
the main model bbox

Definition at line 87 of file c_ogl_3dmodel.h.

87 { return m_model_bbox; }
CBBOX m_model_bbox
global bounding box for this model

References m_model_bbox.

Referenced by C3D_MODEL_VIEWER::OnPaint().

◆ Have_opaque()

bool C_OGL_3DMODEL::Have_opaque ( ) const
inline

Have_opaque - return true if have opaque meshs to render.

Definition at line 66 of file c_ogl_3dmodel.h.

66 { return m_have_opaque_meshes; }
bool m_have_opaque_meshes

References m_have_opaque_meshes.

◆ Have_transparent()

bool C_OGL_3DMODEL::Have_transparent ( ) const
inline

Have_transparent - return true if have transparent meshs to render.

Definition at line 71 of file c_ogl_3dmodel.h.

71 { return m_have_transparent_meshes; }
bool m_have_transparent_meshes

References m_have_transparent_meshes.

◆ MakeBbox()

void C_OGL_3DMODEL::MakeBbox ( const CBBOX aBox,
unsigned int  aIdxOffset,
VERTEX aVtxOut,
GLuint *  aIdxOut,
const glm::vec4 &  aColor 
)
staticprivate

Definition at line 42 of file c_ogl_3dmodel.cpp.

45 {
46  aVtxOut[0].m_pos = { aBox.Min().x, aBox.Min().y, aBox.Min().z };
47  aVtxOut[1].m_pos = { aBox.Max().x, aBox.Min().y, aBox.Min().z };
48  aVtxOut[2].m_pos = { aBox.Max().x, aBox.Max().y, aBox.Min().z };
49  aVtxOut[3].m_pos = { aBox.Min().x, aBox.Max().y, aBox.Min().z };
50 
51  aVtxOut[4].m_pos = { aBox.Min().x, aBox.Min().y, aBox.Max().z };
52  aVtxOut[5].m_pos = { aBox.Max().x, aBox.Min().y, aBox.Max().z };
53  aVtxOut[6].m_pos = { aBox.Max().x, aBox.Max().y, aBox.Max().z };
54  aVtxOut[7].m_pos = { aBox.Min().x, aBox.Max().y, aBox.Max().z };
55 
56  for( unsigned int i = 0; i < 8; ++i )
57  aVtxOut[i].m_color = aVtxOut[i].m_cad_color = glm::clamp( aColor * 255.0f, 0.0f, 255.0f );
58 
59  #define bbox_line( vtx_a, vtx_b )\
60  do { *aIdxOut++ = vtx_a + aIdxOffset; \
61  *aIdxOut++ = vtx_b + aIdxOffset; } while( 0 )
62 
63  bbox_line( 0, 1 );
64  bbox_line( 1, 2 );
65  bbox_line( 2, 3 );
66  bbox_line( 3, 0 );
67 
68  bbox_line( 4, 5 );
69  bbox_line( 5, 6 );
70  bbox_line( 6, 7 );
71  bbox_line( 7, 4 );
72 
73  bbox_line( 0, 4 );
74  bbox_line( 1, 5 );
75  bbox_line( 2, 6 );
76  bbox_line( 3, 7 );
77 
78  #undef bbox_line
79 }
const SFVEC3F & Max() const
Function Max return the maximum vertex pointer.
Definition: cbbox.h:211
#define bbox_line(vtx_a, vtx_b)
const SFVEC3F & Min() const
Function Min return the minimun vertex pointer.
Definition: cbbox.h:204

References bbox_line, C_OGL_3DMODEL::VERTEX::m_pos, CBBOX::Max(), and CBBOX::Min().

Referenced by C_OGL_3DMODEL().

Member Data Documentation

◆ bbox_idx_count

constexpr unsigned int C_OGL_3DMODEL::bbox_idx_count = 24
staticprivate

Definition at line 148 of file c_ogl_3dmodel.h.

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

◆ bbox_vtx_count

constexpr unsigned int C_OGL_3DMODEL::bbox_vtx_count = 8
staticprivate

Definition at line 147 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL().

◆ m_bbox_index_buffer

GLuint C_OGL_3DMODEL::m_bbox_index_buffer = 0
private

Definition at line 151 of file c_ogl_3dmodel.h.

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

◆ m_bbox_index_buffer_type

GLenum C_OGL_3DMODEL::m_bbox_index_buffer_type = GL_INVALID_ENUM
private

Definition at line 152 of file c_ogl_3dmodel.h.

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

◆ m_bbox_vertex_buffer

GLuint C_OGL_3DMODEL::m_bbox_vertex_buffer = 0
private

Definition at line 150 of file c_ogl_3dmodel.h.

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

◆ m_have_opaque_meshes

bool C_OGL_3DMODEL::m_have_opaque_meshes = false
private

Definition at line 142 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), and Have_opaque().

◆ m_have_transparent_meshes

bool C_OGL_3DMODEL::m_have_transparent_meshes = false
private

Definition at line 143 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), and Have_transparent().

◆ m_index_buffer

GLuint C_OGL_3DMODEL::m_index_buffer = 0
private

Definition at line 123 of file c_ogl_3dmodel.h.

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

◆ m_index_buffer_type

GLenum C_OGL_3DMODEL::m_index_buffer_type = GL_INVALID_ENUM
private

Definition at line 124 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), and Draw().

◆ m_logTrace

const wxChar * C_OGL_3DMODEL::m_logTrace = wxT( "KI_TRACE_EDA_OGL_3DMODEL" )
staticprivate

Definition at line 100 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL().

◆ m_material_mode

MATERIAL_MODE C_OGL_3DMODEL::m_material_mode
private

Definition at line 105 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), and Draw().

◆ m_materials

std::vector<MATERIAL> C_OGL_3DMODEL::m_materials
private

Definition at line 137 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), and Draw().

◆ m_meshes_bbox

std::vector<CBBOX> C_OGL_3DMODEL::m_meshes_bbox
private

individual bbox for each mesh

Definition at line 108 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), and Draw_bboxes().

◆ m_model_bbox

CBBOX C_OGL_3DMODEL::m_model_bbox
private

global bounding box for this model

Definition at line 107 of file c_ogl_3dmodel.h.

Referenced by C_OGL_3DMODEL(), and GetBBox().

◆ m_vertex_buffer

GLuint C_OGL_3DMODEL::m_vertex_buffer = 0
private

Definition at line 122 of file c_ogl_3dmodel.h.

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


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