[colobot] 373/390: Implemented per-vertex lighting and set it as default in OpenGL 3.3 engine

Didier Raboud odyx at moszumanska.debian.org
Fri Jun 12 14:22:06 UTC 2015


This is an automated email from the git hooks/post-receive script.

odyx pushed a commit to branch upstream/latest
in repository colobot.

commit fa67e815b8056ad34281dce48113eb82e1e63b69
Author: Tomasz Kapuściński <tomaszkax86 at gmail.com>
Date:   Fri May 22 18:53:51 2015 +0200

    Implemented per-vertex lighting and set it as default in OpenGL 3.3 engine
---
 src/graphics/opengl/gl33device.cpp                 | 186 +++++++++++++--------
 src/graphics/opengl/gl33device.h                   |   2 +
 ...er_33.glsl => fragment_shader_33_perpixel.glsl} |  11 +-
 .../shaders/fragment_shader_33_pervertex.glsl      |  83 +++++++++
 ...ader_33.glsl => vertex_shader_33_perpixel.glsl} |   8 +-
 ...der_33.glsl => vertex_shader_33_pervertex.glsl} |  91 ++++------
 6 files changed, 240 insertions(+), 141 deletions(-)

diff --git a/src/graphics/opengl/gl33device.cpp b/src/graphics/opengl/gl33device.cpp
index 0401edc..8930423 100644
--- a/src/graphics/opengl/gl33device.cpp
+++ b/src/graphics/opengl/gl33device.cpp
@@ -23,6 +23,7 @@
 #include "common/config.h"
 #include "common/image.h"
 #include "common/logger.h"
+#include "common/profile.h"
 
 #include "math/geometry.h"
 
@@ -50,6 +51,8 @@ CGL33Device::CGL33Device(const GLDeviceConfig &config)
     m_colorBuffer = 0;
     m_depthBuffer = 0;
     m_offscreenRenderingEnabled = false;
+
+    m_perPixelLighting = false;
 }
 
 
@@ -243,15 +246,33 @@ bool CGL33Device::Create()
 
     GetLogger()->Info("CDevice created successfully\n");
 
+    int value;
+    if (CProfile::GetInstance().GetIntProperty("Setup", "PerPixelLighting", value))
+    {
+        m_perPixelLighting = value > 0;
+    }
+
+    if (m_perPixelLighting)
+        CLogger::GetInstance().Info("Using per-pixel lighting\n");
+    else
+        CLogger::GetInstance().Info("Using per-vertex lighting\n");
+
     // Create shader program
     GLchar source[65536];
     const GLchar *sources[] = { source };
 
-    PHYSFS_file *file = PHYSFS_openRead("shaders/vertex_shader_33.glsl");
+    char filename[64];
+
+    if (m_perPixelLighting)
+        sprintf(filename, "shaders/vertex_shader_33_perpixel.glsl");
+    else
+        sprintf(filename, "shaders/vertex_shader_33_pervertex.glsl");
+
+    PHYSFS_file *file = PHYSFS_openRead(filename);
     if (file == nullptr)
     {
         CLogger::GetInstance().Error("Cannot read vertex shader code file!\n");
-        assert(false);
+        return false;
     }
 
     int length = PHYSFS_read(file, source, 1, 65536);
@@ -277,14 +298,19 @@ bool CGL33Device::Create()
         GetLogger()->Error("Vertex shader compilation error occured!\n%s\n", message);
 
         delete[] message;
-        assert(false);
+        return false;
     }
 
-    file = PHYSFS_openRead("shaders/fragment_shader_33.glsl");
+    if (m_perPixelLighting)
+        sprintf(filename, "shaders/fragment_shader_33_perpixel.glsl");
+    else
+        sprintf(filename, "shaders/fragment_shader_33_pervertex.glsl");
+
+    file = PHYSFS_openRead(filename);
     if (file == nullptr)
     {
         CLogger::GetInstance().Error("Cannot read fragment shader code file!\n");
-        assert(false);
+        return false;
     }
 
     length = PHYSFS_read(file, source, 1, 65536);
@@ -309,7 +335,7 @@ bool CGL33Device::Create()
         GetLogger()->Error("Fragment shader compilation error occured!\n%s\n", message);
 
         delete[] message;
-        assert(false);
+        return false;
     }
 
     m_shaderProgram = glCreateProgram();
@@ -334,7 +360,7 @@ bool CGL33Device::Create()
         GetLogger()->Error("Shader program linking error occured!\n%s\n", message);
 
         delete[] message;
-        assert(false);
+        return false;
     }
 
     glDeleteShader(vertexShader);
@@ -1218,7 +1244,7 @@ void CGL33Device::DrawPrimitive(PrimitiveType type, const Vertex *vertices, int
     }
     else
     {
-        glBufferData(GL_ARRAY_BUFFER, size, vs, GL_DYNAMIC_DRAW);
+        glBufferData(GL_ARRAY_BUFFER, size, vs, GL_STREAM_DRAW);
         info.size = size;
 
         // Vertex coordinate
@@ -1269,7 +1295,7 @@ void CGL33Device::DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices,
     }
     else
     {
-        glBufferData(GL_ARRAY_BUFFER, size, vs, GL_DYNAMIC_DRAW);
+        glBufferData(GL_ARRAY_BUFFER, size, vs, GL_STREAM_DRAW);
         info.size = size;
 
         // Vertex coordinate
@@ -1320,7 +1346,7 @@ void CGL33Device::DrawPrimitive(PrimitiveType type, const VertexCol *vertices, i
     }
     else
     {
-        glBufferData(GL_ARRAY_BUFFER, size, vs, GL_DYNAMIC_DRAW);
+        glBufferData(GL_ARRAY_BUFFER, size, vs, GL_STREAM_DRAW);
         info.size = size;
 
         // Vertex coordinate
@@ -1498,16 +1524,17 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
         return;
 
     VertexBufferInfo& info = (*it).second;
+
+    unsigned int size = vertexCount * sizeof(Vertex);
+
+    bool changed = (info.vertexType != VERTEX_TYPE_NORMAL) || (size > info.size);
+
     info.primitiveType = primitiveType;
     info.vertexType = VERTEX_TYPE_NORMAL;
     info.vertexCount = vertexCount;
 
-    glBindVertexArray(info.vao);
-
     glBindBuffer(GL_ARRAY_BUFFER, info.vbo);
 
-    unsigned int size = vertexCount * sizeof(Vertex);
-
     if (info.size < size)
     {
         glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
@@ -1518,28 +1545,34 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
         glBufferSubData(GL_ARRAY_BUFFER, 0, size, vertices);
     }
 
-    // Vertex coordinate
-    glEnableVertexAttribArray(0);
-    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, coord)));
+    if (changed)        // Update vertex array bindings
+    {
+        glBindVertexArray(info.vao);
 
-    // Normal
-    glEnableVertexAttribArray(1);
-    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));
+        // Vertex coordinate
+        glEnableVertexAttribArray(0);
+        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, coord)));
 
-    // Color
-    glDisableVertexAttribArray(2);
-    glVertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
+        // Normal
+        glEnableVertexAttribArray(1);
+        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)));
 
-    // Texture coordinate 0
-    glEnableVertexAttribArray(3);
-    glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, texCoord)));
+        // Color
+        glDisableVertexAttribArray(2);
+        glVertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
 
-    // Texture coordinate 1
-    glDisableVertexAttribArray(4);
-    glVertexAttrib2f(4, 0.0f, 0.0f);
+        // Texture coordinate 0
+        glEnableVertexAttribArray(3);
+        glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, texCoord)));
+
+        // Texture coordinate 1
+        glDisableVertexAttribArray(4);
+        glVertexAttrib2f(4, 0.0f, 0.0f);
+
+        glBindVertexArray(0);
+    }
 
     glBindBuffer(GL_ARRAY_BUFFER, 0);
-    glBindVertexArray(0);
 }
 
 void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexTex2* vertices, int vertexCount)
@@ -1549,15 +1582,16 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
         return;
 
     VertexBufferInfo& info = (*it).second;
+
+    unsigned int size = vertexCount * sizeof(VertexTex2);
+
+    bool changed = (info.vertexType != VERTEX_TYPE_TEX2) || (size > info.size);
+
     info.primitiveType = primitiveType;
     info.vertexType = VERTEX_TYPE_TEX2;
     info.vertexCount = vertexCount;
-
-    glBindVertexArray(info.vao);
     glBindBuffer(GL_ARRAY_BUFFER, info.vbo);
 
-    unsigned int size = vertexCount * sizeof(VertexTex2);
-
     if (info.size < size)
     {
         glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
@@ -1568,28 +1602,34 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
         glBufferSubData(GL_ARRAY_BUFFER, 0, size, vertices);
     }
 
-    // Vertex coordinate
-    glEnableVertexAttribArray(0);
-    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, coord)));
+    if (changed)        // Update vertex array bindings
+    {
+        glBindVertexArray(info.vao);
 
-    // Normal
-    glEnableVertexAttribArray(1);
-    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, normal)));
+        // Vertex coordinate
+        glEnableVertexAttribArray(0);
+        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, coord)));
 
-    // Color
-    glDisableVertexAttribArray(2);
-    glVertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
+        // Normal
+        glEnableVertexAttribArray(1);
+        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, normal)));
 
-    // Texture coordinate 0
-    glEnableVertexAttribArray(3);
-    glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord)));
+        // Color
+        glDisableVertexAttribArray(2);
+        glVertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
 
-    // Texture coordinate 1
-    glEnableVertexAttribArray(4);
-    glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord2)));
+        // Texture coordinate 0
+        glEnableVertexAttribArray(3);
+        glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord)));
+
+        // Texture coordinate 1
+        glEnableVertexAttribArray(4);
+        glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTex2), reinterpret_cast<void*>(offsetof(VertexTex2, texCoord2)));
+        
+        glBindVertexArray(0);
+    }
 
     glBindBuffer(GL_ARRAY_BUFFER, 0);
-    glBindVertexArray(0);
 }
 
 void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primitiveType, const VertexCol* vertices, int vertexCount)
@@ -1599,15 +1639,17 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
         return;
 
     VertexBufferInfo& info = (*it).second;
+
+    unsigned int size = vertexCount * sizeof(VertexCol);
+
+    bool changed = (info.vertexType != VERTEX_TYPE_TEX2) || (size > info.size);
+
     info.primitiveType = primitiveType;
     info.vertexType = VERTEX_TYPE_COL;
     info.vertexCount = vertexCount;
 
-    glBindVertexArray(info.vao);
     glBindBuffer(GL_ARRAY_BUFFER, info.vbo);
 
-    unsigned int size = vertexCount * sizeof(VertexCol);
-
     if (info.size < size)
     {
         glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
@@ -1618,28 +1660,34 @@ void CGL33Device::UpdateStaticBuffer(unsigned int bufferId, PrimitiveType primit
         glBufferSubData(GL_ARRAY_BUFFER, 0, size, vertices);
     }
 
-    // Vertex coordinate
-    glEnableVertexAttribArray(0);
-    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, coord)));
+    if (changed)        // Update vertex array bindings
+    {
+        glBindVertexArray(info.vao);
 
-    // Normal
-    glDisableVertexAttribArray(1);
-    glVertexAttrib3f(1, 0.0f, 0.0f, 1.0f);
+        // Vertex coordinate
+        glEnableVertexAttribArray(0);
+        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, coord)));
 
-    // Color
-    glEnableVertexAttribArray(2);
-    glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, color)));
+        // Normal
+        glDisableVertexAttribArray(1);
+        glVertexAttrib3f(1, 0.0f, 0.0f, 1.0f);
 
-    // Texture coordinate 0
-    glDisableVertexAttribArray(3);
-    glVertexAttrib2f(3, 0.0f, 0.0f);
+        // Color
+        glEnableVertexAttribArray(2);
+        glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(VertexCol), reinterpret_cast<void*>(offsetof(VertexCol, color)));
 
-    // Texture coordinate 1
-    glDisableVertexAttribArray(4);
-    glVertexAttrib2f(4, 0.0f, 0.0f);
+        // Texture coordinate 0
+        glDisableVertexAttribArray(3);
+        glVertexAttrib2f(3, 0.0f, 0.0f);
+
+        // Texture coordinate 1
+        glDisableVertexAttribArray(4);
+        glVertexAttrib2f(4, 0.0f, 0.0f);
+
+        glBindVertexArray(0);
+    }
 
     glBindBuffer(GL_ARRAY_BUFFER, 0);
-    glBindVertexArray(0);
 }
 
 void CGL33Device::DrawStaticBuffer(unsigned int bufferId)
diff --git a/src/graphics/opengl/gl33device.h b/src/graphics/opengl/gl33device.h
index d83bf20..242c79d 100644
--- a/src/graphics/opengl/gl33device.h
+++ b/src/graphics/opengl/gl33device.h
@@ -227,6 +227,8 @@ private:
 
     //! Shader program
     GLuint m_shaderProgram;
+    //! true enables per-pixel lighting
+    bool m_perPixelLighting;
 
     //! Auxilliary vertex buffers for general rendering
     unsigned int m_vertex;
diff --git a/src/graphics/opengl/shaders/fragment_shader_33.glsl b/src/graphics/opengl/shaders/fragment_shader_33_perpixel.glsl
similarity index 93%
copy from src/graphics/opengl/shaders/fragment_shader_33.glsl
copy to src/graphics/opengl/shaders/fragment_shader_33_perpixel.glsl
index a2ef596..ee5064a 100644
--- a/src/graphics/opengl/shaders/fragment_shader_33.glsl
+++ b/src/graphics/opengl/shaders/fragment_shader_33_perpixel.glsl
@@ -1,7 +1,7 @@
 /*
  * This file is part of the Colobot: Gold Edition source code
  * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
- * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
+ * http://epsitec.ch; http://colobot.info; http://github.com/colobot
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -57,8 +57,7 @@ uniform bool uni_SmoothShading;
 
 in VertexData
 {
-    vec3 NormalSmooth;
-    flat vec3 NormalFlat;
+    vec3 Normal;
     vec4 Color;
     vec2 TexCoord0;
     vec2 TexCoord1;
@@ -83,10 +82,7 @@ void main()
         {
             if(uni_Light[i].Enabled)
             {
-                ambient += uni_Light[i].Ambient;
-                
-                vec3 normal = (uni_SmoothShading ? data.NormalSmooth : data.NormalFlat);
-                normal = (gl_FrontFacing ? normal : -normal);
+                vec3 normal = (gl_FrontFacing ? data.Normal : -data.Normal);
                 
                 vec3 lightDirection = vec3(0.0f);
                 float atten;
@@ -110,6 +106,7 @@ void main()
                 
                 vec3 reflectDirection = -reflect(lightDirection, normal);
                 
+                ambient += uni_Light[i].Ambient;
                 diffuse += atten * clamp(dot(normal, lightDirection), 0.0f, 1.0f) * uni_Light[i].Diffuse;
                 specular += atten * clamp(pow(dot(normal, lightDirection + reflectDirection), 10.0f), 0.0f, 1.0f) * uni_Light[i].Specular;
             }
diff --git a/src/graphics/opengl/shaders/fragment_shader_33_pervertex.glsl b/src/graphics/opengl/shaders/fragment_shader_33_pervertex.glsl
new file mode 100644
index 0000000..40d4f34
--- /dev/null
+++ b/src/graphics/opengl/shaders/fragment_shader_33_pervertex.glsl
@@ -0,0 +1,83 @@
+/*
+ * This file is part of the Colobot: Gold Edition source code
+ * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
+ * http://epsitec.ch; http://colobot.info; http://github.com/colobot
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://gnu.org/licenses
+ */
+
+// FRAGMENT SHADER - PER-VERTEX LIGHTING
+#version 330
+
+uniform sampler2D uni_PrimaryTexture;
+uniform sampler2D uni_SecondaryTexture;
+uniform sampler2DShadow uni_ShadowTexture;
+
+uniform bool uni_PrimaryTextureEnabled;
+uniform bool uni_SecondaryTextureEnabled;
+uniform bool uni_ShadowTextureEnabled;
+
+uniform bool uni_FogEnabled;
+uniform vec2 uni_FogRange;
+uniform vec4 uni_FogColor;
+
+uniform bool uni_AlphaTestEnabled;
+uniform float uni_AlphaReference;
+
+in VertexData
+{
+    vec4 Color;
+    vec2 TexCoord0;
+    vec2 TexCoord1;
+    vec4 ShadowCoord;
+    vec4 LightColor;
+    float Distance;
+} data;
+
+out vec4 out_FragColor;
+
+void main()
+{
+    vec4 color = data.Color;
+    
+    if (uni_PrimaryTextureEnabled)
+    {
+        color = color * texture(uni_PrimaryTexture, data.TexCoord0);
+    }
+    
+    if (uni_SecondaryTextureEnabled)
+    {
+        color = color * texture(uni_SecondaryTexture, data.TexCoord1);
+    }
+    
+    if (uni_ShadowTextureEnabled)
+    {
+        color = color * (0.35f + 0.65f * texture(uni_ShadowTexture, data.ShadowCoord.xyz));
+    }
+    
+    if (uni_FogEnabled)
+    {
+        float interpolate = (data.Distance - uni_FogRange.x) / (uni_FogRange.y - uni_FogRange.x);
+        
+        color = mix(color, uni_FogColor, clamp(interpolate, 0.0f, 1.0f));
+    }
+    
+    if (uni_AlphaTestEnabled)
+    {
+        if(color.a < uni_AlphaReference)
+            discard;
+    }
+    
+    out_FragColor = color;
+}
diff --git a/src/graphics/opengl/shaders/vertex_shader_33.glsl b/src/graphics/opengl/shaders/vertex_shader_33_perpixel.glsl
similarity index 90%
rename from src/graphics/opengl/shaders/vertex_shader_33.glsl
rename to src/graphics/opengl/shaders/vertex_shader_33_perpixel.glsl
index 97dbc17..a00bf98 100644
--- a/src/graphics/opengl/shaders/vertex_shader_33.glsl
+++ b/src/graphics/opengl/shaders/vertex_shader_33_perpixel.glsl
@@ -1,7 +1,7 @@
 /*
  * This file is part of the Colobot: Gold Edition source code
  * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
- * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
+ * http://epsitec.ch; http://colobot.info; http://github.com/colobot
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -34,8 +34,7 @@ layout(location = 4) in vec2 in_TexCoord1;
 
 out VertexData
 {
-    vec3 NormalSmooth;
-    flat vec3 NormalFlat;
+    vec3 Normal;
     vec4 Color;
     vec2 TexCoord0;
     vec2 TexCoord1;
@@ -53,8 +52,7 @@ void main()
     vec3 normal = normalize((uni_NormalMatrix * vec4(in_Normal, 0.0f)).xyz);
     
     data.Color = in_Color;
-    data.NormalSmooth = normal;
-    data.NormalFlat = normal;
+    data.Normal = normal;
     data.TexCoord0 = in_TexCoord0;
     data.TexCoord1 = in_TexCoord1;
     data.ShadowCoord = uni_ShadowMatrix * position;
diff --git a/src/graphics/opengl/shaders/fragment_shader_33.glsl b/src/graphics/opengl/shaders/vertex_shader_33_pervertex.glsl
similarity index 64%
rename from src/graphics/opengl/shaders/fragment_shader_33.glsl
rename to src/graphics/opengl/shaders/vertex_shader_33_pervertex.glsl
index a2ef596..138ca6b 100644
--- a/src/graphics/opengl/shaders/fragment_shader_33.glsl
+++ b/src/graphics/opengl/shaders/vertex_shader_33_pervertex.glsl
@@ -1,7 +1,7 @@
 /*
  * This file is part of the Colobot: Gold Edition source code
  * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
- * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
+ * http://epsitec.ch; http://colobot.info; http://github.com/colobot
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
  * along with this program. If not, see http://gnu.org/licenses
  */
 
-// FRAGMENT SHADER
+// VERTEX SHADER - PER-VERTEX LIGHTING
 #version 330
 
 struct LightParams
@@ -31,21 +31,6 @@ struct LightParams
     vec3 Attenuation;
 };
 
-uniform sampler2D uni_PrimaryTexture;
-uniform sampler2D uni_SecondaryTexture;
-uniform sampler2DShadow uni_ShadowTexture;
-
-uniform bool uni_PrimaryTextureEnabled;
-uniform bool uni_SecondaryTextureEnabled;
-uniform bool uni_ShadowTextureEnabled;
-
-uniform bool uni_FogEnabled;
-uniform vec2 uni_FogRange;
-uniform vec4 uni_FogColor;
-
-uniform bool uni_AlphaTestEnabled;
-uniform float uni_AlphaReference;
-
 uniform vec4 uni_AmbientColor;
 uniform vec4 uni_DiffuseColor;
 uniform vec4 uni_SpecularColor;
@@ -53,25 +38,41 @@ uniform vec4 uni_SpecularColor;
 uniform bool uni_LightingEnabled;
 uniform LightParams uni_Light[8];
 
-uniform bool uni_SmoothShading;
+uniform mat4 uni_ProjectionMatrix;
+uniform mat4 uni_ViewMatrix;
+uniform mat4 uni_ModelMatrix;
+uniform mat4 uni_ShadowMatrix;
+uniform mat4 uni_NormalMatrix;
 
-in VertexData
+layout(location = 0) in vec4 in_VertexCoord;
+layout(location = 1) in vec3 in_Normal;
+layout(location = 2) in vec4 in_Color;
+layout(location = 3) in vec2 in_TexCoord0;
+layout(location = 4) in vec2 in_TexCoord1;
+
+out VertexData
 {
-    vec3 NormalSmooth;
-    flat vec3 NormalFlat;
     vec4 Color;
     vec2 TexCoord0;
     vec2 TexCoord1;
     vec4 ShadowCoord;
-    vec4 Position;
+    vec4 LightColor;
     float Distance;
 } data;
 
-out vec4 out_FragColor;
-
 void main()
 {
-    vec4 color = data.Color;
+    vec4 position = uni_ModelMatrix * in_VertexCoord;
+    vec4 eyeSpace = uni_ViewMatrix * position;
+    gl_Position = uni_ProjectionMatrix * eyeSpace;
+    
+    data.Color = in_Color;
+    data.TexCoord0 = in_TexCoord0;
+    data.TexCoord1 = in_TexCoord1;
+    data.ShadowCoord = uni_ShadowMatrix * position;
+    data.Distance = abs(eyeSpace.z);
+    
+    vec4 color = in_Color;
     
     if (uni_LightingEnabled)
     {
@@ -83,10 +84,7 @@ void main()
         {
             if(uni_Light[i].Enabled)
             {
-                ambient += uni_Light[i].Ambient;
-                
-                vec3 normal = (uni_SmoothShading ? data.NormalSmooth : data.NormalFlat);
-                normal = (gl_FrontFacing ? normal : -normal);
+                vec3 normal = (uni_NormalMatrix * vec4(in_Normal, 0.0f)).xyz;
                 
                 vec3 lightDirection = vec3(0.0f);
                 float atten;
@@ -100,8 +98,8 @@ void main()
                 // Point light
                 else
                 {
-                    vec3 lightDirection = normalize(uni_Light[i].Position.xyz - data.Position.xyz);
-                    float dist = distance(uni_Light[i].Position.xyz, data.Position.xyz);
+                    vec3 lightDirection = normalize(uni_Light[i].Position.xyz - position.xyz);
+                    float dist = distance(uni_Light[i].Position.xyz, position.xyz);
                     
                     atten = 1.0f / (uni_Light[i].Attenuation.x
                             + uni_Light[i].Attenuation.y * dist
@@ -110,6 +108,7 @@ void main()
                 
                 vec3 reflectDirection = -reflect(lightDirection, normal);
                 
+                ambient += uni_Light[i].Ambient;
                 diffuse += atten * clamp(dot(normal, lightDirection), 0.0f, 1.0f) * uni_Light[i].Diffuse;
                 specular += atten * clamp(pow(dot(normal, lightDirection + reflectDirection), 10.0f), 0.0f, 1.0f) * uni_Light[i].Specular;
             }
@@ -121,35 +120,7 @@ void main()
         
         color.rgb = min(vec3(1.0f), result.rgb);
         color.a = 1.0f; //min(1.0f, 1.0f);
-    }
-    
-    if (uni_PrimaryTextureEnabled)
-    {
-        color = color * texture(uni_PrimaryTexture, data.TexCoord0);
-    }
-    
-    if (uni_SecondaryTextureEnabled)
-    {
-        color = color * texture(uni_SecondaryTexture, data.TexCoord1);
-    }
-    
-    if (uni_ShadowTextureEnabled)
-    {
-        color = color * (0.35f + 0.65f * texture(uni_ShadowTexture, data.ShadowCoord.xyz));
-    }
-    
-    if (uni_FogEnabled)
-    {
-        float interpolate = (data.Distance - uni_FogRange.x) / (uni_FogRange.y - uni_FogRange.x);
         
-        color = mix(color, uni_FogColor, clamp(interpolate, 0.0f, 1.0f));
+        data.Color = color;
     }
-    
-    if (uni_AlphaTestEnabled)
-    {
-        if(color.a < uni_AlphaReference)
-            discard;
-    }
-    
-    out_FragColor = color;
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git



More information about the Pkg-games-commits mailing list