Commit c0518db7 authored by unknown's avatar unknown Committed by jean-pierre charras

3D viewer: Some more support cases to VRML2 parser and fix some issues.

parent 4b680dd5
......@@ -44,6 +44,7 @@
#include <class_pcb_text.h>
#include <colors_selection.h>
#include <convert_basic_shapes_to_polygon.h>
#define GLM_FORCE_RADIANS
#include <gal/opengl/glm/gtc/matrix_transform.hpp>
#include <gal/opengl/opengl_compositor.h>
#ifdef __WINDOWS__
......@@ -1401,13 +1402,13 @@ void EDA_3D_CANVAS::calcBBox()
if( module->GetOrientation() )
fullTransformMatrix = glm::rotate( fullTransformMatrix,
(float)(module->GetOrientation() / 10.0f),
glm::radians( (float)(module->GetOrientation() / 10.0f) ),
S3D_VERTEX( 0.0f, 0.0f, 1.0f ) );
if( module->IsFlipped() )
{
fullTransformMatrix = glm::rotate( fullTransformMatrix, 180.0f, S3D_VERTEX( 0.0f, 1.0f, 0.0f ) );
fullTransformMatrix = glm::rotate( fullTransformMatrix, 180.0f, S3D_VERTEX( 0.0f, 0.0f, 1.0f ) );
fullTransformMatrix = glm::rotate( fullTransformMatrix, glm::radians( 180.0f ), S3D_VERTEX( 0.0f, 1.0f, 0.0f ) );
fullTransformMatrix = glm::rotate( fullTransformMatrix, glm::radians( 180.0f ), S3D_VERTEX( 0.0f, 0.0f, 1.0f ) );
}
// Compute a union bounding box for all the shapes of the model
......
......@@ -31,6 +31,7 @@
#include <common.h>
#include <base_struct.h>
#define GLM_FORCE_RADIANS
#include <gal/opengl/glm/glm.hpp>
class S3D_MASTER;
......
......@@ -30,8 +30,9 @@
#include <fctsys.h>
#include <3d_mesh_model.h>
#include <boost/geometry/algorithms/area.hpp>
#define GLM_FORCE_RADIANS
#include <gal/opengl/glm/gtc/matrix_transform.hpp>
#include <gal/opengl/glm/glm.hpp>
#include <gal/opengl/glm/glm.hpp>
#ifdef __WXMAC__
# ifdef __DARWIN__
......@@ -102,7 +103,7 @@ void S3D_MESH::calcBBoxAllChilds( )
if( m_rotation[3] != 0.0f )
{
glm::mat4 rotationMatrix = glm::rotate( translationMatrix, m_rotation[3],
glm::mat4 rotationMatrix = glm::rotate( translationMatrix, glm::radians( m_rotation[3] ),
S3D_VERTEX( m_rotation[0], m_rotation[1], m_rotation[2] ) );
fullTransformMatrix = glm::scale( rotationMatrix, m_scale );
}
......@@ -237,7 +238,7 @@ void S3D_MESH::openGL_Render( bool aIsRenderingJustNonTransparentObjects,
for( unsigned int idx = 0; idx < m_CoordIndex.size(); idx++ )
{
if( m_Materials )
if( m_Materials && ( m_MaterialIndex.size() != 0 ) )
{
if ( m_MaterialIndex.size() > idx )
{
......@@ -284,13 +285,15 @@ void S3D_MESH::openGL_Render( bool aIsRenderingJustNonTransparentObjects,
{
glm::vec3 normal = m_PerVertexNormalsNormalized[m_NormalIndex[idx][ii]];
glNormal3fv( &normal.x );
// Flag error vertices
/*
#if defined(DEBUG)
// Flag error vertices
if ((normal.x == 0.0) && (normal.y == 0.0) && (normal.z == 0.0))
{
glColor4f( 1.0, 0.0, 1.0, 1.0 );
}
#endif
*/
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
glVertex3fv( &point.x );
}
......@@ -305,11 +308,15 @@ void S3D_MESH::openGL_Render( bool aIsRenderingJustNonTransparentObjects,
glm::vec3 normal = normals_list[ii];
glNormal3fv( &normal.x );
// Flag error vertices
/*
#if defined(DEBUG)
// Flag error vertices
if ((normal.x == 0.0) && (normal.y == 0.0) && (normal.z == 0.0))
{
glColor4f( 1.0, 0.0, 1.0, 1.0 );
}
#endif
*/
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
glVertex3fv( &point.x );
......@@ -321,18 +328,21 @@ void S3D_MESH::openGL_Render( bool aIsRenderingJustNonTransparentObjects,
// Flat
if( m_PerFaceNormalsNormalized.size() > 0 )
{
glm::vec3 normal = m_PerFaceNormalsNormalized[idx];
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
{
glNormal3fv( &normal.x );
// Flag error vertices
S3D_VERTEX normal = m_PerFaceNormalsNormalized[idx];
/*
#if defined(DEBUG)
if ((normal.x == 0.0) && (normal.y == 0.0) && (normal.z == 0.0))
// Flag error vertices
if( (normal.x == 0.0) && (normal.y == 0.0) && (normal.z == 0.0) )
{
DBG( printf("%u\n", idx) );
glColor4f( 1.0, 0.0, 1.0, 1.0 );
}
#endif
*/
glNormal3fv( &normal.x );
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
{
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
glVertex3fv( &point.x );
}
......@@ -479,13 +489,29 @@ void S3D_MESH::calcPerFaceNormals()
if( ( m_PerFaceNormalsNormalized.size() > 0 ) &&
g_Parm_3D_Visu.GetFlag( FL_RENDER_USE_MODEL_NORMALS ) )
{
haveAlreadyNormals_from_model_file = true;
// !TODO: this is a workarround for some VRML2 modules files (ex: from we-online.de website)
// are using (incorrectly) the normals with m_CoordIndex as per face normal. This maybe be addressed by the parser in the future.
if( ( m_PerFaceNormalsNormalized.size() == m_Point.size() ) &&
( m_PerFaceNormalsNormalized.size() != m_CoordIndex.size() ) )
{
//DBG( printf("m_PerFaceNormalsNormalized.size() != m_CoordIndex.size() Appling a workarroudn recover\n") );
m_NormalIndex = m_CoordIndex;
m_PerVertexNormalsNormalized = m_PerFaceNormalsNormalized;
m_PerFaceNormalsNormalized.clear();
haveAlreadyNormals_from_model_file = false;
}
}
else
{
m_PerFaceNormalsNormalized.clear();
m_PerFaceNormalsRaw_X_PerFaceSquaredArea.clear();
}
m_PerFaceNormalsNormalized.resize( m_CoordIndex.size() );
m_PerFaceNormalsRaw_X_PerFaceSquaredArea.clear();
m_PerFaceNormalsRaw_X_PerFaceSquaredArea.resize( m_CoordIndex.size() );
// There are no points defined for the coordIndex
......@@ -530,6 +556,54 @@ void S3D_MESH::calcPerFaceNormals()
m_PerFaceNormalsRaw_X_PerFaceSquaredArea[idx] = cross_prod * area;
if( haveAlreadyNormals_from_model_file == false )
{
if( g_Parm_3D_Visu.GetFlag( FL_RENDER_USE_MODEL_NORMALS ) &&
(m_PerVertexNormalsNormalized.size() > 0) )
{
glm::vec3 normalSum;
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
{
normalSum += m_PerVertexNormalsNormalized[m_NormalIndex[idx][ii]];
}
float l = glm::length( normalSum );
if( l > FLT_EPSILON ) // avoid division by zero
{
normalSum = normalSum / l;
}
else
{
if( ( normalSum.x > normalSum.y ) && ( normalSum.x > normalSum.z ) )
{
normalSum.x = 0.0f;
normalSum.y = 1.0f;
normalSum.z = 0.0f;
}
else if( ( normalSum.y > normalSum.x ) && ( normalSum.y > normalSum.z ) )
{
normalSum.x = 0.0f;
normalSum.y = 1.0f;
normalSum.z = 0.0f;
}
else if( ( normalSum.z > normalSum.x ) && ( normalSum.z > normalSum.y ) )
{
normalSum.x = 0.0f;
normalSum.y = 0.0f;
normalSum.z = 1.0f;
}
else
{
normalSum.x = 0.0f;
normalSum.y = 0.0f;
normalSum.z = 0.0f;
}
}
m_PerFaceNormalsNormalized[idx] = normalSum;
}
else
{
// normalize vertex normal
float l = glm::length( cross_prod );
......@@ -540,12 +614,19 @@ void S3D_MESH::calcPerFaceNormals()
}
else
{
/* DBG( printf( "Cannot calc normal idx: %u cross(%f, %f, %f) l:%f m_CoordIndex[idx].size: %u\n",
/*
for( unsigned int i = 0; i < m_CoordIndex[idx].size(); i++ )
{
glm::vec3 v = m_PointNormalized[m_CoordIndex[idx][i]];
DBG( printf( "v[%u](%f, %f, %f)", i, v.x, v.y, v.z ) );
}
DBG( printf( "Cannot calc normal idx: %u cross(%f, %f, %f) l:%f m_CoordIndex[idx].size: %u\n",
idx,
cross_prod.x, cross_prod.y, cross_prod.z,
l,
(unsigned int)m_CoordIndex[idx].size()) );
*/
*/
if( ( cross_prod.x > cross_prod.y ) && ( cross_prod.x > cross_prod.z ) )
{
cross_prod.x = 0.0f;
......@@ -575,6 +656,7 @@ void S3D_MESH::calcPerFaceNormals()
m_PerFaceNormalsNormalized[idx] = cross_prod;
}
}
}
}
......
......@@ -31,6 +31,7 @@
#define __3D_MESH_MODEL_H__
#include <vector>
#define GLM_FORCE_RADIANS
#include <gal/opengl/glm/glm.hpp>
#include "3d_struct.h"
#include "3d_material.h"
......@@ -55,6 +56,7 @@ public:
std::vector< S3D_VERTEX > m_Point;
std::vector< std::vector<int> > m_CoordIndex;
std::vector< std::vector<int> > m_NormalIndex;
std::vector< S3D_VERTEX > m_PerFaceColor;
std::vector< S3D_VERTEX > m_PerFaceNormalsNormalized;
std::vector< S3D_VERTEX > m_PerVertexNormalsNormalized;
std::vector< int > m_MaterialIndex;
......
......@@ -33,6 +33,7 @@
#include <macros.h>
#include <kicad_string.h>
#include <pgm_base.h>
#define GLM_FORCE_RADIANS
#include <gal/opengl/glm/gtc/matrix_transform.hpp>
#include <3d_viewer.h>
#include <info3d_visu.h>
......@@ -176,11 +177,11 @@ void S3D_MASTER::calcBBox()
m_MatPosition.z * SCALE_3D_CONV) );
if( m_MatRotation.z != 0.0 )
fullTransformMatrix = glm::rotate( fullTransformMatrix, -(float)m_MatRotation.z, S3D_VERTEX( 0.0f, 0.0f, 1.0f ) );
fullTransformMatrix = glm::rotate( fullTransformMatrix, glm::radians(-(float)m_MatRotation.z), S3D_VERTEX( 0.0f, 0.0f, 1.0f ) );
if( m_MatRotation.y != 0.0 )
fullTransformMatrix = glm::rotate( fullTransformMatrix, -(float)m_MatRotation.y, S3D_VERTEX( 0.0f, 1.0f, 0.0f ) );
fullTransformMatrix = glm::rotate( fullTransformMatrix, glm::radians(-(float)m_MatRotation.y), S3D_VERTEX( 0.0f, 1.0f, 0.0f ) );
if( m_MatRotation.x != 0.0 )
fullTransformMatrix = glm::rotate( fullTransformMatrix, -(float)m_MatRotation.x, S3D_VERTEX( 1.0f, 0.0f, 0.0f ) );
fullTransformMatrix = glm::rotate( fullTransformMatrix, glm::radians(-(float)m_MatRotation.x), S3D_VERTEX( 1.0f, 0.0f, 0.0f ) );
fullTransformMatrix = glm::scale( fullTransformMatrix, S3D_VERTEX( m_MatScale.x, m_MatScale.y, m_MatScale.z ) );
......
......@@ -188,7 +188,7 @@ void EDA_3D_FRAME::CreateMenuBar()
KiBitmap( green_xpm ), wxITEM_CHECK );
AddMenuItem( renderOptionsMenu, ID_MENU3D_FL_RENDER_SHOW_MODEL_BBOX,
_( "Show Model Bouding Boxes" ),
_( "Show Model Bounding Boxes" ),
KiBitmap( green_xpm ), wxITEM_CHECK );
prefsMenu->AppendSeparator();
......
......@@ -29,6 +29,7 @@
#ifndef _3D_TYPES_H_
#define _3D_TYPES_H_
#define GLM_FORCE_RADIANS
#include <gal/opengl/glm/glm.hpp>
#include <base_units.h> // for IU_PER_MILS
......
......@@ -30,7 +30,7 @@
#ifndef CBBox_h
#define CBBox_h
#define GLM_FORCE_RADIANS
#include <gal/opengl/glm/glm.hpp>
#include <3d_types.h>
......
......@@ -219,6 +219,9 @@ private:
S3D_MODEL_PARSER* m_ModelParser;
S3D_MASTER* m_Master;
wxString m_debugSpacer; ///< Used to give identation space
int m_counter_DEF_GROUP; ///< Counts the number of DEF * GROUPS used
int m_counter_USE_GROUP; ///< Counts the number of USE * used, in the end, if m_counter_DEF_GROUP > 0 and m_counter_USE_GROUP == 0 then it will add the first group with childs
};
......
......@@ -34,6 +34,7 @@
#include <common.h>
#include <macros.h>
#include <base_struct.h>
#define GLM_FORCE_RADIANS
#include <gal/opengl/glm/glm.hpp>
#include <vector>
#include <kicad_string.h>
......
......@@ -59,6 +59,8 @@ VRML2_MODEL_PARSER::VRML2_MODEL_PARSER( S3D_MODEL_PARSER* aModelParser )
m_normalPerVertex = true;
colorPerVertex = true;
m_debugSpacer = "";
m_counter_DEF_GROUP = 0;
m_counter_USE_GROUP = 0;
}
......@@ -263,6 +265,35 @@ int VRML2_MODEL_PARSER::loadFileModel( S3D_MESH *aTransformationModel )
}
}
// There are VRML2 files, as an example, exported by:
// "Generated by TraceParts CAD VRML Translator" that define the group (DEF * GROUP)
// but don't use it in the end, so force it to add the DEF GROUP
if( ( m_counter_DEF_GROUP > 0 ) && (m_counter_USE_GROUP == 0 ) )
{
wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "loadFileModel: groups defined with DEF * GROUP but not used with USE, forcing add it..." ) );
if( m_defGroupMap.size() > 0 )
{
for( VRML2_DEF_GROUP_MAP::iterator groupIt = m_defGroupMap.begin(); groupIt!=m_defGroupMap.end(); ++groupIt )
{
S3D_MESH* ptrModel = groupIt->second;
if( ((ptrModel->m_Point.size() == 0) || (ptrModel->m_CoordIndex.size() == 0)) &&
(ptrModel->childs.size() == 0) )
{
// Skip this because dont have data to add
continue;
}
else
{
m_ModelParser->childs.push_back( ptrModel );
}
}
}
}
debug_exit();
return 0;
}
......@@ -508,6 +539,9 @@ int VRML2_MODEL_PARSER::read_Transform()
}
else
{
m_counter_USE_GROUP++;
wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_Transform: USE %s Add child model with %lu points, %lu coordIndex, %lu childs." ),
useLabel,
ptrModel->m_Point.size(),
......@@ -784,6 +818,8 @@ int VRML2_MODEL_PARSER::read_DEF()
// It will be the same as read a new Transform
if( read_Transform() == 0 )
{
m_counter_DEF_GROUP++;
std::string groupName = tagName;
m_defGroupMap[groupName] = new_mesh_model;
......@@ -1024,17 +1060,26 @@ int VRML2_MODEL_PARSER::read_appearance()
wxString mat_name;
mat_name = FROM_UTF8( text );
bool found = false;
for( material = m_Master->m_Materials; material; material = material->Next() )
{
if( material->m_Name == mat_name )
{
m_model->m_Materials = material;
debug_exit();
return 0;
// We dont exit here, since it seems that VRML can have
// multiple material defined, so, it will copy the latest one that was defined.
found = true;
}
}
debug_exit();
if( found )
return 0;
wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_appearance error: material not found" ) );
return -1;
}
// Exit loop with error
......@@ -1096,19 +1141,21 @@ int VRML2_MODEL_PARSER::read_material()
if( GetNextTag( m_file, text, sizeof(text) ) )
{
if( strcmp( text, "Material" ) == 0 )
{
// Check if it is NULL, if not, it is because we come
// from an appearance DEF and already have a pointer
if( m_model->m_Materials == NULL )
{
wxString mat_name;
material = new S3D_MATERIAL( m_Master, mat_name );
m_Master->Insert( material );
m_model->m_Materials = material;
}
if( strcmp( text, "Material" ) == 0 )
{
int ret = read_Material();
debug_exit();
return ret;
}
}
else if( strcmp( text, "DEF" ) == 0 )
{
if( GetNextTag( m_file, text, sizeof(text) ) )
......@@ -1462,7 +1509,7 @@ int VRML2_MODEL_PARSER::read_coordIndex()
}
}
//wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_coordIndex m_CoordIndex.size: %u" ), (unsigned int)m_model->m_CoordIndex.size() );
wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_coordIndex m_CoordIndex.size: %lu" ), m_model->m_CoordIndex.size() );
debug_exit();
return 0;
}
......@@ -1518,11 +1565,10 @@ int VRML2_MODEL_PARSER::read_Normal()
if( *text == '}' )
{
// Debug
//if( m_normalPerVertex == false )
// wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_Normal m_PerFaceNormalsNormalized.size: %u" ), (unsigned int)m_model->m_PerFaceNormalsNormalized.size() );
//else
// wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_Normal m_PerVertexNormalsNormalized.size: %u" ), (unsigned int)m_model->m_PerVertexNormalsNormalized.size() );
if( m_normalPerVertex == false )
wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_Normal m_PerFaceNormalsNormalized.size: %lu" ), m_model->m_PerFaceNormalsNormalized.size() );
else
wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_Normal m_PerVertexNormalsNormalized.size: %lu" ), m_model->m_PerVertexNormalsNormalized.size() );
debug_exit();
wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_Normal exit" ) );
return 0;
......@@ -1562,9 +1608,9 @@ int VRML2_MODEL_PARSER::read_Coordinate()
if( *text == '}' )
{
wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_Coordinate m_Point.size: %lu" ), m_model->m_Point.size() );
debug_exit();
wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_Coordinate exit" ) );
//wxLogTrace( traceVrmlV2Parser, m_debugSpacer + wxT( "read_Coordinate m_Point.size: %u" ), (unsigned int)m_model->m_Point.size() );
return 0;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment