X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=examples%2Fsorting%2Fsort.c;h=839c7c55170d48a5a46f4a97618ab93563474bb7;hb=cd2b3c70807c914687f9051b34a8fe029de300ee;hp=7cb54c5aaea033cfeebce8f69df41d27c068d150;hpb=255e2b945fa84f92a64109e078a9665fff7bcc25;p=grits diff --git a/examples/sorting/sort.c b/examples/sorting/sort.c index 7cb54c5..839c7c5 100644 --- a/examples/sorting/sort.c +++ b/examples/sorting/sort.c @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2009-2010 Andy Spencer + * + * 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 . + */ + #define GL_GLEXT_PROTOTYPES #include #include @@ -264,6 +281,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 +356,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);