PDA

View Full Version : glReadPixels



pramod
01-05-2007, 04:25 AM
I have doing a projection from screen coordinate to 3D world space, but I have some problem in glReadPixels function..... the code goes like this:

GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)x;
winY = (float)viewport[3] - (float)y;

glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

return Vector(posX, posY, posZ);

the problem is that I am not able to get 'winz' value from glReadPixels(), I have checked my code by debugging it, but was not able to find out the reason.......

so plzzzzzzzz help me out from this.......

k_szczech
01-05-2007, 05:35 AM
I don't know what compiler you use but try changing int(winY) to (int)winY.

pramod
01-06-2007, 12:46 AM
Sir, I am using Visual Studio C++ compiler....
and I have changed int (winY) to (int) winY, but still I am not able to get the value of winZ, Sir plzzzzzzz help me out in this, bcoz I have to submit this project within Monday, presently I am stuck in this........

Thank U

Komat
01-06-2007, 04:30 AM
Originally posted by pramod:

the problem is that I am not able to get 'winz' value from glReadPixels()
What exactly do you meant with this? Is the winz variable unmodified or it contains different value than you expect?

If the variable is unmodified, use the glGetError() function to determine if there is some error. The glReadPixels() function will fail if it is called within glBegin/glEnd pair or if there is no depth buffer so check that you are calling it at correct place and that your OpenGL initialization code requests the depth buffer.

pramod
01-07-2007, 11:37 PM
Komat Sir, I have not used glReadPixels() within glBegin/glEnd function, & in intialization section I have intialized properly..... but glReadPixels() is not returning any value to winz, winz conatins the default float value even after executing glReadPixels() function......
Sir Plz help out from this

pramod
01-07-2007, 11:39 PM
Komat Sir, I have not used glReadPixels() within glBegin/glEnd function, & in intialization section I have intialized properly..... but glReadPixels() is not returning any value to winz, winz conatins the default float value even after executing glReadPixels() function......
Sir I think there some problem with depth buffer, how can I identify the values of Depth Buffer....
Sir Plz help out from this

Komat
01-08-2007, 12:16 AM
How do you initialize the OpenGL context? Are you using GLUT or do you create the context manually?

In the case of GLUT you have to add GLUT_DEPTH constant to parameters of glutInitDisplayMode() function.

If you doing it manually using the ChoosePixelFormat() function, you need to specify nonzero value (e.g. 24) into cDepthBits field of PIXELFORMATDESCRIPTOR structure.

pramod
01-08-2007, 04:44 AM
Originally posted by Komat:
How do you initialize the OpenGL context? Are you using GLUT or do you create the context manually?

In the case of GLUT you have to add GLUT_DEPTH constant to parameters of glutInitDisplayMode() function.

If you doing it manually using the ChoosePixelFormat() function, you need to specify nonzero value (e.g. 24) into cDepthBits field of PIXELFORMATDESCRIPTOR structure. No Sir, I am not using GLUT, but I am setting the PIXELFORMATDESCRIPTOR structure depth value to 24...... even than I am no0t able to get the value of winz, so is there any other way to get into this..........

Sir, is there other way to find a mouse co-ordinate values in 3D space by not using gluUnProject() function!!!!!!!!!

plzzzzzz help me....... and Thank U for u'r reply Sir

Komat
01-08-2007, 05:17 AM
Try to use the glGetError() function or GLIntercept (http://glintercept.nutty.org/) tool to determine if there was any error reported during the glReadPixels() call.

pramod
01-08-2007, 07:04 PM
Originally posted by Komat:
Try to use the glGetError() function if there was any error reported during the glReadPixels() call. Sir I have not used glGetError() previously, so can u plz give an example in this regrad......

Komat
01-09-2007, 01:02 AM
Nothing special. Simply call the glGetError() function which will return constant indicating error or GL_NO_ERROR if there was no error since last call to glGetError().

If multiple errors happened between calls to glGetError(), repeated calls to glGetError() will return all of them before they return GL_NO_ERROR.

pramod
01-09-2007, 01:48 AM
Sir, I used glGetError() after glReadPixels(), I am getting an constant value returning from glGetError() and the constant value is 1282.....

And I converted this constant to String using gluErrorString() & the value after converting to string was that: -0x00154050 "invalid operation"........... so what to do now!!!!!! I am in desperate position plz help me out of this

Komat
01-09-2007, 03:12 AM
What value is in bitplanes variable after following code is executed?

GLint bitplanes ;
glGetIntegerv( GL_DEPTH_BITS, &bitplanes ) ;If it is zero, the depth buffer is not created correctly. In that case, can you post your OGL context initialization code?

pramod
01-10-2007, 12:56 AM
Originally posted by Komat:
[CODE]GLint bitplanes ;
glGetIntegerv( GL_DEPTH_BITS, &bitplanes ); Sir the bitplanes value is '24', I am not getting '0', so what to do!!!!!!!!!!
Sir if U need I can post my initialization part........ so what do u say?

jide
01-10-2007, 02:32 AM
It appears you have forgotten to initialize the depth buffer when creating your window.
So yes, post your initialization stuff.

pramod
01-10-2007, 03:27 AM
Originally posted by jide:
post your initialization stuff. glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D);

this where I initialize my depth buffer

Flavious
01-10-2007, 03:59 AM
Hi,

Can you post the entire program? That might be easier, if it's not too big.

Cheers

pramod
01-10-2007, 04:04 AM
Originally posted by Flavious:
Hi,

Can you post the entire program? That might be easier, if it's not too big.

Cheers Sir, the program is too big, actually it's an project for an company......i will post the function which initializes:

void CGLEnabledView::OnCreateGL()
{
// perform hidden line/surface removal (enabling Z-Buffer)
//glEnable(GL_DEPTH_TEST);

glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D);

// set background color to black
// glClearColor(0.f,0.f,0.f,1.0f );

// set clear Z-Buffer value
// glClearDepth(1.0f);
}
This is where I am initializing the depth buffer

Flavious
01-10-2007, 04:19 AM
Can you email me the code? I'd be happy to look at it.

Cheers