本文介绍了OpenGL帧缓冲区:可以清除它,但不能对其进行绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac上,我有一个OpenGL设置,除了帧缓冲区外,其他设置都可以正常工作-纹理化工作等.因此,我知道纹理化已启用,我有一个有效的上下文,等等.创建一个帧缓冲区.

On a Mac, I've got an OpenGL setup that is working just fine apart from framebuffers - texturing works, etc. So I know that texturing is enabled, I have a valid context, etc. All works flawlessly until I try to create a framebuffer.

我用glGenFramebuffers,glBindFramebuffer和glFramebufferTexture2D创建了一个帧缓冲区,而glCheckFramebufferStatus返回GL_FRAMEBUFFER_COMPLETE.如果再调用glClear,然后再调用glGetTexImage,返回的数据将显示glClear确实作用在绑定到帧缓冲区的纹理上.我可以将glClearColor设置为所需的任何值,并且glClear可以正确设置纹理数据.

I created a framebuffer with glGenFramebuffers, glBindFramebuffer, and glFramebufferTexture2D, and glCheckFramebufferStatus is returning GL_FRAMEBUFFER_COMPLETE. If I then call glClear, followed by a call to glGetTexImage, the returned data shows that the glClear acted on the texture bound to the framebuffer just as it should. I can set glClearColor to anything I want, and the glClear sets the texture data correctly.

但这就是好消息停止的地方.无论使用VBO还是glBegin/glEnd,我都无法将任何内容绘制到帧缓冲区中.绘制调用未触及绑定到帧缓冲区的纹理中的纹理数据(尽管glClear结果仍会出现).即使我在glGetTexImage调用之前调用glFlush和glFinish也是如此.另外,glGetError不会对我的任何调用返回任何错误.

But that's where the good news stops. I can't draw anything into the framebuffer, whether I use VBOs or glBegin/glEnd. The texture data from the texture bound to the framebuffer is untouched by the draw calls (though the glClear results still appear). This is all true even if I call glFlush and glFinish before the glGetTexImage call. Also, glGetError is returning no error as to any of my calls.

我在下面发布了一些示例代码,这些代码是我在程序的相关位置添加的,目的是尝试解决此问题,以防万一. (这不包括glClear调用,但是我从同一时间的单独测试中知道可以正常工作.)

I've posted below some sample code that I added at a relevant point in the program just to try to work on this issue, in case that gives anyone an idea. (This doesn't include the glClear call, but I know from separate testing at the same point that that works OK).

    glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbTexID, 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
    Debugger();

glEnable(GL_TEXTURE_2D);
glCullFace(GL_NONE);
glGenTextures(1,(GLuint*)&tex);
glBindTexture(GL_TEXTURE_2D,tex);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,1024,1024,0,GL_RGBA,GL_UNSIGNED_BYTE,NULL);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1024, 1024, 0, -5000, 5000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, 1024, 1024);
glColor4f(1,1,1,1);
glBegin(GL_TRIANGLES);
    glTexCoord2f(0, 0);
    glVertex2f(0, 0);
    glTexCoord2f(1, 0);
    glVertex2f(1024, 0);
    glTexCoord2f(0, 1);
    glVertex2f(0, 1024);
glEnd();
glFlush();
glFinish();
unsigned char *dd = new unsigned char[1024*1024*4];
glBindTexture(GL_TEXTURE_2D, fbTexID); //I've tried calling glBindFramebuffer(GL_FRAMEBUFFER,0) before this bind - makes no difference
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, dd);
delete dd;

推荐答案

好的,回答了我自己的问题.看来,您生成的作为帧缓冲区绘制到的表面的纹理必须在生成帧缓冲区之后 生成.因此,这可行:

OK, answered my own question. It seems that the texture you generate to be the surface that the frame buffer draws to must be generated after the frame buffer is generated. So, this works:

glGenFramebuffers...
glBindFramebuffer...
glGenTextures...
glBindTexture...
glTexParameterf etc.
glFramebufferTexture2D...

但这不是:

glGenTextures...
glBindTexture...
glGenFramebuffers
glBindFramebuffer...
glFramebufferTexture2D...

我看不到任何地方都解决了这个问题,这似乎令人惊讶,但是我的代码从不工作变成了仅仅通过移动纹理的生成来工作.

I don't see this addressed anywhere, and it seems surprising, but my code went from not working to working just by moving the generation of the texture.

这篇关于OpenGL帧缓冲区:可以清除它,但不能对其进行绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:22