From 4c1847d3db0d13c2ea289dce3c41ca61632f24e7 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Sun, 9 Aug 2020 11:24:09 +0000 Subject: [PATCH] Removes old matrices from sky rendering in shader path. Tested By: Stan Differential Revision: https://code.wildfiregames.com/D2943 This was SVN commit r23952. --- binaries/data/mods/public/shaders/arb/sky.vp | 12 ++-- binaries/data/mods/public/shaders/arb/sky.xml | 2 + .../public/shaders/effects/sky_simple.xml | 4 +- binaries/data/mods/public/shaders/glsl/sky.fs | 10 ++-- binaries/data/mods/public/shaders/glsl/sky.vs | 6 +- .../data/mods/public/shaders/glsl/sky.xml | 2 + source/renderer/SkyManager.cpp | 59 +++++++++++++------ 7 files changed, 63 insertions(+), 32 deletions(-) diff --git a/binaries/data/mods/public/shaders/arb/sky.vp b/binaries/data/mods/public/shaders/arb/sky.vp index 61a75fd2ac..995613031f 100644 --- a/binaries/data/mods/public/shaders/arb/sky.vp +++ b/binaries/data/mods/public/shaders/arb/sky.vp @@ -3,11 +3,13 @@ ATTRIB position = vertex.position; ATTRIB uv = vertex.texcoord[0]; -DP4 result.position.x, state.matrix.mvp.row[0], position; -DP4 result.position.y, state.matrix.mvp.row[1], position; -DP4 result.position.z, state.matrix.mvp.row[2], position; -DP4 result.position.w, state.matrix.mvp.row[3], position; +PARAM transform[4] = { program.local[0..3] }; + +DP4 result.position.x, transform[0], position; +DP4 result.position.y, transform[1], position; +DP4 result.position.z, transform[2], position; +DP4 result.position.w, transform[3], position; MOV result.texcoord, uv; -END \ No newline at end of file +END diff --git a/binaries/data/mods/public/shaders/arb/sky.xml b/binaries/data/mods/public/shaders/arb/sky.xml index 98bc0a0d02..3ffab3da6b 100644 --- a/binaries/data/mods/public/shaders/arb/sky.xml +++ b/binaries/data/mods/public/shaders/arb/sky.xml @@ -3,6 +3,8 @@ + + diff --git a/binaries/data/mods/public/shaders/effects/sky_simple.xml b/binaries/data/mods/public/shaders/effects/sky_simple.xml index 8453d4043a..4cca7acc92 100644 --- a/binaries/data/mods/public/shaders/effects/sky_simple.xml +++ b/binaries/data/mods/public/shaders/effects/sky_simple.xml @@ -3,12 +3,12 @@ - + - + diff --git a/binaries/data/mods/public/shaders/glsl/sky.fs b/binaries/data/mods/public/shaders/glsl/sky.fs index 3ba02a209f..ba9ca8410e 100644 --- a/binaries/data/mods/public/shaders/glsl/sky.fs +++ b/binaries/data/mods/public/shaders/glsl/sky.fs @@ -5,12 +5,10 @@ varying vec3 v_tex; void main() { - - vec4 tex = textureCube(baseTex, v_tex); + vec4 tex = textureCube(baseTex, v_tex); - float m = (1.0 - v_tex.y) - 0.75; - m *= 4.0; + float m = (1.0 - v_tex.y) - 0.75; + m *= 4.0; - gl_FragColor = (v_tex.y > 0.0) ? (tex * m) : tex; - + gl_FragColor = (v_tex.y > 0.0) ? (tex * m) : tex; } diff --git a/binaries/data/mods/public/shaders/glsl/sky.vs b/binaries/data/mods/public/shaders/glsl/sky.vs index 84ddd5beb9..d1ea6560c2 100644 --- a/binaries/data/mods/public/shaders/glsl/sky.vs +++ b/binaries/data/mods/public/shaders/glsl/sky.vs @@ -4,8 +4,10 @@ varying vec3 v_tex; attribute vec3 a_vertex; attribute vec3 a_uv0; +uniform mat4 transform; + void main() { - gl_Position = gl_ModelViewProjectionMatrix * vec4(a_vertex, 1.0); - v_tex = gl_MultiTexCoord0.xyz; + gl_Position = transform * vec4(a_vertex, 1.0); + v_tex = a_uv0.xyz; } diff --git a/binaries/data/mods/public/shaders/glsl/sky.xml b/binaries/data/mods/public/shaders/glsl/sky.xml index 501a5f0c42..436bbef4a3 100644 --- a/binaries/data/mods/public/shaders/glsl/sky.xml +++ b/binaries/data/mods/public/shaders/glsl/sky.xml @@ -3,7 +3,9 @@ + + diff --git a/source/renderer/SkyManager.cpp b/source/renderer/SkyManager.cpp index 54a9ca7b56..459f5e310d 100644 --- a/source/renderer/SkyManager.cpp +++ b/source/renderer/SkyManager.cpp @@ -198,7 +198,8 @@ void SkyManager::RenderSky() #else // Draw the sky as a small box around the map, with depth write enabled. - // This will be done before anything else is drawn so we'll be overlapped by everything else. + // This will be done before anything else is drawn so we'll be overlapped by + // everything else. // Do nothing unless SetSkySet was called if (m_SkySet.empty()) @@ -210,32 +211,56 @@ void SkyManager::RenderSky() if (g_RenderingOptions.GetRenderPath() == RenderPath::FIXED) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - - // Translate so the sky center is at the camera space origin. - CVector3D cameraPos = g_Renderer.GetViewCamera().GetOrientation().GetTranslation(); - glTranslatef(cameraPos.X, cameraPos.Y, cameraPos.Z); - - // Rotate so that the "left" face, which contains the brightest part of each - // skymap, is in the direction of the sun from our light environment - glRotatef(180.0f + RADTODEG(g_Renderer.GetLightEnv().GetRotation()), 0.0f, 1.0f, 0.0f); - - // Currently we have a hardcoded near plane in the projection matrix. - glScalef(10.0f, 10.0f, 10.0f); - CShaderProgramPtr shader; CShaderTechniquePtr skytech; + const CCamera& camera = g_Renderer.GetViewCamera(); + if (g_RenderingOptions.GetRenderPath() == RenderPath::SHADER) { skytech = g_Renderer.GetShaderManager().LoadEffect(str_sky_simple); skytech->BeginPass(); shader = skytech->GetShader(); shader->BindTexture(str_baseTex, m_SkyCubeMap); + + // Translate so the sky center is at the camera space origin. + CMatrix3D translate; + translate.SetTranslation(camera.GetOrientation().GetTranslation()); + + // Currently we have a hardcoded near plane in the projection matrix. + CMatrix3D scale; + scale.SetScaling(10.0f, 10.0f, 10.0f); + + // Rotate so that the "left" face, which contains the brightest part of + // each skymap, is in the direction of the sun from our light + // environment. + CMatrix3D rotate; + rotate.SetYRotation(M_PI + g_Renderer.GetLightEnv().GetRotation()); + + shader->Uniform( + str_transform, + camera.GetViewProjection() * translate * rotate * scale); } else { + glMatrixMode(GL_MODELVIEW); + // Modify current matrix that already contains view part. + glPushMatrix(); + + // Translate so the sky center is at the camera space origin. + CVector3D cameraPos = camera.GetOrientation().GetTranslation(); + glTranslatef(cameraPos.X, cameraPos.Y, cameraPos.Z); + + // Rotate so that the "left" face, which contains the brightest part of + // each skymap, is in the direction of the sun from our light + // environment. + glRotatef( + 180.0f + RADTODEG(g_Renderer.GetLightEnv().GetRotation()), + 0.0f, 1.0f, 0.0f); + + // Currently we have a hardcoded near plane in the projection matrix. + glScalef(10.0f, 10.0f, 10.0f); + glDisable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_CUBE_MAP); glBindTexture(GL_TEXTURE_CUBE_MAP, m_SkyCubeMap); @@ -290,9 +315,9 @@ void SkyManager::RenderSky() glBindTexture(GL_TEXTURE_CUBE_MAP, 0); glDisable(GL_TEXTURE_CUBE_MAP); glEnable(GL_TEXTURE_2D); - } - glPopMatrix(); + glPopMatrix(); + } if (g_RenderingOptions.GetRenderPath() == RenderPath::FIXED) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);