为什么这个OpenGL分屏代码有效?身份不明的纹理?

时间:2014-04-17 05:31:47

标签: opengl textures

所以我有一个分屏的OpenGL程序。进行研究后,我的代码如下所示:

   glEnable(GL_DEPTH_TEST);
   glClear(GL_COLOR_BUFFER_BIT);

   // 1
   // ----- Camera -----
   glViewport(0, 0, _windowW/2, _windowH);
   //  Tell OpenGL we want to manipulate the projection matrix
   glMatrixMode(GL_PROJECTION);
   //  Undo previous transformations
   glLoadIdentity();
   //  Perspective transformation
   gluPerspective(_fov,_asp,_worldSize/_trolly,_trolly*_worldSize);
   // Move main camera
   _cameraLook[0] = _cameraLoc[0] + Sin(_th1)*Cos(_ph1)*_lookDistance;
   _cameraLook[1] = _cameraLoc[1] +           Sin(_ph1)*_lookDistance;
   _cameraLook[2] = _cameraLoc[2] + Cos(_th1)*Cos(_ph1)*_lookDistance;
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   gluLookAt(_cameraLoc[0], _cameraLoc[1], _cameraLoc[2], _cameraLook[0], _cameraLook[1], _cameraLook[2], 0, 1, 0);

   // ----- Drawing of scene -----
   drawSceneContents();

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glClear(GL_DEPTH_BUFFER_BIT);

   //glBindTexture(GL_TEXTURE_2D,_framebufImg1);
   glBegin(GL_QUADS);
   glTexCoord2f(0.25, 0.0); glVertex2f(-1.0, -1.0);
   glTexCoord2f(0.25, 1.0); glVertex2f(-1.0, +1.0);
   glTexCoord2f(0.75, 1.0); glVertex2f(+0.0, +1.0);
   glTexCoord2f(0.75, 0.0); glVertex2f(+0.0, -1.0);
   glEnd();

   // 2
   // ----- Camera -----
   glViewport(_windowW/2, 0, _windowW/2, _windowH);
   //  Tell OpenGL we want to manipulate the projection matrix
   glMatrixMode(GL_PROJECTION);
   //  Undo previous transformations
   glLoadIdentity();
   //  Perspective transformation
   gluPerspective(_fov,_asp,_worldSize/_trolly,_trolly*_worldSize);

   // Move main camera
   _cameraLook[0] = _cameraLoc[4] + Sin(_th2)*Cos(_ph2)*_lookDistance;
   _cameraLook[1] = _cameraLoc[5] +           Sin(_ph2)*_lookDistance;
   _cameraLook[2] = _cameraLoc[6] + Cos(_th2)*Cos(_ph2)*_lookDistance;
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   gluLookAt(_cameraLoc[4], _cameraLoc[5], _cameraLoc[6], _cameraLook[0], _cameraLook[1], _cameraLook[2], 0, 1, 0);

   // ----- Drawing of scene -----
   drawSceneContents();

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glClear(GL_DEPTH_BUFFER_BIT);

   //glBindTexture(GL_TEXTURE_2D,_framebufImg1);
   glBegin(GL_QUADS);
   glTexCoord2f(0.25, 0.0); glVertex2f(-0.0, -1.0);
   glTexCoord2f(0.25, 1.0); glVertex2f(-0.0, +1.0);
   glTexCoord2f(0.75, 1.0); glVertex2f(+1.0, +1.0);
   glTexCoord2f(0.75, 0.0); glVertex2f(+1.0, -1.0);
   glEnd();

然而,我不明白为什么它正在工作,我必须遗漏一些OpenGL的基本功能。 我不理解的部分是在每个“屏幕”的末尾,我绘制了纹理四边形(最后5行代码)。但这种纹理来自哪里?为什么它恰好是我为该屏幕绘制的场景的纹理?我从来没有做过glBindTexture或glEnable(GL_TEXTURE_2D)的任何事情,实际上在所有这些代码之前我可以放一个glDisable(GL_TEXTURE_2D)并且代码仍然有效!怎么样?除了纹理坐标外,我怎么能在代码中没有提到纹理?#/ p>

1 个答案:

答案 0 :(得分:1)

修改:通过 datenwolf 进行修正,这里有很好的解释=> 10. The Viewport Does Not Clip or Scissor

根据纹理的名称(_framebufImg1),drawSceneContents应实现对FBO的屏幕外渲染,该纹理应将纹理附加为颜色附件。

glViewport将投影坐标映射到屏幕宽度的一半,这样您就可以在2个不同的位置绘制具有相同顶点的纹理四边形

glViewport(0, 0, _windowW/2, _windowH);
//first pass
glViewport(_windowW/2, 0, _windowW/2, _windowH);
//second pass

没有drawSceneContents详细信息,我无法再说明了......