0
votes

So I have an OpenGL program that does split screen. After doing research, my code looks like the following:

   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();

However, I don't understand why it is working and I must be missing something fundamental that OpenGL does. The part I don't understand is at the end of each 'screen', where I draw a textured quad (Last 5 lines of code). But where does this texture come from? Why does it happen to be a texture of the scene I am drawing for that screen? I never did glBindTexture or glEnable(GL_TEXTURE_2D) anything, in fact before all of this code I can put a glDisable(GL_TEXTURE_2D) and the code still works! How? How can I have a textured quad without ever mentioning texture in the code, aside from the texture coordinates?

1

1 Answers

1
votes

Edit: apply correction by datenwolf, well explained here => 10. The Viewport Does Not Clip or Scissor

According to the name of your texture (_framebufImg1), drawSceneContents should implement an offscreen rendering to a FBO which should have the texture attached as color attachment.

glViewport map the projections coodinates to half the width of the screen, which allow you to draw the textured quad with the same vertices at 2 different places

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

without drawSceneContents details, I can't say more...