88 wxLogTrace(
m_logTrace, wxT(
"MODEL_3D::MODEL_3D %u meshes %u materials" ),
92 auto start_time = std::chrono::high_resolution_clock::now();
102 glGenBuffers( 6, buffers );
110 wxASSERT( a3DModel.
m_Meshes !=
nullptr );
140 std::vector<VERTEX> m_vertices;
141 std::vector<GLuint> m_indices;
144 std::vector<MESH_GROUP> mesh_groups(
m_materials.size() );
146 for(
unsigned int mesh_i = 0; mesh_i < a3DModel.
m_MeshesSize; ++mesh_i )
169 const unsigned int vtx_offset = mesh_group.m_vertices.size();
170 mesh_group.m_vertices.resize( mesh_group.m_vertices.size() + mesh.
m_VertexSize );
174 glm::vec3 avg_color = material.
m_Diffuse;
178 for(
unsigned int vtx_i = 0; vtx_i < mesh.
m_VertexSize; ++vtx_i )
182 VERTEX& vtx_out = mesh_group.m_vertices[vtx_offset + vtx_i];
185 vtx_out.
m_nrm = glm::clamp( glm::vec4( mesh.
m_Normals[vtx_i], 1.0f ) * 127.0f,
193 avg_color = ( avg_color + mesh.
m_Color[vtx_i] ) * 0.5f;
196 glm::clamp( glm::vec4( mesh.
m_Color[vtx_i],
202 1 ) * 255.0f, 0.0f, 255.0f );
212 glm::clamp( glm::vec4( material.
m_Diffuse,
229 { avg_color, 1.0f } );
240 const unsigned int idx_offset = mesh_group.m_indices.size();
243 if( use_idx_count % 3 != 0 )
245 wxLogTrace(
m_logTrace, wxT(
" index count %u not multiple of 3, truncating" ),
246 static_cast<unsigned int>( use_idx_count ) );
247 use_idx_count = ( use_idx_count / 3 ) * 3;
250 mesh_group.m_indices.resize( mesh_group.m_indices.size() + use_idx_count );
252 for(
unsigned int idx_i = 0; idx_i < use_idx_count; ++idx_i )
256 wxLogTrace(
m_logTrace, wxT(
" index %u out of range (%u)" ),
257 static_cast<unsigned int>( mesh.
m_FaceIdx[idx_i] ),
263 mesh_group.m_indices[idx_offset + idx_i] = mesh.
m_FaceIdx[idx_i] + vtx_offset;
270 { 0.0f, 1.0f, 0.0f, 1.0f } );
275 glBufferData( GL_ARRAY_BUFFER,
sizeof(
VERTEX ) * bbox_tmp_vertices.size(),
276 bbox_tmp_vertices.data(), GL_STATIC_DRAW );
280 if( bbox_tmp_vertices.size() <= std::numeric_limits<GLushort>::max() )
284 auto u16buf = std::make_unique<GLushort[]>( bbox_tmp_indices.size() );
286 for(
unsigned int i = 0; i < bbox_tmp_indices.size(); ++i )
287 u16buf[i] =
static_cast<GLushort
>( bbox_tmp_indices[i] );
289 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
sizeof( GLushort ) * bbox_tmp_indices.size(),
290 u16buf.get(), GL_STATIC_DRAW );
295 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
sizeof( GLuint ) * bbox_tmp_indices.size(),
296 bbox_tmp_indices.data(), GL_STATIC_DRAW );
300 unsigned int total_vertex_count = 0;
301 unsigned int total_index_count = 0;
303 for(
const MESH_GROUP& mg : mesh_groups )
305 total_vertex_count += mg.m_vertices.size();
306 total_index_count += mg.m_indices.size();
309 wxLogTrace(
m_logTrace, wxT(
" total %u vertices, %u indices" ),
310 total_vertex_count, total_index_count );
313 glBufferData( GL_ARRAY_BUFFER,
sizeof(
VERTEX ) * total_vertex_count,
314 nullptr, GL_STATIC_DRAW );
316 unsigned int idx_size = 0;
318 if( total_vertex_count <= std::numeric_limits<GLushort>::max() )
321 idx_size =
sizeof( GLushort );
326 idx_size =
sizeof( GLuint );
332 std::make_unique<GLuint[]>( ( idx_size * total_index_count + 8 ) /
sizeof( GLuint ) );
334 unsigned int prev_vtx_count = 0;
335 unsigned int idx_offset = 0;
336 unsigned int vtx_offset = 0;
338 for(
unsigned int mg_i = 0; mg_i < mesh_groups.size (); ++mg_i )
340 MESH_GROUP& mg = mesh_groups[mg_i];
342 uintptr_t tmp_idx_ptr =
reinterpret_cast<uintptr_t
>( tmp_idx.get() );
346 GLushort* idx_out =
reinterpret_cast<GLushort*
>( tmp_idx_ptr + idx_offset );
348 for( GLuint idx : mg.m_indices )
349 *idx_out++ =
static_cast<GLushort
>( idx + prev_vtx_count );
353 GLuint* idx_out =
reinterpret_cast<GLuint*
>( tmp_idx_ptr + idx_offset );
355 for( GLuint idx : mg.m_indices )
356 *idx_out++ =
static_cast<GLuint
>( idx + prev_vtx_count );
359 glBufferSubData( GL_ARRAY_BUFFER, vtx_offset, mg.m_vertices.size() *
sizeof(
VERTEX ),
360 mg.m_vertices.data() );
365 prev_vtx_count += mg.m_vertices.size();
366 idx_offset += mg.m_indices.size() * idx_size;
367 vtx_offset += mg.m_vertices.size() *
sizeof(
VERTEX );
371 glBufferData( GL_ELEMENT_ARRAY_BUFFER, idx_size * total_index_count, tmp_idx.get(),
374 glBindBuffer( GL_ARRAY_BUFFER, 0 );
375 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
377 auto end_time = std::chrono::high_resolution_clock::now();
379 wxLogTrace(
m_logTrace, wxT(
" loaded in %u ms\n" ),
380 (
unsigned int)std::chrono::duration_cast<std::chrono::milliseconds> (
381 end_time - start_time).count() );
414void MODEL_3D::Draw(
bool aTransparent,
float aOpacity,
bool aUseSelectedMaterial,
415 const SFVEC3F& aSelectionColor,
416 const glm::mat4 *aModelWorldMatrix,
417 const SFVEC3F *aCameraWorldPos )
const
419 if( aOpacity <= FLT_EPSILON )
423 throw std::runtime_error(
"The OpenGL context no longer exists: unable to draw" );
428 glVertexPointer( 3, GL_FLOAT,
sizeof(
VERTEX ),
429 reinterpret_cast<const void*
>( offsetof(
VERTEX, m_pos ) ) );
431 glNormalPointer( GL_BYTE,
sizeof(
VERTEX ),
432 reinterpret_cast<const void*
>( offsetof(
VERTEX, m_nrm ) ) );
434 glColorPointer( 4, GL_UNSIGNED_BYTE,
sizeof(
VERTEX ),
436 ? offsetof(
VERTEX, m_cad_color )
437 : offsetof(
VERTEX, m_color ) ) );
439 glTexCoordPointer( 2, GL_FLOAT,
sizeof(
VERTEX ),
440 reinterpret_cast<const void*
>( offsetof(
VERTEX, m_tex_uv ) ) );
444 glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (
const float*)¶m.x );
446 std::vector<const MODEL_3D::MATERIAL *> materialsToRender;
450 if( aModelWorldMatrix && aCameraWorldPos )
454 std::vector<std::pair<const MODEL_3D::MATERIAL*, float>> materialsSorted;
459 if( mat.m_render_idx_count == 0 )
464 if( ( mat.IsTransparent() != aTransparent )
465 && ( aOpacity >= 1.0f )
471 const BBOX_3D& bBox = mat.m_bbox;
473 const SFVEC3F bBoxWorld = *aModelWorldMatrix * glm::vec4( bBoxCenter, 1.0f );
475 const float distanceToCamera = glm::length( *aCameraWorldPos - bBoxWorld );
477 materialsSorted.emplace_back( &mat, distanceToCamera );
481 std::sort( materialsSorted.begin(), materialsSorted.end(),
482 [&]( std::pair<const MODEL_3D::MATERIAL*, float>& a,
483 std::pair<const MODEL_3D::MATERIAL*, float>& b )
485 bool aInsideB = a.first->m_bbox.Inside( b.first->m_bbox );
486 bool bInsideA = b.first->m_bbox.Inside( a.first->m_bbox );
489 if( aInsideB != bInsideA )
492 if( a.second != b.second )
493 return a.second > b.second;
495 return a.first > b.first;
498 for(
const std::pair<const MODEL_3D::MATERIAL*, float>& mat : materialsSorted )
500 materialsToRender.push_back( mat.first );
510 if( mat.m_render_idx_count == 0 )
515 if( ( mat.IsTransparent() != aTransparent )
516 && ( aOpacity >= 1.0f )
522 materialsToRender.push_back( &mat );
531 OglSetMaterial( *mat, aOpacity, aUseSelectedMaterial, aSelectionColor );
541 aUseSelectedMaterial, aSelectionColor );
549 reinterpret_cast<const void*
>(
550 static_cast<uintptr_t
>( mat->m_render_idx_buffer_offset ) ) );