2014-07-30 09:01:25 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2015-03-28 11:33:56 +00:00
|
|
|
* Copyright (C) 2014-2015 Mario Luzeiro <mrluzeiro@gmail.com>
|
2015-02-15 21:30:34 +00:00
|
|
|
* Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
|
2014-07-30 09:01:25 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file 3d_mesh_model.cpp
|
2014-08-12 08:12:03 +00:00
|
|
|
* @brief
|
2014-07-30 09:01:25 +00:00
|
|
|
*/
|
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
#include <fctsys.h>
|
2014-07-30 09:01:25 +00:00
|
|
|
#include <3d_mesh_model.h>
|
|
|
|
#include <boost/geometry/algorithms/area.hpp>
|
2015-03-29 10:59:53 +00:00
|
|
|
#define GLM_FORCE_RADIANS
|
2015-12-08 09:56:53 +00:00
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/glm.hpp>
|
2015-03-28 11:33:56 +00:00
|
|
|
|
|
|
|
#ifdef __WXMAC__
|
|
|
|
# ifdef __DARWIN__
|
|
|
|
# include <OpenGL/glu.h>
|
|
|
|
# else
|
|
|
|
# include <glu.h>
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# include <GL/glu.h>
|
|
|
|
#endif
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2014-08-12 08:12:03 +00:00
|
|
|
#ifdef USE_OPENMP
|
|
|
|
#include <omp.h>
|
2014-08-22 10:24:14 +00:00
|
|
|
#endif // USE_OPENMP
|
2014-08-12 08:12:03 +00:00
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
#include "info3d_visu.h"
|
|
|
|
|
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
S3D_MESH::S3D_MESH()
|
|
|
|
{
|
2015-03-28 11:33:56 +00:00
|
|
|
isPerFaceNormalsComputed = false;
|
|
|
|
isPointNormalizedComputed = false;
|
|
|
|
isPerPointNormalsComputed = false;
|
|
|
|
isPerVertexNormalsVerified = false;
|
2014-07-30 09:01:25 +00:00
|
|
|
m_Materials = NULL;
|
|
|
|
childs.clear();
|
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
m_translation = glm::vec3( 0.0f, 0.0f, 0.0f );
|
|
|
|
m_rotation = glm::vec4( 0.0f, 0.0f, 0.0f, 0.0f );
|
|
|
|
m_scale = glm::vec3( 1.0f, 1.0f, 1.0f );
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
S3D_MESH::~S3D_MESH()
|
|
|
|
{
|
2015-03-28 11:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CBBOX &S3D_MESH::getBBox( )
|
|
|
|
{
|
|
|
|
if( !m_BBox.IsInitialized() )
|
|
|
|
calcBBoxAllChilds();
|
2015-04-13 20:56:15 +00:00
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
return m_BBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D_MESH::calcBBoxAllChilds( )
|
|
|
|
{
|
|
|
|
// Calc your own boudingbox
|
|
|
|
calcBBox();
|
|
|
|
|
|
|
|
for( unsigned int idx = 0; idx < childs.size(); idx++ )
|
|
|
|
m_BBox.Union( childs[idx]->getBBox() );
|
|
|
|
|
|
|
|
CBBOX tmpBBox = m_BBox;
|
|
|
|
|
|
|
|
// Calc transformation matrix
|
|
|
|
glm::mat4 fullTransformMatrix;
|
|
|
|
glm::mat4 translationMatrix = glm::translate( glm::mat4(), m_translation );
|
2015-04-13 20:56:15 +00:00
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
if( m_rotation[3] != 0.0f )
|
|
|
|
{
|
2015-03-29 10:59:53 +00:00
|
|
|
glm::mat4 rotationMatrix = glm::rotate( translationMatrix, glm::radians( m_rotation[3] ),
|
2015-03-28 11:33:56 +00:00
|
|
|
S3D_VERTEX( m_rotation[0], m_rotation[1], m_rotation[2] ) );
|
|
|
|
fullTransformMatrix = glm::scale( rotationMatrix, m_scale );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fullTransformMatrix = glm::scale( translationMatrix, m_scale );
|
|
|
|
|
2015-04-13 20:56:15 +00:00
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
// Apply transformation
|
|
|
|
m_BBox.Set( S3D_VERTEX( fullTransformMatrix * glm::vec4( tmpBBox.Min(), 1.0f ) ),
|
|
|
|
S3D_VERTEX( fullTransformMatrix * glm::vec4( tmpBBox.Max(), 1.0f ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D_MESH::calcBBox( )
|
|
|
|
{
|
|
|
|
CBBOX tmpBBox;
|
|
|
|
|
|
|
|
bool firstBBox = true;
|
|
|
|
|
|
|
|
// Calc boudingbox for all coords
|
|
|
|
for( unsigned int idx = 0; idx < m_CoordIndex.size(); idx++ )
|
|
|
|
{
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
if( firstBBox )
|
|
|
|
{
|
|
|
|
firstBBox = false;
|
|
|
|
tmpBBox = CBBOX( m_Point[m_CoordIndex[idx][ii]] ); // Initialize with the first vertex found
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tmpBBox.Union( m_Point[m_CoordIndex[idx][ii]] );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_BBox = tmpBBox;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-15 21:30:34 +00:00
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
void S3D_MESH::openGL_RenderAllChilds( bool aIsRenderingJustNonTransparentObjects,
|
|
|
|
bool aIsRenderingJustTransparentObjects )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-03-13 19:27:25 +00:00
|
|
|
glEnable( GL_COLOR_MATERIAL ) ;
|
|
|
|
SetOpenGlDefaultMaterial();
|
2014-07-31 07:01:30 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef( m_translation.x, m_translation.y, m_translation.z );
|
|
|
|
glRotatef( m_rotation[3], m_rotation[0], m_rotation[1], m_rotation[2] );
|
|
|
|
glScalef( m_scale.x, m_scale.y, m_scale.z );
|
|
|
|
|
|
|
|
// Render your self
|
2015-03-13 19:27:25 +00:00
|
|
|
openGL_Render( aIsRenderingJustNonTransparentObjects,
|
|
|
|
aIsRenderingJustTransparentObjects );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
// Render childs recursively
|
2014-07-30 09:01:25 +00:00
|
|
|
for( unsigned int idx = 0; idx < childs.size(); idx++ )
|
|
|
|
{
|
2015-03-28 11:33:56 +00:00
|
|
|
childs[idx]->openGL_RenderAllChilds( aIsRenderingJustNonTransparentObjects,
|
|
|
|
aIsRenderingJustTransparentObjects );
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SetOpenGlDefaultMaterial();
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
void S3D_MESH::openGL_Render( bool aIsRenderingJustNonTransparentObjects,
|
|
|
|
bool aIsRenderingJustTransparentObjects )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-03-13 19:27:25 +00:00
|
|
|
if( (aIsRenderingJustNonTransparentObjects == true) &&
|
|
|
|
(aIsRenderingJustTransparentObjects == true) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 07:01:30 +00:00
|
|
|
//DBG( printf( "openGL_Render" ) );
|
2014-08-21 11:59:57 +00:00
|
|
|
bool useMaterial = g_Parm_3D_Visu.GetFlag( FL_RENDER_MATERIAL );
|
|
|
|
bool smoothShapes = g_Parm_3D_Visu.IsRealisticMode()
|
2015-03-10 20:15:41 +00:00
|
|
|
&& g_Parm_3D_Visu.GetFlag( FL_RENDER_SMOOTH_NORMALS );
|
2014-07-31 07:01:30 +00:00
|
|
|
|
2015-02-15 21:30:34 +00:00
|
|
|
if( m_CoordIndex.size() == 0 )
|
2015-03-13 19:27:25 +00:00
|
|
|
{
|
2014-07-30 09:01:25 +00:00
|
|
|
return;
|
2015-03-13 19:27:25 +00:00
|
|
|
}
|
2015-04-16 07:43:27 +00:00
|
|
|
/*
|
|
|
|
// DEBUG INFO
|
|
|
|
printf("aIsRenderingJustNonTransparentObjects %d aIsRenderingJustTransparentObjects %d\n", aIsRenderingJustNonTransparentObjects, aIsRenderingJustTransparentObjects);
|
|
|
|
|
|
|
|
printf("m_CoordIndex.size() %lu\n", m_CoordIndex.size() );
|
|
|
|
printf("m_MaterialIndexPerFace.size() %lu\n", m_MaterialIndexPerFace.size() );
|
|
|
|
printf("m_MaterialIndexPerVertex.size() %lu\n", m_MaterialIndexPerVertex.size() );
|
|
|
|
printf("m_PerVertexNormalsNormalized.size() %lu\n", m_PerVertexNormalsNormalized.size() );
|
|
|
|
printf("m_PerFaceVertexNormals.size() %lu\n", m_PerFaceVertexNormals.size() );
|
2015-04-17 11:12:59 +00:00
|
|
|
printf("m_PerFaceNormalsNormalized.size() %lu\n", m_PerFaceNormalsNormalized.size() );
|
2015-04-16 08:07:10 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
printf("smoothShapes %d\n", smoothShapes );
|
2015-04-16 08:07:10 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
if( m_Materials )
|
|
|
|
{
|
|
|
|
printf(" m_Name %s\n", static_cast<const char*>(m_Materials->m_Name.c_str()) );
|
|
|
|
printf(" m_ColorPerVertex %d\n", m_Materials->m_ColorPerVertex );
|
|
|
|
printf(" m_Transparency.size() %lu\n", m_Materials->m_Transparency.size() );
|
|
|
|
printf(" m_DiffuseColor.size() %lu\n", m_Materials->m_DiffuseColor.size() );
|
|
|
|
printf(" m_Shininess.size() %lu\n", m_Materials->m_Shininess.size() );
|
|
|
|
printf(" m_EmissiveColor.size() %lu\n", m_Materials->m_EmissiveColor.size() );
|
|
|
|
printf(" m_SpecularColor.size() %lu\n", m_Materials->m_SpecularColor.size() );
|
|
|
|
printf(" m_AmbientColor.size() %lu\n", m_Materials->m_AmbientColor.size() );
|
|
|
|
}
|
|
|
|
printf("m_Materials %p\n", ( void * )m_Materials );
|
|
|
|
*/
|
|
|
|
|
|
|
|
float lastTransparency_value = 0.0f;
|
2015-03-13 19:27:25 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
if( m_Materials )
|
2015-03-13 19:27:25 +00:00
|
|
|
{
|
2015-04-17 11:12:59 +00:00
|
|
|
bool isTransparent = m_Materials->SetOpenGLMaterial( 0, useMaterial );
|
|
|
|
|
|
|
|
if( isTransparent && aIsRenderingJustNonTransparentObjects )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( !isTransparent && aIsRenderingJustTransparentObjects )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Skip total transparent models
|
|
|
|
if( useMaterial )
|
|
|
|
if( m_Materials->m_Transparency.size() > 0 )
|
|
|
|
{
|
|
|
|
lastTransparency_value = m_Materials->m_Transparency[0];
|
|
|
|
|
|
|
|
if( lastTransparency_value >= 1.0f )
|
|
|
|
return;
|
|
|
|
}
|
2015-03-13 19:27:25 +00:00
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef( m_translation.x, m_translation.y, m_translation.z );
|
|
|
|
glRotatef( m_rotation[3], m_rotation[0], m_rotation[1], m_rotation[2] );
|
|
|
|
glScalef( m_scale.x, m_scale.y, m_scale.z );
|
|
|
|
|
|
|
|
calcPointNormalized();
|
|
|
|
calcPerFaceNormals();
|
|
|
|
|
2015-03-10 20:15:41 +00:00
|
|
|
if( smoothShapes )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-03-10 20:15:41 +00:00
|
|
|
if( (m_PerVertexNormalsNormalized.size() > 0) &&
|
|
|
|
g_Parm_3D_Visu.GetFlag( FL_RENDER_USE_MODEL_NORMALS ) )
|
|
|
|
perVertexNormalsVerify_and_Repair();
|
|
|
|
else
|
2014-07-30 09:01:25 +00:00
|
|
|
calcPerPointNormals();
|
2015-03-11 13:59:43 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
2015-04-16 07:43:27 +00:00
|
|
|
/*
|
|
|
|
#if defined(DEBUG)
|
|
|
|
// Debug Normals
|
|
|
|
glColor4f( 1.0, 0.0, 1.0, 0.7 );
|
2014-07-30 09:01:25 +00:00
|
|
|
for( unsigned int idx = 0; idx < m_CoordIndex.size(); idx++ )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
if( m_PerFaceNormalsNormalized.size() > 0 )
|
2015-03-11 13:59:43 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
S3D_VERTEX normal = m_PerFaceNormalsNormalized[idx];
|
|
|
|
//glNormal3fv( &normal.x );
|
2015-03-13 19:27:25 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][0]];
|
|
|
|
for( unsigned int ii = 1; ii < m_CoordIndex[idx].size(); ii++ )
|
2015-03-13 19:27:25 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
point += m_Point[m_CoordIndex[idx][ii]];
|
2015-03-13 19:27:25 +00:00
|
|
|
}
|
2015-04-16 07:43:27 +00:00
|
|
|
|
|
|
|
point /= m_CoordIndex[idx].size();
|
|
|
|
|
|
|
|
glBegin( GL_LINES );
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
point += normal * 0.01f;
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
glEnd();
|
2015-03-11 13:59:43 +00:00
|
|
|
}
|
2015-04-16 07:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restore material
|
|
|
|
if( m_Materials )
|
|
|
|
m_Materials->SetOpenGLMaterial( 0, useMaterial );
|
|
|
|
#endif
|
|
|
|
*/
|
2014-08-12 08:12:03 +00:00
|
|
|
|
2015-03-29 16:13:58 +00:00
|
|
|
/*
|
|
|
|
#if defined(DEBUG)
|
2015-04-16 07:43:27 +00:00
|
|
|
if( smoothShapes )
|
|
|
|
{
|
|
|
|
// Debug Per Vertex Normals
|
|
|
|
glColor4f( 0.0, 1.0, 1.0, 0.7 );
|
|
|
|
for( unsigned int idx = 0; idx < m_CoordIndex.size(); idx++ )
|
2015-03-29 16:13:58 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
if( (m_PerVertexNormalsNormalized.size() > 0) &&
|
|
|
|
g_Parm_3D_Visu.GetFlag( FL_RENDER_USE_MODEL_NORMALS ) )
|
2015-03-29 16:13:58 +00:00
|
|
|
{
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
glm::vec3 normal = m_PerVertexNormalsNormalized[m_NormalIndex[idx][ii]];
|
|
|
|
//glNormal3fv( &normal.x );
|
|
|
|
|
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
|
2015-03-29 16:13:58 +00:00
|
|
|
glBegin( GL_LINES );
|
2015-04-16 07:43:27 +00:00
|
|
|
glVertex3fv( &point.x );
|
|
|
|
point += normal * 1.00f;
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector< glm::vec3 > normals_list;
|
|
|
|
normals_list = m_PerFaceVertexNormals[idx];
|
|
|
|
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
glm::vec3 normal = normals_list[ii];
|
|
|
|
printf("normal(%f, %f, %f), ", normal.x, normal.y, normal.z );
|
2015-04-16 08:07:10 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
//glNormal3fv( &normal.x );
|
|
|
|
|
2015-03-29 16:13:58 +00:00
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
|
2015-04-16 07:43:27 +00:00
|
|
|
glBegin( GL_LINES );
|
2015-03-29 16:13:58 +00:00
|
|
|
glVertex3fv( &point.x );
|
2015-04-16 07:43:27 +00:00
|
|
|
point += normal * 1.00f;
|
2015-03-29 16:13:58 +00:00
|
|
|
glVertex3fv( &point.x );
|
|
|
|
glEnd();
|
|
|
|
}
|
2015-04-16 07:43:27 +00:00
|
|
|
printf("\n");
|
2015-03-29 16:13:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-16 07:43:27 +00:00
|
|
|
|
|
|
|
// Restore material
|
|
|
|
if( m_Materials )
|
|
|
|
m_Materials->SetOpenGLMaterial( 0, useMaterial );
|
|
|
|
}
|
2015-03-29 16:13:58 +00:00
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
|
2015-10-26 07:46:19 +00:00
|
|
|
if( m_Materials && m_Materials->m_ColorPerVertex == false )
|
|
|
|
{
|
2015-10-25 18:51:41 +00:00
|
|
|
if( m_Materials->m_DiffuseColor.size() == m_Point.size() )
|
|
|
|
m_Materials->m_ColorPerVertex = true;
|
2015-10-26 07:46:19 +00:00
|
|
|
}
|
2015-10-25 18:51:41 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
for( unsigned int idx = 0; idx < m_CoordIndex.size(); idx++ )
|
|
|
|
{
|
|
|
|
if( m_Materials )
|
|
|
|
{
|
|
|
|
// http://accad.osu.edu/~pgerstma/class/vnv/resources/info/AnnotatedVrmlRef/ch3-323.htm
|
|
|
|
// "If colorPerVertex is FALSE, colours are applied to each face, as follows:"
|
|
|
|
if( ( m_Materials->m_ColorPerVertex == false ) &&
|
|
|
|
( m_Materials->m_DiffuseColor.size() > 1 ) )
|
|
|
|
{
|
|
|
|
bool isTransparent;
|
|
|
|
|
|
|
|
// "If the colorIndex field is not empty, then one colour is
|
|
|
|
// used for each face of the IndexedFaceSet. There must be
|
|
|
|
// at least as many indices in the colorIndex field as
|
|
|
|
// there are faces in the IndexedFaceSet."
|
|
|
|
if ( m_MaterialIndexPerFace.size() == m_CoordIndex.size() )
|
|
|
|
{
|
|
|
|
isTransparent = m_Materials->SetOpenGLMaterial( m_MaterialIndexPerFace[idx], useMaterial );
|
|
|
|
|
|
|
|
// Skip total transparent faces
|
|
|
|
if( useMaterial )
|
|
|
|
if( (int)m_Materials->m_Transparency.size() > m_MaterialIndexPerFace[idx] )
|
|
|
|
{
|
|
|
|
if( m_Materials->m_Transparency[m_MaterialIndexPerFace[idx]] >= 1.0f )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// "If the colorIndex field is empty, then the colours in the
|
|
|
|
// Color node are applied to each face of the IndexedFaceSet
|
|
|
|
// in order. There must be at least as many colours in the
|
|
|
|
// Color node as there are faces."
|
|
|
|
isTransparent = m_Materials->SetOpenGLMaterial( idx, useMaterial );
|
|
|
|
|
|
|
|
// Skip total transparent faces
|
|
|
|
if( useMaterial )
|
|
|
|
if( m_Materials->m_Transparency.size() > idx )
|
|
|
|
{
|
|
|
|
if( m_Materials->m_Transparency[idx] >= 1.0f )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 08:07:10 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
if( isTransparent && aIsRenderingJustNonTransparentObjects )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( !isTransparent && aIsRenderingJustTransparentObjects )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
switch( m_CoordIndex[idx].size() )
|
|
|
|
{
|
2015-02-15 21:30:34 +00:00
|
|
|
case 3:
|
|
|
|
glBegin( GL_TRIANGLES );
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
glBegin( GL_QUADS );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
glBegin( GL_POLYGON );
|
|
|
|
break;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
2014-08-12 08:12:03 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-03-10 20:15:41 +00:00
|
|
|
if( smoothShapes )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
if( m_Materials )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
// for VRML2:
|
|
|
|
// http://accad.osu.edu/~pgerstma/class/vnv/resources/info/AnnotatedVrmlRef/ch3-323.htm
|
|
|
|
// "If colorPerVertex is TRUE, colours are applied to each vertex, as follows:
|
2015-04-16 08:07:10 +00:00
|
|
|
if( ( m_Materials->m_ColorPerVertex == true ) &&
|
2015-04-16 07:43:27 +00:00
|
|
|
( m_Materials->m_DiffuseColor.size() > 1 ) )
|
2015-03-10 20:15:41 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
// "If the colorIndex field is not empty, then colours
|
|
|
|
// are applied to each vertex of the IndexedFaceSet in
|
|
|
|
// exactly the same manner that the coordIndex field is
|
|
|
|
// used to choose coordinates for each vertex from the
|
|
|
|
// Coordinate node. The colorIndex field must contain at
|
|
|
|
// least as many indices as the coordIndex field, and
|
|
|
|
// must contain end-of-face markers (-1) in exactly the
|
|
|
|
// same places as the coordIndex field. If the greatest
|
|
|
|
// index in the colorIndex field is N, then there must
|
|
|
|
// be N+1 colours in the Color node."
|
|
|
|
if ( m_MaterialIndexPerVertex.size() != 0 )
|
2015-03-29 10:59:53 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
if( (m_PerVertexNormalsNormalized.size() > 0) &&
|
|
|
|
g_Parm_3D_Visu.GetFlag( FL_RENDER_USE_MODEL_NORMALS ) )
|
|
|
|
{
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
S3D_VERTEX color = m_Materials->m_DiffuseColor[m_MaterialIndexPerVertex[idx][ii]];
|
|
|
|
glColor4f( color.x, color.y, color.z, 1.0f - lastTransparency_value );
|
|
|
|
|
|
|
|
glm::vec3 normal = m_PerVertexNormalsNormalized[m_NormalIndex[idx][ii]];
|
|
|
|
glNormal3fv( &normal.x );
|
|
|
|
|
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector< glm::vec3 > normals_list;
|
|
|
|
normals_list = m_PerFaceVertexNormals[idx];
|
|
|
|
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
S3D_VERTEX color = m_Materials->m_DiffuseColor[m_MaterialIndexPerVertex[idx][ii]];
|
|
|
|
glColor4f( color.x, color.y, color.z, 1.0f - lastTransparency_value );
|
|
|
|
|
|
|
|
glm::vec3 normal = normals_list[ii];
|
|
|
|
glNormal3fv( &normal.x );
|
|
|
|
|
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// "If the colorIndex field is empty, then the
|
|
|
|
// coordIndex field is used to choose colours from
|
|
|
|
// the Color node. If the greatest index in the
|
|
|
|
// coordIndex field is N, then there must be N+1
|
|
|
|
// colours in the Color node."
|
|
|
|
|
|
|
|
|
|
|
|
if( (m_PerVertexNormalsNormalized.size() > 0) &&
|
|
|
|
g_Parm_3D_Visu.GetFlag( FL_RENDER_USE_MODEL_NORMALS ) )
|
|
|
|
{
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
S3D_VERTEX color = m_Materials->m_DiffuseColor[m_CoordIndex[idx][ii]];
|
|
|
|
glColor4f( color.x, color.y, color.z, 1.0f - lastTransparency_value );
|
|
|
|
|
|
|
|
glm::vec3 normal = m_PerVertexNormalsNormalized[m_NormalIndex[idx][ii]];
|
|
|
|
glNormal3fv( &normal.x );
|
|
|
|
|
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector< glm::vec3 > normals_list;
|
|
|
|
normals_list = m_PerFaceVertexNormals[idx];
|
|
|
|
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
S3D_VERTEX color = m_Materials->m_DiffuseColor[m_CoordIndex[idx][ii]];
|
|
|
|
glColor4f( color.x, color.y, color.z, 1.0f - lastTransparency_value );
|
|
|
|
|
|
|
|
glm::vec3 normal = normals_list[ii];
|
|
|
|
glNormal3fv( &normal.x );
|
|
|
|
|
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( (m_PerVertexNormalsNormalized.size() > 0) &&
|
|
|
|
g_Parm_3D_Visu.GetFlag( FL_RENDER_USE_MODEL_NORMALS ) )
|
|
|
|
{
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
glm::vec3 normal = m_PerVertexNormalsNormalized[m_NormalIndex[idx][ii]];
|
|
|
|
glNormal3fv( &normal.x );
|
|
|
|
|
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector< glm::vec3 > normals_list;
|
|
|
|
normals_list = m_PerFaceVertexNormals[idx];
|
|
|
|
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
glm::vec3 normal = normals_list[ii];
|
|
|
|
glNormal3fv( &normal.x );
|
|
|
|
|
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
2015-03-10 20:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
if( (m_PerVertexNormalsNormalized.size() > 0) &&
|
|
|
|
g_Parm_3D_Visu.GetFlag( FL_RENDER_USE_MODEL_NORMALS ) )
|
|
|
|
{
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
glm::vec3 normal = m_PerVertexNormalsNormalized[m_NormalIndex[idx][ii]];
|
|
|
|
glNormal3fv( &normal.x );
|
2015-03-10 20:15:41 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-03-10 20:15:41 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
std::vector< glm::vec3 > normals_list;
|
|
|
|
normals_list = m_PerFaceVertexNormals[idx];
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
2015-03-29 10:59:53 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
glm::vec3 normal = normals_list[ii];
|
|
|
|
glNormal3fv( &normal.x );
|
2015-03-10 20:15:41 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
glm::vec3 point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
2015-03-10 20:15:41 +00:00
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
2014-08-21 11:59:57 +00:00
|
|
|
}
|
|
|
|
else
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
|
|
|
// Flat
|
2015-03-10 20:15:41 +00:00
|
|
|
if( m_PerFaceNormalsNormalized.size() > 0 )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-03-29 10:59:53 +00:00
|
|
|
S3D_VERTEX normal = m_PerFaceNormalsNormalized[idx];
|
|
|
|
glNormal3fv( &normal.x );
|
2015-03-10 20:15:41 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
if( m_Materials )
|
2015-03-29 10:59:53 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
// for VRML2:
|
|
|
|
// http://accad.osu.edu/~pgerstma/class/vnv/resources/info/AnnotatedVrmlRef/ch3-323.htm
|
|
|
|
// "If colorPerVertex is TRUE, colours are applied to each vertex, as follows:
|
2015-04-16 08:07:10 +00:00
|
|
|
if( ( m_Materials->m_ColorPerVertex == true ) &&
|
2015-04-16 07:43:27 +00:00
|
|
|
( m_Materials->m_DiffuseColor.size() > 1 ) )
|
|
|
|
{
|
|
|
|
// "If the colorIndex field is not empty, then colours
|
|
|
|
// are applied to each vertex of the IndexedFaceSet in
|
|
|
|
// exactly the same manner that the coordIndex field is
|
|
|
|
// used to choose coordinates for each vertex from the
|
|
|
|
// Coordinate node. The colorIndex field must contain at
|
|
|
|
// least as many indices as the coordIndex field, and
|
|
|
|
// must contain end-of-face markers (-1) in exactly the
|
|
|
|
// same places as the coordIndex field. If the greatest
|
|
|
|
// index in the colorIndex field is N, then there must
|
|
|
|
// be N+1 colours in the Color node."
|
|
|
|
if ( m_MaterialIndexPerVertex.size() != 0 )
|
|
|
|
{
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
S3D_VERTEX color = m_Materials->m_DiffuseColor[m_MaterialIndexPerVertex[idx][ii]];
|
|
|
|
glColor4f( color.x, color.y, color.z, 1.0f - lastTransparency_value );
|
|
|
|
|
|
|
|
S3D_VERTEX point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// "If the colorIndex field is empty, then the
|
|
|
|
// coordIndex field is used to choose colours from
|
|
|
|
// the Color node. If the greatest index in the
|
|
|
|
// coordIndex field is N, then there must be N+1
|
|
|
|
// colours in the Color node."
|
|
|
|
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
S3D_VERTEX color = m_Materials->m_DiffuseColor[m_CoordIndex[idx][ii]];
|
|
|
|
glColor4f( color.x, color.y, color.z, 1.0f - lastTransparency_value );
|
|
|
|
|
|
|
|
S3D_VERTEX point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
S3D_VERTEX point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
|
|
|
S3D_VERTEX point = m_Point[m_CoordIndex[idx][ii]];
|
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
2015-03-10 20:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
S3D_VERTEX point = m_Point[m_CoordIndex[idx][ii]];
|
2015-03-10 20:15:41 +00:00
|
|
|
glVertex3fv( &point.x );
|
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-10 20:15:41 +00:00
|
|
|
void S3D_MESH::perVertexNormalsVerify_and_Repair()
|
|
|
|
{
|
|
|
|
if( isPerVertexNormalsVerified == true )
|
|
|
|
return;
|
|
|
|
|
|
|
|
isPerVertexNormalsVerified = true;
|
|
|
|
|
|
|
|
//DBG( printf( "perVertexNormalsVerify_and_Repair\n" ) );
|
|
|
|
|
|
|
|
for( unsigned int idx = 0; idx < m_PerVertexNormalsNormalized.size(); idx++ )
|
|
|
|
{
|
|
|
|
glm::vec3 normal = m_PerVertexNormalsNormalized[idx];
|
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
if( (normal.x == 1.0f) && ((normal.y != 0.0f) || (normal.z != 0.0f)) )
|
2015-03-10 20:15:41 +00:00
|
|
|
{
|
2015-03-28 11:33:56 +00:00
|
|
|
normal.y = 0.0f;
|
|
|
|
normal.z = 0.0f;
|
2015-03-10 20:15:41 +00:00
|
|
|
}
|
|
|
|
else
|
2015-03-28 11:33:56 +00:00
|
|
|
if( (normal.y == 1.0f) && ((normal.x != 0.0f) || (normal.z != 0.0f)) )
|
2015-03-10 20:15:41 +00:00
|
|
|
{
|
2015-03-28 11:33:56 +00:00
|
|
|
normal.x = 0.0f;
|
|
|
|
normal.z = 0.0f;
|
2015-03-10 20:15:41 +00:00
|
|
|
}
|
|
|
|
else
|
2015-03-28 11:33:56 +00:00
|
|
|
if( (normal.z == 1.0f) && ((normal.x != 0.0f) || (normal.y != 0.0f)) )
|
2015-03-10 20:15:41 +00:00
|
|
|
{
|
2015-03-28 11:33:56 +00:00
|
|
|
normal.x = 0.0f;
|
|
|
|
normal.y = 0.0f;
|
2015-03-10 20:15:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( (normal.x < FLT_EPSILON) && (normal.x > -FLT_EPSILON) )
|
|
|
|
{
|
2015-03-28 11:33:56 +00:00
|
|
|
normal.x = 0.0f;
|
2015-03-10 20:15:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( (normal.y < FLT_EPSILON) && (normal.y > -FLT_EPSILON) )
|
|
|
|
{
|
2015-03-28 11:33:56 +00:00
|
|
|
normal.y = 0.0f;
|
2015-03-10 20:15:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( (normal.z < FLT_EPSILON) && (normal.z > -FLT_EPSILON) )
|
|
|
|
{
|
2015-03-28 11:33:56 +00:00
|
|
|
normal.z = 0.0f;
|
2015-03-10 20:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float l = glm::length( normal );
|
|
|
|
|
|
|
|
if( l > FLT_EPSILON ) // avoid division by zero
|
|
|
|
{
|
|
|
|
normal = normal / l;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-13 19:27:25 +00:00
|
|
|
DBG( printf( " Cannot normalize precomputed normal at idx:%u\n", idx ) );
|
2015-03-10 20:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_PerVertexNormalsNormalized[idx] = normal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-15 21:30:34 +00:00
|
|
|
void S3D_MESH::calcPointNormalized()
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-07-31 07:01:30 +00:00
|
|
|
//DBG( printf( "calcPointNormalized\n" ) );
|
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
if( isPointNormalizedComputed == true )
|
|
|
|
return;
|
2015-02-15 21:30:34 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
isPointNormalizedComputed = true;
|
|
|
|
|
|
|
|
m_PointNormalized.clear();
|
2015-03-13 19:27:25 +00:00
|
|
|
m_PointNormalized.resize( m_Point.size() );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
float biggerPoint = 0.0f;
|
2015-03-10 20:15:41 +00:00
|
|
|
for( unsigned int i = 0; i < m_Point.size(); i++ )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-03-13 19:27:25 +00:00
|
|
|
float v;
|
|
|
|
v = fabs( m_Point[i].x );
|
|
|
|
if( v > biggerPoint )
|
|
|
|
biggerPoint = v;
|
|
|
|
|
|
|
|
v = fabs( m_Point[i].y );
|
|
|
|
if( v > biggerPoint )
|
|
|
|
biggerPoint = v;
|
|
|
|
|
|
|
|
v = fabs( m_Point[i].z );
|
|
|
|
if( v > biggerPoint )
|
|
|
|
biggerPoint = v;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-15 21:30:34 +00:00
|
|
|
for( unsigned int i = 0; i < m_Point.size(); i++ )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
m_PointNormalized[i] = m_Point[i] / biggerPoint;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-02 07:46:47 +00:00
|
|
|
|
2015-02-15 21:30:34 +00:00
|
|
|
void S3D_MESH::calcPerFaceNormals()
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-07-31 07:01:30 +00:00
|
|
|
//DBG( printf( "calcPerFaceNormals" ) );
|
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
if( isPerFaceNormalsComputed == true )
|
|
|
|
return;
|
|
|
|
|
2015-02-15 21:30:34 +00:00
|
|
|
isPerFaceNormalsComputed = true;
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
bool haveAlreadyNormals_from_model_file = false;
|
|
|
|
|
2015-03-10 20:15:41 +00:00
|
|
|
if( ( m_PerFaceNormalsNormalized.size() > 0 ) &&
|
|
|
|
g_Parm_3D_Visu.GetFlag( FL_RENDER_USE_MODEL_NORMALS ) )
|
2015-03-29 10:59:53 +00:00
|
|
|
{
|
2014-07-30 09:01:25 +00:00
|
|
|
haveAlreadyNormals_from_model_file = true;
|
2015-03-29 10:59:53 +00:00
|
|
|
|
|
|
|
// !TODO: this is a workarround for some VRML2 modules files (ex: from we-online.de website)
|
2015-04-16 07:43:27 +00:00
|
|
|
// are using (incorrectly) the normals with m_CoordIndex as per face normal.
|
|
|
|
// This maybe be addressed by the parser in the future.
|
2015-03-29 10:59:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-03-10 20:15:41 +00:00
|
|
|
else
|
2015-03-29 10:59:53 +00:00
|
|
|
{
|
2015-03-10 20:15:41 +00:00
|
|
|
m_PerFaceNormalsNormalized.clear();
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
2015-03-13 19:27:25 +00:00
|
|
|
|
|
|
|
m_PerFaceNormalsNormalized.resize( m_CoordIndex.size() );
|
2015-03-29 10:59:53 +00:00
|
|
|
|
|
|
|
m_PerFaceNormalsRaw_X_PerFaceSquaredArea.clear();
|
2015-03-13 19:27:25 +00:00
|
|
|
m_PerFaceNormalsRaw_X_PerFaceSquaredArea.resize( m_CoordIndex.size() );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-02-15 21:30:34 +00:00
|
|
|
// There are no points defined for the coordIndex
|
|
|
|
if( m_PointNormalized.size() == 0 )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-02-15 21:30:34 +00:00
|
|
|
m_CoordIndex.clear();
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-02-15 21:30:34 +00:00
|
|
|
for( unsigned int idx = 0; idx < m_CoordIndex.size(); idx++ )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
glm::dvec3 cross_prod = glm::dvec3( 0.0, 0.0, 0.0 );
|
2015-03-13 19:27:25 +00:00
|
|
|
|
|
|
|
// Newell's Method
|
|
|
|
// http://www.opengl.org/wiki/Calculating_a_Surface_Normal
|
|
|
|
// http://tog.acm.org/resources/GraphicsGems/gemsiii/newell.c
|
|
|
|
// http://www.iquilezles.org/www/articles/areas/areas.htm
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < m_CoordIndex[idx].size(); i++ )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-04-16 08:07:10 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
glm::dvec3 u = glm::dvec3( m_PointNormalized[m_CoordIndex[idx][i]] );
|
|
|
|
glm::dvec3 v = glm::dvec3( m_PointNormalized[m_CoordIndex[idx][(i + 1) % m_CoordIndex[idx].size()]] );
|
2015-03-13 19:27:25 +00:00
|
|
|
|
|
|
|
cross_prod.x += (u.y - v.y) * (u.z + v.z);
|
|
|
|
cross_prod.y += (u.z - v.z) * (u.x + v.x);
|
|
|
|
cross_prod.z += (u.x - v.x) * (u.y + v.y);
|
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
double area = glm::dot( cross_prod, cross_prod );
|
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
area = fabs( area );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
m_PerFaceNormalsRaw_X_PerFaceSquaredArea[idx] = glm::vec3( cross_prod * area );
|
|
|
|
|
|
|
|
//printf("cross_prod(%g, %g, %g), area:%g m_PerFaceNormalsRaw_X_PerFaceSquaredArea(%f, %f, %f)\n", cross_prod.x, cross_prod.y, cross_prod.z, area,
|
|
|
|
//m_PerFaceNormalsRaw_X_PerFaceSquaredArea[idx].x,
|
|
|
|
//m_PerFaceNormalsRaw_X_PerFaceSquaredArea[idx].y,
|
|
|
|
//m_PerFaceNormalsRaw_X_PerFaceSquaredArea[idx].z);
|
2014-07-30 09:01:25 +00:00
|
|
|
|
|
|
|
if( haveAlreadyNormals_from_model_file == false )
|
|
|
|
{
|
2015-03-29 10:59:53 +00:00
|
|
|
if( g_Parm_3D_Visu.GetFlag( FL_RENDER_USE_MODEL_NORMALS ) &&
|
|
|
|
(m_PerVertexNormalsNormalized.size() > 0) )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
glm::dvec3 normalSum;
|
2015-03-29 10:59:53 +00:00
|
|
|
|
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[idx].size(); ii++ )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
normalSum += glm::dvec3( m_PerVertexNormalsNormalized[m_NormalIndex[idx][ii]] );
|
2015-02-15 21:30:34 +00:00
|
|
|
}
|
2015-03-29 10:59:53 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
double l = glm::length( normalSum );
|
2015-03-29 10:59:53 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
if( l > DBL_EPSILON ) // avoid division by zero
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-03-29 10:59:53 +00:00
|
|
|
normalSum = normalSum / l;
|
2015-02-15 21:30:34 +00:00
|
|
|
}
|
2015-03-29 10:59:53 +00:00
|
|
|
else
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-03-29 10:59:53 +00:00
|
|
|
if( ( normalSum.x > normalSum.y ) && ( normalSum.x > normalSum.z ) )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
normalSum.x = 0.0;
|
|
|
|
normalSum.y = 1.0;
|
|
|
|
normalSum.z = 0.0;
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
|
|
|
else if( ( normalSum.y > normalSum.x ) && ( normalSum.y > normalSum.z ) )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
normalSum.x = 0.0;
|
|
|
|
normalSum.y = 1.0;
|
|
|
|
normalSum.z = 0.0;
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
|
|
|
else if( ( normalSum.z > normalSum.x ) && ( normalSum.z > normalSum.y ) )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
normalSum.x = 0.0;
|
|
|
|
normalSum.y = 0.0;
|
|
|
|
normalSum.z = 1.0;
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
normalSum.x = 0.0;
|
|
|
|
normalSum.y = 0.0;
|
|
|
|
normalSum.z = 0.0;
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
m_PerFaceNormalsNormalized[idx] = glm::vec3( normalSum );
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// normalize vertex normal
|
2015-04-16 07:43:27 +00:00
|
|
|
double l = glm::length( cross_prod );
|
2015-03-29 10:59:53 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
if( l > DBL_EPSILON ) // avoid division by zero
|
2015-03-29 10:59:53 +00:00
|
|
|
{
|
|
|
|
cross_prod = cross_prod / l;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
2015-03-13 19:27:25 +00:00
|
|
|
else
|
|
|
|
{
|
2015-04-16 08:07:10 +00:00
|
|
|
|
2015-03-29 10:59:53 +00:00
|
|
|
/*
|
|
|
|
for( unsigned int i = 0; i < m_CoordIndex[idx].size(); i++ )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
glm::vec3 v = m_Point[m_CoordIndex[idx][i]];
|
2015-03-29 10:59:53 +00:00
|
|
|
DBG( printf( "v[%u](%f, %f, %f)", i, v.x, v.y, v.z ) );
|
|
|
|
}
|
2015-04-16 07:43:27 +00:00
|
|
|
DBG( printf( "Cannot calc normal idx: %u cross(%g, %g, %g) l:%g m_CoordIndex[idx].size: %u\n",
|
2015-03-29 10:59:53 +00:00
|
|
|
idx,
|
|
|
|
cross_prod.x, cross_prod.y, cross_prod.z,
|
|
|
|
l,
|
|
|
|
(unsigned int)m_CoordIndex[idx].size()) );
|
2015-04-16 08:07:10 +00:00
|
|
|
|
2015-03-29 10:59:53 +00:00
|
|
|
*/
|
2015-04-13 20:56:15 +00:00
|
|
|
|
2015-03-29 10:59:53 +00:00
|
|
|
if( ( cross_prod.x > cross_prod.y ) && ( cross_prod.x > cross_prod.z ) )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
cross_prod.x = 0.0;
|
|
|
|
cross_prod.y = 1.0;
|
|
|
|
cross_prod.z = 0.0;
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
|
|
|
else if( ( cross_prod.y > cross_prod.x ) && ( cross_prod.y > cross_prod.z ) )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
cross_prod.x = 0.0;
|
|
|
|
cross_prod.y = 1.0;
|
|
|
|
cross_prod.z = 0.0;
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
|
|
|
else if( ( cross_prod.z > cross_prod.x ) && ( cross_prod.z > cross_prod.y ) )
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
cross_prod.x = 0.0;
|
|
|
|
cross_prod.y = 0.0;
|
|
|
|
cross_prod.z = 1.0;
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-16 07:43:27 +00:00
|
|
|
cross_prod.x = 0.0;
|
|
|
|
cross_prod.y = 0.0;
|
|
|
|
cross_prod.z = 0.0;
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
2015-03-13 19:27:25 +00:00
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-04-16 07:43:27 +00:00
|
|
|
m_PerFaceNormalsNormalized[idx] = glm::vec3( cross_prod );
|
|
|
|
//printf("normal(%g, %g, %g)\n", m_PerFaceNormalsNormalized[idx].x, m_PerFaceNormalsNormalized[idx].y, m_PerFaceNormalsNormalized[idx].z );
|
2015-03-29 10:59:53 +00:00
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 21:30:34 +00:00
|
|
|
|
2015-03-28 11:33:56 +00:00
|
|
|
// Documentation literature
|
2014-07-30 09:01:25 +00:00
|
|
|
// http://www.bytehazard.com/code/vertnorm.html
|
|
|
|
// http://www.emeyex.com/site/tuts/VertexNormals.pdf
|
2015-02-15 21:30:34 +00:00
|
|
|
void S3D_MESH::calcPerPointNormals()
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2014-07-31 07:01:30 +00:00
|
|
|
//DBG( printf( "calcPerPointNormals" ) );
|
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
if( isPerPointNormalsComputed == true )
|
|
|
|
return;
|
2015-02-15 21:30:34 +00:00
|
|
|
|
2014-07-30 09:01:25 +00:00
|
|
|
isPerPointNormalsComputed = true;
|
|
|
|
|
|
|
|
m_PerFaceVertexNormals.clear();
|
|
|
|
|
2014-08-12 08:12:03 +00:00
|
|
|
// Pre-allocate space for the entire vector of vertex normals so we can do parallel writes
|
|
|
|
m_PerFaceVertexNormals.resize( m_CoordIndex.size() );
|
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
for( unsigned int each_face_A_idx = 0; each_face_A_idx < m_CoordIndex.size(); each_face_A_idx++ )
|
|
|
|
{
|
|
|
|
m_PerFaceVertexNormals[each_face_A_idx].resize( m_CoordIndex[each_face_A_idx].size() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize each vertex normal
|
|
|
|
for( unsigned int each_face_A_idx = 0; each_face_A_idx < m_CoordIndex.size(); each_face_A_idx++ )
|
|
|
|
{
|
|
|
|
glm::vec3 initVertexFaceNormal = m_PerFaceNormalsRaw_X_PerFaceSquaredArea[each_face_A_idx];
|
2015-04-13 20:56:15 +00:00
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
std::vector< glm::vec3 >& face_A_normals = m_PerFaceVertexNormals[each_face_A_idx];
|
|
|
|
|
|
|
|
for( unsigned int each_vert_A_idx = 0; each_vert_A_idx < m_CoordIndex[each_face_A_idx].size(); each_vert_A_idx++ )
|
|
|
|
{
|
|
|
|
face_A_normals[each_vert_A_idx] = initVertexFaceNormal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 20:56:15 +00:00
|
|
|
|
2014-08-12 08:12:03 +00:00
|
|
|
#ifdef USE_OPENMP
|
|
|
|
#pragma omp parallel for
|
|
|
|
#endif /* USE_OPENMP */
|
2015-02-15 21:30:34 +00:00
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
// for each face A in mesh
|
2014-07-30 09:01:25 +00:00
|
|
|
for( unsigned int each_face_A_idx = 0; each_face_A_idx < m_CoordIndex.size(); each_face_A_idx++ )
|
|
|
|
{
|
|
|
|
// n = face A facet normal
|
2014-08-12 08:12:03 +00:00
|
|
|
std::vector< glm::vec3 >& face_A_normals = m_PerFaceVertexNormals[each_face_A_idx];
|
2015-04-13 20:56:15 +00:00
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
// loop through all vertices
|
2014-07-30 09:01:25 +00:00
|
|
|
// for each vert in face A
|
|
|
|
for( unsigned int each_vert_A_idx = 0; each_vert_A_idx < m_CoordIndex[each_face_A_idx].size(); each_vert_A_idx++ )
|
|
|
|
{
|
2015-03-13 19:27:25 +00:00
|
|
|
int vertexIndexFromFaceA = (int)(m_CoordIndex[each_face_A_idx][each_vert_A_idx]);
|
2014-08-02 07:46:47 +00:00
|
|
|
glm::vec3 vector_face_A = m_PerFaceNormalsNormalized[each_face_A_idx];
|
|
|
|
|
|
|
|
// for each face B in mesh
|
2014-07-30 09:01:25 +00:00
|
|
|
for( unsigned int each_face_B_idx = 0; each_face_B_idx < m_CoordIndex.size(); each_face_B_idx++ )
|
|
|
|
{
|
|
|
|
//if A != B { // ignore self
|
2015-02-15 21:30:34 +00:00
|
|
|
if( each_face_A_idx != each_face_B_idx )
|
2014-07-30 09:01:25 +00:00
|
|
|
{
|
2015-03-10 20:15:41 +00:00
|
|
|
for( unsigned int ii = 0; ii < m_CoordIndex[each_face_B_idx].size(); ii++ )
|
|
|
|
{
|
2015-03-13 19:27:25 +00:00
|
|
|
// Check if there is any vertice in the face B that touch the vertice in face A
|
|
|
|
if( m_CoordIndex[each_face_B_idx][ii] == vertexIndexFromFaceA )
|
2015-03-10 20:15:41 +00:00
|
|
|
{
|
2015-03-13 19:27:25 +00:00
|
|
|
glm::vec3 vector_face_B = m_PerFaceNormalsNormalized[each_face_B_idx];
|
2015-03-11 13:59:43 +00:00
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
float dot_prod = glm::dot( vector_face_A, vector_face_B );
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
if( dot_prod > 0.05f )
|
|
|
|
face_A_normals[each_vert_A_idx] += m_PerFaceNormalsRaw_X_PerFaceSquaredArea[each_face_B_idx] * dot_prod;
|
2015-02-15 21:30:34 +00:00
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
// For each face, only one vertice can touch / share
|
|
|
|
// another vertice from the other face, so we exit here
|
|
|
|
break;
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-13 19:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
|
2015-03-13 19:27:25 +00:00
|
|
|
|
|
|
|
#ifdef USE_OPENMP
|
|
|
|
#pragma omp parallel for
|
|
|
|
#endif /* USE_OPENMP */
|
|
|
|
|
|
|
|
// Normalize
|
|
|
|
for( unsigned int each_face_A_idx = 0; each_face_A_idx < m_CoordIndex.size(); each_face_A_idx++ )
|
|
|
|
{
|
|
|
|
std::vector< glm::vec3 >& face_A_normals = m_PerFaceVertexNormals[each_face_A_idx];
|
|
|
|
|
|
|
|
for( unsigned int each_vert_A_idx = 0; each_vert_A_idx < m_CoordIndex[each_face_A_idx].size(); each_vert_A_idx++ )
|
|
|
|
{
|
2014-07-30 09:01:25 +00:00
|
|
|
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;
|
2014-08-12 08:12:03 +00:00
|
|
|
}
|
2014-07-30 09:01:25 +00:00
|
|
|
}
|
|
|
|
}
|