2012-06-01 83 views
2

我試圖使用屏幕之外這篇文章 http://www.idevgames.com/forums/thread-1785-post-54500.html#pid54500的iOS渲染到紋理:爲什麼是清除

首先我初始化FBO渲染。

glGenTextures(1, &tex); 
glBindTexture(GL_TEXTURE_2D, tex); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 
glBindTexture(GL_TEXTURE_2D, 0); 
glGenFramebuffersOES(1, &fbo); 
glBindFramebufferOES(GL_FRAMEBUFFER_OES, fbo); 
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tex, 0); 

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

然後我畫我的紋理

GLint oldFBO, oldViewPort[4]; 
glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &oldFBO); 
glGetIntegerv(GL_VIEWPORT, oldViewPort); 
glBindTexture(GL_TEXTURE_2D, 0); 
glBindFramebufferOES(GL_FRAMEBUFFER_OES, listFrameBuffers[i]); 
glViewport(0, 0, 512, 512); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

// draw code here 
glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFBO); 
glViewport(oldViewPort[0], oldViewPort[1], oldViewPort[2], oldViewPort[3]); 

然後每幀我畫它。

當我完成我的框架調用glSwapBuffers質地連接到幀緩衝區變得清晰 (但是當我畫幀後glSwapBuffers一切都很好,每次緩衝)。

+0

glSwapBuffers在哪裏呢?它不在你提供的上述代碼中。 –

+0

對不起,我的意思是把畫面刷到屏幕上 – JINGE

回答

0

因此,任務完成。 我只是將繪製代碼改爲這個

GLint oldFBO, oldViewPort[4]; 
glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &oldFBO); 
glGetIntegerv(GL_VIEWPORT, oldViewPort); 
glBindTexture(GL_TEXTURE_2D, 0); 
glBindFramebufferOES(GL_FRAMEBUFFER_OES, listFrameBuffers[i]); 
glViewport(0, 0, 512, 512); 
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // HERE 
glDisable(GL_DEPTH_TEST); // AND HERE 

// draw code here 

glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFBO); 
glViewport(oldViewPort[0], oldViewPort[1], oldViewPort[2], oldViewPort[3]); 

它的工作原理! :)