void GLMesh::LoadMeshTextures() {
#ifdef HAVE_DEVIL
  // Ensure DevIL library is initialised
  static bool firsttime = true;
  if (firsttime) {
    ilInit();
    iluInit();
    firsttime = false;
  }
#endif  // HAVE_DEVIL

  // For each material, find associated textures
  for (unsigned int m = 0; m < m_pScene->mNumMaterials; ++m) {
    LoadMaterialTextures(m_pScene->mMaterials[m]);
  }
}
Example #2
0
File: Model.cpp Project: amxxL/Game
Mesh Model::ProcessMesh(aiMesh * mesh, aiScene const * scene, DirectX::XMMATRIX const& transformMatrix)
{
    std::vector<MD5Vertex> vertices;
    std::vector<DWORD> indices;

    // Get vertices
    for (uint32 i = 0; i < mesh->mNumVertices; ++i)
    {
        MD5Vertex vertex;
        vertex.position.x = mesh->mVertices[i].x;
        vertex.position.y = mesh->mVertices[i].y;
        vertex.position.z = mesh->mVertices[i].z;

        vertex.normal.x = mesh->mNormals[i].x;
        vertex.normal.y = mesh->mNormals[i].y;
        vertex.normal.z = mesh->mNormals[i].z;

        if (mesh->mTextureCoords[0])
        {
            vertex.textureCoordinate.x = static_cast<float>(mesh->mTextureCoords[0][i].x);
            vertex.textureCoordinate.y = static_cast<float>(mesh->mTextureCoords[0][i].y);
        }

        vertices.push_back(vertex);
    }

    // Get indices
    for (uint32 i = 0; i < mesh->mNumFaces; ++i)
    {
        aiFace face = mesh->mFaces[i];

        for (uint32 j = 0; j < face.mNumIndices; ++j)
            indices.push_back(face.mIndices[j]);
    }

    std::vector<Texture> textures;
    aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
    std::vector<Texture> diffuseTextures = LoadMaterialTextures(material, aiTextureType::aiTextureType_DIFFUSE, scene);
    textures.insert(textures.end(), diffuseTextures.begin(), diffuseTextures.end());

    return Mesh(device, deviceContext, vertices, indices, textures, transformMatrix);
}
Example #3
0
Mesh Model::ProcessMesh(aiMesh* mesh, const aiScene* scene)
{
	// Data to fill
	vector<Vertex> vertices;
	vector<GLuint> indices;
	vector<Texture> textures;

	// Walk through each of the mesh's vertices
	for (GLuint i = 0; i < mesh->mNumVertices; i++)
	{
		Vertex vertex;
		vec3 vector; 
        // Positions
		vector.x = mesh->mVertices[i].x;
		vector.y = mesh->mVertices[i].y;
		vector.z = mesh->mVertices[i].z;
		vertex.position = vector;
		// Normals
		if (mesh->HasNormals())
		{
			vector.x = mesh->mNormals[i].x;
			vector.y = mesh->mNormals[i].y;
			vector.z = mesh->mNormals[i].z;
			vertex.normal = vector;
		}
		
		// Texture Coordinates
		if (mesh->HasTextureCoords(0))
		{
			vec2 vec;
			// A vertex can contain up to 8 different texture coordinates. We thus make the assumption that we won't 
			// use models where a vertex can have multiple texture coordinates so we always take the first set (0).
			vec.x = mesh->mTextureCoords[0][i].x;
			vec.y = mesh->mTextureCoords[0][i].y;
			vertex.texCoords = vec;
		}
		else
			vertex.texCoords = vec2(0.0f, 0.0f);

		//Tangents
		vector.x = mesh->mTangents[i].x;
		vector.y = mesh->mTangents[i].y;
		vector.z = mesh->mTangents[i].z;
		vertex.tangent = vector;
	
		
		vertices.push_back(vertex);
	}
	// Now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices.
	for (GLuint i = 0; i < mesh->mNumFaces; i++)
	{
		aiFace face = mesh->mFaces[i];
		// Retrieve all indices of the face and store them in the indices vector
		for (GLuint j = 0; j < face.mNumIndices; j++)
		{
			indices.push_back(face.mIndices[j]);
		}
	}
	// Process materials
	if (mesh->mMaterialIndex >= 0){
		aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
		// 1. Diffuse maps
		vector<Texture> diffuseMaps = LoadMaterialTextures(material, aiTextureType_DIFFUSE, "diffuse_map");
		textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
		// 2. Specular maps
		vector<Texture> specularMaps = LoadMaterialTextures(material, aiTextureType_SPECULAR, "specular_map");
		textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
		// 3. Normal maps
		vector<Texture> normalMaps = LoadMaterialTextures(material, aiTextureType_HEIGHT, "normal_map");
		textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
	
	}

	// Return a mesh object created from the extracted mesh data
	return Mesh(&vertices, &indices, &textures);
}
Example #4
0
std::unique_ptr<ModelData> AssimpLoader::LoadData(const std::string& filepath)
{
    std::unique_ptr<ModelData> rVal(std::make_unique<ModelData>());
    Assimp::Importer importer;

    const aiScene* scene = importer.ReadFile(filepath,
                                             aiProcess_GenNormals            |
                                             aiProcess_CalcTangentSpace      |
                                             aiProcess_JoinIdenticalVertices |
                                             //aiProcess_SortByPType           |
                                             aiProcess_Triangulate           |
                                             aiProcess_FlipUVs
                                             );

    if(!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) // if is Not Zero
    {
        std::cout << "ERROR::ASSIMP:: " << importer.GetErrorString() << std::endl;
        return false;
    }

    // Put vertices to data
    for(unsigned int i = 0, offset = 0; i < scene->mNumMeshes; i++)
    {
        aiMesh* curMesh = scene->mMeshes[i];

        // Create new Mesh
        Mesh newMesh;

        // Update its info
        newMesh.dataOffset = offset;

        // Update offset
        offset += curMesh->mNumVertices;

        // Add Data to rVal->data vector
        for(unsigned int j = 0; j < curMesh->mNumVertices; j++)
        {
            // Vertices
            rVal->data.push_back(curMesh->mVertices[j].x);
            rVal->data.push_back(curMesh->mVertices[j].y);
            rVal->data.push_back(curMesh->mVertices[j].z);

            // Normals
            rVal->data.push_back(curMesh->mNormals[j].x);
            rVal->data.push_back(curMesh->mNormals[j].y);
            rVal->data.push_back(curMesh->mNormals[j].z);

            // TexCoords
            if(curMesh->HasTextureCoords(0))
            {
                rVal->data.push_back(curMesh->mTextureCoords[0][j].x);
                rVal->data.push_back(curMesh->mTextureCoords[0][j].y);
                rVal->data.push_back(curMesh->mTextureCoords[0][j].z);
            }
            else
            {
                // TODO: Handle that case
                // rVal->data.push_back(0);
                // rVal->data.push_back(0);
                // rVal->data.push_back(0);
            }
        }

        // Add Indices to vector
        for(GLuint h = 0; h < curMesh->mNumFaces; h++)
        {
            aiFace* face = &(curMesh->mFaces[h]);

            for(GLuint j = 0; j < face->mNumIndices; j++)
                newMesh.indices.push_back(face->mIndices[j] + newMesh.dataOffset);
        }

        // Materials
        if(curMesh->mMaterialIndex >= 0)
        {
            aiMaterial* material = scene->mMaterials[curMesh->mMaterialIndex];

            // Diffuse maps
            std::vector<Texture> diffuseMaps = LoadMaterialTextures(
                material,
                aiTextureType_DIFFUSE,
                SHADER_TEXTURE_DIFFUSE_PREFIX,
                filepath.substr(0, filepath.find_last_of('/')));
            newMesh.textures.insert(newMesh.textures.end(), diffuseMaps.begin(), diffuseMaps.end());

            // Specular maps
            std::vector<Texture> specularMaps = LoadMaterialTextures(
                material,
                aiTextureType_SPECULAR,
                SHADER_TEXTURE_SPECULAR_PREFIX,
                filepath.substr(0, filepath.find_last_of('/')));
            newMesh.textures.insert(newMesh.textures.end(), specularMaps.begin(), specularMaps.end());
        }

        // Add new mesh to vector
        rVal->meshes.push_back(newMesh);
    }

    // Load to gpu
    glGenBuffers(1, &(rVal->vbo));
    glGenVertexArrays(1, &(rVal->vao));

    glBindVertexArray(rVal->vao);
    {
        glBindBuffer(GL_ARRAY_BUFFER, rVal->vbo);
        {
            // Bind data
            glBufferData(
                GL_ARRAY_BUFFER,
                rVal->data.size() * sizeof(GLfloat),
                rVal->data.data(),
                GL_STATIC_DRAW);

            // Vertices
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*)0);
            glEnableVertexAttribArray(0);

            // Normals
            glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*)(3 * sizeof(GLfloat)));
            glEnableVertexAttribArray(1);

            // TexCoords
            glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*)(6 * sizeof(GLfloat)));
            glEnableVertexAttribArray(2);
        }
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        for(Mesh& mesh : rVal->meshes)
        {
            glGenBuffers(1, &mesh.ebo);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.ebo);
            glBufferData(
                GL_ELEMENT_ARRAY_BUFFER,
                mesh.indices.size() * sizeof(GLuint),
                mesh.indices.data(),
                GL_STATIC_DRAW);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        }
    }
    glBindVertexArray(0);

    return std::move(rVal);
}
Example #5
0
Moonlight::Mesh* ModelResource::ProcessMesh(aiMesh *mesh, const aiScene *scene)
{
	std::vector<Moonlight::Vertex> vertices;
	std::vector<unsigned int> indices;
	Moonlight::Material* newMaterial = new Moonlight::Material();
	for (unsigned int i = 0; i < mesh->mNumVertices; i++)
	{
		Moonlight::Vertex vertex;
		DirectX::XMFLOAT3 vector;

		vertex.Position = { mesh->mVertices[i].x,  mesh->mVertices[i].y,  mesh->mVertices[i].z };

		vector.x = mesh->mNormals[i].x;
		vector.y = mesh->mNormals[i].y;
		vector.z = mesh->mNormals[i].z;
		vertex.Normal = vector;

		if (mesh->mTextureCoords[0])
		{
			DirectX::XMFLOAT2 vec;

			// A vertex can contain up to 8 different texture coordinates. We assume that we won't use models where a vertex can have multiple texture coordinates so we always take the first set (0).
			vec.x = mesh->mTextureCoords[0][i].x;
			vec.y = mesh->mTextureCoords[0][i].y;
			vertex.TextureCoord = vec;
		}
		else
		{
			vertex.TextureCoord = DirectX::XMFLOAT2(0.0f, 0.0f);
		}
		if (mesh->mTangents)
		{
			vector.x = mesh->mTangents[i].x;
			vector.y = mesh->mTangents[i].y;
			vector.z = mesh->mTangents[i].z;
			//vertex.Tangent = vector;
		}
		if (mesh->mBitangents)
		{
			vector.x = mesh->mBitangents[i].x;
			vector.y = mesh->mBitangents[i].y;
			vector.z = mesh->mBitangents[i].z;
			//vertex.Bitangent = vector;
		}
		vertices.push_back(vertex);
	}

	for (unsigned int i = 0; i < mesh->mNumFaces; i++)
	{
		aiFace face = mesh->mFaces[i];

		for (unsigned int j = 0; j < face.mNumIndices; j++)
		{
			indices.push_back(face.mIndices[j]);
		}
	}

	aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];

	LoadMaterialTextures(newMaterial, material, aiTextureType_DIFFUSE, Moonlight::TextureType::Diffuse);
	LoadMaterialTextures(newMaterial, material, aiTextureType_SPECULAR, Moonlight::TextureType::Specular);
	LoadMaterialTextures(newMaterial, material, aiTextureType_NORMALS, Moonlight::TextureType::Normal);
	LoadMaterialTextures(newMaterial, material, aiTextureType_HEIGHT, Moonlight::TextureType::Height);
	LoadMaterialTextures(newMaterial, material, aiTextureType_OPACITY, Moonlight::TextureType::Opacity);

	return new Moonlight::Mesh(vertices, indices, newMaterial);
}