bug fixes (normal mapping, directionnal lighting)
This commit is contained in:
parent
021ba006a0
commit
c5d4d90463
@ -75,7 +75,7 @@ void main(void) {
|
||||
#endif
|
||||
|
||||
#ifdef NORMAL_MAP
|
||||
vec3 normal = normalize(texture(normalMap, varTexCoord).xyz * tangentSpace);
|
||||
vec3 normal = normalize((texture(normalMap, varTexCoord).xyz*2 - 1) * tangentSpace);
|
||||
#else
|
||||
vec3 normal = normalize(varNormal);
|
||||
#endif
|
||||
@ -112,7 +112,7 @@ void main(void) {
|
||||
#endif
|
||||
|
||||
#ifdef AMBIENT_LIGHT
|
||||
outColor = vec4(ambient+diffuse*0.1f, 1);
|
||||
outColor = vec4(diffuse*0, 1);
|
||||
#else
|
||||
vec3 light = phongLighting(diffuse, specular, materialNs, lightColor, normal, lightDirInView, halfVecInView);
|
||||
outColor = vec4(light*shadow*(1+cos(1.57 + att*1.57)), 1);
|
||||
|
@ -75,8 +75,8 @@ void main(void) {
|
||||
|
||||
#ifdef NORMAL_MAP
|
||||
tangentSpace = transpose(mat3(normalize(normalMatrix*inTangent),
|
||||
normalize(normalMatrix*inBinormal),
|
||||
normalize(normalMatrix*inNormal)));
|
||||
normalize(normalMatrix*inBinormal),
|
||||
normalize(normalMatrix*inNormal)));
|
||||
#else
|
||||
varNormal = normalize(normalMatrix*inNormal);
|
||||
#endif
|
||||
|
@ -16,7 +16,7 @@ void ForwardModule::renderGL(Camera* myCamera, Scene* scene)
|
||||
{
|
||||
for(Light *light : p.lights)
|
||||
{
|
||||
if(light->getFlags() & Light::AMBIENT_FLAG)
|
||||
if(light->getFlags() & (1 << Light::AMBIENT_FLAG))
|
||||
{
|
||||
// render ambient lighting (opaque)
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
@ -117,11 +117,11 @@ void Light::generateShadowMap(Scene* scene)
|
||||
geometryIt->isValid(); geometryIt->next())
|
||||
{
|
||||
GeometryNode* node = geometryIt->getItem();
|
||||
if(node->mesh->getFlags() & Mesh::MESH_SHADOWED)
|
||||
if(node->mesh->getFlags() & (1 << Mesh::MESH_SHADOWED))
|
||||
{
|
||||
// compute matrix attributes
|
||||
glm::mat4 lightMVP = getProjectionMatrix() * (getViewMatrix() * node->modelMatrix);
|
||||
int hasAlpha = (node->mesh->getFlags() & Mesh::MATERIAL_ALPHA_MASK) > 0;
|
||||
int hasAlpha = (node->mesh->getFlags() & (1 << Mesh::MATERIAL_ALPHA_MASK)) > 0;
|
||||
shaders[hasAlpha]->bind();
|
||||
shaders[hasAlpha]->bindMat4(shaders[hasAlpha]->getLocation("MVP"), lightMVP);
|
||||
node->mesh->draw(shaders[hasAlpha], false, hasAlpha, false);
|
||||
|
73
src/mesh.cpp
73
src/mesh.cpp
@ -66,7 +66,7 @@ void Mesh::clearBuffers()
|
||||
void Mesh::initGL()
|
||||
{
|
||||
destroyGL();
|
||||
|
||||
|
||||
// create VAO
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
@ -89,8 +89,8 @@ void Mesh::initGL()
|
||||
if(!tangents.empty())
|
||||
{
|
||||
auto tangentsBuffer = new TBuffer<Tangents>(tangents, Buffer::VBO);
|
||||
tangentsBuffer->setVertexAttrib(3, 3);
|
||||
tangentsBuffer->setVertexAttrib(4, 3);
|
||||
tangentsBuffer->setVertexAttrib(3, 3, 0);
|
||||
tangentsBuffer->setVertexAttrib(4, 3, sizeof(glm::vec3));
|
||||
addBuffer(tangentsBuffer, TANGENT_BUFFER);
|
||||
}
|
||||
}
|
||||
@ -291,10 +291,7 @@ void Mesh::mergeVertices()
|
||||
if(deleted[indices[i]])
|
||||
{
|
||||
if(!tangents.empty())
|
||||
{
|
||||
tangents[*(ret.first)].tangent += tangents[indices[i]].tangent;
|
||||
tangents[*(ret.first)].binormal += tangents[indices[i]].binormal;
|
||||
}
|
||||
indices[i] = *(ret.first);
|
||||
}
|
||||
}
|
||||
@ -330,10 +327,16 @@ void Mesh::mergeVertices()
|
||||
normals.resize(normals.size()-offset);
|
||||
if(!tangents.empty())
|
||||
tangents.resize(tangents.size()-offset);
|
||||
for(Tangents &t : tangents)
|
||||
for(int i=0; i<tangents.size(); ++i)
|
||||
{
|
||||
t.tangent = glm::normalize(t.tangent);
|
||||
t.binormal = glm::normalize(t.binormal);
|
||||
glm::vec3 &T = tangents[i].tangent;
|
||||
const glm::vec3 &N = normals[i];
|
||||
|
||||
T = glm::normalize(T);
|
||||
// re-orthogonalize T with respect to N
|
||||
T = glm::normalize(T - glm::dot(T, N) * N);
|
||||
// then retrieve perpendicular vector B with the cross product of T and N
|
||||
tangents[i].binormal = glm::normalize(glm::cross(T, N));
|
||||
}
|
||||
}
|
||||
|
||||
@ -377,41 +380,23 @@ void Mesh::computeTangents()
|
||||
int vertexId2 = indices[j+2];
|
||||
|
||||
const glm::vec3 &v1 = positions3D[vertexId0];
|
||||
const glm::vec3 &v2 = positions3D[vertexId1];
|
||||
const glm::vec3 &v3 = positions3D[vertexId2];
|
||||
|
||||
glm::vec3 edge1 = positions3D[vertexId1] - v1;
|
||||
glm::vec3 edge2 = positions3D[vertexId2] - v1;
|
||||
|
||||
const glm::vec2& w1 = texCoords[vertexId0];
|
||||
const glm::vec2& w2 = texCoords[vertexId1];
|
||||
const glm::vec2& w3 = texCoords[vertexId2];
|
||||
|
||||
float x1 = v2.x - v1.x;
|
||||
float x2 = v3.x - v1.x;
|
||||
float y1 = v2.y - v1.y;
|
||||
float y2 = v3.y - v1.y;
|
||||
float z1 = v2.z - v1.z;
|
||||
float z2 = v3.z - v1.z;
|
||||
|
||||
float s1 = w2.x - w1.x;
|
||||
float s2 = w3.x - w1.x;
|
||||
float t1 = w2.y - w1.y;
|
||||
float t2 = w3.y - w1.y;
|
||||
|
||||
float r = 1.0f / (s1 * t2 - s2 * t1);
|
||||
glm::vec3 sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r,
|
||||
(t2 * z1 - t1 * z2) * r);
|
||||
glm::vec3 tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,
|
||||
(s1 * z2 - s2 * z1) * r);
|
||||
|
||||
Tangents& tan1 = tangents[vertexId0];
|
||||
Tangents& tan2 = tangents[vertexId1];
|
||||
Tangents& tan3 = tangents[vertexId2];
|
||||
|
||||
tan1.tangent += sdir;
|
||||
tan2.tangent += sdir;
|
||||
tan3.tangent += sdir;
|
||||
|
||||
tan1.binormal += tdir;
|
||||
tan2.binormal += tdir;
|
||||
tan3.binormal += tdir;
|
||||
glm::vec2 deltaUV1 = texCoords[vertexId1] - w1;
|
||||
glm::vec2 deltaUV2 = texCoords[vertexId2] - w1;
|
||||
|
||||
float f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
|
||||
|
||||
glm::vec3 tangentDir = deltaUV2.y * edge1 - deltaUV1.y * edge2;
|
||||
tangentDir = glm::normalize(tangentDir*f);
|
||||
|
||||
glm::vec3 binormalDir = deltaUV1.x * edge2 - deltaUV2.x * edge1;
|
||||
binormalDir = glm::normalize(binormalDir*f);
|
||||
|
||||
tangents[vertexId0] = {tangentDir, binormalDir};
|
||||
tangents[vertexId1] = {tangentDir, binormalDir};
|
||||
tangents[vertexId2] = {tangentDir, binormalDir};
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user