多个Viewport OpenGL ES

时间:2012-07-05 17:32:44

标签: ios opengl-es viewport

我目前正在尝试制作一个使用多个opengl es视口的ios应用程序(即分屏)。以下是我的表现:

// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
    glGenFramebuffersOES(1, &defaultFramebuffer);
    glGenRenderbuffersOES(1, &colorRenderbuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);

// Replace the implementation of this method to do your own custom drawing
const GLfloat squareVertices[] = {
  -0.5f, -0.5f,
  0.5f,  -0.5f,
  -0.5f,  0.5f,
  0.5f,   0.5f,
};
const GLubyte squareColors[] = {
  255, 255,   0, 255,
  0,   255, 255, 255,
  0,     0,   0,   0,
  255,   0, 255, 255,
 };

[EAGLContext setCurrentContext:context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
glViewport(0, backingHeight/2, backingWidth/2, backingHeight/2);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glRotatef(3.0f, 0.0f, 0.0f, 1.0f);

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
// equivalent to glutswapBuffers()
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

/*********************SECOND VIEW*******/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
glViewport(backingWidth/2, 0, backingWidth/2, backingHeight/2);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glRotatef(3.0f, 0.0f, 0.0f, 1.0f);

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
/*********************END***************/

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
  1. 在模拟器上,仅显示第二个视口(在本例中为屏幕右下角的旋转立方体)。而在设备上它们都出现但闪烁......(左上角和下边缘)
  2. glRotatef被应用两次,但我只想要或者任何其他转换应用一次。
  3. 我想要的只是两次展示同一场景。基本上我试图模拟有2个摄像头看同样的事情,并在屏幕上显示。显然,我做错了,拥有2个独立的帧缓冲区和渲染缓冲区会更好吗?还是有更简单的方法来实现这个目标?

1 个答案:

答案 0 :(得分:2)

问题在于这一行:

/*********************SECOND VIEW*******/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_FRAMEBUFFER);

glClear不受视口影响,如果要部分清除,则需要使用剪刀测试或模板缓冲区。

请参阅glClear的文档。