ecere/gfx/drivers/OpenGL: Exponential fog support in vertex shader
[sdk] / ecere / src / gfx / drivers / gl3 / fixed.vertex
index 7d42ad3..62cf8f1 100644 (file)
@@ -25,6 +25,11 @@ uniform float matOpacity;
 uniform float matPower;
 uniform bool matTwoSided;
 
+// Fog
+uniform bool fogOn;
+uniform float fogDensity;
+uniform vec3 fogColor;
+
 in vec2 texCoord;
 in vec3 vertex;
 in vec3 normal;
@@ -36,10 +41,16 @@ out vec4 fColorBack;
 
 void main(void)
 {
+   float f = 1;
    vec3 n = normalize(mat3x3(modelview_matrix) * normal);
-   gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 1.0);
+   vec4 pos = modelview_matrix * vec4(vertex, 1.0);
+
+   gl_Position = projection_matrix * pos;
    fTexCoord = texCoord;
 
+   if(fogOn)
+      f = clamp(exp(fogDensity * pos.z), 0, 1);
+
    if(lightingOn)
    {
       int i;
@@ -48,7 +59,7 @@ void main(void)
       float opacity = perVertexColor ? color.w : matOpacity;
       vec3 c = vec3(0);
       vec3 c2 = vec3(0);
-      vec3 ecPosition3 = vec3(gl_Position) / gl_Position.w;
+      // vec3 ecPosition3 = vec3(gl_Position) / gl_Position.w;
       vec3 eye = vec3(0.0, 0.0, 1.0);
 
       fColor = vec4(0);
@@ -103,13 +114,24 @@ void main(void)
       c += matEmissive;
       fColor = vec4(c, opacity);
 
+      if(fogOn)
+         fColor = f * fColor + (1-f) * vec4(fogColor, 1);
+
       if(matTwoSided)
       {
          c2 += ambientColor * globalAmbient;
          c2 += matEmissive;
          fColorBack = vec4(c2, opacity);
+
+         if(fogOn)
+            fColorBack = f * fColorBack + (1-f) * vec4(fogColor, 1);
       }
    }
    else
+   {
       fColor = current_color * color;
+
+      if(fogOn)
+         fColor = f * fColor + (1-f) * vec4(fogColor, 1);
+   }
 }