Просмотр исходного кода

changed point shader to use macro

leonsal 8 лет назад
Родитель
Сommit
c2c5ea02f5
2 измененных файлов с 54 добавлено и 28 удалено
  1. 27 14
      renderer/shaders/point_fragment.glsl
  2. 27 14
      renderer/shaders/sources.go

+ 27 - 14
renderer/shaders/point_fragment.glsl

@@ -1,5 +1,19 @@
 #include <material>
 
+// GLSL 3.30 does not allow indexing texture sampler with non constant values.
+// This macro is used to mix the texture with the specified index with the material color.
+// It should be called for each texture index.
+#define MIX_POINT_TEXTURE(i)                                                                                     \
+    if (MatTexVisible(i)) {                                                                                      \
+        vec2 pt = gl_PointCoord - vec2(0.5);                                                                     \
+        vec4 texColor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
+        if (i == 0) {                                                                                            \
+            texMixed = texColor;                                                                                 \
+        } else {                                                                                                 \
+            texMixed = mix(texMixed, texColor, texColor.a);                                                      \
+        }                                                                                                        \
+    }
+
 // Inputs from vertex shader
 in vec3 Color;
 flat in mat2 Rotation;
@@ -9,21 +23,20 @@ out vec4 FragColor;
 
 void main() {
 
-    vec4 texCombined = vec4(1);
-    #if MAT_TEXTURES > 0
-    // Combine all texture colors and opacity
-    for (int i = 0; i < MAT_TEXTURES; i++) {
-        vec2 pt = gl_PointCoord - vec2(0.5);
-        vec4 texcolor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i));
-        if (i  == 0) {
-            texCombined = texcolor;
-        } else {
-            texCombined = mix(texCombined, texcolor, texcolor.a);
-        }
-    }
+    // Mix material color with textures colors
+    vec4 texMixed = vec4(1);
+    #if MAT_TEXTURES==1
+        MIX_POINT_TEXTURE(0)
+    #elif MAT_TEXTURES==2
+        MIX_POINT_TEXTURE(0)
+        MIX_POINT_TEXTURE(1)
+    #elif MAT_TEXTURES==3
+        MIX_POINT_TEXTURE(0)
+        MIX_POINT_TEXTURE(1)
+        MIX_POINT_TEXTURE(2)
     #endif
 
-    // Combine material color with texture
-    FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
+    // Generates final color
+    FragColor = min(vec4(Color, MatOpacity) * texMixed, vec4(1));
 }
 

+ 27 - 14
renderer/shaders/sources.go

@@ -343,6 +343,20 @@ void main() {
 
 const point_fragment_source = `#include <material>
 
+// GLSL 3.30 does not allow indexing texture sampler with non constant values.
+// This macro is used to mix the texture with the specified index with the material color.
+// It should be called for each texture index.
+#define MIX_POINT_TEXTURE(i)                                                                                     \
+    if (MatTexVisible(i)) {                                                                                      \
+        vec2 pt = gl_PointCoord - vec2(0.5);                                                                     \
+        vec4 texColor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
+        if (i == 0) {                                                                                            \
+            texMixed = texColor;                                                                                 \
+        } else {                                                                                                 \
+            texMixed = mix(texMixed, texColor, texColor.a);                                                      \
+        }                                                                                                        \
+    }
+
 // Inputs from vertex shader
 in vec3 Color;
 flat in mat2 Rotation;
@@ -352,22 +366,21 @@ out vec4 FragColor;
 
 void main() {
 
-    vec4 texCombined = vec4(1);
-    #if MAT_TEXTURES > 0
-    // Combine all texture colors and opacity
-    for (int i = 0; i < MAT_TEXTURES; i++) {
-        vec2 pt = gl_PointCoord - vec2(0.5);
-        vec4 texcolor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i));
-        if (i  == 0) {
-            texCombined = texcolor;
-        } else {
-            texCombined = mix(texCombined, texcolor, texcolor.a);
-        }
-    }
+    // Mix material color with textures colors
+    vec4 texMixed = vec4(1);
+    #if MAT_TEXTURES==1
+        MIX_POINT_TEXTURE(0)
+    #elif MAT_TEXTURES==2
+        MIX_POINT_TEXTURE(0)
+        MIX_POINT_TEXTURE(1)
+    #elif MAT_TEXTURES==3
+        MIX_POINT_TEXTURE(0)
+        MIX_POINT_TEXTURE(1)
+        MIX_POINT_TEXTURE(2)
     #endif
 
-    // Combine material color with texture
-    FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
+    // Generates final color
+    FragColor = min(vec4(Color, MatOpacity) * texMixed, vec4(1));
 }
 
 `