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

Started by
5 comments, last by ashoo 16 years, 8 months ago
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
Advertisement
gluUnproject().
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.
Sorry
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:)
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]
thanks everyone ...my problem got solved by using gluUnproject().

This topic is closed to new replies.

Advertisement