From ca1a1e37d915c906f0f0f81aab1fecc20cca4977 Mon Sep 17 00:00:00 2001 From: vlj Date: Sat, 29 Mar 2014 01:22:08 +0100 Subject: [PATCH] Fix normals for normal object. --- data/shaders/instanced_grass.vert | 38 ++----------------------- data/shaders/instanced_object_pass.vert | 10 ++++--- data/shaders/utils/getworldmatrix.vert | 36 +++++++++++++++++++++++ src/graphics/shaders.cpp | 6 +++- 4 files changed, 49 insertions(+), 41 deletions(-) create mode 100644 data/shaders/utils/getworldmatrix.vert diff --git a/data/shaders/instanced_grass.vert b/data/shaders/instanced_grass.vert index ec7b9f32d..2d6953630 100644 --- a/data/shaders/instanced_grass.vert +++ b/data/shaders/instanced_grass.vert @@ -13,42 +13,8 @@ in vec4 Color; out vec3 nor; out vec2 uv; -mat4 getMatrixFromRotation(vec3 rotation) -{ - - // from irrlicht - float cr = cos( rotation.z ); - float sr = sin( rotation.z ); - float cp = cos( rotation.x ); - float sp = sin( rotation.x ); - float cy = cos( rotation.y ); - float sy = sin( rotation.y ); - - float srsp = sr*sp; - float crsp = cr*sp; - - return mat4( - vec4(cp * cy, srsp * cy - cr * sy, crsp * cy + sr * sy, 0.), - vec4(cp * sy, srsp * sy + cr * cy, crsp * sy - sr * cy, 0.), - vec4(-sp, sr * cp, cr * cp, 0.), - vec4(0., 0., 0., 1.)); -} - -mat4 getWorldMatrix(vec3 translation, vec3 rotation) -{ - mat4 result = getMatrixFromRotation(rotation); - // translation - result[3].xyz += translation; - return result; -} - -mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation) -{ - mat4 result = transpose(getMatrixFromRotation(rotation)); - // FIXME: it's wrong but the fourth column is not used - result[3].xyz -= translation; - return result; -} +mat4 getWorldMatrix(vec3 translation, vec3 rotation); +mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation); void main() { diff --git a/data/shaders/instanced_object_pass.vert b/data/shaders/instanced_object_pass.vert index fdb1379f8..a1101831c 100644 --- a/data/shaders/instanced_object_pass.vert +++ b/data/shaders/instanced_object_pass.vert @@ -1,5 +1,5 @@ uniform mat4 ViewProjectionMatrix; -uniform mat4 ViewMatrix; +uniform mat4 InverseViewMatrix; in vec3 Origin; in vec3 Orientation; @@ -11,11 +11,13 @@ in vec2 Texcoord; out vec3 nor; out vec2 uv; +mat4 getWorldMatrix(vec3 translation, vec3 rotation); +mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation); + void main(void) { - mat4 ModelMatrix = mat4(1.); - ModelMatrix[3].xyz += Origin; - mat4 TransposeInverseModelView = transpose(inverse(ViewMatrix * ModelMatrix)); + mat4 ModelMatrix = getWorldMatrix(Origin, Orientation); + mat4 TransposeInverseModelView = transpose(getInverseWorldMatrix(Origin, Orientation) * InverseViewMatrix); gl_Position = ViewProjectionMatrix * ModelMatrix * vec4(Position, 1.); nor = (TransposeInverseModelView * vec4(Normal, 0.)).xyz; uv = Texcoord; diff --git a/data/shaders/utils/getworldmatrix.vert b/data/shaders/utils/getworldmatrix.vert new file mode 100644 index 000000000..9c48b74bf --- /dev/null +++ b/data/shaders/utils/getworldmatrix.vert @@ -0,0 +1,36 @@ +mat4 getMatrixFromRotation(vec3 rotation) +{ + + // from irrlicht + float cr = cos( rotation.z ); + float sr = sin( rotation.z ); + float cp = cos( rotation.x ); + float sp = sin( rotation.x ); + float cy = cos( rotation.y ); + float sy = sin( rotation.y ); + + float srsp = sr*sp; + float crsp = cr*sp; + + return mat4( + vec4(cp * cy, srsp * cy - cr * sy, crsp * cy + sr * sy, 0.), + vec4(cp * sy, srsp * sy + cr * cy, crsp * sy - sr * cy, 0.), + vec4(-sp, sr * cp, cr * cp, 0.), + vec4(0., 0., 0., 1.)); +} + +mat4 getWorldMatrix(vec3 translation, vec3 rotation) +{ + mat4 result = getMatrixFromRotation(rotation); + // translation + result[3].xyz += translation; + return result; +} + +mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation) +{ + mat4 result = transpose(getMatrixFromRotation(rotation)); + // FIXME: it's wrong but the fourth column is not used + result[3].xyz -= translation; + return result; +} \ No newline at end of file diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 22ab6c5e1..e63789022 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -514,6 +514,7 @@ namespace MeshShader void InstancedObjectPass1Shader::init() { Program = LoadProgram( + GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(), GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_object_pass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/object_pass1.frag").c_str()); @@ -521,7 +522,7 @@ namespace MeshShader attrib_position = glGetAttribLocation(Program, "Position"); attrib_normal = glGetAttribLocation(Program, "Normal"); uniform_MP = glGetUniformLocation(Program, "ViewProjectionMatrix"); - uniform_VM = glGetUniformLocation(Program, "ViewMatrix"); + uniform_VM = glGetUniformLocation(Program, "InverseViewMatrix"); } void InstancedObjectPass1Shader::setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &ViewMatrix) @@ -545,6 +546,7 @@ namespace MeshShader void InstancedGrassPass1Shader::init() { Program = LoadProgram( + GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(), GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_grass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass1.frag").c_str()); @@ -627,6 +629,7 @@ namespace MeshShader void InstancedObjectPass2Shader::init() { Program = LoadProgram( + GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(), GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_object_pass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getLightFactor.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/object_pass2.frag").c_str()); @@ -926,6 +929,7 @@ namespace MeshShader void InstancedGrassPass2Shader::init() { Program = LoadProgram( + GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(), GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_grass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getLightFactor.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass2.frag").c_str());