PDA

View Full Version : Rendering in incorrect position and glReadPixels() gives empty array



ntsang
02-15-2004, 03:58 AM
Hi, I have a weird problem. I moved my code over from an existing VC++ project I had going and tidied up the code.

I have 2 windows in the program, the OpenGL rendering window, and a message window (both using the same wndproc but with seperate window handles and device contexts), if I create the message window first, the OpenGL window is centred, but if I create the OpenGL window first, the image is too far to the left (same code), if I resize the window, the image recentres itself. Can anyone tell me why this happens?

Also, the main problem:
I've not messed around with the rendering code (which is in a seperate file), but I have changed parts to make the drawing loop check for any event messages rather than having winMain do it (so the checks are done outside of the winMain function).
I've stepped through the program, and after pressing space (I have added a few areas that detect the space bar to progress through an algorithm), the main loop is entered, with the message handler being called 3 times before going into the rendering function, at which point, it draws the image required (only if the message window is created first. However, glReadPixels writes an empty array rather than the pixels in the window, so what's wrong? Does it have anything to do with the fact that i have 2 windows? I've tried without the message window but it's still writing nothing into the array (this function was working in the previous program, which has not had anything too major added apart from the extra window, and the changing of the loop structure).


I've been staring at this for several days now and there's absolutely nothing that I can see that could cause this!

mikael_aronsson
02-15-2004, 05:06 AM
Hi !

Without having a peek at the code I don't have a clue, but it does sound like there is some micup with the rendering context, are you 100% sure that you have the correct RC for each window ?

Mikael

ntsang
02-15-2004, 06:38 AM
Not too sure which part of the code is causing the problem, but there are some snippets below, I dunno if it's the right bit of code though.

I have a GDI device context for the message window, that doesn't use OpenGL, just processes some TextOut functions for now, the OpenGL rendering function has the window's HDC passed into it like so:

int Render(float c_x, float c_y, BYTE* TempArray, HDC* hDC)

then the hDC is used after all the rendering commands have been issued:

SwapBuffers(*hDC);

So, an image appears on the screen straight after this stage, then we get to the code for the glReadPixels():

//save rendered scene to TempArray
BYTE* DataT = new BYTE[640 * 480 * 3];//the array to hold the image data temporarily
if (DataT)
{
glPixelStorei(GL_PACK_ALIGNMENT, 1);
//read in from the frame buffer and save to file as binary
//format data values in a raw file
glReadPixels(0,0,640,480,GL_RGB,GL_UNSIGNED_BYTE,D ataT);

//now seperate into RGB component
if(!RGB_Sep(DataT, TempArray, 1, 0, 0))
{
MessageBox(NULL, "ERROR EXECUTING RGB_SEP", "ERROR!!!", MB_OK);
}
}

I've also got an HRC OpenGL rendering context used only on the creation of the OpenGL window and killing the window. I haven't passed it into the rendering function and the only time HDC is used in the rendering function is when I swapbuffers. As I said, it works in the previous code, it draws (still does), and gives me values in glReadPixels, so I don't know why it isn't working now. Previously, I could save the pixels to a file, which proved that it was reading in the correct data.

Hope this is the correct information, and thanks for any help you can give!

ntsang
02-15-2004, 10:52 AM
I've got this kinda working, but it only captures the square in the middle of the window that I have, not the rest of the window. I've changed the code like so:

BYTE* DataT = new BYTE[116 * 116 * 3];//the array to hold the image data temporarily
if (DataT)
{
glPixelStorei(GL_PACK_ALIGNMENT, 1);
//read in from the frame buffer and save to file as binary
//format data values in a raw file
glReadPixels(262,182,116,116,GL_RGB,GL_UNSIGNED_BY TE,DataT);

This was the code from my previous program, but now I want the thing to read in the whole 640x480 window, what is stopping me from doing this? As I will get 0 values on DataT when I try to get the whole window (or even 600X400 from position 0,0 or 1,1).

Any clues?