]> Pileus Git - grits/commitdiff
Update sorting example to use a shader (broken)
authorAndy Spencer <andy753421@gmail.com>
Mon, 25 Jan 2010 07:36:11 +0000 (07:36 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 25 Jan 2010 09:33:08 +0000 (09:33 +0000)
Maybe we can use a shader to make the textures work? Not sure if this is
possible or not..

examples/sorting/sort.c
examples/sorting/sort.glsl [new file with mode: 0644]

index 7cb54c5aaea033cfeebce8f69df41d27c068d150..50ccda98c54dd4ef38a62475625b92de32594d2a 100644 (file)
@@ -264,6 +264,56 @@ static guint load_tex(gchar *filename)
        return tex;
 }
 
+gchar *gl_program_log(guint program, int *_len)
+{
+       gchar *buf = NULL;
+       int len = 0;
+       glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len);
+       if (len > 0) {
+               buf = g_malloc(len);
+               glGetProgramInfoLog(program, len, &len, buf);
+       }
+       if (_len)
+               *_len = len;
+       return buf;
+}
+
+static void load_shader(gchar *filename)
+{
+       gchar *source;
+       gboolean status = g_file_get_contents(filename, &source, NULL, NULL);
+       if (!status)
+               g_error("Failed to load shader");
+
+       guint program = glCreateProgram();
+       if (!program)
+               g_error("Error creating program");
+
+       guint shader = glCreateShader(GL_VERTEX_SHADER_ARB);
+       if (!shader)
+               g_error("Error creating shader");
+
+       glShaderSource(shader, 1, (const gchar**)&source, NULL);
+       if (glGetError())
+               g_error("Error setting shader source");
+
+       glCompileShader(shader);
+       if (glGetError())
+               g_error("Error compiling shader");
+
+       glAttachShader(program, shader);
+       if (glGetError())
+               g_error("Error attaching shader");
+
+       glLinkProgram(program);
+       if (glGetError())
+               g_error("Error linking program");
+
+       glUseProgram(program);
+       if (glGetError())
+               g_error("Error using program:\n%s", gl_program_log(program, NULL));
+}
+
 int main(int argc, char **argv)
 {
        gtk_init(&argc, &argv);
@@ -289,6 +339,9 @@ int main(int argc, char **argv)
        /* Load texture */
        tex = load_tex("flag.png");
 
+       /* Load shader */
+       load_shader("sort.glsl");
+
        gtk_main();
 
        gdk_gl_drawable_gl_end(gldrawable);
diff --git a/examples/sorting/sort.glsl b/examples/sorting/sort.glsl
new file mode 100644 (file)
index 0000000..500e23b
--- /dev/null
@@ -0,0 +1,14 @@
+void main()
+{
+       gl_FrontColor = gl_Color;
+       gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
+
+       gl_Position[0] = 1; //gl_Vertex[0];
+       gl_Position[1] = 1; //gl_Vertex[1];
+       gl_Position[2] = 1; //gl_Vertex[2];
+
+       vec4 projected = gl_ModelViewProjectionMatrix * gl_Vertex;
+       gl_Position = projected;
+       //gl_Position[3] = 100 + gl_Vertex[3];
+       //gl_Position = gl_Vertex;
+}