2012-10-12 184 views
1

可能重复:
Glut Mouse Coordinates如何从GLUT坐标转换到窗口坐标

比方说,我有600×600的窗口。
当我收到鼠标事件,我不知道什么是鼠标的实际位置,在OpenGL中我使用它来画点:

(-0.5,0.5) | (0.5,0.5) 
      | 
--------(0,0)------- 
      | 
      | 
(-0.5,-0.5) | (0.5,-0.5) 

但是,当我收到GLUT鼠标事件取决于窗口的大小我得到了不同的坐标。我想要一个相对(到窗口)坐标系。我怎么得到这个?

回答

4

我敢肯定过剩在窗口空间中给出鼠标坐标(例如,如果窗口是800x600,鼠标位置在中间,它会给你x:400,y:300),所以如果你想把它放到你发布的opengl空间上面,你会做到以下几点:

float x = (400/800) - 0.5f; //0.0 
float y = (300/600) - 0.5f; //0.0 

这样一个仿制版本会是这个样子:

float mouseX = (theGlutMouseXCoordinate/theGlutWindowWidth) - 0.5f; 
float mouseY = (theGlutMouseYCoordinate/theGlutWindowHeight) - 0.5f; 
1

也许我误解你的问题,或者简单化的答案,但你不只是寻找类似:

float x = mouse.x/screen.width; //x now in [0,1] 
float y = mouse.y/screen.height; //y now in [0,1] 
x-=0.5f; 
y-=0.5f; 

或反向:

float wx = (x + 0.5f) * screen.width; 
float wy = (Y + 0.5f) * screen.height;