From adfa666208bbf720e0dd33602661384b146e5268 Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Sun, 19 Feb 2012 17:46:50 +0100 Subject: FBO example added --- samples/OpenGL/framebuffer/Makefile | 5 + samples/OpenGL/framebuffer/minimalfbo.c | 217 +++++++++++++++++++++ .../texture_distortion_glsl.c | 2 +- samples/OpenGL/x11argb_opengl/x11argb_opengl.c | 2 + samples/OpenGL/x11argb_opengl_glsl/Makefile | 2 + .../x11argb_opengl_glsl/x11argb_opengl_glsl.c | 2 +- 6 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 samples/OpenGL/framebuffer/Makefile create mode 100644 samples/OpenGL/framebuffer/minimalfbo.c diff --git a/samples/OpenGL/framebuffer/Makefile b/samples/OpenGL/framebuffer/Makefile new file mode 100644 index 0000000..60077c0 --- /dev/null +++ b/samples/OpenGL/framebuffer/Makefile @@ -0,0 +1,5 @@ +OBJS = minimalfbo.o + +minimalfbo: $(OBJS) + $(CC) -o minimalfbo $(OBJS) -lm -lGL -lGLU -lGLEW -lglut + diff --git a/samples/OpenGL/framebuffer/minimalfbo.c b/samples/OpenGL/framebuffer/minimalfbo.c new file mode 100644 index 0000000..f61d9de --- /dev/null +++ b/samples/OpenGL/framebuffer/minimalfbo.c @@ -0,0 +1,217 @@ +#include +#include + +#include +#include +#include + +void init(); +void reshape(int width, int height); +void display(); + +int const fbo_width = 512; +int const fbo_height = 512; + +GLuint fb, color, depth; + +int main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ); + + glutCreateWindow("FBO test"); + glutDisplayFunc(display); + glutReshapeFunc(reshape); + glutIdleFunc(glutPostRedisplay); + + glewInit(); + + init(); + glutMainLoop(); + + return 0; +} + +void CHECK_FRAMEBUFFER_STATUS() +{ + GLenum status; + status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); + switch(status) { + case GL_FRAMEBUFFER_COMPLETE: + break; + + case GL_FRAMEBUFFER_UNSUPPORTED: + /* choose different formats */ + break; + + default: + /* programming error; will fail on all hardware */ + fputs("Framebuffer Error\n", stderr); + exit(-1); + } +} + +float const light_dir[]={1,1,1,0}; +float const light_color[]={1,0.95,0.9,1}; + +void init() +{ + glGenFramebuffers(1, &fb); + glGenTextures(1, &color); + glGenRenderbuffers(1, &depth); + + glBindFramebuffer(GL_FRAMEBUFFER, fb); + + glBindTexture(GL_TEXTURE_2D, color); + glTexImage2D( GL_TEXTURE_2D, + 0, + GL_RGBA, + fbo_width, fbo_height, + 0, + GL_RGBA, + GL_UNSIGNED_BYTE, + NULL); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0); + + glBindRenderbuffer(GL_RENDERBUFFER, depth); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, fbo_width, fbo_height); + glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth); + + CHECK_FRAMEBUFFER_STATUS(); +} + +void reshape(int width, int height) +{ + glutPostRedisplay(); +} + +void prepare() +{ + static float a=0, b=0, c=0; + + glBindTexture(GL_TEXTURE_2D, 0); + glEnable(GL_TEXTURE_2D); + glBindFramebuffer(GL_FRAMEBUFFER, fb); + + glViewport(0,0, fbo_width, fbo_height); + + glClearColor(1,1,1,0); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45, 1, 1, 10); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glEnable(GL_LIGHT0); + glEnable(GL_LIGHTING); + + glEnable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + + glLightfv(GL_LIGHT0, GL_POSITION, light_dir); + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color); + + glTranslatef(0,0,-5); + + glRotatef(a, 1, 0, 0); + glRotatef(b, 0, 1, 0); + glRotatef(c, 0, 0, 1); + + glutSolidTeapot(0.75); + + a=fmod(a+0.1, 360.); + b=fmod(b+0.5, 360.); + c=fmod(c+0.25, 360.); +} + +void final() +{ + static float a=0, b=0, c=0; + + const int win_width = glutGet(GLUT_WINDOW_WIDTH); + const int win_height = glutGet(GLUT_WINDOW_HEIGHT); + const float aspect = (float)win_width/(float)win_height; + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + glViewport(0,0, win_width, win_height); + + glClearColor(1.,1.,1.,0.); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45, aspect, 1, 10); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0,0,-5); + + glRotatef(b, 0, 1, 0); + + b=fmod(b+0.5, 360.); + + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, color); + + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glDisable(GL_LIGHTING); + + float cube[][5]= + { + {-1, -1, -1, 0, 0}, + { 1, -1, -1, 1, 0}, + { 1, 1, -1, 1, 1}, + {-1, 1, -1, 0, 1}, + + {-1, -1, 1, -1, 0}, + { 1, -1, 1, 0, 0}, + { 1, 1, 1, 0, 1}, + {-1, 1, 1, -1, 1}, + }; + unsigned int faces[]= + { + 0, 1, 2, 3, + 1, 5, 6, 2, + 5, 4, 7, 6, + 4, 0, 3, 7, + 3, 2, 6, 7, + 4, 5, 1, 0 + }; + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + glVertexPointer(3, GL_FLOAT, 5*sizeof(float), &cube[0][0]); + glTexCoordPointer(2, GL_FLOAT, 5*sizeof(float), &cube[0][3]); + + glCullFace(GL_BACK); + glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, faces); + + glCullFace(GL_FRONT); + glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, faces); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + +} + +void display() +{ + prepare(); + final(); + + glutSwapBuffers(); +} + diff --git a/samples/OpenGL/texture_distortion_glsl/texture_distortion_glsl.c b/samples/OpenGL/texture_distortion_glsl/texture_distortion_glsl.c index 7742746..437dd92 100644 --- a/samples/OpenGL/texture_distortion_glsl/texture_distortion_glsl.c +++ b/samples/OpenGL/texture_distortion_glsl/texture_distortion_glsl.c @@ -43,7 +43,7 @@ static const GLchar *fragment_shader_source = " float ts = gl_TexCoord[0].s;\n" " vec2 mod_texcoord = gl_TexCoord[0].st*vec2(1., 2.) + vec2(0, -0.5 + 0.5*sin(T + 1.5*ts*pi));\n" " if( mod_texcoord.t < 0. || mod_texcoord.t > 1. ) { discard; }\n" -" gl_FragColor = -texture2D(texCMYK, mod_texcoord) + texture2D(texRGB, gl_TexCoord[0].st);\n" +" gl_FragColor.rgb = -texture2D(texCMYK, mod_texcoord).xyz + texture2D(texRGB, gl_TexCoord[0].st).xyz;\n" "}\n\0"; GLuint shaderFragment = 0; diff --git a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c index 8eaddf8..7a073fb 100644 --- a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c +++ b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c @@ -423,8 +423,10 @@ static void redrawTheWindow() glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); +#if 0 glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +#endif glLightfv(GL_LIGHT0, GL_POSITION, light0_dir); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color); diff --git a/samples/OpenGL/x11argb_opengl_glsl/Makefile b/samples/OpenGL/x11argb_opengl_glsl/Makefile index 6c234cb..e32b597 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/Makefile +++ b/samples/OpenGL/x11argb_opengl_glsl/Makefile @@ -1,3 +1,5 @@ +CC=pcc + x11argb_opengl_glsl: x11argb_opengl_glsl.c Makefile $(CC) -std=c99 -g3 -o x11argb_opengl_glsl x11argb_opengl_glsl.c -lX11 -lXrender -lGLEW -lm diff --git a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c index 643c0ca..d918f0b 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c +++ b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c @@ -62,7 +62,7 @@ static const GLchar *fragment_shader_source = " vec2 mod_texcoord = gl_TexCoord[0].st*vec2(1., 2.) + vec2(0, -0.5 + 0.5*sin(T + 1.5*ts*pi));\n" " if( mod_texcoord.t < 0. || mod_texcoord.t > 1. ) { discard; }\n" " gl_FragColor = -texture2D(texCMYK, mod_texcoord) + texture2D(texRGB, gl_TexCoord[0].st);\n" -" gl_FragColor.a = -0.5;\n" +" gl_FragColor.a = 1.;\n" "}\n\0"; GLuint shaderFragment = 0; -- cgit v1.2.3