Archived

This topic is now archived and is closed to further replies.

Pixels and Units?!?!?!?!

This topic is 6077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, I''m writing an open GL game which needs a mouse in it. I''ve written a class to handle the mouse and that works. The problem is when I try to translate the screen into pixels from floating point units. At first I used glOrtho() but it didn''t work. So could any one tell me how meny pixels there are to a unit, or how much of a unit to one pixel, or any other help you can give. Thanks, J

Share this post


Link to post
Share on other sites
It all depends on your projection matrix.
In my experiences, these are true:
(-1,1) for upper left
(1,-1) for lower right
(0,0) is the center of the screen.

Sometimes, this rule gets scewed, usually because of translations. In this case use glLoadIdentity() beforehand.
This rule may or may not be true for all screen resolutions/aspect ratio combinations.

I hope this answers your question!





Feel free to email me.

Share this post


Link to post
Share on other sites
This isn't really what you asked, but I think this is what you wanted it for: It convertes window cords to world cords.



inline void convert_cords(int mx,int my,GLdouble *rx,GLdouble *ry,GLdouble *rz)
{
GLdouble modelMatrix[16];
GLdouble projMatrix[16];
GLdouble dist,tempx,tempy;
GLint viewport[4];

glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);

gluProject(0,0,0,modelMatrix,projMatrix,viewport,&tempx,&tempy,&dist);
gluUnProject(mx, viewport[3]-my, dist, modelMatrix, projMatrix, viewport, rx, ry, rz);
}





That at works as if there was a plane facing the user intersecting the origin and and rx,ry, and rz are set to a point on it's surface that when projected, would be at mx,my.

If you want to point onto the rendered geometry, use this instead:





inline void convert_cords(int mx,int my,GLdouble *rx,GLdouble *ry,GLdouble *rz)
{
GLdouble modelMatrix[16];
GLdouble projMatrix[16];
GLdouble dist;
GLint viewport[4];

glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);

glReadPixels(mx,viewport[3]-my,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,(void *)&dist);
gluUnProject(mx, viewport[3]-my, dist, modelMatrix, projMatrix, viewport, rx, ry, rz);
}




Edited by - smart_idiot on April 25, 2001 12:35:14 PM

Share this post


Link to post
Share on other sites