Ejemplo n.º 1
0
void addToMesh(String prefix, Polycode::Mesh *tmesh, const struct aiScene *sc, const struct aiNode* nd, bool swapZY, bool addSubmeshes, bool listOnly, ObjectEntry *parentSceneObject) {
	int i, nIgnoredPolygons = 0;
	unsigned int n = 0, t;
	// draw all meshes assigned to this node

	for (; n < nd->mNumMeshes; ++n) {
	
		if(!addSubmeshes) {
			tmesh = new Polycode::Mesh(Mesh::TRI_MESH);
            tmesh->indexedMesh = true;
		}
	
		const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
        
        Vector3 bBox;
        
		if(listOnly) {
			if(!addSubmeshes) {
				printf("%s%s.mesh\n", prefix.c_str(), nd->mName.data);
			}
		} else {
			printf("Importing mesh:%s (%d vertices) (%d faces) \n", mesh->mName.data, mesh->mNumVertices, mesh->mNumFaces);
		}
		//apply_material(sc->mMaterials[mesh->mMaterialIndex]);
        
		for (t = 0; t < mesh->mNumVertices; ++t) {
            Vertex *vertex = new Vertex();
            int index = t;
            if(mesh->mColors[0] != NULL) {
                vertex->vertexColor.setColorRGBA(mesh->mColors[0][index].r, mesh->mColors[0][index].g, mesh->mColors[0][index].b, mesh->mColors[0][index].a);
            }

            if(mesh->mTangents != NULL)  {
                if(swapZY)
                    vertex->tangent = Vector3(mesh->mTangents[index].x, mesh->mTangents[index].z, -mesh->mTangents[index].y);
                else
                    vertex->tangent = Vector3(mesh->mTangents[index].x, mesh->mTangents[index].y, mesh->mTangents[index].z);
            }

            if(mesh->mNormals != NULL)  {
                if(swapZY)
                    vertex->setNormal(mesh->mNormals[index].x, mesh->mNormals[index].z, -mesh->mNormals[index].y);
                else
                    vertex->setNormal(mesh->mNormals[index].x, mesh->mNormals[index].y, mesh->mNormals[index].z);
            }

            if(mesh->HasTextureCoords(0))
            {
                vertex->setTexCoord(mesh->mTextureCoords[0][index].x, mesh->mTextureCoords[0][index].y);
            }

            if(mesh->HasTextureCoords(1))
            {
                vertex->setSecondaryTexCoord(mesh->mTextureCoords[1][index].x, mesh->mTextureCoords[1][index].y);
            }

            
            for( unsigned int a = 0; a < mesh->mNumBones; a++) {
                aiBone* bone = mesh->mBones[a];
                unsigned int boneIndex = addBone(bone);

                for( unsigned int b = 0; b < bone->mNumWeights; b++) {
                    if(bone->mWeights[b].mVertexId == index) {
                        vertex->addBoneAssignment(boneIndex, bone->mWeights[b].mWeight);
                        hasWeights = true;
                    }
                }
            }

            if(swapZY) {
					vertex->set(mesh->mVertices[index].x, mesh->mVertices[index].z, -mesh->mVertices[index].y);
            } else {
                vertex->set(mesh->mVertices[index].x, mesh->mVertices[index].y, mesh->mVertices[index].z);
            }
            
            if(fabs(vertex->x) > bBox.x) {
                bBox.x = vertex->x;
            }
            if(fabs(vertex->y) > bBox.y) {
                bBox.y = vertex->y;
            }
            if(fabs(vertex->z) > bBox.z) {
                bBox.z = vertex->z;
            }
            
            tmesh->addVertex(vertex);
		}
        

         for (t = 0; t < mesh->mNumFaces; ++t) {
             const struct aiFace* face = &mesh->mFaces[t];
			 if (face->mNumIndices != 3) {
				 nIgnoredPolygons++;
				 continue;
			 }

             for(i = 0; i < face->mNumIndices; i++) {
                 int index = face->mIndices[i];
                 tmesh->addIndex(index);
             }
         }
		
		if(!addSubmeshes && !listOnly) {
			String fileNameMesh = prefix+String(nd->mName.data)+".mesh";			
			OSFILE *outFile = OSBasics::open(fileNameMesh.c_str(), "wb");	
			tmesh->saveToFile(outFile, writeNormals, writeTangents, writeColors, writeBoneWeights, writeUVs, writeSecondaryUVs);
			OSBasics::close(outFile);
			delete tmesh;
            
            ObjectEntry *meshEntry = parentSceneObject->addChild("child");
            meshEntry->addChild("id", String(nd->mName.data));
            meshEntry->addChild("tags", "");
            meshEntry->addChild("type", "SceneMesh");
            meshEntry->addChild("cR", "1");
            meshEntry->addChild("cG", "1");
            meshEntry->addChild("cB", "1");
            meshEntry->addChild("cA", "1");
            meshEntry->addChild("blendMode", "0");
            
            aiVector3D p;
            aiVector3D s;
            aiQuaternion r;
            nd->mTransformation.Decompose(s, r, p);
            
            meshEntry->addChild("sX", s.x);
            meshEntry->addChild("sY", s.y);
            meshEntry->addChild("sZ", s.z);

            meshEntry->addChild("rX", r.x);
            meshEntry->addChild("rY", r.y);
            meshEntry->addChild("rZ", r.z);
            meshEntry->addChild("rW", r.w);
            
            meshEntry->addChild("pX", p.x);
            meshEntry->addChild("pY", p.y);
            meshEntry->addChild("pZ", p.z);
            
            meshEntry->addChild("bbX", bBox.x);
            meshEntry->addChild("bbY", bBox.y);
            meshEntry->addChild("bbZ", bBox.z);
            
            ObjectEntry *sceneMeshEntry = meshEntry->addChild("SceneMesh");
            sceneMeshEntry->addChild("file", fileNameMesh);
            
            String materialName = "Default";
            int materialIndex = mesh->mMaterialIndex;
            if(materialIndex < scene->mNumMaterials) {
                aiString name;
                scene->mMaterials[materialIndex]->Get(AI_MATKEY_NAME,name);
                if(name.length > 0) {
                    materialName = String(name.data);
                }
            }
            sceneMeshEntry->addChild("material", materialName);
		}
		if (nIgnoredPolygons) {
			printf("Ignored %d non-triangular polygons\n", nIgnoredPolygons);
		}
	}
	   
	// draw all children
	for (n = 0; n < nd->mNumChildren; ++n) {
		addToMesh(prefix, tmesh, sc, nd->mChildren[n], swapZY, addSubmeshes, listOnly, parentSceneObject);
	}
}
Ejemplo n.º 2
0
void Mesh::loadFromFile(OSFILE *inFile) {
    
    unsigned char meshFlags;
    OSBasics::read(&meshFlags, sizeof(unsigned char), 1, inFile);
    
    indexedMesh = meshFlags & (1 << 0);
    bool hasNormals = meshFlags & (1 << 1);
    bool hasTangents = meshFlags & (1 << 2);
    bool hasColors = meshFlags & (1 << 3);
    bool hasUV = meshFlags & (1 << 4);
    bool hasSecondaryUVs = meshFlags & (1 << 5);
    bool hasBoneWeights = meshFlags & (1 << 6);
    
    unsigned int meshType;		
    OSBasics::read(&meshType, sizeof(unsigned int), 1, inFile);
    setMeshType(meshType);
    
    unsigned int numVertices;
    OSBasics::read(&numVertices, sizeof(unsigned int), 1, inFile);
    
    Vector3_struct pos;
    Vector3_struct nor;
    Vector3_struct tan;
    Vector4_struct col;			
    Vector2_struct tex;
    
    for(int i=0; i < numVertices; i++) {
        OSBasics::read(&pos, sizeof(Vector3_struct), 1, inFile);
        Vertex *vertex = new Vertex(pos.x, pos.y, pos.z);
        
        if(hasNormals) {
            OSBasics::read(&nor, sizeof(Vector3_struct), 1, inFile);
            vertex->setNormal(nor.x,nor.y, nor.z);
            vertex->restNormal.set(nor.x,nor.y, nor.z);
        }
        if(hasTangents) {
            OSBasics::read(&tan, sizeof(Vector3_struct), 1, inFile);
            vertex->tangent = Vector3(tan.x, tan.y, tan.z);
        }
        
        if(hasColors) {
            OSBasics::read(&col, sizeof(Vector4_struct), 1, inFile);
            vertex->vertexColor.setColor(col.x,col.y, col.z, col.w);
        }
        
        if(hasUV) {
            OSBasics::read(&tex, sizeof(Vector2_struct), 1, inFile);
            vertex->setTexCoord(tex.x, tex.y);
        }
        
        if(hasSecondaryUVs) {
            OSBasics::read(&tex, sizeof(Vector2_struct), 1, inFile);
            vertex->setSecondaryTexCoord(tex.x, tex.y);
        }
        
        if(hasBoneWeights) {
            unsigned int numBoneWeights;
            OSBasics::read(&numBoneWeights, sizeof(unsigned int), 1, inFile);								
            for(int b=0; b < numBoneWeights; b++) {
                float weight;
                unsigned int boneID;
                OSBasics::read(&boneID, sizeof(unsigned int), 1, inFile);													
                OSBasics::read(&weight, sizeof(float), 1, inFile);																		
                vertex->addBoneAssignment(boneID, weight);
            }
            
            Number totalWeight = 0;				
            for(int m=0; m < vertex->getNumBoneAssignments(); m++) {
                BoneAssignment *ba = vertex->getBoneAssignment(m);					
                totalWeight += ba->weight;
            }				

            for(int m=0; m < vertex->getNumBoneAssignments(); m++) {
                BoneAssignment *ba = vertex->getBoneAssignment(m);					
                ba->weight = ba->weight/totalWeight;
            }				
        }
        addVertex(vertex);
    }
    
    if(indexedMesh) {
        unsigned int numIndices;
        OSBasics::read(&numIndices, sizeof(unsigned int), 1, inFile);
        indices.clear();
        unsigned int val;
        for(int i=0; i < numIndices; i++) {
            OSBasics::read(&val, sizeof(unsigned int), 1, inFile);
            indices.push_back(val);
        }
    }

    dirtyArrays();
}
Ejemplo n.º 3
0
void addToMesh(Polycode::Mesh *tmesh, const struct aiScene *sc, const struct aiNode* nd, bool swapZY) {
	int i;
	unsigned int n = 0, t;
	// draw all meshes assigned to this node

	for (; n < nd->mNumMeshes; ++n) {
		const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
		printf("Importing mesh:%s\n", mesh->mName.data);

		//apply_material(sc->mMaterials[mesh->mMaterialIndex]);

		for (t = 0; t < mesh->mNumFaces; ++t) {
			const struct aiFace* face = &mesh->mFaces[t];
			Polycode::Polygon *poly = new Polycode::Polygon();			
	
			for(i = 0; i < face->mNumIndices; i++) {
				Vertex *vertex = new Vertex();

				int index = face->mIndices[i];
				if(mesh->mColors[0] != NULL) {
					vertex->vertexColor.setColorRGBA(mesh->mColors[0][index].r, mesh->mColors[0][index].g, mesh->mColors[0][index].b, mesh->mColors[0][index].a);
				}

				if(mesh->mNormals != NULL)  {
					if(swapZY)
						vertex->setNormal(mesh->mNormals[index].x, mesh->mNormals[index].z, -mesh->mNormals[index].y);
					else
						vertex->setNormal(mesh->mNormals[index].x, mesh->mNormals[index].y, mesh->mNormals[index].z);
				}

				if(mesh->HasTextureCoords(0))
				{
					vertex->setTexCoord(mesh->mTextureCoords[0][index].x, mesh->mTextureCoords[0][index].y);
				}

				for( unsigned int a = 0; a < mesh->mNumBones; a++) {
					aiBone* bone = mesh->mBones[a];
					unsigned int boneIndex = addBone(bone);

					for( unsigned int b = 0; b < bone->mNumWeights; b++) {
						if(bone->mWeights[b].mVertexId == index) {
							vertex->addBoneAssignment(boneIndex, bone->mWeights[b].mWeight);
							hasWeights = true;
						}
					}
				}
	
				if(swapZY)
					vertex->set(mesh->mVertices[index].x, mesh->mVertices[index].z, -mesh->mVertices[index].y);
				else
					vertex->set(mesh->mVertices[index].x, mesh->mVertices[index].y, mesh->mVertices[index].z);
				poly->addVertex(vertex);
			}
			tmesh->addPolygon(poly);
		}
	}

	// draw all children
	for (n = 0; n < nd->mNumChildren; ++n) {
		addToMesh(tmesh, sc, nd->mChildren[n], swapZY);
	}
}
Ejemplo n.º 4
0
	void Mesh::loadFromFile(OSFILE *inFile) {

		unsigned int meshType;		
		OSBasics::read(&meshType, sizeof(unsigned int), 1, inFile);				
		setMeshType(meshType);
		
		int verticesPerFace;
		switch(meshType) {
			case TRI_MESH:
				verticesPerFace = 3;
			break;
			case QUAD_MESH:
				verticesPerFace = 4;
			break;
			default:
				verticesPerFace = 1;				
			break;
		}
		
		unsigned int numFaces;		
		OSBasics::read(&numFaces, sizeof(unsigned int), 1, inFile);
		
		Vector3_struct pos;
		Vector3_struct nor;
		Vector4_struct col;			
		Vector2_struct tex;
		
		for(int i=0; i < numFaces; i++) {	
			Polygon *poly = new Polygon();			
			
			for(int j=0; j < verticesPerFace; j++) {
				OSBasics::read(&pos, sizeof(Vector3_struct), 1, inFile);
				OSBasics::read(&nor, sizeof(Vector3_struct), 1, inFile);
				OSBasics::read(&col, sizeof(Vector4_struct), 1, inFile);						
				OSBasics::read(&tex, sizeof(Vector2_struct), 1, inFile);						
				
				Vertex *vertex = new Vertex(pos.x, pos.y, pos.z);
				vertex->setNormal(nor.x,nor.y, nor.z);
				vertex->restNormal.set(nor.x,nor.y, nor.z);
				vertex->vertexColor.setColor(col.x,col.y, col.z, col.w);
				vertex->setTexCoord(tex.x, tex.y);
				
				unsigned int numBoneWeights;
				OSBasics::read(&numBoneWeights, sizeof(unsigned int), 1, inFile);								
				for(int b=0; b < numBoneWeights; b++) {
					float weight;
					unsigned int boneID;
					OSBasics::read(&boneID, sizeof(unsigned int), 1, inFile);													
					OSBasics::read(&weight, sizeof(float), 1, inFile);																		
					vertex->addBoneAssignment(boneID, weight);
				}
				
				Number totalWeight = 0;				
				for(int m=0; m < vertex->getNumBoneAssignments(); m++) {
					BoneAssignment *ba = vertex->getBoneAssignment(m);					
					totalWeight += ba->weight;
				}				

				for(int m=0; m < vertex->getNumBoneAssignments(); m++) {
					BoneAssignment *ba = vertex->getBoneAssignment(m);					
					ba->weight = ba->weight/totalWeight;
				}				

				
				poly->addVertex(vertex);
			}
			addPolygon(poly);
		}
		
		arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;		
		arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;				
		arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
		arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;							
	}