Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Test GL_EXT_framebuffer_object |
| 3 | * |
| 4 | * Brian Paul |
| 5 | * 7 Feb 2005 |
| 6 | */ |
| 7 | |
| 8 | |
| 9 | #define GL_GLEXT_PROTOTYPES |
| 10 | #include <assert.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <math.h> |
| 14 | #include <GL/glut.h> |
| 15 | |
Brian | 2282d81 | 2007-03-06 16:33:00 -0700 | [diff] [blame] | 16 | static int Win; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 17 | static int Width = 400, Height = 400; |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 18 | static GLuint MyFB, MyRB; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | static void |
| 22 | CheckError(int line) |
| 23 | { |
| 24 | GLenum err = glGetError(); |
| 25 | if (err) { |
| 26 | printf("GL Error 0x%x at line %d\n", (int) err, line); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | |
| 31 | static void |
| 32 | Display( void ) |
| 33 | { |
| 34 | GLubyte *buffer = malloc(Width * Height * 4); |
| 35 | GLenum status; |
| 36 | |
| 37 | /* draw to user framebuffer */ |
| 38 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 39 | glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT); |
| 40 | glReadBuffer(GL_COLOR_ATTACHMENT1_EXT); |
| 41 | |
| 42 | status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
| 43 | if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 44 | printf("Framebuffer incomplete!!!\n"); |
| 45 | } |
| 46 | |
| 47 | glClearColor(0.5, 0.5, 1.0, 0.0); |
| 48 | glClear( GL_COLOR_BUFFER_BIT ); |
| 49 | |
| 50 | glBegin(GL_POLYGON); |
| 51 | glColor3f(1, 0, 0); |
| 52 | glVertex2f(-1, -1); |
| 53 | glColor3f(0, 1, 0); |
| 54 | glVertex2f(1, -1); |
| 55 | glColor3f(0, 0, 1); |
| 56 | glVertex2f(0, 1); |
| 57 | glEnd(); |
| 58 | |
| 59 | /* read from user framebuffer */ |
| 60 | glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 61 | |
| 62 | /* draw to window */ |
| 63 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 64 | glWindowPos2iARB(0, 0); |
| 65 | glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 66 | |
| 67 | free(buffer); |
| 68 | glutSwapBuffers(); |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 69 | CheckError(__LINE__); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| 73 | static void |
| 74 | Reshape( int width, int height ) |
| 75 | { |
Brian Paul | cf085db | 2005-10-08 14:42:51 +0000 | [diff] [blame] | 76 | #if 0 |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 77 | float ar = (float) width / (float) height; |
Brian Paul | cf085db | 2005-10-08 14:42:51 +0000 | [diff] [blame] | 78 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 79 | glViewport( 0, 0, width, height ); |
| 80 | glMatrixMode( GL_PROJECTION ); |
| 81 | glLoadIdentity(); |
| 82 | #if 0 |
| 83 | glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 ); |
| 84 | #else |
| 85 | glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0 ); |
| 86 | #endif |
| 87 | glMatrixMode( GL_MODELVIEW ); |
| 88 | glLoadIdentity(); |
| 89 | glTranslatef( 0.0, 0.0, -15.0 ); |
| 90 | Width = width; |
| 91 | Height = height; |
| 92 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static void |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 97 | CleanUp(void) |
| 98 | { |
| 99 | glDeleteFramebuffersEXT(1, &MyFB); |
| 100 | glDeleteRenderbuffersEXT(1, &MyRB); |
| 101 | assert(!glIsFramebufferEXT(MyFB)); |
| 102 | assert(!glIsRenderbufferEXT(MyRB)); |
Brian | 2282d81 | 2007-03-06 16:33:00 -0700 | [diff] [blame] | 103 | glutDestroyWindow(Win); |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 104 | exit(0); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static void |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 109 | Key( unsigned char key, int x, int y ) |
| 110 | { |
| 111 | (void) x; |
| 112 | (void) y; |
| 113 | switch (key) { |
| 114 | case 27: |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 115 | CleanUp(); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 116 | break; |
| 117 | } |
| 118 | glutPostRedisplay(); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static void |
| 123 | Init( void ) |
| 124 | { |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 125 | GLint i; |
| 126 | |
| 127 | if (!glutExtensionSupported("GL_EXT_framebuffer_object")) { |
| 128 | printf("GL_EXT_framebuffer_object not found!\n"); |
| 129 | /*exit(0);*/ |
| 130 | } |
| 131 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 132 | |
| 133 | glGenFramebuffersEXT(1, &MyFB); |
| 134 | assert(MyFB); |
Brian Paul | 9e920fb | 2005-10-04 15:01:51 +0000 | [diff] [blame] | 135 | assert(!glIsFramebufferEXT(MyFB)); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 136 | glDeleteFramebuffersEXT(1, &MyFB); |
| 137 | assert(!glIsFramebufferEXT(MyFB)); |
| 138 | /* Note, continue to use MyFB below */ |
| 139 | |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 140 | glGenRenderbuffersEXT(1, &MyRB); |
| 141 | assert(MyRB); |
| 142 | assert(!glIsRenderbufferEXT(MyRB)); |
| 143 | glDeleteRenderbuffersEXT(1, &MyRB); |
| 144 | assert(!glIsRenderbufferEXT(MyRB)); |
| 145 | MyRB = 42; /* an arbitrary ID */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 146 | |
| 147 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 148 | assert(glIsFramebufferEXT(MyFB)); |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 149 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, MyRB); |
| 150 | assert(glIsRenderbufferEXT(MyRB)); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 151 | |
| 152 | glGetIntegerv(GL_RENDERBUFFER_BINDING_EXT, &i); |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 153 | assert(i == MyRB); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 154 | |
| 155 | glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &i); |
| 156 | assert(i == MyFB); |
| 157 | |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 158 | CheckError(__LINE__); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 159 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 160 | GL_RENDERBUFFER_EXT, MyRB); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 161 | |
| 162 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height); |
| 163 | |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 164 | CheckError(__LINE__); |
| 165 | |
Brian Paul | 58a9573 | 2005-07-01 01:34:29 +0000 | [diff] [blame] | 166 | { |
| 167 | GLint r, g, b, a; |
| 168 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 169 | GL_RENDERBUFFER_RED_SIZE_EXT, &r); |
| 170 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 171 | GL_RENDERBUFFER_GREEN_SIZE_EXT, &g); |
| 172 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 173 | GL_RENDERBUFFER_BLUE_SIZE_EXT, &b); |
| 174 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 175 | GL_RENDERBUFFER_ALPHA_SIZE_EXT, &a); |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 176 | CheckError(__LINE__); |
Brian Paul | 58a9573 | 2005-07-01 01:34:29 +0000 | [diff] [blame] | 177 | printf("renderbuffer RGBA sizes = %d %d %d %d\n", r, g, b, a); |
Brian Paul | cf085db | 2005-10-08 14:42:51 +0000 | [diff] [blame] | 178 | |
| 179 | glGetIntegerv(GL_RED_BITS, &r); |
| 180 | glGetIntegerv(GL_GREEN_BITS, &g); |
| 181 | glGetIntegerv(GL_BLUE_BITS, &b); |
| 182 | glGetIntegerv(GL_ALPHA_BITS, &a); |
| 183 | printf("Visual RGBA sizes = %d %d %d %d\n", r, g, b, a); |
Brian Paul | 58a9573 | 2005-07-01 01:34:29 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 186 | /* restore to default */ |
| 187 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
Brian Paul | f81c78f8 | 2005-11-17 02:08:10 +0000 | [diff] [blame] | 188 | CheckError(__LINE__); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | int |
| 193 | main( int argc, char *argv[] ) |
| 194 | { |
| 195 | glutInit( &argc, argv ); |
| 196 | glutInitWindowPosition( 0, 0 ); |
| 197 | glutInitWindowSize(Width, Height); |
| 198 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
Brian | 2282d81 | 2007-03-06 16:33:00 -0700 | [diff] [blame] | 199 | Win = glutCreateWindow(argv[0]); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 200 | glutReshapeFunc( Reshape ); |
| 201 | glutKeyboardFunc( Key ); |
| 202 | glutDisplayFunc( Display ); |
| 203 | Init(); |
| 204 | glutMainLoop(); |
| 205 | return 0; |
| 206 | } |