PDA

View Full Version : glDrawPixels and FBO



nachovall
10-04-2010, 06:59 AM
Hi,

I'm trying to copy to GL_COLOR_ATTACHMENT0_EXT an array stored in CPU. To this end I use glDrawPixels after attaching glFramebufferTexture2DEXT.

The code bellow is not what I want to do but I'm testing the correctness of the code:



GLuint colorTex;
glGenTextures(1, &colorTex);
glBindTexture(GL_TEXTURE_2D, colorTex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCREEN_RES_X, SCREEN_RES_Y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);

GLuint fbo=0;
glGenFramebuffersEXT(1, &fbo);

//render to texture to avoid scissors test by qglwidget
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

//depth cannot be rendered without color
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_CO LOR_ATTACHMENT0_EXT,GL_TEXTURE_2D,colorTex,0);

GLubyte *pixels=new GLubyte[SCREEN_RES*3];
//create a red image
for(int i=0;i<SCREEN_RES;i++)
{
pixels[i*3]=255;
pixels[i*3+1]=pixels[i*3+2]=0;
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDrawPixels(SCREEN_RES_X,SCREEN_RES_Y,GL_RGB,GL_U NSIGNED_BYTE,pixels);

GLubyte * color=new GLubyte[SCREEN_RES*3];
glReadPixels(0,0,SCREEN_RES_X,SCREEN_RES_Y,GL_RGB, GL_UNSIGNED_BYTE,color);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);

glDeleteFramebuffersEXT(1, &amp;fbo);
glDeleteTextures(1,&amp;colorTex);


The problem is that color is all white, like DrawPixels has no effect. Am I doing something wrong?

Thank you.

carsten neumann
10-04-2010, 07:38 AM
I think you are missing calls to glDrawBuffer/glReadBuffer to specify the FBO attachment glDrawPixels/glReadPixels work with.

nachovall
10-05-2010, 01:58 AM
I think you are missing calls to glDrawBuffer/glReadBuffer to specify the FBO attachment glDrawPixels/glReadPixels work with.

This:


GLint db;
glGetIntegerv(GL_DRAW_BUFFER,&amp;db);


called before glDrawPixels returns GL_COLOR_ATTACHMENT0_EXT, which is reasonable since:



glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_CO LOR_ATTACHMENT0_EXT,GL_TEXTURE_2D,colorTex,0);


I'm attaching GL_COLOR_ATTACHMENT0_EXT as the color buffer.

In addition:



GLuint colorTex;
glGenTextures(1, &amp;colorTex);
glBindTexture(GL_TEXTURE_2D, colorTex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCREEN_RES_X, SCREEN_RES_Y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);

GLuint fbo=0;
glGenFramebuffersEXT(1, &amp;fbo);

//render to texture to avoid scissors test by qglwidget
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

//depth cannot be rendered without color
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_CO LOR_ATTACHMENT0_EXT,GL_TEXTURE_2D,colorTex,0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(1,0,0);
glPolygonMode(GL_FRONT, GL_FILL);
glBegin(GL_QUADS);
glVertex2f(0.2,0.2);
glVertex2f(0.4,0.5);
glVertex2f(0.4,0.4);
glVertex2f(0.2,0.4);
glEnd();

GLubyte * color=new GLubyte[SCREEN_RES*3];
glReadPixels(0,0,SCREEN_RES_X,SCREEN_RES_Y,GL_RGB, GL_UNSIGNED_BYTE,color);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);

glDeleteFramebuffersEXT(1, &amp;fbo);
glDeleteTextures(1,&amp;colorTex);


works fine. Any other ideas?