CreateDC 和 wglMakeCurrent 的问题

发布于 2024-09-14 23:08:33 字数 1099 浏览 2 评论 0原文



PIXELFORMATDESCRIPTOR pfd = { /* otherwise fine for a window with 32-bit color */ };

HDC hDC = CreateDC(TEXT("Display"),NULL,NULL,NULL); // always OK

int ipf = ChoosePixelFormat(hDC,&pfd); // always OK

SetPixelFormat(hDC,ipf,&pfd); // always OK

HGLRC hRC = wglCreateContext(hDC); // always OK

wglMakeCurrent(hDC,hRC); // ! read error: 0xbaadf039 (debug, obviously)

但以下内容适用于相同的 hRC:


wglMakeCurrent(hSomeWindowDC,hRC);

以上是 Windows OpenGL 3.0+ 初始化系统的一部分。

为了美观,我试图避免创建虚拟窗口。

我以前从未使用过 CreateDC,所以也许我错过了一些东西。

编辑: hSomeWindowDC 将指向具有适当像素格式的窗口 DC。


更多信息:

我希望创建一个独立于窗口的 OpenGL 渲染上下文。

由于选择的答案,似乎我需要使用一个虚拟窗口(其实没什么大不了的,只是一个传递的句柄)。

为什么我要这样做:由于可以在同一线程中对具有相同像素格式的多个窗口使用相同的渲染上下文,因此可以创建渲染上下文(实际上,只是与 gl 相关的对象的容器) )独立于特定窗口。通过这种方式,可以在图形和 UI 初始化之间创建清晰的分离。

上下文的最初目的不是用于渲染(尽管我相信可以使用它渲染为纹理)。如果想要更改特定上下文中缓冲区的内容,则只需将所需的上下文对象本身设置为当前对象(因为它带有虚拟窗口,所以这是可能的)。渲染到窗口中很简单:正如上面所暗示的,窗口的 DC 仅需要具有相同的像素格式。只需设置渲染上下文和窗口的 DC 电流,然后进行渲染。

请注意,在撰写本文时,此想法仍在测试中。如果发生这种变化,我将更新这篇文章(或者如果我记得:P)。



PIXELFORMATDESCRIPTOR pfd = { /* otherwise fine for a window with 32-bit color */ };

HDC hDC = CreateDC(TEXT("Display"),NULL,NULL,NULL); // always OK

int ipf = ChoosePixelFormat(hDC,&pfd); // always OK

SetPixelFormat(hDC,ipf,&pfd); // always OK

HGLRC hRC = wglCreateContext(hDC); // always OK

wglMakeCurrent(hDC,hRC); // ! read error: 0xbaadf039 (debug, obviously)

But the following works with the same hRC:


wglMakeCurrent(hSomeWindowDC,hRC);

The above is part of an OpenGL 3.0+ initialization system for Windows.

I am trying to avoid creating a dummy window for the sake of aesthetics.

I have never used CreateDC before, so perhaps I've missed something.

edit: hSomeWindowDC would point to a window DC with an appropriate pixel format.

More info:

I wish to create a window-independent OpenGL rendering context.

Due to the answer selected, it seems I need to use a dummy window (not really a big deal, just a handle to pass around all the same).

Why I would want to do this: Since it is possible to use the same rendering context for multiple windows with the same pixel format in the same thread, it is possible to create a rendering context (really, just a container for gl-related objects) that is independent of a particular window. In this way, one can create a clean separation between the graphics and UI initializations.

The purpose of the context initially isn't for rendering (although I believe one could render into textures using it). If one wanted to change the contents of a buffer within a particular context, the desired context object itself would just need to be made current (since it's carrying the dummy window around with it, this is possible). Rendering into a window is simple: As implied by the above, the window's DC only needs to have the same pixel format. Simply make the rendering context and the window's DC current, and render.

Please note that, at the time of this writing, this idea is still in testing. I will update this post should this change (or if I can remember :P ).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如何视而不见 2024-09-21 23:08:33

15 年前读过佩措尔德的著作后,我的一个休眠脑细胞又重新焕发了生机。来自 CreateDC() 的 DC 受到限制。非常适合获取有关显示设备、测量等信息。不适合用作普通绘画 DC。您几乎肯定需要 GetDC()。

I've got a dormant brain cell from reading Petzold 15 years ago that just sprang back to life. The DC from CreateDC() is restricted. Good for getting info about the display device, measurement, that sort of stuff. Not good to use as a regular painting DC. You almost certainly need GetDC().

桃酥萝莉 2024-09-21 23:08:33

我当前的 OpenGL 3+ 初始化例程不需要虚拟窗口。您可以简单地尝试创建第二个 RC 并使用真实窗口的 DC 使其成为当前窗口。查看 OpenGL wiki 教程:OpenGL 3.1 第一个三角形(C++/Win)

My current OpenGL 3+ initialization routine doesn't require a dummy window. You can simply attempt to make a second RC and make it current using the DC of the real window. Take a look at the OpenGL wiki Tutorial: OpenGL 3.1 The First Triangle (C++/Win)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文