C++ 如何从GLUT坐标转换为窗口坐标
可能重复: 假设我有一个600x600的窗口。 当我收到鼠标事件时,我不知道鼠标的真实位置,在OpenGL中,我使用它来绘制点:C++ 如何从GLUT坐标转换为窗口坐标,c++,opengl,glut,C++,Opengl,Glut,可能重复: 假设我有一个600x600的窗口。 当我收到鼠标事件时,我不知道鼠标的真实位置,在OpenGL中,我使用它来绘制点: (-0.5,0.5) | (0.5,0.5) | --------(0,0)------- | | (-0.5,-0.5) | (0.5,-0.5) 但是,根据窗口的大小,当我收到GLUT鼠标事件时,我会得到不同的坐标。我想要一个相对于窗口坐标系的坐标。我如何得到它?也许我误解了你的问题,或者过于
(-0.5,0.5) | (0.5,0.5)
|
--------(0,0)-------
|
|
(-0.5,-0.5) | (0.5,-0.5)
但是,根据窗口的大小,当我收到GLUT鼠标事件时,我会得到不同的坐标。我想要一个相对于窗口坐标系的坐标。我如何得到它?也许我误解了你的问题,或者过于简化了答案,但你不是在寻找类似以下的东西:
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;
也许我误读了你的问题,或者过于简单化了答案,但你不是在寻找这样的答案吗:
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;
我很确定GLUTT给了你窗口空间中的鼠标坐标,即如果窗口是800×600,鼠标位置在中间,它会给你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;
我很确定GLUTT给了你窗口空间中的鼠标坐标,即如果窗口是800×600,鼠标位置在中间,它会给你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;