窗口大小调整后,将鼠标位置转换为 2D openGL 中的世界坐标
当我编写的程序启动时,窗口中有一些顶点。视口设置为占据整个窗口。单击鼠标时,将计算从鼠标位置到每个顶点的距离。如果它们中的任何一个在某个阈值内(假设为 5 个单位),则选择该顶点。这部分工作没有问题。
然而,在调整窗口大小后,我限制新窗口的视口以保持宽高比。这也有效。
然而,调整大小后,尝试单击顶点会产生奇怪的行为,因为鼠标位置似乎不再与世界坐标对齐。如何将鼠标位置转换为存储这些顶点的 x/y 值的世界坐标系?
我以前见过这个问题,但是是在 3D 环境中。解决这个问题的方法似乎是使用 gluUnproject。我不确定这是否适用于 2D 程序。任何帮助将不胜感激!
这是我的调整大小函数的代码:
//resize function; used to maintain aspect ratio
void resize(int w, int h)
{
screenHeight = h;
screenWidth = w;
int width, height;
//taller
if ((float)(w/h) < R)
{
width = w;
height = w/R;
}
//wider
else
{
width = h*R;
height = h;
}
widthBound = width;
heightBound = height;
glViewport(0, 0, width, height);
} //end resize
我尝试通过执行以下操作来重置代码内的投影矩阵:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
其中左、右、下和上是我的新视口的限制。我在设置新视口之前和之后都尝试过执行这些操作。似乎没有解决问题。
When the program I wrote starts up, I have some vertices in a window. The viewport is set to take up the entire window. On a mouse click, the distance from the mouse position to each of the vertices is calculated. If any of them are within a certain threshold (let's say 5 units), the vertex is selected. This part works with no problem.
After a window resize, however, I restrict the viewport of the new window in order to maintain aspect ratio. This also works.
However, after resizing, trying to click on vertices produces weird behavior, since the mouse position doesn't seem to align to world coordinates anymore. How can I convert the mouse position to the world coordinate system that the x/y values of these vertices is stored?
I've seen this question asked before, but in a 3D context. The fix for that seems to be to use gluUnproject. I'm not sure if that's applicable to a 2D program. Any help would be appreciated!
Here is the code to my resize function:
//resize function; used to maintain aspect ratio
void resize(int w, int h)
{
screenHeight = h;
screenWidth = w;
int width, height;
//taller
if ((float)(w/h) < R)
{
width = w;
height = w/R;
}
//wider
else
{
width = h*R;
height = h;
}
widthBound = width;
heightBound = height;
glViewport(0, 0, width, height);
} //end resize
I've tried resetting the projection matrix inside the code, by doing:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
Where left, right, bottom, and top are the limits of my new viewport. I've tried doing those both before and after setting up the new viewport. Doesn't seem to fix the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查一下:
您是否使用
screen***
或***Bound
来转换鼠标坐标?这可能是一个问题,因为它们在纵横比强制下具有不同的尺寸,并且在其他方面是相等的。Check this:
Do you use
screen***
or***Bound
to transform mouse coordinates? this may be an issue since they hold different sizes with aspect-ratio enforcing and are equal otherwise.从评论来看,您的问题似乎是在调整窗口大小时重新计算投影。
只需在调整大小后调用 glOrtho() 即可(确保将矩阵模式设置为 GL_PROJECTION),然后就可以开始了。
From the comments it appears your problem is recalculating your projection when your window is resized.
Just call
glOrtho()
after the resize (making sure to set the matrix mode to GL_PROJECTION) and you should be good to go.