Normal mapping code

Normal mapping code for normal mapping tutorial (+ few extra's for next
tutorial)
This commit is contained in:
Joey de Vries
2015-05-26 22:00:13 +02:00
parent e1e307f3de
commit 2a0a415c8f
20 changed files with 13805 additions and 158 deletions

View File

@@ -19,6 +19,10 @@ struct Vertex {
glm::vec3 Normal;
// TexCoords
glm::vec2 TexCoords;
// Tangent
glm::vec3 Tangent;
// Bitangent
glm::vec3 Bitangent;
};
struct Texture {
@@ -53,6 +57,7 @@ public:
// Bind appropriate textures
GLuint diffuseNr = 1;
GLuint specularNr = 1;
GLuint normalNr = 1;
for(GLuint i = 0; i < this->textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i); // Active proper texture unit before binding
@@ -64,9 +69,11 @@ public:
ss << diffuseNr++; // Transfer GLuint to stream
else if(name == "texture_specular")
ss << specularNr++; // Transfer GLuint to stream
else if(name == "texture_normal")
ss << normalNr++; // Transfer GLuint to stream
number = ss.str();
// Now set the sampler to the correct texture unit
glUniform1f(glGetUniformLocation(shader.Program, (name + number).c_str()), i);
glUniform1i(glGetUniformLocation(shader.Program, (name + number).c_str()), i);
// And finally bind the texture
glBindTexture(GL_TEXTURE_2D, this->textures[i].id);
}
@@ -118,6 +125,12 @@ private:
// Vertex Texture Coords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, TexCoords));
// Vertex Tangent
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Tangent));
// Vertex Bitangent
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Bitangent));
glBindVertexArray(0);
}

View File

@@ -50,7 +50,7 @@ private:
{
// Read file via ASSIMP
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
// Check for errors
if(!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) // if is Not Zero
{
@@ -117,6 +117,16 @@ private:
}
else
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
// Tangent
vector.x = mesh->mTangents[i].x;
vector.y = mesh->mTangents[i].y;
vector.z = mesh->mTangents[i].z;
vertex.Tangent = vector;
// Bitangent
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);
}
// Now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices.
@@ -144,6 +154,9 @@ private:
// 2. Specular maps
vector<Texture> specularMaps = this->loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
// 3. Normal maps
std::vector<Texture> normalMaps = this->loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
}
// Return a mesh object created from the extracted mesh data