Brian Paul | 28bc3cd | 2006-04-14 16:25:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Test GL_EXT_framebuffer_object |
| 3 | * |
| 4 | * Brian Paul |
| 5 | * 19 Mar 2006 |
| 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 | 6fda763 | 2007-03-25 10:25:29 -0600 | [diff] [blame] | 16 | static int Win = 0; |
Brian Paul | 28bc3cd | 2006-04-14 16:25:27 +0000 | [diff] [blame] | 17 | static int Width = 400, Height = 400; |
| 18 | static GLuint MyFB, ColorRb, DepthRb; |
| 19 | static GLboolean Animate = GL_TRUE; |
| 20 | static GLfloat Rotation = 0.0; |
| 21 | |
| 22 | |
| 23 | static void |
| 24 | CheckError(int line) |
| 25 | { |
| 26 | GLenum err = glGetError(); |
| 27 | if (err) { |
| 28 | printf("fbotest2: GL Error 0x%x at line %d\n", (int) err, line); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | |
| 33 | static void |
| 34 | Display( void ) |
| 35 | { |
| 36 | GLubyte *buffer = malloc(Width * Height * 4); |
| 37 | GLenum status; |
| 38 | |
| 39 | CheckError(__LINE__); |
| 40 | |
| 41 | /* draw to user framebuffer */ |
| 42 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 43 | glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT); |
| 44 | glReadBuffer(GL_COLOR_ATTACHMENT1_EXT); |
| 45 | |
| 46 | status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
| 47 | if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 48 | printf("fbotest2: Error: Framebuffer is incomplete!!!\n"); |
| 49 | } |
| 50 | |
| 51 | CheckError(__LINE__); |
| 52 | |
| 53 | glClearColor(0.5, 0.5, 1.0, 0.0); |
| 54 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 55 | |
| 56 | glEnable(GL_DEPTH_TEST); |
| 57 | glEnable(GL_LIGHTING); |
| 58 | glEnable(GL_LIGHT0); |
| 59 | |
| 60 | glPushMatrix(); |
| 61 | glRotatef(30.0, 1, 0, 0); |
| 62 | glRotatef(Rotation, 0, 1, 0); |
| 63 | glutSolidTeapot(2.0); |
| 64 | glPopMatrix(); |
| 65 | |
| 66 | /* read from user framebuffer */ |
| 67 | glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 68 | |
| 69 | /* draw to window */ |
| 70 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
Brian Paul | 4566880 | 2008-05-08 08:39:30 -0600 | [diff] [blame] | 71 | glDisable(GL_DEPTH_TEST); /* in case window has depth buffer */ |
Brian Paul | 28bc3cd | 2006-04-14 16:25:27 +0000 | [diff] [blame] | 72 | glWindowPos2iARB(0, 0); |
| 73 | glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 74 | |
| 75 | free(buffer); |
| 76 | glutSwapBuffers(); |
| 77 | CheckError(__LINE__); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static void |
| 82 | Reshape( int width, int height ) |
| 83 | { |
| 84 | float ar = (float) width / (float) height; |
| 85 | |
| 86 | glViewport( 0, 0, width, height ); |
| 87 | glMatrixMode( GL_PROJECTION ); |
| 88 | glLoadIdentity(); |
| 89 | glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 ); |
| 90 | |
| 91 | glMatrixMode( GL_MODELVIEW ); |
| 92 | glLoadIdentity(); |
| 93 | glTranslatef( 0.0, 0.0, -15.0 ); |
| 94 | |
| 95 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, ColorRb); |
| 96 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, width, height); |
| 97 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, DepthRb); |
| 98 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, |
| 99 | width, height); |
| 100 | |
| 101 | Width = width; |
| 102 | Height = height; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static void |
| 107 | CleanUp(void) |
| 108 | { |
| 109 | glDeleteFramebuffersEXT(1, &MyFB); |
| 110 | glDeleteRenderbuffersEXT(1, &ColorRb); |
| 111 | glDeleteRenderbuffersEXT(1, &DepthRb); |
| 112 | assert(!glIsFramebufferEXT(MyFB)); |
| 113 | assert(!glIsRenderbufferEXT(ColorRb)); |
| 114 | assert(!glIsRenderbufferEXT(DepthRb)); |
Brian | 6fda763 | 2007-03-25 10:25:29 -0600 | [diff] [blame] | 115 | glutDestroyWindow(Win); |
Brian Paul | 28bc3cd | 2006-04-14 16:25:27 +0000 | [diff] [blame] | 116 | exit(0); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | static void |
| 121 | Idle(void) |
| 122 | { |
| 123 | Rotation = glutGet(GLUT_ELAPSED_TIME) * 0.1; |
| 124 | glutPostRedisplay(); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static void |
| 129 | Key( unsigned char key, int x, int y ) |
| 130 | { |
| 131 | (void) x; |
| 132 | (void) y; |
| 133 | switch (key) { |
| 134 | case 'a': |
| 135 | Animate = !Animate; |
| 136 | if (Animate) |
| 137 | glutIdleFunc(Idle); |
| 138 | else |
| 139 | glutIdleFunc(NULL); |
| 140 | break; |
| 141 | case 27: |
| 142 | CleanUp(); |
| 143 | break; |
| 144 | } |
| 145 | glutPostRedisplay(); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | static void |
| 150 | Init( void ) |
| 151 | { |
| 152 | if (!glutExtensionSupported("GL_EXT_framebuffer_object")) { |
| 153 | printf("fbotest2: GL_EXT_framebuffer_object not found!\n"); |
| 154 | exit(0); |
| 155 | } |
| 156 | printf("fbotest2: GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 157 | |
| 158 | glGenFramebuffersEXT(1, &MyFB); |
| 159 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 160 | assert(glIsFramebufferEXT(MyFB)); |
| 161 | |
| 162 | /* set color buffer */ |
| 163 | glGenRenderbuffersEXT(1, &ColorRb); |
| 164 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, ColorRb); |
| 165 | assert(glIsRenderbufferEXT(ColorRb)); |
| 166 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, |
| 167 | GL_RENDERBUFFER_EXT, ColorRb); |
| 168 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height); |
| 169 | |
| 170 | /* setup depth buffer */ |
| 171 | glGenRenderbuffersEXT(1, &DepthRb); |
| 172 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, DepthRb); |
| 173 | assert(glIsRenderbufferEXT(DepthRb)); |
| 174 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, |
| 175 | GL_RENDERBUFFER_EXT, DepthRb); |
| 176 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, Width, Height); |
| 177 | |
| 178 | CheckError(__LINE__); |
| 179 | |
| 180 | /* restore to default */ |
| 181 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 182 | CheckError(__LINE__); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | int |
| 187 | main( int argc, char *argv[] ) |
| 188 | { |
| 189 | glutInit( &argc, argv ); |
| 190 | glutInitWindowPosition( 0, 0 ); |
| 191 | glutInitWindowSize(Width, Height); |
| 192 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
Brian | 6fda763 | 2007-03-25 10:25:29 -0600 | [diff] [blame] | 193 | Win = glutCreateWindow(argv[0]); |
Brian Paul | 28bc3cd | 2006-04-14 16:25:27 +0000 | [diff] [blame] | 194 | glutReshapeFunc( Reshape ); |
| 195 | glutKeyboardFunc( Key ); |
| 196 | glutDisplayFunc( Display ); |
| 197 | if (Animate) |
| 198 | glutIdleFunc(Idle); |
| 199 | Init(); |
| 200 | glutMainLoop(); |
| 201 | return 0; |
| 202 | } |