132001f49Smrg/* Framebuffer object test */ 232001f49Smrg 332001f49Smrg 432001f49Smrg#include <GL/glew.h> 532001f49Smrg#include "glut_wrap.h" 632001f49Smrg#include <assert.h> 732001f49Smrg#include <stdio.h> 832001f49Smrg#include <stdlib.h> 932001f49Smrg 1032001f49Smrg/* For debug */ 1132001f49Smrg 1232001f49Smrg 1332001f49Smrgstatic int Win = 0; 1432001f49Smrgstatic int Width = 512, Height = 512; 1532001f49Smrg 1632001f49Smrgstatic GLenum TexTarget = GL_TEXTURE_2D; 1732001f49Smrgstatic int TexWidth = 512, TexHeight = 512; 1832001f49Smrg 1932001f49Smrgstatic GLuint MyFB; 2032001f49Smrgstatic GLuint TexObj; 2132001f49Smrgstatic GLboolean Anim = GL_FALSE; 2232001f49Smrgstatic GLfloat Rot = 0.0; 2332001f49Smrgstatic GLuint TextureLevel = 0; /* which texture level to render to */ 2432001f49Smrgstatic GLenum TexIntFormat = GL_RGB; /* either GL_RGB or GL_RGBA */ 2532001f49Smrg 2632001f49Smrg 2732001f49Smrgstatic void 2832001f49SmrgCheckError(int line) 2932001f49Smrg{ 3032001f49Smrg GLenum err = glGetError(); 3132001f49Smrg if (err) { 3232001f49Smrg printf("GL Error 0x%x at line %d\n", (int) err, line); 3332001f49Smrg } 3432001f49Smrg} 3532001f49Smrg 3632001f49Smrg 3732001f49Smrgstatic void 3832001f49SmrgIdle(void) 3932001f49Smrg{ 4032001f49Smrg Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1; 4132001f49Smrg glutPostRedisplay(); 4232001f49Smrg} 4332001f49Smrg 4432001f49Smrg 4532001f49Smrgstatic void 4632001f49SmrgRenderTexture(void) 4732001f49Smrg{ 4832001f49Smrg GLenum status; 4932001f49Smrg 5032001f49Smrg glMatrixMode(GL_PROJECTION); 5132001f49Smrg glLoadIdentity(); 5232001f49Smrg glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); 5332001f49Smrg glMatrixMode(GL_MODELVIEW); 5432001f49Smrg glLoadIdentity(); 5532001f49Smrg glTranslatef(0.0, 0.0, -15.0); 5632001f49Smrg 5732001f49Smrg if (1) { 5832001f49Smrg /* draw to texture image */ 5932001f49Smrg glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); 6032001f49Smrg 6132001f49Smrg status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); 6232001f49Smrg if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { 6332001f49Smrg printf("Framebuffer incomplete!!!\n"); 6432001f49Smrg } 6532001f49Smrg 6632001f49Smrg glViewport(0, 0, TexWidth, TexHeight); 6732001f49Smrg 6832001f49Smrg glClearColor(0.5, 0.5, 1.0, 0.0); 6932001f49Smrg glClear(GL_COLOR_BUFFER_BIT); 7032001f49Smrg 7132001f49Smrg CheckError(__LINE__); 7232001f49Smrg 7332001f49Smrg glBegin(GL_POLYGON); 7432001f49Smrg glColor3f(1, 0, 0); 7532001f49Smrg glVertex2f(-1, -1); 7632001f49Smrg glColor3f(0, 1, 0); 7732001f49Smrg glVertex2f(1, -1); 7832001f49Smrg glColor3f(0, 0, 1); 7932001f49Smrg glVertex2f(0, 1); 8032001f49Smrg glEnd(); 8132001f49Smrg 8232001f49Smrg /* Bind normal framebuffer */ 8332001f49Smrg glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 8432001f49Smrg } 8532001f49Smrg else { 8632001f49Smrg } 8732001f49Smrg 8832001f49Smrg CheckError(__LINE__); 8932001f49Smrg} 9032001f49Smrg 9132001f49Smrg 9232001f49Smrg 9332001f49Smrgstatic void 9432001f49SmrgDisplay(void) 9532001f49Smrg{ 9632001f49Smrg float ar = (float) Width / (float) Height; 9732001f49Smrg 9832001f49Smrg RenderTexture(); 9932001f49Smrg 10032001f49Smrg /* draw textured quad in the window */ 10132001f49Smrg glMatrixMode(GL_PROJECTION); 10232001f49Smrg glLoadIdentity(); 10332001f49Smrg glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0); 10432001f49Smrg glMatrixMode(GL_MODELVIEW); 10532001f49Smrg glLoadIdentity(); 10632001f49Smrg glTranslatef(0.0, 0.0, -7.0); 10732001f49Smrg 10832001f49Smrg glViewport(0, 0, Width, Height); 10932001f49Smrg 11032001f49Smrg glClearColor(0.25, 0.25, 0.25, 0); 11132001f49Smrg glClear(GL_COLOR_BUFFER_BIT); 11232001f49Smrg 11332001f49Smrg glPushMatrix(); 11432001f49Smrg glRotatef(Rot, 0, 1, 0); 11532001f49Smrg glEnable(TexTarget); 11632001f49Smrg glBindTexture(TexTarget, TexObj); 11732001f49Smrg 11832001f49Smrg { 11932001f49Smrg glBegin(GL_POLYGON); 12032001f49Smrg glColor3f(0.25, 0.25, 0.25); 12132001f49Smrg glTexCoord2f(0, 0); 12232001f49Smrg glVertex2f(-1, -1); 12332001f49Smrg glTexCoord2f(1, 0); 12432001f49Smrg glVertex2f(1, -1); 12532001f49Smrg glColor3f(1.0, 1.0, 1.0); 12632001f49Smrg glTexCoord2f(1, 1); 12732001f49Smrg glVertex2f(1, 1); 12832001f49Smrg glTexCoord2f(0, 1); 12932001f49Smrg glVertex2f(-1, 1); 13032001f49Smrg glEnd(); 13132001f49Smrg } 13232001f49Smrg 13332001f49Smrg glPopMatrix(); 13432001f49Smrg glDisable(TexTarget); 13532001f49Smrg 13632001f49Smrg glutSwapBuffers(); 13732001f49Smrg CheckError(__LINE__); 13832001f49Smrg} 13932001f49Smrg 14032001f49Smrg 14132001f49Smrgstatic void 14232001f49SmrgReshape(int width, int height) 14332001f49Smrg{ 14432001f49Smrg glViewport(0, 0, width, height); 14532001f49Smrg Width = width; 14632001f49Smrg Height = height; 14732001f49Smrg} 14832001f49Smrg 14932001f49Smrg 15032001f49Smrgstatic void 15132001f49SmrgCleanUp(void) 15232001f49Smrg{ 15332001f49Smrg glDeleteFramebuffersEXT(1, &MyFB); 15432001f49Smrg 15532001f49Smrg glDeleteTextures(1, &TexObj); 15632001f49Smrg 15732001f49Smrg glutDestroyWindow(Win); 15832001f49Smrg 15932001f49Smrg exit(0); 16032001f49Smrg} 16132001f49Smrg 16232001f49Smrg 16332001f49Smrgstatic void 16432001f49SmrgKey(unsigned char key, int x, int y) 16532001f49Smrg{ 16632001f49Smrg (void) x; 16732001f49Smrg (void) y; 16832001f49Smrg switch (key) { 16932001f49Smrg case 'a': 17032001f49Smrg Anim = !Anim; 17132001f49Smrg if (Anim) 17232001f49Smrg glutIdleFunc(Idle); 17332001f49Smrg else 17432001f49Smrg glutIdleFunc(NULL); 17532001f49Smrg break; 17632001f49Smrg case 's': 17732001f49Smrg Rot += 2.0; 17832001f49Smrg break; 17932001f49Smrg case 27: 18032001f49Smrg CleanUp(); 18132001f49Smrg break; 18232001f49Smrg } 18332001f49Smrg glutPostRedisplay(); 18432001f49Smrg} 18532001f49Smrg 18632001f49Smrg 18732001f49Smrgstatic void 18832001f49SmrgInit(int argc, char *argv[]) 18932001f49Smrg{ 19032001f49Smrg if (!glutExtensionSupported("GL_EXT_framebuffer_object")) { 19132001f49Smrg printf("GL_EXT_framebuffer_object not found!\n"); 19232001f49Smrg exit(0); 19332001f49Smrg } 19432001f49Smrg 19532001f49Smrg printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 19632001f49Smrg 19732001f49Smrg 19832001f49Smrg /* Make texture object/image */ 19932001f49Smrg glGenTextures(1, &TexObj); 20032001f49Smrg glBindTexture(TexTarget, TexObj); 20132001f49Smrg glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 20232001f49Smrg glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 20332001f49Smrg glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel); 20432001f49Smrg glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel); 20532001f49Smrg 20632001f49Smrg glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0, 20732001f49Smrg GL_RGBA, GL_UNSIGNED_BYTE, NULL); 20832001f49Smrg 20932001f49Smrg 21032001f49Smrg glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 21132001f49Smrg 21232001f49Smrg 21332001f49Smrg 21432001f49Smrg 21532001f49Smrg /* gen framebuffer id, delete it, do some assertions, just for testing */ 21632001f49Smrg glGenFramebuffersEXT(1, &MyFB); 21732001f49Smrg glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); 21832001f49Smrg assert(glIsFramebufferEXT(MyFB)); 21932001f49Smrg 22032001f49Smrg 22132001f49Smrg CheckError(__LINE__); 22232001f49Smrg 22332001f49Smrg /* Render color to texture */ 22432001f49Smrg glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, 22532001f49Smrg TexTarget, TexObj, TextureLevel); 22632001f49Smrg 22732001f49Smrg 22832001f49Smrg 22932001f49Smrg CheckError(__LINE__); 23032001f49Smrg 23132001f49Smrg /* bind regular framebuffer */ 23232001f49Smrg glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 23332001f49Smrg 23432001f49Smrg 23532001f49Smrg} 23632001f49Smrg 23732001f49Smrg 23832001f49Smrgint 23932001f49Smrgmain(int argc, char *argv[]) 24032001f49Smrg{ 24132001f49Smrg glutInit(&argc, argv); 24232001f49Smrg glutInitWindowPosition(0, 0); 24332001f49Smrg glutInitWindowSize(Width, Height); 24432001f49Smrg glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 24532001f49Smrg Win = glutCreateWindow(argv[0]); 24632001f49Smrg glewInit(); 24732001f49Smrg glutReshapeFunc(Reshape); 24832001f49Smrg glutKeyboardFunc(Key); 24932001f49Smrg glutDisplayFunc(Display); 25032001f49Smrg if (Anim) 25132001f49Smrg glutIdleFunc(Idle); 25232001f49Smrg Init(argc, argv); 25332001f49Smrg glutMainLoop(); 25432001f49Smrg return 0; 25532001f49Smrg} 256