glReadPixels

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…

I don’t know what compiler you use but try changing int(winY) to (int)winY.

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

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.

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

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

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.

Originally posted by Komat:
[b] 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. [/b]
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

Try to use the glGetError() function or GLIntercept tool to determine if there was any error reported during the glReadPixels() call.

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…

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.

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

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?

Originally posted by Komat:
[b]

GLint bitplanes ;
glGetIntegerv( GL_DEPTH_BITS, &bitplanes ); [/b]

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?

It appears you have forgotten to initialize the depth buffer when creating your window.
So yes, post your initialization stuff.

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

Hi,

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

Cheers

Originally posted by Flavious:
[b] Hi,

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

Cheers [/b]
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

Can you email me the code? I’d be happy to look at it.

Cheers