PDA

View Full Version : Weird glDrawPixels() and glReadPixels() problem



jang
03-31-2005, 08:34 AM
I've a funny problem when doing glReadPixels() and glDrawPixels(). I have a buffer in main memory called theFB. In my render function, I:

(1) Clear the draw buffer
(2) Draw the contents of theFB
(3) Read it right back into theFB

So I'm expecting the initial contents of theFB to be displayed all the time (since the should be no change). However, it seems to read in the window border and title pixels as well. What am I doing wrong?

Here's the code (run the program a few times to see what I mean). The first display() draws fine (if I comment out the glutPostRedisplay() call), but subsequent one is messed up.

Any ideas?


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glxew.h>
#include <GL/glut.h>

static int theRes[2] = {256, 256};
static GLubyte theFB[256][256][4];

void glErrorCheck(char *s)
{
GLenum error;
while((error = glGetError()) != GL_NO_ERROR)
{
fprintf(stderr, "ERROR (%s): %s\n", s, (char *)gluErrorString(error));
}
}

void idle(void)
{
glutPostRedisplay();
}

void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 1, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glRasterPos2i(0, 0);
glDrawPixels(theRes[0], theRes[1], GL_RGBA, GL_UNSIGNED_BYTE, theFB);
glReadPixels(0, 0, theRes[0], theRes[1], GL_RGBA, GL_UNSIGNED_BYTE, theFB);

glutSwapBuffers();
}

void init(void)
{
GLenum err = glewInit();
if(err != GLEW_OK)
{
fprintf(stderr, "ERROR: %s\n", glewGetErrorString(err));
exit(1);
}
reshape(theRes[0], theRes[1]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

for(register int i = 0; i < theRes[1]; ++i) {
for(register int j = 0; j < theRes[0]; ++j) {
for(register int c = 0; c < 4; ++c) {
theFB[i][j][c] = (GLubyte) 127;
}
}
}
}

int main(int argc, char *argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(theRes[0], theRes[1]);
glutCreateWindow("Read buffer problem");
glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutDisplayFunc(display);

init();

glutMainLoop();

return(0);
}

mikael_aronsson
03-31-2005, 11:09 PM
Hi !

Do you have a screenshot of the problem ?

The code looks ok to me, there is one problem, and that is if you resize the window you do not update the theRes variables to reflect the new size of the window.

You also do not set glPixelStore, I don't remember the default values for this.

Mikael

jang
04-04-2005, 06:23 AM
Hi Mikael,



The code looks ok to me, there is one problem, and that is if you resize the window you do not update the theRes variables to reflect the new size of the window.
Thanks, this has been fixed.



You also do not set glPixelStore, I don't remember the default values for this.
The defaults are 4 for UNPACK_ALIGNMENT and PACK_ALIGNMENT. I've set both to 1 (or just PACK) but still get the same results. Unfortunately I can't figure out how to attach images to the post.