PDA

View Full Version : glDraw/ReadPixels



JoeMac
11-26-2001, 11:44 AM
Hi Everyone,
I realize this is a common question, but reviewing the faq's and redbook hasn't helped. Its probably an assumption I'm making, so maybe a few thousand fresh eyes may help. I'm trying to read the back buffer into an array, do some image processing on the pixel values, write the array back to the back buffer, then swap buffers. I haven't even made it to the image proc. point since I can't seem to get Draw pixels to work. Her's the code:
GLubyte Pixels = (GLubyte *)calloc((2*640*480*4),sizeof(GLubyte));//2* to make sure
if(Pixels==NULL)
exit(0);
glReadBuffer(GL_BACK);
glReadPixels(0,0,640,480,GL_RGBA,GL_UNSIGNED_BYTE, Pixels);
//image processing to go here
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//Wind->Clear();
glRasterPos2i(0,0);
glDrawPixels(640,480,GL_RGBA,GL_UNSIGNED_BYTE,Pixe ls);
SwapBuffers();
Any suggestions?
Joe

Gavin
11-27-2001, 02:17 AM
What do you mean it won't work?

Bob
11-27-2001, 02:21 AM
Make sure both the modelview is set to identity, and the projection matrix is set to an orthographic projection, with (0,0) in the lower left corner, before calling glRasterPos, especially if you want (0,0) to point to the lower left corner.

The corodinate passed to glRasterPos is transformed by both modelview and projection matrix, and if the resulting coordinate is outside the viewport, it will ve invalid and all drawing operations using the raster position won't do anything.

For a 640x480 sized window, you can do this.



glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);

This way, you can use glRasterPos to directly target a certain pixel in the window.

JoeMac
11-29-2001, 04:28 AM
Thanks Bob, You've lead me to a minor bug, but my big problem was my textures. I didn't realize that glDrawPixels was sensitive to textures.
Joe