//draw a frame void RenderFrame() { //Draw to pbuffer pbuffer.MakeCurrent(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //reset modelview matrix gluLookAt( 0.0f, 0.0f, 4.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); //Draw scene if(drawTextured) { glBindTexture(GL_TEXTURE_2D, decalTexture); glEnable(GL_TEXTURE_2D); glPushMatrix(); glRotatef(timer.GetTime()/20, 0.0f, 1.0f, 0.0f); glutSolidTeapot(0.8f); glPopMatrix(); glDisable(GL_TEXTURE_2D); } else { glPushMatrix(); glRotatef(timer.GetTime()/20, 0.0f, 1.0f, 0.0f); glRotatef(55.0f, 1.0f, 0.0f, 0.0f); glutWireTorus(0.3f, 1.0f, 12, 24); glPopMatrix(); glPushMatrix(); glRotatef(timer.GetTime()/20, 0.0f, 1.0f, 0.0f); glRotatef(-55.0f, 1.0f, 0.0f, 0.0f); glutWireTorus(0.3f, 1.0f, 12, 24); glPopMatrix(); } //Draw to window window.MakeCurrent(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); camera.SetupViewMatrix(); glLoadMatrixf(camera.viewMatrix); glBindTexture(GL_TEXTURE_2D, pbufferTexture); //use the pbuffer as the texture wglBindTexImageARB(pbuffer.hBuffer, WGL_FRONT_LEFT_ARB); //Draw simple rectangle glBegin(GL_TRIANGLE_STRIP); { glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); } glEnd(); //release the pbuffer for further rendering wglReleaseTexImageARB(pbuffer.hBuffer, WGL_FRONT_LEFT_ARB); fpsCounter.Update(); //update frames per second counter glColor4f(1.0f, 1.0f, 0.0f, 1.0f); window.StartTextMode(); window.Print(0, 28, "FPS: %.2f", fpsCounter.GetFps()); //print the fps glColor4f(1.0f, 0.0f, 0.0f, 1.0f); window.Print(0, 48, "%dx Anisotropy", currentAnisotropy); glColor4f(0.0f, 1.0f, 0.0f, 1.0f); window.Print(0, 68, "%s", useMipmapFilter ? "LINEAR_MIPMAP_LINEAR filtering" : "LINEAR filtering"); window.EndTextMode(); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); if(window.isKeyPressed(VK_F1)) { window.SaveScreenshot(); window.SetKeyReleased(VK_F1); } window.SwapBuffers(); //swap buffers //check for any opengl errors window.CheckGLError(); //quit if necessary if(window.isKeyPressed(VK_ESCAPE)) PostQuitMessage(0); }
//Set up variables bool DemoInit() { if(!window.Init("Render To Texture", 640, 480, 32, 24, 8, WINDOWED_SCREEN)) return 0; //quit if not created camera.Init(VECTOR3D(0.0f, 0.0f, -2.5f), 2.0f, 100.0f); //Set up extensions if( !SetUpWGL_ARB_extensions_string()) return false; SetUpEXT_texture_filter_anisotropic(); SetUpSGIS_generate_mipmap(); //Get the WGL extensions string const char * wglExtensions; wglExtensions=wglGetExtensionsStringARB(window.hDC); //Set up wgl extensions if( !SetUpWGL_ARB_pbuffer(wglExtensions) || !SetUpWGL_ARB_pixel_format(wglExtensions) || !SetUpWGL_ARB_render_texture(wglExtensions)) return false; //Init the pbuffer int pbufferExtraIAttribs[]={WGL_BIND_TO_TEXTURE_RGBA_ARB, true, 0}; int pbufferFlags[]={WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB, WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, //request mipmap space if mipmaps are to be used SGIS_generate_mipmap_supported ? WGL_MIPMAP_TEXTURE_ARB : 0, SGIS_generate_mipmap_supported ? true : 0, 0}; if(!pbuffer.Init(pbufferSize, pbufferSize, 32, 24, 8, 1, pbufferExtraIAttribs, pbufferFlags)) return false; //Create the texture object to relate to the pbuffer glGenTextures(1, &pbufferTexture); glBindTexture(GL_TEXTURE_2D, pbufferTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //Use generated mipmaps if supported if(SGIS_generate_mipmap_supported) { glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, true); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glHint(GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST); useMipmapFilter=true; } //Use maximum anisotropy if supported if(EXT_texture_filter_anisotropic_supported) { glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy); currentAnisotropy=maxAnisotropy; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, currentAnisotropy); } //Load the decal texture //Note: This MUST be done when the pbuffer is the current context pbuffer.MakeCurrent(); IMAGE decalImage; decalImage.Load("decal.bmp"); glGenTextures(1, &decalTexture); glBindTexture(GL_TEXTURE_2D, decalTexture); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, decalImage.width, decalImage.height, 0, decalImage.format, GL_UNSIGNED_BYTE, decalImage.data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //reset timer for start timer.Reset(); return true; }
//Set up openGL bool GLInit() { //Set up for pbuffer pbuffer.MakeCurrent(); //set viewport glViewport(0, 0, pbufferSize, pbufferSize); //set up projection matrix glMatrixMode(GL_PROJECTION); //select projection matrix glLoadIdentity(); //reset gluPerspective(45.0f, 1.0f, 1.0f, 100.0f); //load identity modelview glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //other states //shading glShadeModel(GL_SMOOTH); glClearColor( pbufferBackgroundColor.r, pbufferBackgroundColor.g, pbufferBackgroundColor.b, pbufferBackgroundColor.a); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); //depth glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); //hints glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Set up for window window.MakeCurrent(); //set viewport int height; if (window.height==0) height=1; else height=window.height; glViewport(0, 0, window.width, height); //reset viewport //set up projection matrix glMatrixMode(GL_PROJECTION); //select projection matrix glLoadIdentity(); //reset gluPerspective(45.0f, (GLfloat)window.width/(GLfloat)height, 1.0f, 100.0f); //load identity modelview glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //other states //shading glShadeModel(GL_SMOOTH); glClearColor( windowBackgroundColor.r, windowBackgroundColor.g, windowBackgroundColor.b, windowBackgroundColor.a); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); //depth glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); //hints glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_TEXTURE_2D); return true; }