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 } );
242 if( use_idx_count % 3 != 0 )
244 wxLogTrace(
m_logTrace, wxT(
" index count %u not multiple of 3, truncating" ),
245 static_cast<unsigned int>( use_idx_count ) );
246 use_idx_count = ( use_idx_count / 3 ) * 3;
249 for(
unsigned int idx_i = 0; idx_i < use_idx_count; idx_i += 3 )
253 wxLogTrace(
m_logTrace, wxT(
" triangle at index %u out of range (%u), skipping" ),
254 static_cast<unsigned int>( idx_i ),
260 mesh_group.m_indices.push_back( mesh.
m_FaceIdx[idx_i + 0] + vtx_offset );
261 mesh_group.m_indices.push_back( mesh.
m_FaceIdx[idx_i + 1] + vtx_offset );
262 mesh_group.m_indices.push_back( mesh.
m_FaceIdx[idx_i + 2] + vtx_offset );
269 { 0.0f, 1.0f, 0.0f, 1.0f } );
274 glBufferData( GL_ARRAY_BUFFER,
sizeof(
VERTEX ) * bbox_tmp_vertices.size(),
275 bbox_tmp_vertices.data(), GL_STATIC_DRAW );
279 if( bbox_tmp_vertices.size() <= std::numeric_limits<GLushort>::max() )
283 auto u16buf = std::make_unique<GLushort[]>( bbox_tmp_indices.size() );
285 for(
unsigned int i = 0; i < bbox_tmp_indices.size(); ++i )
286 u16buf[i] =
static_cast<GLushort
>( bbox_tmp_indices[i] );
288 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
sizeof( GLushort ) * bbox_tmp_indices.size(),
289 u16buf.get(), GL_STATIC_DRAW );
294 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
sizeof( GLuint ) * bbox_tmp_indices.size(),
295 bbox_tmp_indices.data(), GL_STATIC_DRAW );
299 unsigned int total_vertex_count = 0;
300 unsigned int total_index_count = 0;
302 for(
const MESH_GROUP& mg : mesh_groups )
304 total_vertex_count += mg.m_vertices.size();
305 total_index_count += mg.m_indices.size();
308 wxLogTrace(
m_logTrace, wxT(
" total %u vertices, %u indices" ),
309 total_vertex_count, total_index_count );
312 glBufferData( GL_ARRAY_BUFFER,
sizeof(
VERTEX ) * total_vertex_count,
313 nullptr, GL_STATIC_DRAW );
315 unsigned int idx_size = 0;
317 if( total_vertex_count <= std::numeric_limits<GLushort>::max() )
320 idx_size =
sizeof( GLushort );
325 idx_size =
sizeof( GLuint );
331 std::make_unique<GLuint[]>( ( idx_size * total_index_count + 8 ) /
sizeof( GLuint ) );
333 unsigned int prev_vtx_count = 0;
334 unsigned int idx_offset = 0;
335 unsigned int vtx_offset = 0;
337 for(
unsigned int mg_i = 0; mg_i < mesh_groups.size (); ++mg_i )
339 MESH_GROUP& mg = mesh_groups[mg_i];
341 uintptr_t tmp_idx_ptr =
reinterpret_cast<uintptr_t
>( tmp_idx.get() );
345 GLushort* idx_out =
reinterpret_cast<GLushort*
>( tmp_idx_ptr + idx_offset );
347 for( GLuint idx : mg.m_indices )
348 *idx_out++ =
static_cast<GLushort
>( idx + prev_vtx_count );
352 GLuint* idx_out =
reinterpret_cast<GLuint*
>( tmp_idx_ptr + idx_offset );
354 for( GLuint idx : mg.m_indices )
355 *idx_out++ =
static_cast<GLuint
>( idx + prev_vtx_count );
358 glBufferSubData( GL_ARRAY_BUFFER, vtx_offset, mg.m_vertices.size() *
sizeof(
VERTEX ),
359 mg.m_vertices.data() );
364 prev_vtx_count += mg.m_vertices.size();
365 idx_offset += mg.m_indices.size() * idx_size;
366 vtx_offset += mg.m_vertices.size() *
sizeof(
VERTEX );
370 glBufferData( GL_ELEMENT_ARRAY_BUFFER, idx_size * total_index_count, tmp_idx.get(),
373 glBindBuffer( GL_ARRAY_BUFFER, 0 );
374 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
376 auto end_time = std::chrono::high_resolution_clock::now();
378 wxLogTrace(
m_logTrace, wxT(
" loaded in %u ms\n" ),
379 (
unsigned int)std::chrono::duration_cast<std::chrono::milliseconds> (
380 end_time - start_time).count() );
413void MODEL_3D::Draw(
bool aTransparent,
float aOpacity,
bool aUseSelectedMaterial,
414 const SFVEC3F& aSelectionColor,
415 const glm::mat4 *aModelWorldMatrix,
416 const SFVEC3F *aCameraWorldPos )
const
418 if( aOpacity <= FLT_EPSILON )
422 throw std::runtime_error(
"The OpenGL context no longer exists: unable to draw" );
427 glVertexPointer( 3, GL_FLOAT,
sizeof(
VERTEX ),
428 reinterpret_cast<const void*
>( offsetof(
VERTEX, m_pos ) ) );
430 glNormalPointer( GL_BYTE,
sizeof(
VERTEX ),
431 reinterpret_cast<const void*
>( offsetof(
VERTEX, m_nrm ) ) );
433 glColorPointer( 4, GL_UNSIGNED_BYTE,
sizeof(
VERTEX ),
435 ? offsetof(
VERTEX, m_cad_color )
436 : offsetof(
VERTEX, m_color ) ) );
438 glTexCoordPointer( 2, GL_FLOAT,
sizeof(
VERTEX ),
439 reinterpret_cast<const void*
>( offsetof(
VERTEX, m_tex_uv ) ) );
443 glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (
const float*)¶m.x );
445 std::vector<const MODEL_3D::MATERIAL *> materialsToRender;
449 if( aModelWorldMatrix && aCameraWorldPos )
453 std::vector<std::pair<const MODEL_3D::MATERIAL*, float>> materialsSorted;
458 if( mat.m_render_idx_count == 0 )
463 if( ( mat.IsTransparent() != aTransparent )
464 && ( aOpacity >= 1.0f )
470 const BBOX_3D& bBox = mat.m_bbox;
472 const SFVEC3F bBoxWorld = *aModelWorldMatrix * glm::vec4( bBoxCenter, 1.0f );
474 const float distanceToCamera = glm::length( *aCameraWorldPos - bBoxWorld );
476 materialsSorted.emplace_back( &mat, distanceToCamera );
480 std::sort( materialsSorted.begin(), materialsSorted.end(),
481 [&]( std::pair<const MODEL_3D::MATERIAL*, float>& a,
482 std::pair<const MODEL_3D::MATERIAL*, float>& b )
484 bool aInsideB = a.first->m_bbox.Inside( b.first->m_bbox );
485 bool bInsideA = b.first->m_bbox.Inside( a.first->m_bbox );
488 if( aInsideB != bInsideA )
491 if( a.second != b.second )
492 return a.second > b.second;
494 return a.first > b.first;
497 for(
const std::pair<const MODEL_3D::MATERIAL*, float>& mat : materialsSorted )
499 materialsToRender.push_back( mat.first );
509 if( mat.m_render_idx_count == 0 )
514 if( ( mat.IsTransparent() != aTransparent )
515 && ( aOpacity >= 1.0f )
521 materialsToRender.push_back( &mat );
530 OglSetMaterial( *mat, aOpacity, aUseSelectedMaterial, aSelectionColor );
540 aUseSelectedMaterial, aSelectionColor );
548 reinterpret_cast<const void*
>(
549 static_cast<uintptr_t
>( mat->m_render_idx_buffer_offset ) ) );