如何在 OpenGL 窗口中将光标更改为一条线?
我想在我的应用程序(用 C++ 和 OpenGL 编写)中使用红线而不是鼠标指针。例如,当我将鼠标移到 OpenGL 窗口上时,我希望光标变成一条红线。我怎样才能做到这一点?
另外,我如何调用该行的鼠标相关函数(OpenGL)并指定功能?
I want to have a red line instead of mouse pointer in my (written in C++ with OpenGL) application. For example when I move the mouse over an OpenGL window, I would like the cursor to become a red line. How can I do that?
Also, how can I call the mouse related functions (OpenGL) for that line and specify the functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Nicol Bolas 在评论中所说,OpenGL 对鼠标一无所知。您需要以一种或另一种方式与窗口系统交互(通过直接的 Win32/X11/等 API 或通过便携式窗口库,如 Qt、wxWidgets 等)来监视鼠标位置。
如果您尝试绘制的光标是位图,则最好的选择可能是处理发送到窗口的鼠标进入/离开事件,并通过使用 API 函数更改光标来响应它们。这将在鼠标移动时自动更新光标,并将为应用程序增加最小的开销(窗口系统光标通常在覆盖平面上绘制,以避免每次鼠标移动时向窗口发送重绘事件)。
如果您对光标有更程序化的描述(即,您打算使用 OpenGL 绘图命令来绘制它),您将需要使用 HideCursor()/ShowCursor() 来处理鼠标进入/离开事件 命令或等效命令,当鼠标悬停在窗口上时关闭窗口系统的本机光标。然后,您将挂钩鼠标移动回调并重新绘制场景,添加在鼠标移动事件中指定的位置绘制光标所需的任何命令。
对于性能和性能而言,第一种方法绝对是首选。延迟原因 - 但有一些光标类型(例如全屏十字准线)无法以这种方式适应。
As Nicol Bolas said in his comment, OpenGL knows nothing of the mouse. You'll need to interact with the windowing system one way or another (via direct Win32/X11/etc. API or via a portable windowing library a la Qt, wxWidgets, etc) to monitor mouse position.
If the cursor you're trying to draw is a bitmap, your best bet is likely to handle mouse enter/leave events sent to your window and respond to them by using an API function to change the cursor. This will handle automatically updating the cursor as the mouse moves around and will add minimal overhead to your application (windowing system cursors generally draw in an overlay plane that avoids sending redraw events to your window every time the mouse moves).
If you have a more procedural description of your cursor (that is, you intend to draw it with OpenGL drawing commands), you'll instead want to handle the mouse enter/leave events by using
HideCursor()/ShowCursor()
commands or the equivalent to turn off the windowing system's native cursor when the mouse is over your window. Then, you'll hook the mouse move callbacks and redraw your scene, adding whatever commands you need to draw the cursor at the position specified in the mouse move event.The first approach is definitely preferred for performance & latency reasons--but there are some cursor types (think full screen crosshairs) that can't be accomodated that way.