如何在OpenGL对象的纹理中显示OpenGL渲染场景?

时间:2014-02-25 00:32:58

标签: opengl

我假设我必须渲染屏幕,然后将其复制到我已应用于主OpenGL场景的纹理中。是这样的吗?

如果是这样,那么使用常规OpenGL函数调用(IIRC称为合成)是怎么回事?

我在OS / X下这样做,但我希望它是可移植的,例如到OpenGL / ES。

2 个答案:

答案 0 :(得分:3)

您需要使用FBO(Framebuffer对象)。帧缓冲对象使用当前的OpenGL上下文,但绘制到附加到它的纹理或其他类型的缓冲区而不是绘制到屏幕上。 Apple有一些非常好的documentation on getting it working here

一旦你绘制了一个纹理,就可以将它从FBO中分离出来并像其他纹理一样使用它,比如将它应用到你正在绘制到屏幕上的对象上。

答案 1 :(得分:0)

一般来说,下面是你如何使用FBO。这不是合成。

//Creation of FBO
glGenFramebuffers(NUM_FBO, fboId);
//fbo texture
glGenTextures(NUM_FBO, fboTextureId);
//Regular first texture
glGenTextures(1, &regularTextureId);
//Bind offscreen texture
GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, fboTextureId[i]));
GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, inTextureWidth, inTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, fboId[i]));
GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fboTextureId[i], 0));
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
    goto err;
}
//Bind regular texture
GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, regularTextureId));
add_texture(inTextureWidth, inTextureHeight, textureData, inPixelFormat);
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
//Draw with regular draw calls to FBO
GL_CHECK(_test17(1));
//Now get back display framebuffer and unbind the FBO
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0));
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
    goto err;
}
//bind to texture
GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, fboTextureId[i]));
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
//draw other 3D objects with regular vertex/texure coordinates, use the above texture and then call swapbuffers
draw_regular();

可以在多个在线教程中找到完整的工作版本,包括我的https://github.com/prabindh/sgxperf/blob/master/sgxperf_test17