OpenGEX: fix invalid access to textures. Next steps to camera and light.

pull/881/head
Kim Kulling 2016-05-08 11:09:53 +02:00
parent 513576616c
commit 82f73b6d03
2 changed files with 84 additions and 26 deletions

View File

@ -191,6 +191,21 @@ namespace OpenGEX {
USE_ODDLPARSER_NS USE_ODDLPARSER_NS
//------------------------------------------------------------------------------------------------
static void propId2StdString( Property *prop, std::string &name, std::string &key ) {
name = key = "";
if ( NULL == prop ) {
return;
}
if ( NULL != prop->m_key ) {
name = prop->m_key->m_buffer;
if ( Value::ddl_string == prop->m_value->m_type ) {
key = prop->m_value->getString();
}
}
}
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
OpenGEXImporter::VertexContainer::VertexContainer() OpenGEXImporter::VertexContainer::VertexContainer()
: m_numVerts( 0 ) : m_numVerts( 0 )
@ -238,6 +253,8 @@ OpenGEXImporter::OpenGEXImporter()
, m_currentVertices() , m_currentVertices()
, m_currentMesh( NULL ) , m_currentMesh( NULL )
, m_currentMaterial( NULL ) , m_currentMaterial( NULL )
, m_currentLight( NULL )
, m_currentCamera( nullptr )
, m_tokenType( Grammar::NoneType ) , m_tokenType( Grammar::NoneType )
, m_materialCache() , m_materialCache()
, m_cameraCache() , m_cameraCache()
@ -348,6 +365,14 @@ void OpenGEXImporter::handleNodes( DDLNode *node, aiScene *pScene ) {
handleGeometryObject( *it, pScene ); handleGeometryObject( *it, pScene );
break; break;
case Grammar::CameraObjectToken:
handleCameraObject( *it, pScene );
break;
case Grammar::LightObjectToken:
handleCameraObject( *it, pScene );
break;
case Grammar::TransformToken: case Grammar::TransformToken:
handleTransformNode( *it, pScene ); handleTransformNode( *it, pScene );
break; break;
@ -506,7 +531,18 @@ void OpenGEXImporter::handleCameraNode( DDLNode *node, aiScene *pScene ) {
aiCamera *camera( new aiCamera ); aiCamera *camera( new aiCamera );
const size_t camIdx( m_cameraCache.size() ); const size_t camIdx( m_cameraCache.size() );
m_cameraCache.push_back( camera ); m_cameraCache.push_back( camera );
m_currentCamera = camera;
aiNode *newNode = new aiNode;
m_tokenType = Grammar::CameraNodeToken;
m_currentNode = newNode;
pushNode( newNode, pScene );
handleNodes( node, pScene );
popNode();
m_currentCamera->mName.Set( newNode->mName.C_Str() );
} }
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
@ -514,6 +550,7 @@ void OpenGEXImporter::handleLightNode( ODDLParser::DDLNode *node, aiScene *pScen
aiLight *light( new aiLight ); aiLight *light( new aiLight );
const size_t lightIdx( m_lightCache.size() ); const size_t lightIdx( m_lightCache.size() );
m_lightCache.push_back( light ); m_lightCache.push_back( light );
m_currentLight = light;
aiNode *newNode = new aiNode; aiNode *newNode = new aiNode;
m_tokenType = Grammar::LightNodeToken; m_tokenType = Grammar::LightNodeToken;
@ -524,7 +561,7 @@ void OpenGEXImporter::handleLightNode( ODDLParser::DDLNode *node, aiScene *pScen
popNode(); popNode();
light->mName.Set( newNode->mName.C_Str() ); m_currentLight->mName.Set( newNode->mName.C_Str() );
} }
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
@ -599,21 +636,6 @@ void OpenGEXImporter::handleTransformNode( ODDLParser::DDLNode *node, aiScene *p
} }
} }
//------------------------------------------------------------------------------------------------
static void propId2StdString( Property *prop, std::string &name, std::string &key ) {
name = key = "";
if( NULL == prop ) {
return;
}
if( NULL != prop->m_key ) {
name = prop->m_key->m_buffer;
if( Value::ddl_string == prop->m_value->m_type ) {
key = prop->m_value->getString();
}
}
}
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
void OpenGEXImporter::handleMeshNode( ODDLParser::DDLNode *node, aiScene *pScene ) { void OpenGEXImporter::handleMeshNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
m_currentMesh = new aiMesh; m_currentMesh = new aiMesh;
@ -777,9 +799,16 @@ void OpenGEXImporter::handleIndexArrayNode( ODDLParser::DDLNode *node, aiScene *
m_currentMesh->mFaces = new aiFace[ numItems ]; m_currentMesh->mFaces = new aiFace[ numItems ];
m_currentMesh->mNumVertices = numItems * 3; m_currentMesh->mNumVertices = numItems * 3;
m_currentMesh->mVertices = new aiVector3D[ m_currentMesh->mNumVertices ]; m_currentMesh->mVertices = new aiVector3D[ m_currentMesh->mNumVertices ];
bool hasNormalCoords( false );
if ( m_currentVertices.m_numNormals > 0 ) {
m_currentMesh->mNormals = new aiVector3D[ m_currentMesh->mNumVertices ]; m_currentMesh->mNormals = new aiVector3D[ m_currentMesh->mNumVertices ];
m_currentMesh->mNumUVComponents[ 0 ] = 3; hasNormalCoords = true;
}
bool hasTexCoords( false );
if ( m_currentVertices.m_numUVComps[ 0 ] > 0 ) {
m_currentMesh->mTextureCoords[ 0 ] = new aiVector3D[ m_currentMesh->mNumVertices ]; m_currentMesh->mTextureCoords[ 0 ] = new aiVector3D[ m_currentMesh->mNumVertices ];
hasTexCoords = true;
}
unsigned int index( 0 ); unsigned int index( 0 );
for( size_t i = 0; i < m_currentMesh->mNumFaces; i++ ) { for( size_t i = 0; i < m_currentMesh->mNumFaces; i++ ) {
@ -790,15 +819,17 @@ void OpenGEXImporter::handleIndexArrayNode( ODDLParser::DDLNode *node, aiScene *
for( size_t indices = 0; indices < current.mNumIndices; indices++ ) { for( size_t indices = 0; indices < current.mNumIndices; indices++ ) {
const int idx( next->getUnsignedInt32() ); const int idx( next->getUnsignedInt32() );
ai_assert( static_cast<size_t>( idx ) <= m_currentVertices.m_numVerts ); ai_assert( static_cast<size_t>( idx ) <= m_currentVertices.m_numVerts );
aiVector3D &pos = ( m_currentVertices.m_vertices[ idx ] );
aiVector3D &normal = ( m_currentVertices.m_normals[ idx ] );
aiVector3D &tex = ( m_currentVertices.m_textureCoords[ 0 ][ idx ] );
ai_assert( index < m_currentMesh->mNumVertices ); ai_assert( index < m_currentMesh->mNumVertices );
aiVector3D &pos = ( m_currentVertices.m_vertices[ idx ] );
m_currentMesh->mVertices[ index ].Set( pos.x, pos.y, pos.z ); m_currentMesh->mVertices[ index ].Set( pos.x, pos.y, pos.z );
if ( hasNormalCoords ) {
aiVector3D &normal = ( m_currentVertices.m_normals[ idx ] );
m_currentMesh->mNormals[ index ].Set( normal.x, normal.y, normal.z ); m_currentMesh->mNormals[ index ].Set( normal.x, normal.y, normal.z );
}
if ( hasTexCoords ) {
aiVector3D &tex = ( m_currentVertices.m_textureCoords[ 0 ][ idx ] );
m_currentMesh->mTextureCoords[ 0 ][ index ].Set( tex.x, tex.y, tex.z ); m_currentMesh->mTextureCoords[ 0 ][ index ].Set( tex.x, tex.y, tex.z );
}
current.mIndices[ indices ] = index; current.mIndices[ indices ] = index;
index++; index++;
@ -920,6 +951,30 @@ void OpenGEXImporter::handleTextureNode( ODDLParser::DDLNode *node, aiScene *pSc
} }
} }
//------------------------------------------------------------------------------------------------
void OpenGEXImporter::handleParamNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
if ( NULL == node ) {
return;
}
Property *prop = node->findPropertyByName( "attrib" );
if ( nullptr != prop ) {
if ( NULL != prop->m_value ) {
Value *val( node->getValue() );
if ( NULL != val ) {
if ( "fov" == val->getString() ) {
} else if ( "near" == val->getString() ) {
}else if ( "far" == val->getString() ) {
}
}
}
}
}
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
void OpenGEXImporter::copyMeshes( aiScene *pScene ) { void OpenGEXImporter::copyMeshes( aiScene *pScene ) {
if( m_meshCache.empty() ) { if( m_meshCache.empty() ) {

View File

@ -125,6 +125,7 @@ protected:
void handleMaterialNode( ODDLParser::DDLNode *node, aiScene *pScene ); void handleMaterialNode( ODDLParser::DDLNode *node, aiScene *pScene );
void handleColorNode( ODDLParser::DDLNode *node, aiScene *pScene ); void handleColorNode( ODDLParser::DDLNode *node, aiScene *pScene );
void handleTextureNode( ODDLParser::DDLNode *node, aiScene *pScene ); void handleTextureNode( ODDLParser::DDLNode *node, aiScene *pScene );
void handleParamNode( ODDLParser::DDLNode *node, aiScene *pScene );
void copyMeshes( aiScene *pScene ); void copyMeshes( aiScene *pScene );
void resolveReferences(); void resolveReferences();
void pushNode( aiNode *node, aiScene *pScene ); void pushNode( aiNode *node, aiScene *pScene );
@ -186,6 +187,8 @@ private:
VertexContainer m_currentVertices; VertexContainer m_currentVertices;
aiMesh *m_currentMesh; aiMesh *m_currentMesh;
aiMaterial *m_currentMaterial; aiMaterial *m_currentMaterial;
aiLight *m_currentLight;
aiCamera *m_currentCamera;
int m_tokenType; int m_tokenType;
std::vector<aiMaterial*> m_materialCache; std::vector<aiMaterial*> m_materialCache;
std::vector<aiCamera*> m_cameraCache; std::vector<aiCamera*> m_cameraCache;