在OpenGL ES(iPhone)中绘制到屏幕外渲染缓冲区 [英] Draw to offscreen renderbuffer in OpenGL ES (iPhone)

查看:143
本文介绍了在OpenGL ES(iPhone)中绘制到屏幕外渲染缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在iPhone上的OpenGL ES中创建一个屏幕外渲染缓冲区。我已经创建了这样的缓冲区:

I'm trying to create an offscreen render buffer in OpenGL ES on the iPhone. I've created the buffer like this:

        glGenFramebuffersOES(1, &offscreenFramebuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, offscreenFramebuffer);

    glGenRenderbuffersOES(1, &offscreenRenderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, offscreenRenderbuffer);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, offscreenRenderbuffer);

但我对如何渲染存储感到困惑。 Apple的文档说要使用EAGLContext renderBufferStorage:fromDrawable:方法,但这似乎只适用于一个渲染缓冲区(显示的主渲染缓冲区)。如果我使用普通的OpenGL函数glRenderBufferStorageOES,那么我似乎无法让它显示出来。这是代码:

But I'm confused on how to render the storage. Apple's documentation says to use the EAGLContext renderBufferStorage:fromDrawable: method, but this seems to only work for one render buffer (the main one being displayed). If I use the normal OpenGL function glRenderBufferStorageOES, then I can't seem to get it to display. Here's the code:

        // this is in the initialization section:
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGB8_OES, backingWidth, backingHeight);

    // and this is when I'm trying to draw to it and display it:
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, offscreenFramebuffer);
    GLfloat vc[] = {
        0.0f, 0.0f, 0.0f,
        10.0f, 10.0f, 10.0f,
        0.0f, 0.0f, 0.0f,
        -10.0f, -10.0f, -10.0f,         
    };

    glLoadIdentity();
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vc);
    glDrawArrays(GL_LINES, 0, 4);
    glDisableClientState(GL_VERTEX_ARRAY);

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, offscreenRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];

这样做,屏幕上不显示任何内容。但是,如果我将对offscreen ... Buffer的引用切换到使用renderBufferStorage方法创建的缓冲区,则它可以正常工作。

Doing it this way, nothing is displayed on the screen. However, if I switch out the references to "offscreen...Buffer" to the buffers that were created with the renderBufferStorage method, it works fine.

有任何建议吗? / p>

Any suggestions?

推荐答案

@david好主意..你需要做的是@prideout说的......创建一个纹理并渲染它。并且每次都使用四边形上的纹理。确保你只绘制一次纹理,就像你的情况一样持久。

@david good idea.. what you need to do is what @prideout said.. create a texture and render to it.. and use the texture on a quad every time. Make sure you draw to the texture only once, as in your case things are persistent.

- (void)setUpTextureBuffer 
{
 glGenFramebuffersOES(1, &texturebuffer); 
 glBindFramebufferOES(GL_FRAMEBUFFER_OES, texturebuffer);

 // create the texture
 glGenTextures(1, &canvastexture); 
 glBindTexture(GL_TEXTURE_2D, canvastexture); 
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,  512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 
 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, canvastexture, 0);

 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES); 
 if(status != GL_FRAMEBUFFER_COMPLETE_OES) { 
  NSLog(@"failed to make complete framebuffer object %x", status);
 }

 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

 glClearColor(1.0, 1.0, 1.0, 1.0);
 glViewport(0, 0, 512, 512);
 glClear(GL_COLOR_BUFFER_BIT);
}



//setTargetToTexture() function

 glBindFramebufferOES(GL_FRAMEBUFFER_OES, tbuffer);
 glBindTexture(GL_TEXTURE_2D, allbrushes);
 glViewport(0, 0, 512, 512);

//reset pointers after finishing drawing to textures

 glViewport(0, 0, BWIDTH, BHEIGHT);
 glVertexPointer(2, GL_FLOAT, 0, canvas);   //canvas vertices
 glTexCoordPointer(2, GL_FLOAT, 0, texels);
 glBindTexture(GL_TEXTURE_2D, boundtexture);   //bind to the texture which is the special render target 
 glBindFramebufferOES(GL_FRAMEBUFFER_OES, fbuffer); //back to normal framebuffer

这篇关于在OpenGL ES(iPhone)中绘制到屏幕外渲染缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆