[PySoy-dev] [PATCH] A first draft of the glsl.py tool and refactoring of shader code into separate files

Ondrej Skopek skopekondrej at gmail.com
Sat Dec 28 16:47:24 EST 2013


# HG changeset patch
# User Ondrej Skopek <skopekondrej at gmail.com>
# Date 1388267190 -3600
#      Sat Dec 28 22:46:30 2013 +0100
# Node ID 0bac2d2d5b71628e94502f0542d70f45ef373e6a
# Parent  3d2e698adec036e7196880672fa879175cee2470
A first draft of the glsl.py tool and refactoring of shader code into separate files

diff -r 3d2e698adec0 -r 0bac2d2d5b71 .hgignore
--- a/.hgignore	Fri Dec 27 15:59:14 2013 -0500
+++ b/.hgignore	Sat Dec 28 22:46:30 2013 +0100
@@ -5,6 +5,7 @@
 __pycache__
 genie.pyc
 genie.pyo
+glsl.pyc
 .lock-w*
 *.orig
 *.backup
diff -r 3d2e698adec0 -r 0bac2d2d5b71 glsl.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/glsl.py	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+# encoding: utf-8
+
+# A part of this is based on https://github.com/wsmind/oak/blob/master/build/waf-tools/shaders.py
+
+import os.path
+from waflib.TaskGen import extension, after_method
+
+ at extension('.glslv', '.glslf')
+def glsl_file(self, node):
+
+    src = node.get_src()
+    bld = node.get_bld()
+
+    srcPath = src.abspath()
+    bldPath = bld.abspath()
+
+    # Change extension
+    extension = os.path.splitext(os.path.basename(bldPath))[1];
+    bld = bld.change_ext(extension + '.c')
+    bldPath = bld.abspath()
+
+    # Create dirs if non-existant
+    dirs = os.path.split(bldPath)[0]
+    if not os.path.exists(dirs):
+        os.makedirs(dirs)
+
+    inFile = open(srcPath, 'r')
+    outFile = open(bldPath, 'w')
+
+    # deduce variable name from filename
+    split = os.path.splitext(os.path.basename(srcPath))
+    variableName = split[0] + split[1].upper()[1:]
+
+    outFile.write("const char *" + variableName + "String = \"")
+    for line in inFile:
+        # escape '\' and '"'
+        line = line.replace("\\", "\\\\")
+        line = line.replace("\"", "\\\"")
+
+        # content, with \n between lines
+        outFile.write(line.strip() + "\\n")
+
+    outFile.write("\";\n")
+
+    inFile.close()
+    outFile.close()
+
+    return 0
+
+#@extension('.glslv.c', '.glslf.c')
+ at after_method('glsl_file')
+def glslc_file(self, node):
+    print 'test'
+    print node.get_src().abspath()
+    print node.get_bld().abspath()
+
+    #with open()
+
+    return 0
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Branch.glslf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Branch.glslf	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,6 @@
+precision mediump float;
+niform vec4 color;
+void main()
+{
+    gl_FragColor = color;
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Branch.glslv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Branch.glslv	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,5 @@
+attribute vec2 vertex;
+void main()
+{
+    gl_Position = vec4(vertex,0.0,1.0);
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Canvas.glslf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Canvas.glslf	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,7 @@
+precision mediump float;
+uniform sampler2D tex;
+varying vec2 vTexCoord;
+void main()
+{
+    gl_FragColor = texture2D(tex, vTexCoord);
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Canvas.glslv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Canvas.glslv	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,8 @@
+attribute vec2 vertex;
+attribute vec2 texcoord;
+varying vec2 vTexCoord;
+void main()
+{
+    gl_Position = vec4(vertex,0.0,1.0);
+    vTexCoord = texcoord;
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Colored.glslf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Colored.glslf	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,34 @@
+precision mediump float;
+uniform vec4 scene_ambient;
+uniform vec4 light_diffuse[8], light_specular[8];
+uniform vec4 mat_ambient, mat_diffuse, mat_specular;
+uniform vec4 mat_emission;
+uniform float mat_shininess;
+uniform vec3 light_pos[8];
+uniform int light_num;
+varying vec3 vVertex, vNormal;
+void main()
+{
+    vec3 n = normalize(vNormal);
+    vec3 v = normalize(-vVertex);
+
+    vec4 total = mat_ambient * scene_ambient;
+
+    for (int i = 0; i < 8; i++)
+    {
+        if (i >= light_num) {break;}
+        vec3 l = light_pos[i]-vVertex;
+        float d = length(l);
+        l /= d;
+        if (d > 100.0) {continue;}
+        vec3 h = normalize(l+v);
+        float d2 = d/(1.0-pow(d/20.0,2.0));
+        float attenuation = 1.0/pow(1.0 + 0.01*d2,2.0);
+        vec4 diffuse = max(dot(l, n), 0.0) * mat_diffuse *
+                    light_diffuse[i];
+        vec4 specular = pow(max(dot(n, h), 0.0), mat_shininess)
+                        * mat_specular * light_specular[i];
+        total += (diffuse+specular)*attenuation;
+    }
+    gl_FragColor = total + mat_emission;
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Colored.glslv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Colored.glslv	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,9 @@
+attribute vec3 vertex, normal;
+uniform mat4 mv_matrix, p_matrix;
+varying vec3 vVertex, vNormal;
+void main()
+{
+    gl_Position = p_matrix*mv_matrix*vec4(vertex,1.0);
+    vNormal = vec3(mv_matrix*vec4(normal,0.0));
+    vVertex = vec3(mv_matrix*vec4(vertex,1.0));
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/HScroll.glslf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/HScroll.glslf	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,6 @@
+precision mediump float;
+uniform vec4 color;
+void main()
+{
+    gl_FragColor = color;
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/HScroll.glslv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/HScroll.glslv	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,5 @@
+attribute vec2 vertex;
+void main()
+{
+    gl_Position = vec4(vertex,0.0,1.0);
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Light.glslf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Light.glslf	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,7 @@
+precision mediump float;
+uniform sampler2D tex;
+varying vec2 vTexCoord;
+void main()
+{
+    gl_FragColor = texture2D(tex, vTexCoord);
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Light.glslv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Light.glslv	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,9 @@
+attribute vec3 vertex;
+attribute vec2 texcoord;
+uniform mat4 mv_matrix, p_matrix;
+varying vec2 vTexCoord;
+void main()
+{
+    gl_Position = p_matrix*mv_matrix*vec4(vertex,1.0);
+    vTexCoord = texcoord;
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Material.glslf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Material.glslf	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,32 @@
+fragment_shader : array of string = {"
+precision mediump float;
+uniform vec4 scene_ambient;
+uniform vec4 light_diffuse[8], light_specular[8];
+uniform vec3 light_pos[8];
+uniform int light_num;
+varying vec3 vVertex, vNormal;
+void main()
+{
+    vec3 n = normalize(vNormal);
+    vec3 v = normalize(-vVertex);
+
+    vec4 total = scene_ambient;
+
+    for (int i = 0; i < 8; i++)
+    {
+        if (i >= light_num) {break;}
+        vec3 l = light_pos[i]-vVertex;
+        float d = length(l);
+        l /= d;
+        if (d > 100.0) {continue;}
+        vec3 h = normalize(l+v);
+        float d2 = d/(1.0-pow(d/20.0,2.0));
+        float attenuation = 1.0/pow(1.0 + 0.01*d2,2.0);
+        vec4 diffuse = max(dot(l, n), 0.0) * light_diffuse[i];
+        vec4 specular = pow(max(dot(n, h), 0.0), 32.0)
+                        * light_specular[i];
+        total += (diffuse+specular)*attenuation;
+    }
+
+    gl_FragColor = total;
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Material.glslv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Material.glslv	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,9 @@
+attribute vec3 vertex, normal;
+uniform mat4 mv_matrix, p_matrix;
+varying vec3 vVertex, vNormal;
+void main()
+{
+    gl_Position = p_matrix*mv_matrix*vec4(vertex,1.0);
+    vNormal = vec3(mv_matrix*vec4(normal,0.0));
+    vVertex = vec3(mv_matrix*vec4(vertex,1.0));
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Scene.glslf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Scene.glslf	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,21 @@
+precision mediump float;
+uniform sampler2D bodies;
+varying vec2 vVertex;
+
+void main()
+{
+    vec2 c1 = vec2(fract(4.0*vVertex.x),
+                    floor(4.0*vVertex.x)/4.0);
+    vec2 c2 = vec2(fract(4.0*vVertex.y),
+                    floor(4.0*vVertex.y)/4.0);
+    vec4 body1 = texture2D(bodies,c1);
+    vec4 body2 = texture2D(bodies,c2);
+    float x = body1.r - body2.r;
+    float y = body1.g - body2.g;
+    float z = body1.b - body2.b;
+    float r_sum = body1.a + body2.a;
+    if (x*x + y*y + z*z > r_sum*r_sum) {
+        discard;
+    }
+    gl_FragColor = vec4(1.0,1.0,1.0,1.0);
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Scene.glslv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Scene.glslv	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,9 @@
+attribute vec2 vertex;
+varying vec2 vVertex;
+
+void main()
+{
+    gl_Position = vec4(vertex*2.0-1.0,0.0,1.0);
+    gl_PointSize = 1.0;
+    vVertex = vertex;
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Textured.glslf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Textured.glslf	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,49 @@
+precision mediump float;
+uniform vec4 scene_ambient;
+uniform vec4 light_diffuse[8], light_specular[8];
+uniform vec4 mat_ambient, mat_diffuse, mat_specular;
+uniform vec4 mat_emission;
+uniform float mat_shininess;
+uniform sampler2D colormap, glowmap, bumpmap;
+uniform samplerCube colormap_cube, glowmap_cube, bumpmap_cube;
+uniform vec3 light_pos[8];
+uniform int light_num;
+varying vec3 vVertex, vVertexRaw, vNormal, vTangent;
+varying vec2 vTexCoordColor, vTexCoordGlow, vTexCoordBump;
+void main() {
+    vec3 texN = vec3(textureCube(bumpmap_cube, normalize(
+                vVertexRaw))+texture2D(bumpmap,vTexCoordBump))
+                * 2.0 - 1.0;
+    vec3 tangentN = normalize(vNormal);
+    vec3 tangentT = normalize(vTangent);
+    vec3 tangentB = normalize(cross(tangentN,tangentT));
+    vec3 n = mat3(tangentT,tangentB,tangentN) * texN;
+    vec3 v = normalize(-vVertex);
+
+    vec4 totalDiffuse = mat_ambient * scene_ambient;
+    vec4 totalSpecular = vec4(0.0);
+    for (int i = 0; i < 8; i++)
+    {
+        if (i >= light_num) {break;}
+        vec3 l = light_pos[i]-vVertex;
+        float d = length(l);
+        l /= d;
+        if (d > 20.0) {continue;}
+        vec3 h = normalize(l+v);
+        float d2 = d/(1.0-pow(d/20.0,2.0));
+        float attenuation = 1.0/pow(1.0 + 0.01*d2,2.0);
+        totalDiffuse += max(dot(l, n), 0.0) * mat_diffuse *
+                        light_diffuse[i] * attenuation;
+        totalSpecular += pow(max(dot(n, h), 0.0), mat_shininess)
+                            * mat_specular * light_specular[i] *
+                            attenuation;
+    }
+
+    vec3 vertexNorm = normalize(vVertexRaw);
+    gl_FragColor = (totalDiffuse*(textureCube(colormap_cube
+                    ,vertexNorm)+texture2D(colormap,
+                    vTexCoordColor)) + totalSpecular) +
+                    mat_emission + textureCube(glowmap_cube,
+                    vertexNorm) + texture2D(glowmap,
+                    vTexCoordGlow);
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Textured.glslv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Textured.glslv	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,17 @@
+attribute vec3 vertex, normal, tangent;
+attribute vec2 texcoord;
+uniform mat4 mv_matrix, p_matrix, tc_matrix, tg_matrix;
+uniform mat4 tb_matrix;
+varying vec3 vVertex, vVertexRaw, vNormal, vTangent;
+varying vec2 vTexCoordColor, vTexCoordGlow, vTexCoordBump;
+void main()
+{
+    gl_Position = p_matrix*mv_matrix*vec4(vertex,1.0);
+    vNormal = vec3(mv_matrix*vec4(normal,0.0));
+    vVertex = vec3(mv_matrix*vec4(vertex,1.0));
+    vTangent = vec3(mv_matrix*vec4(tangent,0.0));
+    vVertexRaw = vertex;
+    vTexCoordColor = vec2(tc_matrix*vec4(texcoord,0.0,1.0));
+    vTexCoordGlow = vec2(tg_matrix*vec4(texcoord,0.0,1.0));
+    vTexCoordBump = vec2(tb_matrix*vec4(texcoord,0.0,1.0));
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/VScroll.glslf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/VScroll.glslf	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,6 @@
+precision mediump float;
+uniform vec4 color;
+void main()
+{
+    gl_FragColor = color;
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/VScroll.glslv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/VScroll.glslv	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,5 @@
+attribute vec2 vertex;
+void main()
+{
+    gl_Position = vec4(vertex,0.0,1.0);
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Wireframed.glslf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Wireframed.glslf	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,4 @@
+void main()
+{
+    gl_FragColor = vec4(1.0,1.0,1.0,1.0);
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/shaders/Wireframed.glslv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shaders/Wireframed.glslv	Sat Dec 28 22:46:30 2013 +0100
@@ -0,0 +1,6 @@
+attribute vec3 vertex, normal;
+uniform mat4 mv_matrix, p_matrix;
+void main()
+{
+    gl_Position = p_matrix*mv_matrix*vec4(vertex,1.0);
+}
\ No newline at end of file
diff -r 3d2e698adec0 -r 0bac2d2d5b71 src/wscript_build
--- a/src/wscript_build	Fri Dec 27 15:59:14 2013 -0500
+++ b/src/wscript_build	Sat Dec 28 22:46:30 2013 +0100
@@ -1,5 +1,5 @@
 # encoding: utf-8
-source = ['init.c'] + bld.path.ant_glob('**/*.gs')
+source = ['init.c'] + bld.path.ant_glob('**/*.gs') + bld.path.ant_glob('**/*.glsl*')
 uselib = ['DBUS_GLIB', 'GEE', 'RSVG', 'LM', 'NICE', 'BLUEZ', 'GL', 'ODE']
 packages = ['config', 'gles-2.0', 'gee-1.0', 'cairo', 'gdk-2.0', 'librsvg-2.0',
             'ode-0.11',
diff -r 3d2e698adec0 -r 0bac2d2d5b71 wscript
--- a/wscript	Fri Dec 27 15:59:14 2013 -0500
+++ b/wscript	Sat Dec 28 22:46:30 2013 +0100
@@ -38,7 +38,7 @@
 
 def configure(conf) :
     # Check for a C compiler, valac
-    conf.load('compiler_c genie')
+    conf.load('compiler_c genie glsl')
 
     # Unit testing
     conf.load('waf_unit_test')


More information about the PySoy-dev mailing list