Переглянути джерело

- BUGFIX: Fixed a crash in the normal-loader method of obj loader.
- CHANGE: Improve and fix a bug in texture coordinate loading in obj loader.
- CHANGE: Use ai_assert instead of assert.

git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@112 67173fc5-114c-0410-ac8e-9d2fd5bffc1f

kimmi 17 роки тому
батько
коміт
fa8edfe207

+ 16 - 1
code/ObjFileData.h

@@ -44,6 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <vector>
 #include <map>
 #include "../include/aiTypes.h"
+#include "../include/aiMesh.h"
 
 namespace Assimp
 {
@@ -151,12 +152,16 @@ struct Material
 	//!	Illumination model 
 	int illumination_model;
 
+	//!	Constructor
 	Material()
 	{
+		// empty
 	}
 
+	// Destructor
 	~Material()
 	{
+		// empty
 	}
 };
 
@@ -165,19 +170,27 @@ struct Material
 //!	\brief	Data structure to store a mesh
 struct Mesh
 {
+	///	Array with pointer to all stored faces
 	std::vector<Face*> m_Faces;
+	///	Assigned material
 	Material *m_pMaterial;
+	///	Number of stored indices.
 	unsigned int m_uiNumIndices;
+	/// Number of UV
+	unsigned int m_uiUVCoordinates[ AI_MAX_NUMBER_OF_TEXTURECOORDS ];
+	///	Material index.
 	unsigned int m_uiMaterialIndex;
 
+	///	Constructor
 	Mesh() :
 		m_pMaterial(NULL),
 		m_uiNumIndices(0),
 		m_uiMaterialIndex(0)
 	{
-		// empty
+		memset(m_uiUVCoordinates, 0, sizeof( unsigned int ) * AI_MAX_NUMBER_OF_TEXTURECOORDS);
 	}
 
+	///	Destructor
 	~Mesh() 
 	{
 		// empty
@@ -260,6 +273,8 @@ struct Model
 	}
 };
 
+// ------------------------------------------------------------------------------------------------
+
 } // Namespace ObjFile
 } // Namespace Assimp
 

+ 92 - 33
code/ObjFileImporter.cpp

@@ -1,3 +1,43 @@
+/*
+---------------------------------------------------------------------------
+Open Asset Import Library (ASSIMP)
+---------------------------------------------------------------------------
+
+Copyright (c) 2006-2008, ASSIMP Development Team
+
+All rights reserved.
+
+Redistribution and use of this software in source and binary forms, 
+with or without modification, are permitted provided that the following 
+conditions are met:
+
+* Redistributions of source code must retain the above
+  copyright notice, this list of conditions and the
+  following disclaimer.
+
+* Redistributions in binary form must reproduce the above
+  copyright notice, this list of conditions and the
+  following disclaimer in the documentation and/or other
+  materials provided with the distribution.
+
+* Neither the name of the ASSIMP team, nor the names of its
+  contributors may be used to endorse or promote products
+  derived from this software without specific prior
+  written permission of the ASSIMP Development Team.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+---------------------------------------------------------------------------
+*/
 #include "ObjFileImporter.h"
 #include "ObjFileParser.h"
 #include "ObjFileData.h"
@@ -6,8 +46,8 @@
 #include "../include/aiMesh.h"
 #include "../include/aiScene.h"
 #include "../include/aiAssert.h"
-#include "MaterialSystem.h"
 #include "../include/DefaultLogger.h"
+#include "MaterialSystem.h"
 
 #include <boost/scoped_ptr.hpp>
 #include <boost/format.hpp>
@@ -116,7 +156,7 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene
 	else
 	{
 		// This is an error, so break down the application
-		assert (false);
+		ai_assert (false);
 	}
 
 	// Create nodes for the whole scene	
@@ -224,29 +264,33 @@ void ObjFileImporter::createTopology(const ObjFile::Model* pModel,
 
 	// Create faces
 	ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
-	pMesh->mNumFaces = (unsigned int) pObjMesh->m_Faces.size();
-	pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
-	pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
-
-	// Copy all data from all stored meshes
-	for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
+	ai_assert( NULL != pObjMesh );
+	pMesh->mNumFaces = static_cast<unsigned int>( pObjMesh->m_Faces.size() );
+	if ( pMesh->mNumFaces > 0 )
 	{
-		aiFace *pFace = &pMesh->mFaces[ index ];
-		const unsigned int uiNumIndices = (unsigned int) pObjMesh->m_Faces[ index ]->m_pVertices->size();
-		pFace->mNumIndices = (unsigned int) uiNumIndices;
-		if (pFace->mNumIndices > 0)
+		pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
+		pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
+
+		// Copy all data from all stored meshes
+		for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
 		{
-			pFace->mIndices = new unsigned int[ uiNumIndices ];
-			ObjFile::Face::IndexArray *pIndexArray = pObjMesh->m_Faces[ index ]->m_pVertices;
-			ai_assert ( NULL != pIndexArray );
-			for ( size_t a=0; a<pFace->mNumIndices; a++ )
+			aiFace *pFace = &pMesh->mFaces[ index ];
+			const unsigned int uiNumIndices = (unsigned int) pObjMesh->m_Faces[ index ]->m_pVertices->size();
+			pFace->mNumIndices = (unsigned int) uiNumIndices;
+			if (pFace->mNumIndices > 0)
 			{
-				pFace->mIndices[ a ] = pIndexArray->at( a );
+				pFace->mIndices = new unsigned int[ uiNumIndices ];
+				ObjFile::Face::IndexArray *pIndexArray = pObjMesh->m_Faces[ index ]->m_pVertices;
+				ai_assert ( NULL != pIndexArray );
+				for ( size_t a=0; a<pFace->mNumIndices; a++ )
+				{
+					pFace->mIndices[ a ] = pIndexArray->at( a );
+				}
+			}
+			else
+			{
+				pFace->mIndices = NULL;
 			}
-		}
-		else
-		{
-			pFace->mIndices = NULL;
 		}
 	}
 
@@ -255,13 +299,14 @@ void ObjFileImporter::createTopology(const ObjFile::Model* pModel,
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Creates a vretex array
 void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel, 
 										const ObjFile::Object* pCurrentObject, 
 										unsigned int uiMeshIndex,
 										aiMesh* pMesh)
 {
 	// Checking preconditions
-	ai_assert ( NULL != pCurrentObject );
+	ai_assert( NULL != pCurrentObject );
 	
 	// Break, if no faces are stored in object
 	if (pCurrentObject->m_Faces.empty())
@@ -283,18 +328,25 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
 	// Allocate buffer for texture coordinates
 	if ( !pModel->m_TextureCoord.empty() )
 	{
-		for ( size_t i=0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; i++)
-			pMesh->mTextureCoords[ i ] = new aiVector3D[ pModel->m_TextureCoord.size() ];
+		for ( size_t i=0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; i++ )
+		{
+			const unsigned int num_uv = pObjMesh->m_uiUVCoordinates[ i ];
+			if ( num_uv > 0 )
+			{
+				pMesh->mNumUVComponents[ i ] = num_uv;
+				pMesh->mTextureCoords[ i ] = new aiVector3D[ num_uv ];
+			}
+		}
 	}
 	
 	// Copy vertices, normals and textures into aiMesh instance
 	unsigned int newIndex = 0;
 	for ( size_t index=0; index < pObjMesh->m_Faces.size(); index++ )
 	{
-		// get destination face
+		// Get destination face
 		aiFace *pDestFace = &pMesh->mFaces[ index ];
 		
-		// get source face
+		// Get source face
 		ObjFile::Face *pSourceFace = pObjMesh->m_Faces[ index ]; 
 
 		// Copy all index arrays
@@ -304,32 +356,38 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
 			assert ( vertex < pModel->m_Vertices.size() );
 			pMesh->mVertices[ newIndex ] = *pModel->m_Vertices[ vertex ];
 			
-			if ( !pModel->m_Normals.empty() )
+			// Copy all normals 
+			if ( !pSourceFace->m_pNormals->empty() )
 			{
 				const unsigned int normal = pSourceFace->m_pNormals->at( vertexIndex );
-				assert( normal < pModel->m_Normals.size() );
+				ai_assert( normal < pModel->m_Normals.size() );
 				pMesh->mNormals[ newIndex ] = *pModel->m_Normals[ normal ];
 			}
 			
+			// Copy all texture coordinates
 			if ( !pModel->m_TextureCoord.empty() )
 			{
 				const unsigned int tex = pSourceFace->m_pTexturCoords->at( vertexIndex );
-				ai_assert ( tex < pModel->m_TextureCoord.size() );
+				ai_assert( tex < pModel->m_TextureCoord.size() );
 				for ( size_t i=0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; i++)
 				{
-					aiVector2D coord2d = *pModel->m_TextureCoord[ tex ];
-					pMesh->mTextureCoords[ i ][ newIndex ] = aiVector3D( coord2d.x, coord2d.y, 0.0 );
+					if ( pMesh->mNumUVComponents[ i ] > 0 )
+					{
+						aiVector2D coord2d = *pModel->m_TextureCoord[ tex ];
+						pMesh->mTextureCoords[ i ][ newIndex ] = aiVector3D( coord2d.x, coord2d.y, 0.0 );
+					}
 				}
 			}
 
-			assert( pMesh->mNumVertices > newIndex );
+			ai_assert( pMesh->mNumVertices > newIndex );
 			pDestFace->mIndices[ vertexIndex ] = newIndex;
-			newIndex++;
+			++newIndex;
 		}
 	}	
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Counts all stored meshes 
 void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects, int &iNumMeshes)
 {
 	iNumMeshes = 0;
@@ -349,6 +407,7 @@ void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Creates tha material 
 void ObjFileImporter::createMaterial(const ObjFile::Model* pModel, const ObjFile::Object* pData, 
 									 aiScene* pScene)
 {

+ 7 - 1
code/ObjFileImporter.h

@@ -57,8 +57,10 @@ struct Object;
 struct Model;
 }
 
+// ------------------------------------------------------------------------------------------------
 ///	\class	ObjFileImporter
 ///	\brief	Imports a waveform obj file
+// ------------------------------------------------------------------------------------------------
 class ObjFileImporter :
 	BaseImporter
 {	
@@ -112,7 +114,7 @@ private:
 	//!	\brief	Appends a child node to a parentnode and updates the datastructures.
 	void appendChildToParentNode(aiNode *pParent, aiNode *pChild);
 
-	//!	\brief
+	//!	\brief TODO!
 	void createAnimations();
 
 private:
@@ -124,11 +126,15 @@ private:
 	std::string m_strAbsPath;
 };
 
+// ------------------------------------------------------------------------------------------------
+//	
 inline void ObjFileImporter::GetExtensionList(std::string& append)
 {
 	append.append("*.obj");
 }
 
+// ------------------------------------------------------------------------------------------------
+
 } // Namespace Assimp
 
 #endif

+ 50 - 0
code/ObjFileMtlImporter.cpp

@@ -1,3 +1,43 @@
+/*
+---------------------------------------------------------------------------
+Open Asset Import Library (ASSIMP)
+---------------------------------------------------------------------------
+
+Copyright (c) 2006-2008, ASSIMP Development Team
+
+All rights reserved.
+
+Redistribution and use of this software in source and binary forms, 
+with or without modification, are permitted provided that the following 
+conditions are met:
+
+* Redistributions of source code must retain the above
+  copyright notice, this list of conditions and the
+  following disclaimer.
+
+* Redistributions in binary form must reproduce the above
+  copyright notice, this list of conditions and the
+  following disclaimer in the documentation and/or other
+  materials provided with the distribution.
+
+* Neither the name of the ASSIMP team, nor the names of its
+  contributors may be used to endorse or promote products
+  derived from this software without specific prior
+  written permission of the ASSIMP Development Team.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+---------------------------------------------------------------------------
+*/
 #include "ObjFileMtlImporter.h"
 #include "../include/aiTypes.h"
 #include "../include/aiAssert.h"
@@ -10,6 +50,7 @@ namespace Assimp
 {
 
 // -------------------------------------------------------------------
+//	Constructor
 ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer, 
 									   const std::string &strAbsPath,
 									   ObjFile::Model *pModel ) :
@@ -28,18 +69,21 @@ ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
 }
 
 // -------------------------------------------------------------------
+//	Destructor
 ObjFileMtlImporter::~ObjFileMtlImporter()
 {
 	// empty
 }
 
 // -------------------------------------------------------------------
+//	Private copy constructor
 ObjFileMtlImporter::ObjFileMtlImporter(const ObjFileMtlImporter &rOther)
 {
 	// empty
 }
 	
 // -------------------------------------------------------------------
+//	Private copy constructor
 ObjFileMtlImporter &ObjFileMtlImporter::operator = (
 	const ObjFileMtlImporter &rOther)
 {
@@ -47,6 +91,7 @@ ObjFileMtlImporter &ObjFileMtlImporter::operator = (
 }
 
 // -------------------------------------------------------------------
+//	Loads the material description
 void ObjFileMtlImporter::load()
 {
 	if ( m_DataIt == m_DataItEnd )
@@ -119,6 +164,7 @@ void ObjFileMtlImporter::load()
 }
 
 // -------------------------------------------------------------------
+//	Loads a color definition
 void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
 {
 	ai_assert( NULL != pColor );
@@ -139,6 +185,7 @@ void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
 }
 
 // -------------------------------------------------------------------
+//	Loads the kind of illumination model.
 void ObjFileMtlImporter::getIlluminationModel( int &illum_model )
 {
 	m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
@@ -146,6 +193,7 @@ void ObjFileMtlImporter::getIlluminationModel( int &illum_model )
 }
 
 // -------------------------------------------------------------------
+//	Loads a single float value. 
 void ObjFileMtlImporter::getFloatValue( float &value )
 {
 	m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
@@ -153,6 +201,7 @@ void ObjFileMtlImporter::getFloatValue( float &value )
 }
 
 // -------------------------------------------------------------------
+//	Creates a material from loaded data.
 void ObjFileMtlImporter::createMaterial()
 {	
 	std::string strName;
@@ -177,6 +226,7 @@ void ObjFileMtlImporter::createMaterial()
 }
 
 // -------------------------------------------------------------------
+//	Gets a texture name from data.
 void ObjFileMtlImporter::getTexture()
 {
 	std::string strTexture;

+ 10 - 7
code/ObjFileMtlImporter.h

@@ -76,20 +76,21 @@ public:
 	~ObjFileMtlImporter();
 
 private:
-	//!	\brief	Copy constructor, empty.
+	///	Copy constructor, empty.
 	ObjFileMtlImporter(const ObjFileMtlImporter &rOther);
-	
-	//!	\brief	Assignment operator, returns only a reference of this instance.
+	///	\brief	Assignment operator, returns only a reference of this instance.
 	ObjFileMtlImporter &operator = (const ObjFileMtlImporter &rOther);
-
-	//!	\brief	Load the whole material description
+	///	Load the whole material description
 	void load();
-	
-	//!	
+	///	Get color data.
 	void getColorRGBA( aiColor3D *pColor);
+	///	Get illumination model from loaded data
 	void getIlluminationModel( int &illum_model );
+	///	Gets a float value from data.	
 	void getFloatValue( float &value );
+	///	Creates a new material from loaded data.
 	void createMaterial();
+	///	Get texture name from loaded data.
 	void getTexture();
 
 private:
@@ -107,6 +108,8 @@ private:
 	char m_buffer[BUFFERSIZE];
 };
 
+// ------------------------------------------------------------------------------------------------
+
 } // Namespace Assimp
 
 #endif

+ 6 - 3
code/ObjFileParser.cpp

@@ -18,6 +18,7 @@ namespace Assimp
 const std::string ObjFileParser::DEFAULT_MATERIAL = "defaultmaterial";
 
 // -------------------------------------------------------------------
+//	Constructor with loaded data and directories.
 ObjFileParser::ObjFileParser(std::vector<char> &Data, 
 							 const std::string &strAbsPath, 
 							 const std::string &strModelName) :
@@ -302,9 +303,11 @@ void ObjFileParser::getFace()
 		m_pModel->m_pCurrentMesh = new ObjFile::Mesh();
 		m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh );
 	}
+	
+	// Store the face
 	m_pModel->m_pCurrentMesh->m_Faces.push_back( face );
-	if ( !face->m_pVertices->empty() )
-		m_pModel->m_pCurrentMesh->m_uiNumIndices += face->m_pVertices->size();
+	m_pModel->m_pCurrentMesh->m_uiNumIndices += face->m_pVertices->size();
+	m_pModel->m_pCurrentMesh->m_uiUVCoordinates[ 0 ] += face->m_pTexturCoords[0].size(); 
 	
 	// Skip the rest of the line
 	m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
@@ -577,7 +580,7 @@ void ObjFileParser::reportErrorTokenInFace()
 
 // -------------------------------------------------------------------
 //	Extracts the extention from a filename
-void ObjFileParser::extractExtension(const std::string strFile, 
+void ObjFileParser::extractExtension(const std::string &strFile, 
 									 std::string &strExt)
 {
 	strExt = "";

+ 22 - 2
code/ObjFileParser.h

@@ -74,30 +74,50 @@ public:
 	typedef std::vector<char>::const_iterator ConstDataArrayIt;
 
 public:
+	///	\brief	Constructor with data array.
 	ObjFileParser(std::vector<char> &Data, const std::string &strAbsPath, const std::string &strModelName);
+	///	\brief	Destructor
 	~ObjFileParser();
+	///	\brief	Model getter.
 	ObjFile::Model *GetModel() const;
 
 private:
+	///	Parse the loadedfile
 	void parseFile();
+	///	Method to copy the new delimited word in the current line.
 	void copyNextWord(char *pBuffer, size_t length);
+	///	Method to copy the new line.
 	void copyNextLine(char *pBuffer, size_t length);
-	void getVector3(std::vector<aiVector3D*> &point3d_array);
+	///	Stores the following 3d vector.
+	void getVector3( std::vector<aiVector3D*> &point3d_array );
+	///	Stores the following 3d vector.
 	void getVector2(std::vector<aiVector2D*> &point2d_array);
+	///	Stores the following face.
 	void getFace();
 	void getMaterialDesc();
+	///	Gets a comment.
 	void getComment();
+	/// Gets a a material library.
 	void getMaterialLib();
+	/// Creates a new material.
 	void getNewMaterial();
+	/// Gets the groupname from file.
 	void getGroupName();
+	/// Gets the group number from file.
 	void getGroupNumber();
+	///
 	int getMaterialIndex( const std::string &strMaterialName );
+	///
 	void getObjectName();
+	///
 	void createObject(const std::string &strObjectName);
+	///	Error report in token
 	void reportErrorTokenInFace();
-	void extractExtension(const std::string strFile, std::string &strExt);
+	///	Extractor for extention
+	void extractExtension(const std::string &strFile, std::string &strExt);
 
 private:
+	///	Default material name
 	static const std::string DEFAULT_MATERIAL;/* = "defaultmaterial";*/
 	//!	Absolute filepath to model
 	std::string m_strAbsPath;