Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个OpenGL分屏代码可以工作?不明纹理?_Opengl_Textures - Fatal编程技术网

为什么这个OpenGL分屏代码可以工作?不明纹理?

为什么这个OpenGL分屏代码可以工作?不明纹理?,opengl,textures,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 glMatrix

所以我有一个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),代码仍然有效!怎么用?除了纹理坐标之外,我怎么能拥有一个纹理四边形而不在代码中提及纹理呢?

编辑:通过datenwolf应用校正,这里有很好的解释=>

根据纹理的名称(
\u framebufImg1
),
drawSceneContents
应实现屏幕外渲染,并将纹理作为颜色附件附加到

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

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

如果没有
drawSceneContents
详细信息,我不能说更多…

实际上,viewport只设置从NDC到窗口坐标的映射,但它不限制渲染。内容仍然可以显示在视口之外。此外,还必须使用剪刀测试,以便限制视口中的内容<代码>glEnable(德国劳埃德大学剪刀式测试);glScissor()@datenwolf:像在
stackexchange
游戏中那样试图获得积分肯定有点愚蠢,但事实上,我学到了很多这样做的东西。@datenwolf:值得注意的是,通过法线顶点管道的任何东西都会被剪裁,因此永远不会出现在视口之外。在某些情况下,可能会出现严重的视区外写入,但大多数都不推荐使用。剩下的几件事之一就是
glClear
@derhass:framebuffer复制操作,如
glBlitFramebuffer
,也会受到剪切测试的影响。@datenwolf:是的,您就在这里。可能还有一些。我不想说你最初的评论是错误的或无用的。我只是觉得有必要补充一点,那些“视窗外”的书写是一种特殊的操作,在许多情况下,人们可以不用剪刀测试就可以逃脱。YMMV。