PDA

View Full Version : Problems with glDrawPixels()



chisum
01-12-2006, 01:15 AM
Hello,

now I give up to search an infinite room (the internet) to match a tiny little problem that steels my sleep at the moment. So.

What I want:
Reading Pixels out of a rectangular subregion of the window. Then I shift this region by doing some kind of translation. So far everything is working. I've seen the results on the command line per printf().
Then I want the shifted Pixels to be drawn anywhere on the screen. Depending on glRaster (I know)

What I have:
- I can move an image on the window, if it's passing the region I'm reading with glReadPixels, the values stored in my pixel Array pixels, are changing. So I get proper pixel-values per glReadPixels().

- Proper shift-values.

- glDrawPixels() fills the rectangle with only one color for each pixel, but the values in my pixel array are different/varying.

My code:


float pixels[120*120*4]; //stores rgba values for the subregion 120*120 pixels

int PIXEL_RES=120;

static void readPixels(){
glReadBuffer(GL_FRONT); glReadPixels(0,0,PIXEL_RES,PIXEL_RES,GL_RGBA,GL_FL OAT,pixels);
printPixels();
}

static void printPixels(){
int i=0;
while(i<PIXEL_RES*PIXEL_RES*4){
printf("(%i)=(%.4f,%.4f,%.4f)\n",i,pixels[i],pixels[i+1],pixels[i+2],pixels[i+3]);
i +=4;
}

}
static void drawPixels(){
glDrawBuffer(GL_BACK);

glRasterPos2i(0,0);
glDrawPixels(PIXEL_RES,PIXEL_RES,GL_RGBA,GL_FLOAT, pixels);
}So if anyone has got an idea why drawPixels is not working properly, answer and get me to sleep again :eek:
Thank you.

Relic
01-12-2006, 01:35 AM
1.) You're drawing to back and reading from front. Is there a swap buffers in between?
2.) Talking about windows means this is on the visible screen?
Those have a fixed point format. You should not read and write with type GL_FLOAT then but GL_UNSIGNED_BYTE to remove the need for fixed to float conversions and introduced rounding errors. As a sideeffect not using GL_FLOAT will speed up the process a lot.
3.) If you don't need the data in the pixels array for anything else, glCopyPixels could be used.
4.) Mind that glRasterPos is a transformed position. Make sure your projection is set up to transform the coordinates to exact pixel corners when working with gl*Pixels commands.
5.) Reading pixels fails the pixelownership if you're reading from outside the window areas or if the GL context's pixels are overlapped by other windows. Only offscreen surfaces like pbuffers or FBOs are guaranteed to be unclipped.
6.) glRasterPos can be invalid and draw nothing when it's outside the view. The extension rasterpos_clip can help there.
7.) glDrawPixels uses the transformed rasterpos.z value to generate depth data. Make sure you have depth testing disabled if you only want 2D pixel rendering.

Bob
01-12-2006, 03:40 AM
Originally posted by Relic:

4.) Mind that glRasterPos is a transformed position. Make sure your projection is set up to transform the coordinates to exact pixel corners when working with gl*Pixels commands.
...
6.) glRasterPos can be invalid and draw nothing when it's outside the view. The extension rasterpos_clip can help there.
Just as a side node; Clicky (http://oss.sgi.com/projects/ogl-sample/registry/ARB/window_pos.txt) . Was added to the 1.4 core, and is a really handy function. No reason to care about point 4 and 6 when working with window coordinates.

chisum
01-12-2006, 04:15 AM
relic wrote:

1.) You're drawing to back and reading from front. Is there a swap buffers in between?
the algo order is:
reading from front buffer
drawing to back buffer
swapping the buffers



2.) Talking about windows means this is on the visible screen?
Those have a fixed point format. You should not read and write with type GL_FLOAT then but GL_UNSIGNED_BYTE to remove the need for fixed to float conversions and introduced rounding errors. As a sideeffect not using GL_FLOAT will speed up the process a lot.
I tried it also with gl_unsigned_byte



3.) If you don't need the data in the pixels array for anything else, glCopyPixels could be used.
4.) Mind that glRasterPos is a transformed position. Make sure your projection is set up to transform the coordinates to exact pixel corners when working with gl*Pixels commands.
5.) Reading pixels fails the pixelownership if you're reading from outside the window areas or if the GL context's pixels are overlapped by other windows. Only offscreen surfaces like pbuffers or FBOs are guaranteed to be unclipped.
6.) glRasterPos can be invalid and draw nothing when it's outside the view. The extension rasterpos_clip can help there.
7.) glDrawPixels uses the transformed rasterpos.z value to generate depth data. Make sure you have depth testing disabled if you only want 2D pixel rendering.As I already said. The problem is not the reading procedure. And I do make a pixel shift. That means I can't just use glCopyPixels()

I do see the rectangle with size PIXEL_RES*PIXEL_RES. The problem I have is that the pixel I draw aren't those I read. I can see that when printing the pixels array to command line (different pixel colors) and compare to the rectangel on the screen (where every pixel has the same color) :(

Relic
01-12-2006, 04:47 AM
Originally posted by chisum:
And I do make a pixel shift. That means I can't just use glCopyPixels()
Of course glCopyPixels can do that!
8.) Make sure texturing, lighting and other operations which would not result in a pure data copy are off.
I can't help further without seeing the whole code.

dorbie
01-12-2006, 10:37 AM
Moving to the beginners forum.