Beispiel #1
0
            void VertexNormalsModifier::Modify(TriangleBuffer& Buffer)
            {
                const IndexContainer& Indices = Buffer.GetIndices();
                VertexContainer& Vertices = Buffer.GetVertices();

                if( this->Compute == VertexNormalsModifier::CM_Triangle ) {
                    for( Whole Index = 0 ; Index < Indices.size() ; Index += 3 )
                    {
                        Vector3 v1 = Vertices[Indices[Index]].Position;
                        Vector3 v2 = Vertices[Indices[Index+1]].Position;
                        Vector3 v3 = Vertices[Indices[Index+2]].Position;
                        Vector3 VertNormal = (v2-v1).CrossProduct(v3-v1).GetNormal();
                        Vertices[Indices[Index]].Normal = VertNormal;
                        Vertices[Indices[Index+1]].Normal = VertNormal;
                        Vertices[Indices[Index+2]].Normal = VertNormal;
                    }
                }else if( this->Compute == VertexNormalsModifier::CM_Vertex ) {
                    std::vector< std::vector<Vector3> > TempNormals;
                    TempNormals.resize(Vertices.size());
                    for( Whole Index = 0 ; Index < Indices.size() ; Index += 3 )
                    {
                        Vector3 v1 = Vertices[Indices[Index]].Position;
                        Vector3 v2 = Vertices[Indices[Index+1]].Position;
                        Vector3 v3 = Vertices[Indices[Index+2]].Position;
                        Vector3 VertNormal = (v2-v1).CrossProduct(v3-v1);
                        TempNormals[Indices[Index]].push_back(VertNormal);
                        TempNormals[Indices[Index+1]].push_back(VertNormal);
                        TempNormals[Indices[Index+2]].push_back(VertNormal);
                    }
                    for( Whole CurrVertex = 0 ; CurrVertex < Vertices.size() ; ++CurrVertex )
                    {
                        Vector3 VertNormal(0.0,0.0,0.0);
                        for( Whole CurrNormal = 0 ; CurrNormal < TempNormals[CurrVertex].size() ; ++CurrNormal )
                        {
                            VertNormal += TempNormals[CurrVertex][CurrNormal];
                        }
                        Vertices[CurrVertex].Normal = VertNormal.GetNormal();
                    }
                }
            }
kuMesh kuModelObject::processMesh(aiMesh * mesh, const aiScene * scene)
{
	vector<kuVertex>		vertices;
	vector<GLuint>			indices;
	vector<kuTexture>		textures;

#pragma region // Fill vertices // 
	// vertices完全靠vertex的數量決定(喂,廢話)
	for (int i = 0; i < mesh->mNumVertices; i++)
	{
		kuVertex	Vert;
		glm::vec3 VertPos(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
		glm::vec3 VertNormal(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z);
		glm::vec2 VertTexCoord;
		// Assimp allows a model to have up to 8 different texture coordinates per vertex
		if (mesh->mTextureCoords[0])				// Does the mesh contain texture coordinates?
		{
			VertTexCoord.x = mesh->mTextureCoords[0][i].x;
			VertTexCoord.y = mesh->mTextureCoords[0][i].y;
		}
		else
		{
			VertTexCoord.x = 0.0f;
			VertTexCoord.y = 0.0f;
		}
		
		Vert.Position = VertPos;
		Vert.Normal   = VertNormal;
		Vert.TexCoord = VertTexCoord;

		vertices.push_back(Vert);
	}
#pragma endregion

#pragma region // Fill face indices //
	for (int i = 0; i < mesh->mNumFaces; i++)
	{
		aiFace face = mesh->mFaces[i];			// 從mesh內取出faces
		for (int j = 0; j < face.mNumIndices; j++)
		{
			indices.push_back(face.mIndices[j]);
		}
	}
#pragma endregion

	// 嗯 看不懂 改天k完lighting再說
	aiColor4D diffuse;
	aiColor4D specular;
	aiColor4D ambient;
	kuMaterial materialTemp;

	if (mesh->mMaterialIndex >= 0)
	{
		aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
		
		aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, &diffuse);
		aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, &specular);
		aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &ambient);

		materialTemp.Ambient  = glm::vec3(ambient.r, ambient.g, ambient.b);
		materialTemp.Diffuse  = glm::vec3(diffuse.r, diffuse.g, diffuse.b);
		materialTemp.Specular = glm::vec3(specular.r, specular.g, specular.b);

		ObjectMaterials.push_back(materialTemp);
	}

	return kuMesh(vertices, indices, textures);
}