FBO 深度测试
opengl fbo 测试程序,前几天别人问,帮写的
头文件
1 const int SCREEN_WIDTH = 400; 2 const int SCREEN_HEIGHT = 400; 3 const float CAMERA_DISTANCE = 6.0f; 4 const int TEXT_WIDTH = 8; 5 const int TEXT_HEIGHT = 8; 6 const int TEXTURE_WIDTH = 400; // NOTE: texture size cannot be larger than 7 const int TEXTURE_HEIGHT = 400; // the rendering window size in non-FBO mode 8 const int DATA_SIZE = TEXTURE_WIDTH * TEXTURE_HEIGHT * sizeof(float); 9 class GAMEINFO 10 { 11 public: 12 GAMEINFO(){} 13 ~GAMEINFO(){} 14 void init_game(); 15 void init_light(); 16 void Draw(); 17 void Clear(); 18 private: 19 void initGL(); 20 void initFBO(); 21 22 23 private: 24 CVector3 m_eye,m_look; 25 26 27 28 29 30 glInfo OglInfo; 31 32 private: 33 34 GLuint fboId; // ID of FBO 35 GLuint textureId; // ID of texture 36 GLuint rboId; // ID of Renderbuffer object 37 GLuint pboId; 38 };
实现
1 #include"GAME.h" 2 #include"FreeType.h" 3 #include"FreeTypeReal.h" 4 void DrawBox(); 5 void GAMEINFO::init_game() 6 { 7 8 LOG::Instance()->OutputError("OpenGL 1.3 required"); 9 initGL(); 10 11 init_light(); 12 TimerInit(); 13 14 15 16 17 /*vbo_supported(); 18 FBO_supported(); */ 19 OglInfo.getInfo(); 20 //OglInfo.printSelf(); 21 22 23 24 25 if(GLEE_ARB_vertex_buffer_object) 26 { 27 printf("Supput VBO! \n"); 28 29 } 30 if(GLEE_ARB_pixel_buffer_object) 31 { 32 printf("Supput PBO! \n"); 33 } 34 if(GLEE_ARB_framebuffer_object) 35 { 36 printf("Supput FBO! \n"); 37 38 39 } 40 41 } 42 43 void GAMEINFO::initGL() 44 { 45 int height=WINDOWSWIDTH; 46 if(height==0) 47 height=1; 48 49 glViewport(0, 0, WINDOWSWIDTH, WINDOWSHEIGHT); 50 51 //Set up projection matrix 52 glMatrixMode(GL_PROJECTION); 53 glLoadIdentity(); 54 gluPerspective( 45.0f, (GLfloat)WINDOWSWIDTH/(GLfloat)WINDOWSHEIGHT,1.0f, 3000.0f); 55 56 // glOrtho(0, 500, 0, 500, -1, 100); 57 //Load identity modelview 58 glMatrixMode(GL_MODELVIEW); 59 glLoadIdentity(); 60 61 //Shading states 62 glShadeModel(GL_SMOOTH); 63 glClearColor(0.0, 0.0f, 0.0f, 1.0f); 64 glColor4f(1.0f, 1.0f, 1.0f, 1.0f); 65 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 66 glDepthFunc(GL_LEQUAL); 67 68 69 glClearDepth(1.0f); 70 71 glEnable(GL_DEPTH_TEST); 72 glDepthFunc(GL_LEQUAL); 73 74 //our_font.init("C:\\WINDOWS\\Fonts\\simhei.TTF",16); 75 76 init_light(); 77 78 initFBO(); 79 80 } 81 82 83 void GAMEINFO::initFBO() 84 { 85 86 glGenTextures(1, &textureId); 87 glBindTexture(GL_TEXTURE_2D, textureId); 88 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 89 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 90 //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 91 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 92 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 93 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 94 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 95 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // automatic mipmap generation included in OpenGL v1.4 96 //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); 97 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); 98 //glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0); 99 glBindTexture(GL_TEXTURE_2D, 0); 100 101 102 103 glGenFramebuffers(1, &fboId); 104 glBindFramebuffer(GL_FRAMEBUFFER, fboId); 105 106 // create a renderbuffer object to store depth info 107 // NOTE: A depth renderable image should be attached the FBO for depth test. 108 // If we don't attach a depth renderable image to the FBO, then 109 // the rendering output will be corrupted because of missing depth test. 110 // If you also need stencil test for your rendering, then you must 111 // attach additional image to the stencil attachement point, too. 112 glGenRenderbuffers(1, &rboId); 113 glBindRenderbuffer(GL_RENDERBUFFER, rboId); 114 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, TEXTURE_WIDTH, TEXTURE_HEIGHT); 115 glBindRenderbuffer(GL_RENDERBUFFER, 0); 116 117 // attach a texture to FBO depth attachement point 118 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textureId, 0); 119 120 // attach a renderbuffer to depth attachment point 121 //glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER_EXT, rboId); 122 123 //@@ disable color buffer if you don't attach any color buffer image, 124 //@@ for example, rendering depth buffer only to a texture. 125 //@@ Otherwise, glCheckFramebufferStatusEXT will not be complete. 126 glDrawBuffer(GL_NONE); 127 glReadBuffer(GL_NONE); 128 129 // check FBO status 130 131 glBindFramebuffer(GL_FRAMEBUFFER, 0); 132 133 134 glGenBuffersARB(1, &pboId); 135 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboId); 136 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, DATA_SIZE, 0, GL_STREAM_READ_ARB); 137 138 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); 139 140 } 141 void GAMEINFO::init_light() 142 { 143 static GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f }; // 环境光参数 144 static GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }; // 漫射光参数 145 static GLfloat LightPosition[]= { 0.0f, 100.0f, 2.0f, 1.0f }; // 光源位置 146 147 148 149 glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // 设置环境光 150 glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // 设置漫射光 151 glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // 设置光源位置 152 153 glEnable(GL_LIGHT1); // 启用一号光源 154 glEnable(GL_LIGHTING); 155 156 } 157 158 159 void GAMEINFO::Clear() 160 { 161 162 163 } 164 165 void GAMEINFO::Draw() 166 { 167 static float xrot=0,yrot=0,zrot=0,angle=0.0; 168 169 static float abc; 170 171 /*glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 172 173 174 175 ////glOrtho(0,WINDOWSWIDTH,0,WINDOWSHEIGHT,1,1000); 176 177 glMatrixMode(GL_MODELVIEW); 178 glLoadIdentity(); 179 180 glTranslatef(0,0,-20); 181 //glRotatef(xrot++,1.0f,0.0f,0.0f); 182 //glRotatef(yrot++,0.0f,1.0f,0.0f); 183 //glRotatef(zrot++,0.0f,0.0f,1.0f); 184 // 185 glEnable(GL_TEXTURE_2D); 186 glBindTexture(GL_TEXTURE_2D,0); 187 glEnable(GL_LIGHTING); 188 glColor3f(1.0f,1.0f,1.0f); 189 glPushMatrix();*/ 190 /* 191 glScalef(0.5f,0.5f,0.5f); 192 glBegin(GL_QUADS); 193 // Front Face 194 195 glNormal3f( 0.0f, 0.0f, 1.0f); // 法线指向观察者 196 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 197 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 198 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 199 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 200 // 后侧面 201 202 glNormal3f( 0.0f, 0.0f,-1.0f); // 法线背向观察者 203 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 204 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 205 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 206 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 207 // 顶面 208 209 glNormal3f( 0.0f, 1.0f, 0.0f); // 法线向上 210 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 211 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 212 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 213 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 214 // 底面 215 216 glNormal3f( 0.0f,-1.0f, 0.0f); // 法线朝下 217 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 218 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 219 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 220 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 221 // 右侧面 222 glNormal3f( 1.0f, 0.0f, 0.0f); // 法线朝右 223 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 224 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 225 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 226 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 227 // 左侧面 228 glNormal3f(-1.0f, 0.0f, 0.0f); // 法线朝左 229 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 230 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 231 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 232 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 233 glEnd(); 234 235 236 */ 237 /* 238 glPopMatrix(); 239 glEnable(GL_DEPTH_TEST); 240 glEnable(GL_TEXTURE_2D); 241 242 glDisable(GL_LIGHTING); 243 244 abc+=0.1f; 245 //glScalef(0.5f,0.5f,0.5f); 246 247 glEnable(GL_MULTISAMPLE_ARB); 248 glColor3f(1.0,0.0f,0.0); 249 250 mFont.print(100,10,"C_Dragon fps:%d ",nfps); 251 252 253 fpsCount();*/ 254 255 glViewport(0, 0, 400, 400); 256 glMatrixMode(GL_PROJECTION); 257 glLoadIdentity(); 258 gluPerspective(60.0f, (float)(400)/400, 1.0f, 100.0f); 259 260 // with FBO 261 // render directly to a texture 262 263 // set the rendering destination to FBO 264 glBindFramebuffer(GL_FRAMEBUFFER, fboId); 265 266 // clear buffer 267 glClearColor(1, 1, 1, 1); 268 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 269 270 // draw a rotating teapot 271 glTranslatef(0, 0, -CAMERA_DISTANCE); 272 273 glRotatef(angle*0.5f, 1, 0, 0); 274 glRotatef(angle, 0, 1, 0); 275 glRotatef(angle*0.7f, 0, 0, 1); 276 glEnable(GL_TEXTURE_2D); 277 glBindTexture(GL_TEXTURE_2D, 0); 278 // draw a cube with the dynamic texture 279 // glScalef(10.0f,10.0f,10.0f); 280 DrawBox(); 281 glBindTexture(GL_TEXTURE_2D, 0); 282 283 // back to normal window-system-provided framebuffer 284 glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind 285 286 287 glBindTexture(GL_TEXTURE_2D, textureId); 288 glGenerateMipmap(GL_TEXTURE_2D); 289 glBindTexture(GL_TEXTURE_2D, 0); 290 glClearColor(0, 0, 0, 1); 291 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 292 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 293 glColor3f(1.0f,0.0f,0.0); 294 for(int ii=0 ;ii<2 ;ii++) 295 { 296 if(ii==0) 297 { 298 glViewport(0, 0, 400, 400); 299 glMatrixMode(GL_PROJECTION); 300 glLoadIdentity(); 301 gluPerspective(60.0f, (float)(400)/400, 1.0f, 100.0f); 302 303 } 304 305 if(ii==1) 306 { 307 glViewport(400, 0, 800, 400); 308 glMatrixMode(GL_PROJECTION); 309 glLoadIdentity(); 310 glOrtho(0, 400, 0, 400, -1, 100); 311 //gluPerspective(60.0f, (float)(400)/400, 1.0f, 100.0f); 312 313 314 } 315 316 glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix 317 glLoadIdentity (); 318 319 glClear (GL_DEPTH_BUFFER_BIT); 320 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 321 if(ii==0) 322 { 323 glTranslatef(0, 0, -CAMERA_DISTANCE); 324 325 glRotatef(angle*0.5f, 1, 0, 0); 326 glRotatef(angle, 0, 1, 0); 327 glRotatef(angle*0.7f, 0, 0, 1); 328 glEnable(GL_TEXTURE_2D); 329 glBindTexture(GL_TEXTURE_2D, 0); 330 // draw a cube with the dynamic texture 331 // glScalef(10.0f,10.0f,10.0f); 332 DrawBox(); 333 glBindTexture(GL_TEXTURE_2D, 0); 334 335 336 337 338 } 339 340 if(ii==1) 341 { 342 343 344 glEnable(GL_TEXTURE_2D); 345 glBindTexture(GL_TEXTURE_2D,textureId); 346 // draw a cube with the dynamic texture 347 glBegin(GL_QUADS); 348 glNormal3f( 0.0f, 0.0f, 1.0f); // 法线指向观察者 349 glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.0f, 0.0f, 0.0f); 350 glTexCoord2f(1.0f, 0.0f); glVertex3f( 200.0f, 0.0f, 0.0f); 351 glTexCoord2f(1.0f, 1.0f); glVertex3f( 200.0f, 400.0f, 0.0f); 352 glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, 400.0f, 0.0f); 353 glEnd(); 354 glBindTexture(GL_TEXTURE_2D, 0); 355 356 357 358 359 } 360 361 } 362 363 angle+=5.0f; 364 365 } 366 void DrawBox() 367 { 368 369 glBegin(GL_QUADS); 370 // Front Face 371 glNormal3f( 0.0f, 0.0f, 1.0f); // 法线指向观察者 372 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 373 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 374 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 375 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 376 // 后侧面 377 glNormal3f( 0.0f, 0.0f,-1.0f); // 法线背向观察者 378 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 379 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 380 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 381 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 382 // 顶面 383 glNormal3f( 0.0f, 1.0f, 0.0f); // 法线向上 384 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 385 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 386 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 387 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 388 // 底面 389 glNormal3f( 0.0f,-1.0f, 0.0f); // 法线朝下 390 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 391 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 392 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 393 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 394 // 右侧面 395 glNormal3f( 1.0f, 0.0f, 0.0f); // 法线朝右 396 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 397 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 398 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 399 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 400 // 左侧面 401 glNormal3f(-1.0f, 0.0f, 0.0f); // 法线朝左 402 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 403 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 404 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 405 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 406 glEnd(); 407 408 }
效果图