summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Franzke <benjaminfranzke@googlemail.com>2012-03-14 22:05:48 +0100
committerBenjamin Franzke <benjaminfranzke@googlemail.com>2012-03-14 22:24:58 +0100
commit741859d0bda6ae5281ed6b7e5169a1d2d1746744 (patch)
tree815cd1c9c11b16b7e99abd8533876ec3f1ca80d7
parent5b50311c440dfbb2f8667161d78ca673dfdcff4b (diff)
downloadcv-741859d0bda6ae5281ed6b7e5169a1d2d1746744.tar.gz
cv-741859d0bda6ae5281ed6b7e5169a1d2d1746744.tar.bz2
cv-741859d0bda6ae5281ed6b7e5169a1d2d1746744.zip
wimmel: Render to a fbo
-rw-r--r--wimmel.frag2
-rw-r--r--wimmel_gl.c80
2 files changed, 54 insertions, 28 deletions
diff --git a/wimmel.frag b/wimmel.frag
index d4cb7ef..d3cf745 100644
--- a/wimmel.frag
+++ b/wimmel.frag
@@ -21,7 +21,7 @@ main()
}
}
- gl_FragColor = vec4(float(diff < barrier), 0.0, 0.0, 0.0);
+ gl_FragColor = vec4(float(diff < barrier), 0.0, 0.0, 1.0);
}
// vim:ft=c:
diff --git a/wimmel_gl.c b/wimmel_gl.c
index 4d6456e..223a9de 100644
--- a/wimmel_gl.c
+++ b/wimmel_gl.c
@@ -6,7 +6,7 @@
#include <GL/gl.h>
#include <GL/glext.h>
-int window = 0;
+//int window = 0;
GLuint vbo;
GLint proj_uniform, tex_uniform, pixelsize_uniform,
search_rect_uniform, barrier_uniform;
@@ -106,23 +106,6 @@ init_shaders()
}
static void
-keypress(unsigned char key, int x, int y)
-{
- switch (key) {
- case 27:
- glutDestroyWindow(window);
- exit(0);
- break;
- }
-}
-
-static void
-resize(int w, int h)
-{
- glViewport(0, 0, w, h);
-}
-
-static void
create_texture(GdkPixbuf *pixbuf)
{
void *data;
@@ -170,7 +153,7 @@ draw(void)
frames = 0;
}
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glClear(GL_COLOR_BUFFER_BIT);
glUniformMatrix4fv(proj_uniform, 1, GL_FALSE, proj);
glUniform1i(tex_uniform, 0);
@@ -196,7 +179,7 @@ draw(void)
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- glutSwapBuffers();
+ glFinish();
printf("finished\n");
}
@@ -212,7 +195,6 @@ main(int argc, char **argv)
glutInitDisplayMode(GLUT_RGBA /*| GLUT_DEPTH */ | GLUT_DOUBLE);
- glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
pixbuf = gdk_pixbuf_new_from_file(argv[1], NULL);
if (!pixbuf)
@@ -226,18 +208,62 @@ main(int argc, char **argv)
mwidth = atoi(argv[4]);
mheight = atoi(argv[5]);
- glutInitWindowSize(width, height);
- window = glutCreateWindow("wimmel");
+ glutInitWindowSize(1,1);
+ int window = glutCreateWindow("wimmel");
+ glutDestroyWindow(window);
+
+ GLuint fb, rb;
+
+ glGenFramebuffersEXT(1, &fb);
+ glGenRenderbuffersEXT(1, &rb);
- glutDisplayFunc(draw);
- glutKeyboardFunc(keypress);
- glutReshapeFunc(resize);
+ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
+ glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb);
+
+ glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
+ GL_COLOR_ATTACHMENT0_EXT,
+ GL_RENDERBUFFER_EXT,
+ rb);
+ glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,
+ GL_RGBA,
+ width, height);
+
+ if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) !=
+ GL_FRAMEBUFFER_COMPLETE_EXT) {
+ fprintf(stderr, "framebuffer not complete\n");
+ return 1;
+ }
init_shaders();
create_texture(pixbuf);
glViewport(0,0,width,height);
- glutMainLoop();
+ glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+
+ draw();
+
+ glPixelStorei(GL_PACK_ALIGNMENT, 2);
+
+ gint stride = width * 4;
+ guchar *buffer = malloc(stride * height);
+ glReadPixels(0, 0, width, height,
+ GL_RGBA, GL_UNSIGNED_BYTE,
+ buffer);
+
+ GdkPixbuf *t_output, *output;
+
+ t_output = gdk_pixbuf_new_from_data(buffer,
+ GDK_COLORSPACE_RGB,
+ TRUE, 8,
+ width, height, stride,
+ NULL, NULL);
+ output = gdk_pixbuf_flip(t_output, FALSE);
+
+ gdk_pixbuf_save(output, "output_gl.png", "png", NULL, NULL);
+ g_object_unref(output);
+ g_object_unref(t_output);
+ free(buffer);
+ g_object_unref(pixbuf);
return 0;
}