コード例 #1
0
ファイル: ModelLoader.cpp プロジェクト: valche5/ValEngine
void ModelLoader::processMesh(const aiScene *aiscene, ScenePtr &scene, aiMesh * mesh, MeshPtr &glMesh, const glm::mat4 &model) {
	//Properties
	glMesh->shaderConf.shadingType = "lighting";
	glMesh->shaderConf.lightingFct = "phong";
	glMesh->name = mesh->mName.C_Str();
	glMesh->vertexCount = mesh->mNumVertices;
	glMesh->model = model;

	//Vertex geometry
	VBOFactory vboFactory;
	vboFactory.setVertexCount(mesh->mNumVertices);

	//Position
	vboFactory.setData(VertexPosition, &mesh->mVertices[0][0]);

	//Normals
	if (mesh->HasNormals()) {
		vboFactory.setData(VertexNormal, &mesh->mNormals[0][0]);
	}

	if (mesh->HasTangentsAndBitangents()) {
		vboFactory.setData(VertexTangent, &mesh->mTangents[0][0]);
		vboFactory.setData(VertexBitangent, &mesh->mBitangents[0][0]);
	}

	//Texture coords
	std::vector<GLfloat> texCoords;
	if (mesh->HasTextureCoords(0)) {
		//Une petite correction s'impose Vec3 -> Vec2
		texCoords.reserve(2 * mesh->mNumVertices);
		for (int i = 0; i < mesh->mNumVertices; i++) {
			texCoords.push_back(mesh->mTextureCoords[0][i].x);
			texCoords.push_back(mesh->mTextureCoords[0][i].y);
		}
		vboFactory.setData(VertexTexCoord, &texCoords[0]);
	}

	IBOFactory iboFactory;
	//Indices
	std::vector<GLuint> indices;
	indices.reserve(mesh->mNumFaces * 3);
	for (GLuint i = 0; i < mesh->mNumFaces; i++) {
		aiFace face = mesh->mFaces[i];
		for (int j = 0; j < face.mNumIndices; j++)
			indices.push_back(face.mIndices[j]);
	}
	iboFactory.setData(&indices[0], indices.size());

	//Set mesh VBO and IBO
	glMesh->addVBO(vboFactory.getBlockVBO());
	glMesh->addIBO(iboFactory.getIBO(), indices.size());

	//Compute AABB
	computeAABB(mesh, glMesh->bBox);

	//Material
	if (mesh->mMaterialIndex >= 0) {
		aiMaterial *material = aiscene->mMaterials[mesh->mMaterialIndex];

		//Constants
		aiString name;
		material->Get(AI_MATKEY_NAME, name);
		glMesh->material.name = name.C_Str();

		aiColor3D color(0.f, 0.f, 0.f);
		material->Get(AI_MATKEY_COLOR_AMBIENT, color);
		glMesh->material.addProperty(Ka, aiToGLM(color));

		material->Get(AI_MATKEY_COLOR_DIFFUSE, color);
		glMesh->material.addProperty(Kd, aiToGLM(color));

		material->Get(AI_MATKEY_COLOR_SPECULAR, color);
		glMesh->material.addProperty(Ks, aiToGLM(color));

		float shininess = 0;
		material->Get(AI_MATKEY_SHININESS, shininess);
		glMesh->material.addProperty(Shininess, shininess);

		//Textures
		loadTextures(scene, material, glMesh, aiTextureType_AMBIENT, TextureType::Ambient);
		loadTextures(scene, material, glMesh, aiTextureType_DIFFUSE, TextureType::Diffuse);
		loadTextures(scene, material, glMesh, aiTextureType_SPECULAR, TextureType::Specular);
		loadTextures(scene, material, glMesh, aiTextureType_NORMALS, TextureType::Normal);
		//La Height Map peut être la Normal map
		if (!(glMesh->material.textureTypes & TextureType::Normal)) {
			loadTextures(scene, material, glMesh, aiTextureType_HEIGHT, TextureType::Normal);
		}

		//Si pas de diffuse map mais une ambient map, ambient map = diffuse map
		if ((glMesh->material.textureTypes & Diffuse) && !(glMesh->material.textureTypes & Ambient)) {
			loadTextures(scene, material, glMesh, aiTextureType_DIFFUSE, TextureType::Ambient);
		}
	}
}