OpenGL ES渲染到纹理

OpenGL ES渲染到纹理

问题描述:

我一直无法找到简单的代码来将场景渲染到OpenGL ES中的纹理(特别是对于iPhone,如果这很重要)。我有兴趣了解以下内容:

I have been having trouble finding straightforward code to render a scene to a texture in OpenGL ES (specifically for the iPhone, if that matters). I am interested in knowing the following:


  1. 如何在OpenGL ES中为场景渲染场景?

  2. 您必须使用哪些参数来创建能够成为OpenGL ES中渲染目标的纹理?

  3. 是否有任何影响应用于此渲染纹理到其他原语?


这就是我的做法。

我定义了一个纹理变量(我使用Apple的 Texture2D 类,但如果你愿意,可以使用OpenGL纹理id)和帧缓冲区:

I define a texture variable (I use Apple's Texture2D class, but you can use an OpenGL texture id if you want), and a frame buffer:

Texture2d * texture;
GLuint textureFrameBuffer;

然后在某些时候,我创建纹理,帧缓冲区并附加渲染缓冲区。这只需要执行一次:

Then at some point, I create the texture, frame buffer and attach the renderbuffer. This you only need to do it once:

texture = [[Texture2D alloc] initWithData:0 
                             pixelFormat:kTexture2DPixelFormat_RGB888
                             pixelsWide:32
                             pixelsHigh:32
                             contentSize:CGSizeMake(width, height)];

// create framebuffer
glGenFramebuffersOES(1, &textureFrameBuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);

// attach renderbuffer
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture.name, 0);

// unbind frame buffer
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);

每次我想渲染纹理时,我都会这样做:

Every time I want to render to the texture, I do:

glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);

...
// GL commands
...

glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);

关于你的问题3,就是这样,你可以使用纹理,就好像它是任何其他纹理一样。

About your question 3, that's it, you can use the texture as if it is any other texture.