Skip to main content
gamedev.net
🔒 Locked

glDrawPixels Problem

Started by faculaganymede 1 reply 400+ views
Original Post
faculaganymede
faculaganymede
glExperts, I have a problem with glDrawPixels. It appears there's some conflicts when I use glDrawPixels and glReadPixels. I get the same image on all frames - frame n is the same as frame 0 (n=0,1,2,...); Below is my draw callback function. Any idea?

// Draw process callback
static void DrawChannel (pfChannel *chan, void *)
{
	int g_width=500;
	int g_height=500;
	char *intensity_img;
	static int frame_num=0;
	char fname[256];

	intensity_img=new char[500*500*3];

	chan->clear(); //clears framebuffer
    	pfDraw ();  //draws scene

	glReadPixels( 0, 0, g_width, g_height, GL_RGB,
GL_UNSIGNED_BYTE, intensity_img);

	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	glMatrixMode(GL_PROJECTION); 
	glPushMatrix(); 
	glLoadIdentity(); 
	gluOrtho2D( 0.0, g_width, 0.0, g_height);

	glMatrixMode(GL_MODELVIEW); 
	glPushMatrix(); 
	glLoadIdentity(); 

	glDrawPixels( g_width, g_height, GL_RGB,
GL_UNSIGNED_BYTE, intensity_img);

	glMatrixMode(GL_MODELVIEW); 
	glPopMatrix();

	glMatrixMode(GL_PROJECTION); 
	glPopMatrix();

	sprintf(fname, "snap_%d.rgb", frame_num);
	pfuSaveImage(fname, 0, 0, g_width, g_height, 0);
	frame_num++;

	if(intensity_img)
		free(intensity_img);
}


pfuSaveImage basically does the following:

int pfuSaveImage(char *name, int xorg, int yorg, int xsize, int ysize, int alpha)
{
...
open image file
...
    /* read from FRONT-buffer to get channel stats info */
    glReadBuffer(GL_FRONT);
    
    /* read the display */
    glReadPixels((short)xorg, (short)yorg, (short)xsize, (short)ysize,
	 GL_RGB, GL_UNSIGNED_BYTE, scrbuf);	

    /* write the data to the image file */
...
write data to file
...

...
close image file
...
}


zedzeek
zedzeek
try glReadBuffer(GL_BACK);

read and draw have to be to the same buffer (discount swapping back->front)
also stick a glFinish() before u readback the buffer
faculaganymede
faculaganymede
Thank you, Zedzeek. You are a genius!

I tried glReadBuffer(GL_BACK), and it worked. Removing this line also worked. Worked with or without glFinish.

How did you arrive to this conclusion? Please do explain.

I didn't write the pfuSaveImage function (it's from the OpenGL Performer API). Generally, Performer functions do the buffer swaps automatically. But, I guess it may not be the case if I have both OpenGL and Performer functions in the code.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.