gpt4 book ai didi

c++ - 如何使用 OpenGL 在 Windows 上的同一个应用程序中绘制两个单独的 3D 窗口?

转载 作者:可可西里 更新时间:2023-11-01 11:55:13 25 4
gpt4 key购买 nike

我正在 Windows 上使用 C++ 的第 3 方程序中实现插件。

第 3 方程序有一个使用 OpenGL 显示 3D 图形的窗口。但是我需要插件来创建另一个窗口,该窗口也使用 OpenGL 显示 3D 图形。

我是否需要为我的窗口创建一个新的 OpenGL 渲染上下文,或者有什么方法可以“重用”第 3 方程序使用的 OpenGL 渲染上下文?

我假设我必须创建一个新的 OpenGL 渲染上下文并尝试了以下操作:

// create a rendering context  
hglrc = wglCreateContext (hdc);

// make it the calling thread's current rendering context
wglMakeCurrent (hdc, hglrc);

但是最后一个函数失败了。读书the documentation of wglMakeCurrent 我注意到

A thread can have one current rendering context. A process can have multiple rendering contexts by means of multithreading.

这是否意味着我的窗口需要在与第 3 方程序不同的线程中运行?

最佳答案

您没有发布 wglMakeCurrent() 生成的错误代码,所以我不会猜测原因。然而,这不是绑定(bind)本身。句子“A thread can have one current rendering context”的意思是,新上下文将“替换”旧上下文并成为当前上下文。我不知道您为什么要尝试将两个上下文设置为当前(或运行另一个线程),但这不是可行的方法。除非绝对必要,否则避免在渲染中使用多线程。所以,回答你的问题:

是的,您可以“重用”OpenGL 渲染上下文。

为什么,你可能会问?渲染上下文是为特定的设备上下文(HDC)创建的,这是每个窗口(HWND)的独有属性!那这怎么可能呢?!

好吧,由于函数原型(prototype),这似乎在某种程度上是不可能的:

    HWND my_window = CreateWindow(...);    HDC my_dc = GetDC(my_new_window);    //Setup pixel format for 'my_dc'...    HGLRC my_rc = wglCreateContext(my_dc);    wglMakeCurrent(my_dc, my_rc);

这真的让你认为渲染上下文绑定(bind)到这个特定的设备上下文并且只对它有效。但事实并非如此。关键部分是注释(设置像素格式)。渲染上下文是为特定的 DC 类 创建的,更准确地说:为具有相同像素格式的 DC 创建。所以下面的代码是完全有效的:

    //window_1 = main window, window_2 = your window    HDC dc_1 = GetDC(window_1);    Set_pixel_format_for_dc_1(); //Usual stuff    HGLRC rc = wglCreateContext(dc_1);    wglMakeCurrent(dc_1, rc);    ultra_super_draw();    //.....    HDC dc_2 = GetDC(window_2);    //Get dc_1's PF to make sure it's compatible with rc.    int pf_index = GetPixelFormat(dc_1);    PIXELFORMATDESCRIPTOR pfd;    ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));    DescribePixelFormat(dc_1, pf_index, sizeof(PIXELFORMATDESCRIPTOR), &pfd);    SetPixelFormat(dc_2, pf_index, &pfd);    wglMakeCurrent(dc_2, rc);    another_awesome_render();    wglMakeCurrent(NULL, NULL);

如果你还不服气,MSDN :

wglMakeCurrent(hdc, hglrc): The hdc parameter must refer to a drawing surface supported by OpenGL. It need not be the same hdc that was passed to wglCreateContext when hglrc was created, but it must be on the same device and have the same pixel format.

我猜你已经很熟悉这些调用了。现在,我不知道你的渲染必须满足什么条件,但如果没有额外的要求,我看不出有什么困难:

    HDC my_dc = Create_my_DC();    //...    void my_new_render    {        //Probably you want to save current binding:        HDC current_dc = wglGetCurrentDC();        HGLRC current_context = wglGetCurrentContext();        wglMakeCurrent(my_dc, current_context);        MyUltraSuperRender(...);        wglMakeCurrent(current_dc, current_context);    }

希望这有帮助:)

关于c++ - 如何使用 OpenGL 在 Windows 上的同一个应用程序中绘制两个单独的 3D 窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18987658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com