NormalMaps Working and started work on specular maps.

This commit is contained in:
Tleety
2014-05-19 01:01:46 +02:00
parent 2520fba1b4
commit 7455f4b865
9 changed files with 167 additions and 6 deletions
+116 -2
View File
@@ -22,6 +22,8 @@ Model::Model(OBJ &obj, ResourceManager* rm)
auto texture = std::shared_ptr<Texture>(rm->Load<Texture>("Texture", currentMaterial->DiffuseTexture.FileName));
// TODO: Load normal map
std::shared_ptr<Texture> normalMap = nullptr;
if (!currentMaterial->NormalMap.FileName.empty())
normalMap = std::shared_ptr<Texture>(rm->Load<Texture>("Texture", currentMaterial->NormalMap.FileName));
// Load specular map
std::shared_ptr<Texture> specularMap = nullptr;
if (!currentMaterial->SpecularMap.FileName.empty())
@@ -63,7 +65,9 @@ Model::Model(OBJ &obj, ResourceManager* rm)
if (Vertices.size() > 0)
{
CreateBuffers(Vertices, Normals, TextureCoords);
CreateTangents();
getSimilarVertexIndex();
CreateBuffers(Vertices, Normals, TangentNormals, BiTangentNormals, TextureCoords);
}
else
{
@@ -71,7 +75,7 @@ Model::Model(OBJ &obj, ResourceManager* rm)
}
}
void Model::CreateBuffers( std::vector<glm::vec3> vertices, std::vector<glm::vec3> normals, std::vector<glm::vec2>textureCoords)
void Model::CreateBuffers( std::vector<glm::vec3> vertices, std::vector<glm::vec3> normals, std::vector<glm::vec3> tangents, std::vector<glm::vec3> biTangents, std::vector<glm::vec2>textureCoords)
{
LOG_INFO("Generating VertexBuffer");
@@ -100,6 +104,32 @@ void Model::CreateBuffers( std::vector<glm::vec3> vertices, std::vector<glm::vec
LOG_WARNING("Created empty normal buffer!");
}
LOG_INFO("Generating TangentNormalsBuffer");
glGenBuffers(1, &TangentNormalsBuffer);
if (tangents.size() > 0)
{
glBindBuffer(GL_ARRAY_BUFFER, TangentNormalsBuffer);
glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3), &tangents[0], GL_STATIC_DRAW);
GLERROR("GLEW: BufferFail, TangentNormalsBuffer");
}
else
{
LOG_WARNING("Created empty tangent buffer!");
}
LOG_INFO("Generating BiTangentNormalsBuffer");
glGenBuffers(1, &BiTangentNormalsBuffer);
if (biTangents.size() > 0)
{
glBindBuffer(GL_ARRAY_BUFFER, BiTangentNormalsBuffer);
glBufferData(GL_ARRAY_BUFFER, biTangents.size() * sizeof(glm::vec3), &biTangents[0], GL_STATIC_DRAW);
GLERROR("GLEW: BufferFail, BiTangentNormalsBuffer");
}
else
{
LOG_WARNING("Created empty biTangent buffer!");
}
LOG_INFO("Generating textureCoordBuffer");
glGenBuffers(1, &TextureCoordBuffer);
@@ -130,10 +160,94 @@ void Model::CreateBuffers( std::vector<glm::vec3> vertices, std::vector<glm::vec
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLERROR("GLEW: BufferFail5");
glBindBuffer(GL_ARRAY_BUFFER, TangentNormalsBuffer);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, 0);
GLERROR("GLEW: BufferFail5");
glBindBuffer(GL_ARRAY_BUFFER, BiTangentNormalsBuffer);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, 0);
GLERROR("GLEW: BufferFail5");
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
GLERROR("GLEW: BufferFail5");
}
bool Model::IsNear( float v1, float v2 )
{
return fabs(v1 - v2) < 0.01f;
}
void Model::getSimilarVertexIndex()
{
for(int i = 0; i < Vertices.size(); i++)
{
for(int t = 0; t < Vertices.size(); t++)
{
if(i != t)
{
if(IsNear(Vertices[i].x, Vertices[t].x)
& IsNear(Vertices[i].y, Vertices[t].y)
& IsNear(Vertices[i].z, Vertices[t].z)
)
{
glm::vec3 tempNormal, tempTangent, tempBiTangent;
tempNormal = glm::normalize(Normals[i] + Normals[t]);
tempTangent = glm::normalize(TangentNormals[i] + TangentNormals[t]);
tempBiTangent = glm::normalize(BiTangentNormals[i] + BiTangentNormals[t]);
Normals[i] = tempNormal;
Normals[t] = tempNormal;
TangentNormals[i] = tempTangent;
TangentNormals[t] = tempTangent;
BiTangentNormals[i] = tempBiTangent;
BiTangentNormals[t] = tempBiTangent;
}
}
}
}
}
void Model::CreateTangents()
{
for(int i = 0; i < Vertices.size(); i += 3)
{
glm::vec3 v0 = Vertices[i];
glm::vec3 v1 = Vertices[i+1];
glm::vec3 v2 = Vertices[i+2];
glm::vec2 uv0 = TextureCoords[i];
glm::vec2 uv1 = TextureCoords[i+1];
glm::vec2 uv2 = TextureCoords[i+2];
//Calculate the edge of the triangle
glm::vec3 edge1 = v1-v0;
glm::vec3 edge2 = v2-v0;
glm::vec2 deltaUV1 = uv1 - uv0;
glm::vec2 deltaUV2 = uv2 - uv0;
float r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
glm::vec3 tangent, biTangent;
tangent = (edge1 * deltaUV2.y - edge2 * deltaUV1.y) * r;
biTangent = (edge2 * deltaUV1.x - edge1 * deltaUV2.x) * r;
TangentNormals.push_back(tangent);
TangentNormals.push_back(tangent);
TangentNormals.push_back(tangent);
BiTangentNormals.push_back(biTangent);
BiTangentNormals.push_back(biTangent);
BiTangentNormals.push_back(biTangent);
}
}
+11 -1
View File
@@ -38,10 +38,14 @@ public:
private:
std::vector<glm::vec3> Normals;
std::vector<glm::vec3> TangentNormals;
std::vector<glm::vec3> BiTangentNormals;
std::vector<glm::vec2> TextureCoords;
GLuint VertexBuffer;
GLuint NormalBuffer;
GLuint TangentNormalsBuffer;
GLuint BiTangentNormalsBuffer;
GLuint TextureCoordBuffer;
bool Loadobj(
@@ -53,9 +57,15 @@ private:
void CreateBuffers(
std::vector<glm::vec3> _Vertices,
std::vector<glm::vec3> _Normals,
std::vector<glm::vec3> _Normals,
std::vector<glm::vec3> _Tangents,
std::vector<glm::vec3> _BiTangents,
std::vector<glm::vec2>_TextureCoords
);
void CreateTangents();
bool IsNear(float v1, float v2);
void getSimilarVertexIndex();
};
#endif // Model_h__
+16 -1
View File
@@ -17,7 +17,7 @@ Renderer::Renderer()
CAtt = 1.0f;
LAtt = 0.0f;
QAtt = 3.0f;
m_ShadowMapRes = 2048*16;
m_ShadowMapRes = 2048*6;
m_SunPosition = glm::vec3(0, 3.5f, 10);
m_SunTarget = glm::vec3(0, 0, 0);
m_SunProjection = glm::ortho<float>(10.f, -10.f, 10.f, -10.f, 10.f, -10.f);
@@ -551,6 +551,15 @@ void Renderer::FrameBufferTextures()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//Generate and bind normal texture
glGenTextures(1, &m_fSpecularTexture);
glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
/*glGenTextures(1, &m_fShadowTexture);
glBindTexture(GL_TEXTURE_2D, m_fShadowTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
@@ -567,6 +576,7 @@ void Renderer::FrameBufferTextures()
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fDiffuseTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fPositionTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_fNormalsTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fSpecularTexture, 0);
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fShadowTexture, 0);
GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
@@ -713,6 +723,11 @@ void Renderer::DrawFBOScene()
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, *texGroup.Texture);
if (texGroup.NormalMap)
{
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, *texGroup.NormalMap);
}
glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1);
}
}
+2
View File
@@ -102,6 +102,7 @@ private:
GLuint m_fDiffuseTexture;
GLuint m_fPositionTexture;
GLuint m_fNormalsTexture;
GLuint m_fSpecularTexture;
GLuint m_fBlendTexture;
GLuint m_fbLightingPass;
GLuint m_fLightingTexture;
@@ -142,6 +143,7 @@ private:
void BindFragDataLocation();
glm::mat4 CreateLightMatrix(Light &_light);
void UpdateSunProjection();
void CreateNormalMapTangent();
GLuint CreateQuad();
+1 -1
View File
@@ -24,6 +24,6 @@ void main()
vec4 _FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel;
FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a);
//FragmentColor = ShadowTexel;
//FragmentColor = DiffuseTexel;
}
+13 -1
View File
@@ -2,6 +2,9 @@
layout (binding=0) uniform sampler2D DiffuseTexture;
layout (binding=1) uniform sampler2D ShadowTexture;
layout (binding=2) uniform sampler2D NormalMapTexture;
layout (binding=3) uniform sampler2D SpecularMapTexture;
in VertexData
{
@@ -9,11 +12,14 @@ in VertexData
vec3 Normal;
vec2 TextureCoord;
vec4 ShadowCoord;
vec3 Tangent;
vec3 BiTangent;
} Input;
out vec4 frag_Diffuse;
out vec4 frag_Position;
out vec4 frag_Normal;
out vec4 frag_specular;
float Shadow(vec4 ShadowCoord)
{
@@ -32,6 +38,7 @@ float Shadow(vec4 ShadowCoord)
void main()
{
// Diffuse Texture
frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord) * Shadow(Input.ShadowCoord);
@@ -39,5 +46,10 @@ void main()
frag_Position = vec4(Input.Position.xyz, 1.0);
// G-buffer Normal
frag_Normal = vec4(Input.Normal, 0.0);
mat3 TBN = transpose(mat3(Input.Tangent, Input.BiTangent, Input.Normal));
frag_Normal = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0));
//frag_Normal = vec4(Input.Normal, 0.0);
//G-buffer Specular
frag_specular = texture(SpecularMapTexture, Input.TextureCoord);
}
+1
View File
@@ -81,4 +81,5 @@ void main()
vec4 NormalTexel = texture(NormalsTexture, TextureCoord);
FragColor = phong(vec3(PositionTexel), vec3(NormalTexel));
//FragColor = NormalTexel;
}
+6
View File
@@ -9,6 +9,8 @@ uniform mat4 DepthMVP;
layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Normal;
layout (location = 2) in vec2 TextureCoord;
layout (location = 3) in vec3 Tangent;
layout (location = 4) in vec3 BiTangent;
out VertexData
{
@@ -16,6 +18,8 @@ out VertexData
vec3 Normal;
vec2 TextureCoord;
vec4 ShadowCoord;
vec3 Tangent;
vec3 BiTangent;
} Output;
void main()
@@ -26,4 +30,6 @@ void main()
Output.Normal = normalize(vec3(inverse(transpose(V * M)) * vec4(Normal, 0.0)));
Output.TextureCoord = TextureCoord;
Output.ShadowCoord = DepthMVP * vec4(Position, 1.0);
Output.Tangent = normalize(vec3(inverse(transpose(V * M)) * vec4(Tangent, 0.0)));
Output.BiTangent = normalize(vec3(inverse(transpose(V * M)) * vec4(BiTangent, 0.0)));
}
+1
View File
@@ -24,4 +24,5 @@ private:
std::unordered_map<std::string, GLuint> m_TextureCache;
};
#endif // Texture_h__