From 60330de55198f3a62cc40597de0ee16bf597f388 Mon Sep 17 00:00:00 2001 From: Andrew Zonenberg Date: Sat, 2 Aug 2014 15:20:25 -0400 Subject: [PATCH 1/3] 3D model vertex normals are now calculated in parallel --- 3d-viewer/3d_mesh_model.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/3d-viewer/3d_mesh_model.cpp b/3d-viewer/3d_mesh_model.cpp index 4b0b19ef00..b3ad2924bf 100644 --- a/3d-viewer/3d_mesh_model.cpp +++ b/3d-viewer/3d_mesh_model.cpp @@ -354,13 +354,16 @@ void S3D_MESH::calcPerPointNormals () } m_PerFaceVertexNormals.clear(); + + // Pre-allocate space for the entire vector of vertex normals so we can do parallel writes + m_PerFaceVertexNormals.resize(m_CoordIndex.size()); // for each face A in mesh + #pragma omp parallel for for( unsigned int each_face_A_idx = 0; each_face_A_idx < m_CoordIndex.size(); each_face_A_idx++ ) { // n = face A facet normal - std::vector< glm::vec3 > face_A_normals; - face_A_normals.clear(); + std::vector< glm::vec3 >& face_A_normals = m_PerFaceVertexNormals[each_face_A_idx]; face_A_normals.resize(m_CoordIndex[each_face_A_idx].size()); // loop through all 3 vertices @@ -402,7 +405,5 @@ void S3D_MESH::calcPerPointNormals () } } - - m_PerFaceVertexNormals.push_back( face_A_normals ); } } From b6e85e0f821e900277b89be57e4d7221776d2539 Mon Sep 17 00:00:00 2001 From: Andrew Zonenberg Date: Sat, 2 Aug 2014 16:17:11 -0400 Subject: [PATCH 2/3] Fixed a few code style issues, added #ifdef USE_OPENMP --- 3d-viewer/3d_mesh_model.cpp | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/3d-viewer/3d_mesh_model.cpp b/3d-viewer/3d_mesh_model.cpp index b3ad2924bf..1beaf4c199 100644 --- a/3d-viewer/3d_mesh_model.cpp +++ b/3d-viewer/3d_mesh_model.cpp @@ -24,13 +24,17 @@ /** * @file 3d_mesh_model.cpp - * @brief + * @brief */ #include <3d_mesh_model.h> #include +#ifdef USE_OPENMP +#include +#endif /* USE_OPENMP */ + S3D_MESH::S3D_MESH() { isPerFaceNormalsComputed = false; @@ -118,21 +122,21 @@ void S3D_MESH::openGL_Render() for( unsigned int idx = 0; idx < m_CoordIndex.size(); idx++ ) { if( m_MaterialIndex.size() > 1 ) - { + { if( m_Materials ) { m_Materials->SetOpenGLMaterial( m_MaterialIndex[idx] ); } - } - - + } + + switch( m_CoordIndex[idx].size() ) { case 3: glBegin( GL_TRIANGLES );break; case 4: glBegin( GL_QUADS ); break; default: glBegin( GL_POLYGON ); break; } - + if( m_PerVertexNormalsNormalized.size() > 0 ) { @@ -167,7 +171,7 @@ void S3D_MESH::openGL_Render() glNormal3fv( &normal.x ); glm::vec3 point = m_Point[m_CoordIndex[idx][ii]]; - glVertex3fv( &point.x ); + glVertex3fv( &point.x ); } } @@ -258,7 +262,7 @@ void S3D_MESH::calcPerFaceNormals () //DBG( printf("m_CoordIndex.size %u\n", m_CoordIndex.size()) ); //DBG( printf("m_PointNormalized.size %u\n", m_PointNormalized.size()) ); - + for( unsigned int idx = 0; idx < m_CoordIndex.size(); idx++ ) { @@ -307,7 +311,7 @@ void S3D_MESH::calcPerFaceNormals () if( haveAlreadyNormals_from_model_file == false ) { - // normalize vertex normal + // normalize vertex normal float l = glm::length( cross_prod ); if( l > FLT_EPSILON ) // avoid division by zero @@ -331,7 +335,7 @@ void S3D_MESH::calcPerFaceNormals () m_PerFaceNormalsNormalized.push_back( cross_prod ); } - + } } @@ -354,17 +358,19 @@ void S3D_MESH::calcPerPointNormals () } m_PerFaceVertexNormals.clear(); - + // Pre-allocate space for the entire vector of vertex normals so we can do parallel writes - m_PerFaceVertexNormals.resize(m_CoordIndex.size()); + m_PerFaceVertexNormals.resize( m_CoordIndex.size() ); // for each face A in mesh + #ifdef USE_OPENMP #pragma omp parallel for + #endif for( unsigned int each_face_A_idx = 0; each_face_A_idx < m_CoordIndex.size(); each_face_A_idx++ ) { // n = face A facet normal std::vector< glm::vec3 >& face_A_normals = m_PerFaceVertexNormals[each_face_A_idx]; - face_A_normals.resize(m_CoordIndex[each_face_A_idx].size()); + face_A_normals.resize( m_CoordIndex[each_face_A_idx].size() ); // loop through all 3 vertices // for each vert in face A @@ -396,14 +402,14 @@ void S3D_MESH::calcPerPointNormals () } } - // normalize vertex normal + // normalize vertex normal float l = glm::length( face_A_normals[each_vert_A_idx] ); if( l > FLT_EPSILON ) // avoid division by zero { face_A_normals[each_vert_A_idx] /= l; } - + } } } From 90ce21fa51b4a7611424a80dff7bde7498d893b9 Mon Sep 17 00:00:00 2001 From: Andrew Zonenberg Date: Sat, 2 Aug 2014 16:20:23 -0400 Subject: [PATCH 3/3] Added comment to ifdef for USE_OPENMP --- 3d-viewer/3d_mesh_model.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3d-viewer/3d_mesh_model.cpp b/3d-viewer/3d_mesh_model.cpp index 1beaf4c199..de8a99a595 100644 --- a/3d-viewer/3d_mesh_model.cpp +++ b/3d-viewer/3d_mesh_model.cpp @@ -365,7 +365,7 @@ void S3D_MESH::calcPerPointNormals () // for each face A in mesh #ifdef USE_OPENMP #pragma omp parallel for - #endif + #endif /* USE_OPENMP */ for( unsigned int each_face_A_idx = 0; each_face_A_idx < m_CoordIndex.size(); each_face_A_idx++ ) { // n = face A facet normal