From 589b02c6620bedc4bdf8603249d3a9d8a4937f04 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 2 Aug 2014 09:46:47 +0200 Subject: [PATCH] 3D viewer bug fixes --- 3d-viewer/3d_canvas.cpp | 4 +- 3d-viewer/3d_draw.cpp | 1 - 3d-viewer/3d_draw_basic_functions.cpp | 10 ++- 3d-viewer/3d_mesh_model.cpp | 30 +++++--- 3d-viewer/vrml_v2_modelparser.cpp | 17 ++--- 3d-viewer/x3dmodelparser.cpp | 103 ++++++++++++++------------ 6 files changed, 93 insertions(+), 72 deletions(-) diff --git a/3d-viewer/3d_canvas.cpp b/3d-viewer/3d_canvas.cpp index 17913c4ed3..6ab883c29e 100644 --- a/3d-viewer/3d_canvas.cpp +++ b/3d-viewer/3d_canvas.cpp @@ -52,8 +52,8 @@ void CheckGLError(const char *aFileName, int aLineNumber) } errLast = err; - - wxLogError( wxT( "OpenGL error %d\nAt: %s, line: %d" ), err, aFileName, aLineNumber ); + + wxLogError( wxT( "OpenGL error %d At: %s, line: %d" ), err, GetChars( FROM_UTF8( aFileName ) ), aLineNumber ); } } diff --git a/3d-viewer/3d_draw.cpp b/3d-viewer/3d_draw.cpp index 879ce47310..e9086e656f 100644 --- a/3d-viewer/3d_draw.cpp +++ b/3d-viewer/3d_draw.cpp @@ -531,7 +531,6 @@ void EDA_3D_CANVAS::Redraw() glBindTexture( GL_TEXTURE_2D, m_text_fake_shadow_back ); glCallList( m_glLists[GL_ID_SHADOW_BACK] ); } - glColor4f( 1.0, 1.0, 1.0, 1.0 ); glEnable( GL_DEPTH_TEST ); diff --git a/3d-viewer/3d_draw_basic_functions.cpp b/3d-viewer/3d_draw_basic_functions.cpp index d098c3dc9a..8f72f3def7 100644 --- a/3d-viewer/3d_draw_basic_functions.cpp +++ b/3d-viewer/3d_draw_basic_functions.cpp @@ -172,9 +172,11 @@ void Draw3D_SolidHorizontalPolyPolygons( const CPOLYGONS_LIST& aPolysList, // Draw solid areas contained in this list CPOLYGONS_LIST polylist = aPolysList; // temporary copy for gluTessVertex + int startContour; + for( int side = 0; side < 2; side++ ) { - int startContour = 1; + startContour = 1; for( unsigned ii = 0; ii < polylist.GetCornersCount(); ii++ ) { @@ -217,6 +219,12 @@ void Draw3D_SolidHorizontalPolyPolygons( const CPOLYGONS_LIST& aPolysList, SetNormalZneg(); } + if( startContour == 0 ) + { + gluTessEndContour( tess ); + gluTessEndPolygon( tess ); + } + gluDeleteTess( tess ); if( aThickness == 0 ) diff --git a/3d-viewer/3d_mesh_model.cpp b/3d-viewer/3d_mesh_model.cpp index 52b70b09a1..4b0b19ef00 100644 --- a/3d-viewer/3d_mesh_model.cpp +++ b/3d-viewer/3d_mesh_model.cpp @@ -121,7 +121,7 @@ void S3D_MESH::openGL_Render() { if( m_Materials ) { - m_Materials->SetOpenGLMaterial(m_MaterialIndex[idx]); + m_Materials->SetOpenGLMaterial( m_MaterialIndex[idx] ); } } @@ -136,7 +136,7 @@ void S3D_MESH::openGL_Render() if( m_PerVertexNormalsNormalized.size() > 0 ) { - for(unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ ) + for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ ) { glm::vec3 normal = m_PerVertexNormalsNormalized[m_NormalIndex[idx][ii]]; glNormal3fv( &normal.x ); @@ -211,9 +211,12 @@ void S3D_MESH::calcPointNormalized () p = m_Point[i] * biggerPoint; m_PointNormalized.push_back( p ); } + + //DBG( printf("m_Point.size %u\n", m_Point.size()) ); } -bool IsClockwise(glm::vec3 v0, glm::vec3 v1, glm::vec3 v2) + +bool IsClockwise( glm::vec3 v0, glm::vec3 v1, glm::vec3 v2 ) { double sum = 0.0; @@ -253,6 +256,9 @@ void S3D_MESH::calcPerFaceNormals () m_PerFaceNormalsRaw.clear(); m_PerFaceSquaredArea.clear(); + //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++ ) { @@ -289,7 +295,7 @@ void S3D_MESH::calcPerFaceNormals () area = -area; } - if (area < FLT_EPSILON) + if( area < FLT_EPSILON ) { area = FLT_EPSILON * 2.0f; } @@ -311,10 +317,10 @@ void S3D_MESH::calcPerFaceNormals () else { // Cannot calc normal - if( (cross_prod.x > cross_prod.y) && (cross_prod.x > cross_prod.z)) + if( ( cross_prod.x > cross_prod.y ) && ( cross_prod.x > cross_prod.z ) ) { cross_prod.x = 1.0; cross_prod.y = 0.0; cross_prod.z = 0.0; - } else if( (cross_prod.y > cross_prod.x) && (cross_prod.y > cross_prod.z)) + } else if( ( cross_prod.y > cross_prod.x ) && ( cross_prod.y > cross_prod.z )) { cross_prod.x = 0.0; cross_prod.y = 1.0; cross_prod.z = 0.0; } else @@ -363,17 +369,19 @@ void S3D_MESH::calcPerPointNormals () { face_A_normals[each_vert_A_idx] = m_PerFaceNormalsRaw[each_face_A_idx] * (m_PerFaceSquaredArea[each_face_A_idx]); - // for each face A in mesh + int vertexIndex = (int)(m_CoordIndex[each_face_A_idx][each_vert_A_idx]); + glm::vec3 vector_face_A = m_PerFaceNormalsNormalized[each_face_A_idx]; + + // for each face B in mesh for( unsigned int each_face_B_idx = 0; each_face_B_idx < m_CoordIndex.size(); each_face_B_idx++ ) { //if A != B { // ignore self if ( each_face_A_idx != each_face_B_idx) { - if( (m_CoordIndex[each_face_B_idx][0] == (int)(m_CoordIndex[each_face_A_idx][each_vert_A_idx])) || - (m_CoordIndex[each_face_B_idx][1] == (int)(m_CoordIndex[each_face_A_idx][each_vert_A_idx])) || - (m_CoordIndex[each_face_B_idx][2] == (int)(m_CoordIndex[each_face_A_idx][each_vert_A_idx])) ) + if( (m_CoordIndex[each_face_B_idx][0] == vertexIndex) || + (m_CoordIndex[each_face_B_idx][1] == vertexIndex) || + (m_CoordIndex[each_face_B_idx][2] == vertexIndex) ) { - glm::vec3 vector_face_A = m_PerFaceNormalsNormalized[each_face_A_idx]; glm::vec3 vector_face_B = m_PerFaceNormalsNormalized[each_face_B_idx]; float dot_prod = glm::dot(vector_face_A, vector_face_B); diff --git a/3d-viewer/vrml_v2_modelparser.cpp b/3d-viewer/vrml_v2_modelparser.cpp index b59a2a2ad9..8d0beb4682 100644 --- a/3d-viewer/vrml_v2_modelparser.cpp +++ b/3d-viewer/vrml_v2_modelparser.cpp @@ -80,7 +80,6 @@ void VRML2_MODEL_PARSER::Load( const wxString aFilename ) #define SCALE_3D_CONV ((IU_PER_MILS * 1000.0f) / UNITS3D_TO_UNITSPCB) - //glPushMatrix(); glTranslatef( matPos.x * SCALE_3D_CONV, matPos.y * SCALE_3D_CONV, matPos.z * SCALE_3D_CONV ); glRotatef(-matRot.z, 0.0f, 0.0f, 1.0f ); @@ -140,7 +139,7 @@ int VRML2_MODEL_PARSER::read_Transform() { char text[128]; - ///DBG( printf( "Transform\n" ) ); + //DBG( printf( "Transform\n" ) ); while( GetNextTag( m_file, text ) ) { @@ -204,7 +203,7 @@ int VRML2_MODEL_PARSER::read_Transform() read_DEF(); } else { - //DBG( printf( " %s NotImplemented\n", text ) ); + DBG( printf( " %s NotImplemented\n", text ) ); read_NotImplemented( m_file, '}' ); } } @@ -264,7 +263,7 @@ int VRML2_MODEL_PARSER::read_DEF() } } - //DBG( printf( " DEF failed\n" ) ); + DBG( printf( " DEF failed\n" ) ); return -1; } @@ -302,12 +301,12 @@ int VRML2_MODEL_PARSER::read_Shape() read_IndexedFaceSet(); } else { - //DBG( printf( " %s NotImplemented\n", text ) ); + DBG( printf( " %s NotImplemented\n", text ) ); read_NotImplemented( m_file, '}' ); } } - //DBG( printf( " Shape failed\n") ); + DBG( printf( " Shape failed\n") ); return -1; } @@ -336,7 +335,7 @@ int VRML2_MODEL_PARSER::read_Appearance() } } - //DBG( printf( " Appearance failed\n") ); + DBG( printf( " Appearance failed\n") ); return -1; } @@ -407,12 +406,12 @@ int VRML2_MODEL_PARSER::read_material() return 0; } } - //DBG( printf( " read_material error: material not found\n" ) ); + DBG( printf( " read_material error: material not found\n" ) ); } } } - //DBG( printf( " failed material\n") ); + DBG( printf( " failed material\n") ); return -1; } diff --git a/3d-viewer/x3dmodelparser.cpp b/3d-viewer/x3dmodelparser.cpp index 5dc8b03ea8..dee18f6b67 100644 --- a/3d-viewer/x3dmodelparser.cpp +++ b/3d-viewer/x3dmodelparser.cpp @@ -72,20 +72,23 @@ void X3D_MODEL_PARSER::Load( const wxString aFilename ) float vrmlunits_to_3Dunits = g_Parm_3D_Visu.m_BiuTo3Dunits * UNITS3D_TO_UNITSPCB; glScalef( vrmlunits_to_3Dunits, vrmlunits_to_3Dunits, vrmlunits_to_3Dunits ); - glm::vec3 matScale( GetMaster()->m_MatScale.x, GetMaster()->m_MatScale.y, GetMaster()->m_MatScale.z ); - glm::vec3 matRot( GetMaster()->m_MatRotation.x, GetMaster()->m_MatRotation.y, GetMaster()->m_MatRotation.z ); - glm::vec3 matPos( GetMaster()->m_MatPosition.x, GetMaster()->m_MatPosition.y, GetMaster()->m_MatPosition.z ); - + glm::vec3 matScale( GetMaster()->m_MatScale.x, GetMaster()->m_MatScale.y, + GetMaster()->m_MatScale.z ); + glm::vec3 matRot( GetMaster()->m_MatRotation.x, GetMaster()->m_MatRotation.y, + GetMaster()->m_MatRotation.z ); + glm::vec3 matPos( GetMaster()->m_MatPosition.x, GetMaster()->m_MatPosition.y, + GetMaster()->m_MatPosition.z ); + #define SCALE_3D_CONV ((IU_PER_MILS * 1000.0f) / UNITS3D_TO_UNITSPCB) - //glPushMatrix(); + glTranslatef( matPos.x * SCALE_3D_CONV, matPos.y * SCALE_3D_CONV, matPos.z * SCALE_3D_CONV ); glRotatef(-matRot.z, 0.0f, 0.0f, 1.0f ); glRotatef(-matRot.y, 0.0f, 1.0f, 0.0f ); glRotatef(-matRot.x, 1.0f, 0.0f, 0.0f ); - + glScalef( matScale.x, matScale.y, matScale.z ); // Switch the locale to standard C (needed to print floating point numbers) @@ -247,25 +250,26 @@ void X3D_MODEL_PARSER::readMaterial( wxXmlNode* aMatNode ) color ) ) { DBG( printf("diffuseColor parsing error") ); - } else + } + else { - // Do not use this diffuse color + m_model->m_Materials->m_DiffuseColor.push_back( color ); } - if( !parseDoubleTriplet( properties[ wxT( "specularColor" ) ], - color ) ) + if( !parseDoubleTriplet( properties[ wxT( "specularColor" ) ], color ) ) { DBG( printf("specularColor parsing error") ); - } else + } + else { m_model->m_Materials->m_SpecularColor.push_back( color ); } - if( !parseDoubleTriplet( properties[ wxT( "emissiveColor" ) ], - color ) ) + if( !parseDoubleTriplet( properties[ wxT( "emissiveColor" ) ], color ) ) { DBG( printf("emissiveColor parsing error") ); - } else + } + else { m_model->m_Materials->m_EmissiveColor.push_back( color ); } @@ -334,12 +338,12 @@ void X3D_MODEL_PARSER::readMaterial( wxXmlNode* aMatNode ) material->m_SpecularColor[0].x, material->m_SpecularColor[0].y, material->m_SpecularColor[0].z ) ); -/* + vrml_material.Append( wxString::Format( wxT( "diffuseColor %f %f %f\n" ), material->m_DiffuseColor[0].x, material->m_DiffuseColor[0].y, material->m_DiffuseColor[0].z ) ); -*/ + vrml_material.Append( wxString::Format( wxT( "emissiveColor %f %f %f\n" ), material->m_EmissiveColor[0].x, material->m_EmissiveColor[0].y, @@ -505,8 +509,6 @@ void X3D_MODEL_PARSER::readIndexedFaceSet( wxXmlNode* aFaceNode, point.y += translation.y; point.z += translation.z; - //triplets.push_back(point); - m_model->m_Point.push_back( point ); // VRML @@ -522,44 +524,49 @@ void X3D_MODEL_PARSER::readIndexedFaceSet( wxXmlNode* aFaceNode, NODE_LIST color; GetChildsByName( aFaceNode, wxT( "Color" ), color); - PROPERTY_MAP color_properties; - // IndexedFaceSet has one Coordinate child node - GetNodeProperties( color[0], color_properties ); - - // Save points to vector as doubles - wxStringTokenizer colorpoint_tokens( color_properties[ wxT("color") ] ); - double color_point = 0.0; - - while( colorpoint_tokens.HasMoreTokens() ) + // Some models lack color information, need to handle this safely + if( !color.empty() ) { - if( colorpoint_tokens.GetNextToken().ToDouble( &color_point ) ) + PROPERTY_MAP color_properties; + // IndexedFaceSet has one Coordinate child node + GetNodeProperties( color[0], color_properties ); + + // Save points to vector as doubles + wxStringTokenizer colorpoint_tokens( color_properties[ wxT("color") ] ); + double color_point = 0.0; + + while( colorpoint_tokens.HasMoreTokens() ) { - color_points.push_back( color_point ); + if( colorpoint_tokens.GetNextToken().ToDouble( &color_point ) ) + { + color_points.push_back( color_point ); + } + else + { + wxLogError( wxT( "Error converting to double" ) ); + } } - else + + if( color_points.size() % 3 != 0 ) { - wxLogError( wxT( "Error converting to double" ) ); + DBG( printf( "Number of points is incorrect" ) ); + return; } - } - if( color_points.size() % 3 != 0 ) - { - DBG( printf( "Number of points is incorrect" ) ); - return; - } + /* Create 3D face color from 3 color points + */ + m_model->m_Materials->m_DiffuseColor.clear(); + for( unsigned id = 0; id < color_points.size() / 3; id++ ) + { + m_model->m_MaterialIndex.push_back( id ); - /* Create 3D face color from 3 color points - */ - for( unsigned id = 0; id < color_points.size() / 3; id++ ) - { - m_model->m_MaterialIndex.push_back( id ); + int color_triplet_indx = id * 3; + glm::vec3 colorface( color_points[ color_triplet_indx + 0 ], + color_points[ color_triplet_indx + 1 ], + color_points[ color_triplet_indx + 2 ] ); - int color_triplet_indx = id * 3; - glm::vec3 colorface( color_points[ color_triplet_indx + 0 ], - color_points[ color_triplet_indx + 1 ], - color_points[ color_triplet_indx + 2 ] ); - - m_model->m_Materials->m_DiffuseColor.push_back( colorface ); + m_model->m_Materials->m_DiffuseColor.push_back( colorface ); + } }