gluUnproject Question/moving character question
hi every1 i have been reading lots of posts here about how to use gluUnproject and i think ive got basically what its doing. but yeah my problem is this.. ive got 1 plane and if i click in there theres no problem my object goes there but if i click outside of i开发者_运维知识库t well my object dissapears any idea why is it doing this and how can i fix it? and this is my function of gluUnproject
GetOGLPos(float x, float y)
{
GLint viewport[4];
GLfloat depth[2];
GLdouble modelview[16];
GLdouble projection[16];
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, depth );
gluUnProject( winX, winY, depth[0], modelview, projection, viewport, &posX, &posY, &posZ);
std::cout<< posX<<" "<<posZ<<std::endl;
PosP.x = posX;
PosP.z = posZ;
PosP.y = 20.0f;
}
im just printing the values to know where the object is going and PosP is a Vector and im moving my character passing it that variable(ive got no physics or anything like that in this project btw)
Check the depth you read from the depth buffer. If the depth value is the glClearDepth value you set (or close enough to it), then you know that nothing was rendered there. So don't position the object at that location.
精彩评论