Пример #1
0
TTexturePtr AssimpLoader::LoadTexture(const CFile& file)
{
	TTexturePtr texturePtr;
	try
	{
		texturePtr = Texture::LoadFromFile(file.Filename());
	} catch (CException e)
	{
		texturePtr = Texture::LoadFromFile("unknowTexture.tga");
	}
	return texturePtr;
}
Пример #2
0
void AssimpLoader::BuildGroup(IMeshSceneNode* meshSceneNode,
		const aiScene* scene, aiNode* nd, const Math::CMatrix4& matrixUpp)
{
	// Load transformation
	struct aiMatrix4x4 m = nd->mTransformation;
	//aiTransposeMatrix4(&m);
	Math::CMatrix4 matrixLocalTrans = Math::CMatrix4(m.a1, m.a2, m.a3, m.a4,
			m.b1, m.b2, m.b3, m.b4, m.c1, m.c2, m.c3, m.c4, m.d1, m.d2, m.d3,
			m.d4);
	Math::CMatrix4 globalTransformation = matrixLocalTrans * matrixUpp;

	// draw all meshes assigned to this node
	for (unsigned int n = 0; n < nd->mNumMeshes; ++n)
	{
		/*
		 * Construct the Object
		 */
		const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
		if (mesh->mNumFaces == 0)
		{
			Logger::Log() << "[INFO] Skip assimp mesh. No faces. \n";
			continue;
		}
		RenderableObject* assimpMesh = new RenderableObject;

		// Build the indice faces
		std::vector<unsigned int> indicesVector;
		std::vector<float> vertexVector;
		unsigned int maxIndice = 0;
		for (unsigned int t = 0; t < mesh->mNumFaces; ++t)
		{
			// Get a face elements
			const struct aiFace* face = &mesh->mFaces[t];
			if (face->mNumIndices != 3)
			{
				Logger::Log() << "[Warning] More or less indices faces ("
						<< face->mNumIndices << "). \n";
				continue;
			}
			// Get all informations
			for (int i = 0; i < face->mNumIndices; i++)
			{
				int index = face->mIndices[i];
				indicesVector.push_back(index);
				maxIndice = std::max(maxIndice, face->mIndices[i]);
			}
		}
		// Create the indice array
		unsigned int * indiceArray = new unsigned int[indicesVector.size()];for
(		unsigned int i = 0; i < indicesVector.size(); i++)
		{
			indiceArray[i] = indicesVector[i];
		}
		// Set all buffers
		// * Indice Buffer
		Logger::Log() << "[INFO] Add indice buffer : " << indicesVector.size()
				<< "\n";
		assimpMesh->SetIndiceBuffer(indiceArray, indicesVector.size());
		// * Vertex buffer
		Logger::Log() << "[INFO] Add Vertex buffer ... \n";
		RenderableObject::RenderableBuffer buffer;
		buffer.buffer = &mesh->mVertices[0].x;
		buffer.dimension = 3;
		buffer.size = maxIndice * 3 + 3;
		buffer.owner = false;
		Logger::Log() << "   * size : " << buffer.size << "\n";
		assimpMesh->AddBuffer(buffer, VERTEX_ATTRIBUT);
		//  * Normal buffer
		if (mesh->HasNormals())
		{
			buffer.buffer = &mesh->mNormals[0].x;
			assimpMesh->AddBuffer(buffer, NORMAL_ATTRIBUT);
		}
		//  * Tangentes and bitangantes
		if (mesh->HasTangentsAndBitangents())
		{
			buffer.buffer = &mesh->mTangents[0].x;
			assimpMesh->AddBuffer(buffer, TANGENT_ATTRIBUT);
			buffer.buffer = &mesh->mBitangents[0].x;
			assimpMesh->AddBuffer(buffer, BITANGENT_ATTRIBUT);
		}
		// * Couleurs
		if (mesh->GetNumColorChannels() > 0) //FIXME : Verfier le Loader ...
		{
			// TODO: Gestion des couleurs multiples
			if (mesh->GetNumColorChannels() > 1)
			{
				throw CException("Too Color Channels");
			}
			else
			{
				buffer.size = maxIndice * 3 + 3;
				buffer.owner = true;
				buffer.dimension = 3;
				float * ColorArray = new float[maxIndice * 3 + 3];for
(				int i = 0; i < maxIndice+1; i++)
				{
					ColorArray[i*3] = mesh->mColors[0][i].r;
					ColorArray[i*3+1] = mesh->mColors[0][i].g;
					ColorArray[i*3+2] = mesh->mColors[0][i].b;
				}
				buffer.buffer = ColorArray;
				assimpMesh->AddBuffer(buffer, COLOR_ATTRIBUT);
			}
		}
		//  * UV Coords
		if (mesh->GetNumUVChannels() > 0)
		{
			// TODO: Gestion des materiaux multiples
			if (mesh->GetNumUVChannels() > 1)
			{
				throw CException("Too UV Channels");
			}
			else
			{
				buffer.size = maxIndice * 2 + 2;
				buffer.owner = true;
				buffer.dimension = 2;
				float * UVArray = new float[maxIndice * 2 + 2];for
(				int i = 0; i < maxIndice+1; i++)
				{
					UVArray[i*2] = mesh->mTextureCoords[0][i].x;
					UVArray[i*2+1] = mesh->mTextureCoords[0][i].y;
				}
				buffer.buffer = UVArray;
				assimpMesh->AddBuffer(buffer, TEXCOORD_ATTRIBUT);
				// Load diffuse image
				const struct aiMaterial* material =
						scene->mMaterials[mesh->mMaterialIndex];
				if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0)
				{
					aiString AiPath;
					material->GetTexture(aiTextureType_DIFFUSE, 0, &AiPath);
					CFile texturePath = std::string(AiPath.data);
					Logger::Log() << "[INFO] Diffuse texture : "
							<< texturePath.Filename() << "\n";
					assimpMesh->AddTextureMap(DIFFUSE_TEXTURE,
							LoadTexture(texturePath));
				}
				if (material->GetTextureCount(aiTextureType_SPECULAR) > 0)
				{
					aiString AiPath;
					material->GetTexture(aiTextureType_SPECULAR, 0, &AiPath);
					CFile texturePath = std::string(AiPath.data);
					Logger::Log() << "[INFO] Specular Texture : "
							<< texturePath.Filename() << "\n";
					assimpMesh->AddTextureMap(SPECULAR_TEXTURE,
							LoadTexture(texturePath));
				}
				//TODO: Desactivation du bump mapping
//				if(material->GetTextureCount(aiTextureType_HEIGHT)>0)
//				{
//					aiString AiPath;
//					material->GetTexture(aiTextureType_HEIGHT, 0, &AiPath);
//					CFile texturePath = std::string(AiPath.data);
//					Logger::Log() << "[INFO] Normal texture : " << texturePath.Filename() << "\n";
//					assimpMesh->AddTextureMap(NORMAL_TEXTURE, LoadTexture(texturePath));
//				}
			}
		}

		// Chargement des materiaux
		GetMaterialPropreties(assimpMesh,
				scene->mMaterials[mesh->mMaterialIndex]);
		// Check if is an Instance
		bool foundInstance = false;
		//TODO: Reactivativate instance system
//		for(CachedAssimpMeshMap::iterator it = m_cached_geom.begin(); it != m_cached_geom.end(); it++)
//		{
//			if((*it)->IsInstance(*assimpMesh))
//			{
//				(*it)->SetInstance(*assimpMesh);
//				foundInstance = true;
//				break;
//			}
//		}

		if (foundInstance)
		{
			Logger::Log() << "[INFO] Found an Instance ... \n";
			Assert(false);
		}
		else
		{
			// Compile all buffers
			Logger::Log() << "[INFO] Compile all buffers ... \n";
			assimpMesh->CompileBuffers();
			Logger::Log() << "[INFO] Add cached resources : " << nd->mMeshes[n]
					<< "\n";
			m_cached_geom.push_back(assimpMesh);
		}
		// Attach to group
		Logger::Log() << "[INFO] Add to father node... \n";

		meshSceneNode->AddRenderableObject(assimpMesh, globalTransformation);
	}

	// Add all childrens
	for (int n = 0; n < nd->mNumChildren; ++n)
	{
		BuildGroup(meshSceneNode, scene, nd->mChildren[n],
				globalTransformation);
	}

}