Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pixels and Units?!?!?!?!

Started by Jason2Jason Apr 23, 2001 at 4:38 PM 2 replies 300+ views
Original Post
Jason2Jason
Jason2Jason
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
Lord Karnus
Lord Karnus
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.
[email=JValentine_13@hotmail.com]contact[/email]
smart_idiot
smart_idiot
you can use gluunproject to convert screen cords to unit''s, but I can''t tell you how right now. can you wait until 12:15 server time?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
smart_idiot
smart_idiot
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
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.