CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #1
    Join Date
    Jan 2010
    Posts
    26

    Construction issue

    ModelLoader.h
    Code:
    //..................................
    struct Mesh
    {
    	unsigned int* indices;
    	float* vertices;
    	Matrix4x4 matrix;
    	unsigned int numVertex;
    	unsigned int numFace;
    };
    
    int loadNode(const aiScene* scene, aiNode* ptr, Mesh* mesh, unsigned int j =0);
    
    class Scene
    {
    public:
    	Scene(const char* fileName);
    public:
    	unsigned int numMesh;
    	Mesh* mesh;
    
    };
    ModelLoader.cpp
    Code:
    Scene::Scene(const char* fileName) : numMesh(0), mesh(0)
    {
    	Assimp::Importer importer;
    
    	 const aiScene* scene = importer.ReadFile( fileName, 
            aiProcess_CalcTangentSpace       | 
            aiProcess_Triangulate            |
            aiProcess_JoinIdenticalVertices  |
            aiProcess_SortByPType);
    
    	 numMesh = scene->mNumMeshes;
    	 mesh = new Mesh[numMesh];
    	 loadNode(scene, scene->mRootNode, mesh);
    
    }
    
    int loadNode(const aiScene* scene, aiNode* ptr, Mesh* mesh, unsigned int j)
    {
    	unsigned int numMesh = ptr->mNumMeshes;
    	if(numMesh > 0)
    	{
    
    		Matrix4x4 tempMatrix;
    		Matrix4x4 parentMatrix;
    		for(unsigned int i = 0; i < numMesh; ++i)
    		{
    			unsigned int meshID = ptr->mMeshes[i];
    			mesh[j].numVertex = scene->mMeshes[meshID]->mNumVertices;
    			mesh[j].numFace   = scene->mMeshes[meshID]->mNumFaces;
    			mesh[j].vertices  = new float[3*mesh[j].numVertex];
    			mesh[j].indices   = new unsigned int[3*mesh[j].numFace];
    
    
    			tempMatrix.setMatrix(ptr->mTransformation.a1, ptr->mTransformation.a2, ptr->mTransformation.a3, ptr->mTransformation.a4,
    							 ptr->mTransformation.b1, ptr->mTransformation.b2, ptr->mTransformation.b3, ptr->mTransformation.b4,
    							 ptr->mTransformation.c1, ptr->mTransformation.c2, ptr->mTransformation.c3, ptr->mTransformation.c4,
    							 ptr->mTransformation.d1, ptr->mTransformation.d2, ptr->mTransformation.d3, ptr->mTransformation.d4);			
    
    			aiNode* parentNode = ptr->mParent;
    			while(parentNode != NULL)
    			{
    				parentMatrix.setMatrix(parentNode->mTransformation.a1, parentNode->mTransformation.a2, parentNode->mTransformation.a3, parentNode->mTransformation.a4,
    									   parentNode->mTransformation.b1, parentNode->mTransformation.b2, parentNode->mTransformation.b3, parentNode->mTransformation.b4,
    									   parentNode->mTransformation.c1, parentNode->mTransformation.c2, parentNode->mTransformation.c3, parentNode->mTransformation.c4,
    									   parentNode->mTransformation.d1, parentNode->mTransformation.d2, parentNode->mTransformation.d3, parentNode->mTransformation.d4);
    				tempMatrix =  parentMatrix * tempMatrix;
    				parentNode = parentNode->mParent;
    			}
    
    			mesh[j].matrix = tempMatrix;
    				//Vertex datayı al
    			for(unsigned int i = 0; i < mesh[j].numVertex; ++i)
    			{
    				mesh[j].vertices[3*i]   = scene->mMeshes[meshID]->mVertices[i].x;
    				mesh[j].vertices[3*i+1] = scene->mMeshes[meshID]->mVertices[i].y;
    				mesh[j].vertices[3*i+2] = scene->mMeshes[meshID]->mVertices[i].z;
    			}
    				//index datayı al
    			for(unsigned int i = 0; i < mesh[j].numFace; ++i)
    			{
    				mesh[j].indices[3*i]   = scene->mMeshes[meshID]->mFaces[i].mIndices[0];
    				mesh[j].indices[3*i+1] = scene->mMeshes[meshID]->mFaces[i].mIndices[1];
    				mesh[j].indices[3*i+2] = scene->mMeshes[meshID]->mFaces[i].mIndices[2];
    			}
    
    			++j;
    		}
    
    	}
    
    
    	unsigned int numChild = ptr->mNumChildren;
    	if(numChild > 0)
    	{
    		aiNode** child = ptr->mChildren;
    		for(unsigned int i = 0; i < numChild; ++i)
    		{
    			loadNode(scene, child[i], mesh, j);
    		}
    	}
    	return 1;
    }
    Matrix4x4 class has attribution following

    class Matrix4x4
    {
    public:
    Matrix4x4(); // !!! make identity matrix
    Matrix4x4(const float* arr);
    Matrix4x4(const Matrix4x4& matrix1);
    Matrix4x4(const Matrix3x3& matrix1);
    ...........................................
    ...........................................
    Matrix4x4& operator=(const Matrix4x4& matrix1);

    public:
    float m[16];
    };


    i make matrix and mesh computing in recursive loadNode() function. when matrix comptuion done in loadNode(). i control each mesh's matrix in loadNode(). all matrix computing is OK. but if i control each mesh's matrix after loadNode() execution i get wrong matrix that is identity matrix. where is the problem. i apologize for my english
    thanks in advance
    Last edited by glsl09; August 20th, 2010 at 03:16 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured