Sign in to follow this  
ashoo

OpenGL Is there a simple way to transform window coordinates to opengl coordinates

Recommended Posts

hello friends, i am kinda new to graphics ..my question is : Is there a simple way to transform window coordinates to opengl coordinates ? my requirement is: I need to select area by mouse on my drawn graph. the window form is using OpenGL as control and not using GLUT . any help on this topic is highly appreciated . thanks in advance, ashoo

Share this post


Link to post
Share on other sites
There are an infinite number of points in world space corresponding to any point on the screen.

Using the eyepoint, the near clipping plane distance, and the field-of-view, you can easily construct a ray which runs from the eyepoint and through the point on the screen.

Share this post


Link to post
Share on other sites
Im not really sure if this will help but when i made my GUI system for my current game, when using GetCursorPos on win32, it tells the mouses position from the top right corner of the screen. OpenGL's coordinates start in the bottom left corner of the screen.
eg:

If my screen width is 800*600 and i use glRasterPos2i(100,100); it will draw whatever 100,100 from the bottom left corner of the screen.

When using GetCursorPos if the mouse is the same position as the thing drawn at 100,100 GetCursorPos will return 100,500

If i draw a rectangle 320 pixels wide and 25 pixels high I use this formula to convert the mouse coords to OpenGl coords:

POINT MousePos;
GetCursorPos(&MousePos);
if(MousePos.x > x && MousePos.x < (100+320) && MousePos.y < (600-100) && MousePos.y > (600-100)-25)
{
MouseOnRectangle = true;
}
else MouseOnRectangle = false;



Hope that helps:)

Share this post


Link to post
Share on other sites
If you set an orthografic vision with:

glOrtho(xi,xf,yi,yf,anyvalue1,anyvalue2);
(z values don't make a difference here)

To transform a windows coordinates ((0,0) is the top-left corner and (Rx,Ry) is the lower-right corner) to the one set by the call to glOrtho, you must first invert the Y axis. Then, scale and translate the axes to get in that range.

Inverting the Y axis and translating bottom-left corner to origin:

x' = x
y' = Ry - y

Scaling the axes to range [0,Rx] and [0,Ry]:

x'' = x'*(xf - xi)/Rx
y'' = y'*(yf - yi)/Ry

Translating the origin to (xi,yi):

x = x'' + xi
y = y'' + yi

Now you have (x,y) as your final OpenGL coordinate. Putting it all together (input x,y; output x,y):

x = xi + x*(xf - xi)/Rx
y = yi + (Ry - y)*(yf - yi)/Ry

[Edited by - Jaiminho on August 2, 2007 3:10:24 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this