PDA

View Full Version : My glReadPixels do not work ?



KUAN-MING YU
05-24-2004, 01:18 AM
hello Every body :

I have a question for glReadPixels.
That I render silhouette using ID IMAGE method, it is render using different color for every silhouette, but I check the frame buffer color miss my result.
I do not know what is happened?
My sample code is next ,wish have someone can help me.

my window is 512*512
my output data are all 255;



void drawSilhouette() {
int i,num,tr0,tr1;
GLfloat dot0,dot1;
GLfloat eyeVec0[3],eyeVec1[3];
GLfloat eyeT[3];
GLfloat modelview_mat[16], iview[16];
GLubyte RGB[3];
GLubyte *ID_image;
int sil=0;
.................
.................

glColor3f(0.0,0.0,0.0);
glLineWidth(2.0);
glBegin(GL_LINES);

.................
.................
if (.....)
{
RGB[2] = (GLubyte)(sil++ % 256);
RGB[1] = (GLubyte)((sil / 256) % 256);
RGB[0] = (GLubyte)128;
glColor3ub(RGB[0],RGB[1],RGB[2]);
glVertex3fv(edges[num].edge[i].ver0);
glVertex3fv(edges[num].edge[i].ver1);
}
glEnd();
glDisable(GL_CULL_FACE);

/* write id image to file */
ID_image=new GLubyte [2*512*512*3];
FILE *file;
char str[40];

strcpy(str,
idmage.txt );

if((file = fopen(str,
w ))== NULL)
return;
glEnable(GL_READ_BUFFER);
glReadBuffer(GL_BACK);
glReadPixels(0,0,512,512,GL_RGB,GL_UNSIGNED_BYTE,I D_image);

for(int y = 0 ; y < 512 ; y++)
for(int x = 0 ; x < 512 ; x++)
fprintf(file,
(%3d,%3d,%3d) \n ,ID_image[y*x*3+x*3],ID_image[y*x*3+x*3+1],ID_image[y*x*3+x*3+2]);
fclose(file);
delete ID_image;

}Thank you very much!
KUAN-MING YU
from TAIWAN

Ysaneya
05-24-2004, 03:09 AM
fprintf(file, "(%3d,%3d,%3d) \n" ,ID_image[y*x*3+x*3],ID_image[y*x*3+x*3+1],ID_image[y*x*3+x*3+2]);Isn't that supposed to be ID_image[y*512*3+x*3] etc.. ?

No offense, but you should double (or even triple) check your code before you submit it to an online forum.

Y.

yooyo
05-24-2004, 04:04 AM
Are you sure about your viewport size? even if window are 512x512 viewport can be smaller than window because of titlebar.

Read viewport size with

GLuint vport[4]; // 0,1 - topleft; 2,3 width height
glGetIntegerv(GL_VIEWPORT, vport);Then use width and height to allocate memory for image. Also check for gl errors every time if your code doesn't work as you expect. Here are example:


Add this macro on top of every your c/cpp file where you have gl calls:

#define GLCALL(a) {(a); glerr(#a, __LINE__);}
//#define GLCALL(a) a
static void glerr(char *str, int line)
{
int err;

err=glGetError();
if (err!=0)
{
char errstr[1000];
sprintf(errstr, "gl error: %s %d: %s", __FILE__, line, gluErrorString(err));
MessageBox(NULL, errstr, "GL ERROR", MB_OK);
}
}

and use it as:
GLCALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Xres, m_Yres, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));yooyo

Honk
05-24-2004, 04:52 AM
Aside from whats being said already:

At the time you call glReadPixels() nothing may have drawn so far.

A glFinish() before the glReadPixels() may come in handy.

MattS
05-24-2004, 05:24 AM
I have found that if you have anti-aliasing enabled either through the control panel or programmatically glReadPixels will not give you the image data as you expect when reading from the back buffer. I found it gave me the background colour instead. I can see why this would be the case but unfortunately do not know any useful way around it. My only suggestions are to either turn AA off or read the front buffer which will give you the AA image but for certain applications this is clearly inappropriate.

If anyone has any suggestions for how to read an AA back buffer I'd be very grateful. I have to admit I have spent no time looking into whether there was a way around it case due to having very little time recently.

Matt