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; |
| 17 | static GLuint MyFB; |
| 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(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | static void |
| 72 | Reshape( int width, int height ) |
| 73 | { |
| 74 | float ar = (float) width / (float) height; |
| 75 | glViewport( 0, 0, width, height ); |
| 76 | glMatrixMode( GL_PROJECTION ); |
| 77 | glLoadIdentity(); |
| 78 | #if 0 |
| 79 | glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 ); |
| 80 | #else |
| 81 | glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0 ); |
| 82 | #endif |
| 83 | glMatrixMode( GL_MODELVIEW ); |
| 84 | glLoadIdentity(); |
| 85 | glTranslatef( 0.0, 0.0, -15.0 ); |
| 86 | Width = width; |
| 87 | Height = height; |
| 88 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | static void |
| 93 | Key( unsigned char key, int x, int y ) |
| 94 | { |
| 95 | (void) x; |
| 96 | (void) y; |
| 97 | switch (key) { |
| 98 | case 27: |
| 99 | exit(0); |
| 100 | break; |
| 101 | } |
| 102 | glutPostRedisplay(); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static void |
| 107 | Init( void ) |
| 108 | { |
| 109 | GLuint rb; |
| 110 | GLint i; |
| 111 | |
| 112 | if (!glutExtensionSupported("GL_EXT_framebuffer_object")) { |
| 113 | printf("GL_EXT_framebuffer_object not found!\n"); |
| 114 | /*exit(0);*/ |
| 115 | } |
| 116 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 117 | |
| 118 | glGenFramebuffersEXT(1, &MyFB); |
| 119 | assert(MyFB); |
| 120 | assert(glIsFramebufferEXT(MyFB)); |
| 121 | glDeleteFramebuffersEXT(1, &MyFB); |
| 122 | assert(!glIsFramebufferEXT(MyFB)); |
| 123 | /* Note, continue to use MyFB below */ |
| 124 | |
| 125 | glGenRenderbuffersEXT(1, &rb); |
| 126 | assert(rb); |
| 127 | assert(glIsRenderbufferEXT(rb)); |
| 128 | glDeleteRenderbuffersEXT(1, &rb); |
| 129 | assert(!glIsRenderbufferEXT(rb)); |
| 130 | rb = 42; /* an arbitrary ID */ |
| 131 | |
| 132 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 133 | assert(glIsFramebufferEXT(MyFB)); |
| 134 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb); |
| 135 | assert(glIsRenderbufferEXT(rb)); |
| 136 | |
| 137 | glGetIntegerv(GL_RENDERBUFFER_BINDING_EXT, &i); |
| 138 | assert(i == rb); |
| 139 | |
| 140 | glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &i); |
| 141 | assert(i == MyFB); |
| 142 | |
| 143 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, |
| 144 | GL_RENDERBUFFER_EXT, rb); |
| 145 | |
| 146 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height); |
| 147 | |
Brian Paul | 58a9573 | 2005-07-01 01:34:29 +0000 | [diff] [blame^] | 148 | { |
| 149 | GLint r, g, b, a; |
| 150 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 151 | GL_RENDERBUFFER_RED_SIZE_EXT, &r); |
| 152 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 153 | GL_RENDERBUFFER_GREEN_SIZE_EXT, &g); |
| 154 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 155 | GL_RENDERBUFFER_BLUE_SIZE_EXT, &b); |
| 156 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 157 | GL_RENDERBUFFER_ALPHA_SIZE_EXT, &a); |
| 158 | printf("renderbuffer RGBA sizes = %d %d %d %d\n", r, g, b, a); |
| 159 | } |
| 160 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 161 | CheckError(__LINE__); |
| 162 | |
| 163 | /* restore to default */ |
| 164 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | int |
| 169 | main( int argc, char *argv[] ) |
| 170 | { |
| 171 | glutInit( &argc, argv ); |
| 172 | glutInitWindowPosition( 0, 0 ); |
| 173 | glutInitWindowSize(Width, Height); |
| 174 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 175 | glutCreateWindow(argv[0]); |
| 176 | glutReshapeFunc( Reshape ); |
| 177 | glutKeyboardFunc( Key ); |
| 178 | glutDisplayFunc( Display ); |
| 179 | Init(); |
| 180 | glutMainLoop(); |
| 181 | return 0; |
| 182 | } |