OpenGL 上下文和设备上下文如何工作?
我是 U/I 编程新手,我正在尝试开始使用 OpenGL。 当我运行一个使用 GLUT 创建新 OpenGL 窗口的示例程序时,它工作正常。 好的。 然而,在另一个程序的上下文中,我必须响应 Draw 事件(在 Windows 上),并将设备上下文传递给我 - 并且我可能没有可用的 GLUT - 我的困惑是:
When is a设备上下文创建和销毁? 我可以绘制给我的任何设备上下文,还是只能绘制其中的某些设备上下文(我如何知道)?
我是否必须创建自己的 OpenGL 上下文并使用它进行绘制,或者我可以使用“当前”OpenGL 上下文吗? 我是否必须在每次发送绘制事件时重新创建上下文?
基本上我的问题是,考虑到我收到“绘制”事件的情况,我多久尝试创建 OpenGL 上下文以及这与设备上下文的创建/销毁周期有何关系?
I'm new to U/I programming, and I'm trying to get started with OpenGL. When I run an example program which creates a new OpenGL window with GLUT, it works fine. Good. However, in the context of another program, where I have to respond to Draw events (on Windows), with a device context passed to me - and where I might not have GLUT available - my confusion is this:
When is a device context created and destroyed? Can I draw to any device context given to me, or only some of them (and how do I know)?
Do I have to create my own OpenGL context and use that to draw to, or can I use a "current" OpenGL context? Do I have to re-create the context every time a draw event is sent?
Basically my question is, given a situation where I am sent "Draw" events, how often do I attempt to create OpenGL contexts and how does this relate to the creation/destruction cycle of device contexts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,将单个 OpenGL 上下文视为一个窗口通常是安全的,尤其是在 Windows 上。
设备上下文(通常)将映射到窗口句柄(HWND)。 它实际上是一个 DC(HDC 是句柄),但通常您将一个 HDC 与一个 HWND 相关联。 在 Windows 中,您将根据屏幕上要渲染的窗口创建一个要使用的窗口。
通常,您将在应用程序的整个运行时重用此设备上下文。 如果要渲染到不同的窗口中,则需要为新窗口句柄生成设备上下文 (HDC)。 此外,离屏渲染有点不同,因为您也需要为此创建兼容的设备上下文。
至于您的问题:
1)当您创建要进行渲染的窗口时,您将获取设备上下文,并在该窗口的生命周期内使用它。
2) 您需要始终使用为要渲染的窗口创建的设备上下文。
In general, it's usually safe to think of a single OpenGL context as a window, especially on windows.
A device context will (typically) map to an Window Handle (HWND). It's actually a DC (HDC is the handle), but normally you associate one HDC with a single HWND. In Windows, you'll create a window to use based off the window on screen where you want to render.
Typically, you'll reuse this device context for the entire runtime of your application. If you want to render into a different window, you'll need to generate a device context (HDC) for the new window handle. Also, offscreen rendering is a bit different, since you'd create a compatible device context for that, as well.
As for your questions:
1) When you create the window where you want to do the rendering, you'll grab a device context, and use it for the lifetime of that window.
2) You'll want to always use the device context created for the window where you are rendering.