From 01b88fde9d4ce1a678d592805294e491ebea83e1 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Fri, 21 Mar 2014 18:17:21 +0100 Subject: [PATCH] Factorize DecodeNormal from misc shaders. --- data/shaders/diffuseenvmap.frag | 7 +------ data/shaders/pointlight.frag | 7 +------ data/shaders/ssao.frag | 7 +------ data/shaders/sunlight.frag | 7 +------ data/shaders/sunlightshadow.frag | 7 +------ data/shaders/utils/decodeNormal.frag | 7 +++++++ src/graphics/shaders.cpp | 5 +++++ 7 files changed, 17 insertions(+), 30 deletions(-) create mode 100644 data/shaders/utils/decodeNormal.frag diff --git a/data/shaders/diffuseenvmap.frag b/data/shaders/diffuseenvmap.frag index eb2822598..0c9f70777 100644 --- a/data/shaders/diffuseenvmap.frag +++ b/data/shaders/diffuseenvmap.frag @@ -14,12 +14,7 @@ varying vec2 uv; #define Spec gl_FragData[1] #endif -vec3 DecodeNormal(vec2 n) -{ - float z = dot(n, n) * 2. - 1.; - vec2 xy = normalize(n) * sqrt(1. - z * z); - return vec3(xy,z); -} +vec3 DecodeNormal(vec2 n); mat4 getMatrix(float L[9]) { diff --git a/data/shaders/pointlight.frag b/data/shaders/pointlight.frag index 0c2c9a1cf..cba9e2e49 100644 --- a/data/shaders/pointlight.frag +++ b/data/shaders/pointlight.frag @@ -12,12 +12,7 @@ flat in vec3 col; out vec4 Diffuse; out vec4 Specular; -vec3 DecodeNormal(vec2 n) -{ - float z = dot(n, n) * 2. - 1.; - vec2 xy = normalize(n) * sqrt(1. - z * z); - return vec3(xy,z); -} +vec3 DecodeNormal(vec2 n); void main() { vec2 texc = gl_FragCoord.xy / screen; diff --git a/data/shaders/ssao.frag b/data/shaders/ssao.frag index dacabddbc..e38e75067 100644 --- a/data/shaders/ssao.frag +++ b/data/shaders/ssao.frag @@ -25,12 +25,7 @@ vec3 rand(vec2 co) return texture(noise_texture, co*20.16).xyz; } -vec3 DecodeNormal(vec2 n) -{ - float z = dot(n, n) * 2. - 1.; - vec2 xy = normalize(n) * sqrt(1. - z * z); - return vec3(xy,z); -} +vec3 DecodeNormal(vec2 n); void main(void) { diff --git a/data/shaders/sunlight.frag b/data/shaders/sunlight.frag index fccf941d2..0f56d1671 100644 --- a/data/shaders/sunlight.frag +++ b/data/shaders/sunlight.frag @@ -19,12 +19,7 @@ varying vec2 uv; #endif -vec3 DecodeNormal(vec2 n) -{ - float z = dot(n, n) * 2. - 1.; - vec2 xy = normalize(n) * sqrt(1. - z * z); - return vec3(xy,z); -} +vec3 DecodeNormal(vec2 n); void main() { float z = texture(dtex, uv).x; diff --git a/data/shaders/sunlightshadow.frag b/data/shaders/sunlightshadow.frag index 69ffafd41..939d7fff1 100644 --- a/data/shaders/sunlightshadow.frag +++ b/data/shaders/sunlightshadow.frag @@ -23,12 +23,7 @@ varying vec2 uv; #endif -vec3 DecodeNormal(vec2 n) -{ - float z = dot(n, n) * 2. - 1.; - vec2 xy = normalize(n) * sqrt(1. - z * z); - return vec3(xy,z); -} +vec3 DecodeNormal(vec2 n); float getShadowFactor(vec3 pos, float bias, int index) { diff --git a/data/shaders/utils/decodeNormal.frag b/data/shaders/utils/decodeNormal.frag new file mode 100644 index 000000000..d3601509c --- /dev/null +++ b/data/shaders/utils/decodeNormal.frag @@ -0,0 +1,7 @@ + +vec3 DecodeNormal(vec2 n) +{ + float z = dot(n, n) * 2. - 1.; + vec2 xy = normalize(n) * sqrt(1. - z * z); + return vec3(xy,z); +} \ No newline at end of file diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 572fcc954..0941542c8 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -1231,6 +1231,7 @@ namespace LightShader { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/pointlight.vert").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/decodeNormal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/pointlight.frag").c_str()); attrib_Position = glGetAttribLocation(Program, "Position"); attrib_Color = glGetAttribLocation(Program, "Color"); @@ -1553,6 +1554,7 @@ namespace FullScreenShader { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/decodeNormal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/sunlight.frag").c_str()); uniform_ntex = glGetUniformLocation(Program, "ntex"); uniform_dtex = glGetUniformLocation(Program, "dtex"); @@ -1583,6 +1585,7 @@ namespace FullScreenShader { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/decodeNormal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/diffuseenvmap.frag").c_str()); uniform_ntex = glGetUniformLocation(Program, "ntex"); uniform_blueLmn = glGetUniformLocation(Program, "blueLmn[0]"); @@ -1615,6 +1618,7 @@ namespace FullScreenShader { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/decodeNormal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/sunlightshadow.frag").c_str()); uniform_ntex = glGetUniformLocation(Program, "ntex"); uniform_dtex = glGetUniformLocation(Program, "dtex"); @@ -1801,6 +1805,7 @@ namespace FullScreenShader { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/decodeNormal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/ssao.frag").c_str()); uniform_ntex = glGetUniformLocation(Program, "ntex"); uniform_dtex = glGetUniformLocation(Program, "dtex");