diff --git a/data/shaders/diffuseenvmap.frag b/data/shaders/diffuseenvmap.frag index 1e0777fcd..eb2822598 100644 --- a/data/shaders/diffuseenvmap.frag +++ b/data/shaders/diffuseenvmap.frag @@ -2,6 +2,7 @@ uniform float blueLmn[9]; uniform float greenLmn[9]; uniform float redLmn[9]; uniform sampler2D ntex; +uniform mat4 TransposeViewMatrix; #if __VERSION__ >= 130 in vec2 uv; @@ -35,7 +36,9 @@ mat4 getMatrix(float L[9]) void main(void) { vec3 normal = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.)); - vec4 extendednormal = vec4(normal, 1.); + // Convert normal in world space (where SH coordinates were computed) + vec4 extendednormal = TransposeViewMatrix * vec4(normal, 1.); + extendednormal.w = 1.; mat4 rmat = getMatrix(redLmn); mat4 gmat = getMatrix(greenLmn); mat4 bmat = getMatrix(blueLmn); diff --git a/data/shaders/objectpass_spheremap.frag b/data/shaders/objectpass_spheremap.frag index 8a02ae7ab..13467bbad 100644 --- a/data/shaders/objectpass_spheremap.frag +++ b/data/shaders/objectpass_spheremap.frag @@ -1,6 +1,7 @@ uniform samplerCube tex; uniform mat4 invproj; uniform vec2 screen; +uniform mat4 TransposeViewMatrix; #if __VERSION__ >= 130 in vec3 nor; @@ -17,7 +18,10 @@ void main() { xpos = invproj * xpos; xpos.xyz /= xpos.w; - vec4 detail0 = texture(tex, reflect(xpos.xyz, nor)); + vec3 viewSampleDir = reflect(xpos.xyz, nor); + // Convert sampleDir in world space (where tex was generated) + vec4 sampleDir = TransposeViewMatrix * vec4(viewSampleDir, 0.); + vec4 detail0 = texture(tex, sampleDir.xyz); FragColor = vec4(detail0.xyz, 1.); } diff --git a/src/graphics/post_processing.cpp b/src/graphics/post_processing.cpp index e4fa28b4a..b10dad033 100644 --- a/src/graphics/post_processing.cpp +++ b/src/graphics/post_processing.cpp @@ -291,7 +291,8 @@ void PostProcessing::renderDiffuseEnvMap(const float *bSHCoeff, const float *gSH glBindVertexArray(FullScreenShader::DiffuseEnvMapShader::vao); setTexture(0, getTextureGLuint(irr_driver->getRTT(RTT_NORMAL_AND_DEPTH)), GL_NEAREST, GL_NEAREST); - FullScreenShader::DiffuseEnvMapShader::setUniforms(bSHCoeff, gSHCoeff, rSHCoeff, 0); + core::matrix4 TVM = irr_driver->getViewMatrix().getTransposed(); + FullScreenShader::DiffuseEnvMapShader::setUniforms(TVM, bSHCoeff, gSHCoeff, rSHCoeff, 0); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index f6e3c280e..405003ac4 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -707,6 +707,7 @@ namespace MeshShader GLuint SphereMapShader::attrib_normal; GLuint SphereMapShader::uniform_MVP; GLuint SphereMapShader::uniform_TIMV; + GLuint SphereMapShader::uniform_TVM; GLuint SphereMapShader::uniform_invproj; GLuint SphereMapShader::uniform_screen; GLuint SphereMapShader::TU_tex; @@ -718,6 +719,7 @@ namespace MeshShader attrib_normal = glGetAttribLocation(Program, "Normal"); uniform_MVP = glGetUniformLocation(Program, "ModelViewProjectionMatrix"); uniform_TIMV = glGetUniformLocation(Program, "TransposeInverseModelView"); + uniform_TVM = glGetUniformLocation(Program, "TransposeViewMatrix"); GLuint uniform_tex = glGetUniformLocation(Program, "tex"); uniform_invproj = glGetUniformLocation(Program, "invproj"); uniform_screen = glGetUniformLocation(Program, "screen"); @@ -728,10 +730,11 @@ namespace MeshShader glUseProgram(0); } - void SphereMapShader::setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &TransposeInverseModelView, const core::matrix4 &InvProj, const core::vector2df& screen) + void SphereMapShader::setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &TransposeViewMatrix, const core::matrix4 &TransposeInverseModelView, const core::matrix4 &InvProj, const core::vector2df& screen) { glUniformMatrix4fv(uniform_MVP, 1, GL_FALSE, ModelViewProjectionMatrix.pointer()); glUniformMatrix4fv(uniform_TIMV, 1, GL_FALSE, TransposeInverseModelView.pointer()); + glUniformMatrix4fv(uniform_TVM, 1, GL_FALSE, TransposeViewMatrix.pointer()); glUniformMatrix4fv(uniform_invproj, 1, GL_FALSE, InvProj.pointer()); glUniform2f(uniform_screen, screen.X, screen.Y); } @@ -1456,6 +1459,7 @@ namespace FullScreenShader GLuint DiffuseEnvMapShader::uniform_blueLmn; GLuint DiffuseEnvMapShader::uniform_greenLmn; GLuint DiffuseEnvMapShader::uniform_redLmn; + GLuint DiffuseEnvMapShader::uniform_TVM; GLuint DiffuseEnvMapShader::vao; void DiffuseEnvMapShader::init() @@ -1465,11 +1469,13 @@ namespace FullScreenShader uniform_blueLmn = glGetUniformLocation(Program, "blueLmn[0]"); uniform_greenLmn = glGetUniformLocation(Program, "greenLmn[0]"); uniform_redLmn = glGetUniformLocation(Program, "redLmn[0]"); + uniform_TVM = glGetUniformLocation(Program, "TransposeViewMatrix"); vao = createVAO(Program); } - void DiffuseEnvMapShader::setUniforms(const float *blueSHCoeff, const float *greenSHCoeff, const float *redSHCoeff, unsigned TU_ntex) + void DiffuseEnvMapShader::setUniforms(const core::matrix4 &TransposeViewMatrix, const float *blueSHCoeff, const float *greenSHCoeff, const float *redSHCoeff, unsigned TU_ntex) { + glUniformMatrix4fv(uniform_TVM, 1, GL_FALSE, TransposeViewMatrix.pointer()); glUniform1i(uniform_ntex, TU_ntex); glUniform1fv(uniform_blueLmn, 9, blueSHCoeff); glUniform1fv(uniform_greenLmn, 9, greenSHCoeff); diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index 4015fdb3e..e68fd7e40 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -163,11 +163,11 @@ class SphereMapShader public: static GLuint Program; static GLuint attrib_position, attrib_normal; - static GLuint uniform_MVP, uniform_TIMV, uniform_invproj, uniform_screen; + static GLuint uniform_MVP, uniform_TIMV, uniform_TVM, uniform_invproj, uniform_screen; static GLuint TU_tex; static void init(); - static void setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &TransposeInverseModelView, const core::matrix4 &InvProj, const core::vector2df& screen); + static void setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &TransposeViewMatrix, const core::matrix4 &TransposeInverseModelView, const core::matrix4 &InvProj, const core::vector2df& screen); }; class SplattingShader @@ -427,11 +427,11 @@ class DiffuseEnvMapShader { public: static GLuint Program; - static GLuint uniform_ntex, uniform_blueLmn, uniform_greenLmn, uniform_redLmn; + static GLuint uniform_ntex, uniform_TVM, uniform_blueLmn, uniform_greenLmn, uniform_redLmn; static GLuint vao; static void init(); - static void setUniforms(const float *blueSHCoeff, const float *greenSHCoeff, const float *redSHCoeff, unsigned TU_ntex); + static void setUniforms(const core::matrix4 &TransposeViewMatrix, const float *blueSHCoeff, const float *greenSHCoeff, const float *redSHCoeff, unsigned TU_ntex); }; class ShadowedSunLightShader diff --git a/src/graphics/stkmesh.cpp b/src/graphics/stkmesh.cpp index edcbfdc0c..98ac9d682 100644 --- a/src/graphics/stkmesh.cpp +++ b/src/graphics/stkmesh.cpp @@ -284,7 +284,7 @@ void drawSphereMap(const GLMesh &mesh, const core::matrix4 &ModelViewProjectionM glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } - MeshShader::SphereMapShader::setUniforms(ModelViewProjectionMatrix, TransposeInverseModelView, irr_driver->getInvProjMatrix(), core::vector2df(UserConfigParams::m_width, UserConfigParams::m_height)); + MeshShader::SphereMapShader::setUniforms(ModelViewProjectionMatrix, irr_driver->getViewMatrix().getTransposed(), TransposeInverseModelView, irr_driver->getInvProjMatrix(), core::vector2df(UserConfigParams::m_width, UserConfigParams::m_height)); glBindVertexArray(mesh.vao_second_pass); glDrawElements(ptype, count, itype, 0);