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