C++ 无法创建GLFW窗口

C++ 无法创建GLFW窗口,c++,opengl,glfw,C++,Opengl,Glfw,我想创建共享GLFW窗口,但GLFW无法创建第二个窗口 #include <thread> #include <mutex> #include <condition_variable> GLFWwindow *wnd = nullptr; bool wnd2created = false; std::mutex mtx; std::condition_variable cv; void wnd2func( void ) { GLFWwindow *

我想创建共享GLFW窗口,但GLFW无法创建第二个窗口

#include <thread>
#include <mutex>
#include <condition_variable>

GLFWwindow *wnd = nullptr;
bool wnd2created = false;
std::mutex mtx;
std::condition_variable cv;

void wnd2func( void )
{
    GLFWwindow *wnd2 = glfwCreateWindow( 800, 600, "window 2", nullptr, wnd );

    {
        std::unique_lock<std::mutex> lck(mtx);
        wnd2created = true;
        cv.notify_one();
    }

    if (wnd2 == nullptr)
        return;

    glfwMakeContextCurrent(wnd2);

    // [...]
}

int main()
{
    // [...]

    wnd = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", 0, nullptr);
    if (wnd2 == nullptr)
        return -1;

    std::thread wnd2thread(wnd2func); 

    {
      std::unique_lock<std::mutex> lck(mtx);
      cv.wait(lck, []() -> bool { return wnd2created; });
    }

    glfwMakeContextCurrent(wnd);

    // [...]
}
我可以创建一个窗口,但不能创建两个窗口

这是我的密码

我想在另一个线程中使用第二个窗口,以便共享它们的上下文

#包括“pch.h”
#包括
#包括
#包括
int SCR_宽度=1920;
int SCR_高度=1080;
int main()
{
glfwInit();
glfwWindowHint(GLFW_上下文_版本_专业,3);
glfwWindowHint(GLFW_上下文_版本_小调,3);
glfwWindowHint(GLFW_OPENGL_配置文件、GLFW_OPENGL_核心配置文件);
//glfw窗口创建
// --------------------
GLFWwindow*sharedWindow=NULL;
GLFWwindow*window=glfwCreateWindow(SCR_宽度,SCR_高度,“LearnOpenGL”,0,sharedWindow);
如果(窗口==NULL)
{

std::cout
share
是一个输入参数。请参阅。
创建第一个窗口,并为第二个窗口第二次调用
glfwCreateWindow

GLFWwindow*wnd=glfwCreateWindow(SCR_宽度,SCR_高度,“LearnOpenGL”,0,nullptr);
GLFWwindow*wnd2=glfwCreateWindow(SCR_宽度,SCR_高度,“窗口2”,0,窗口);
该窗口甚至可以在单独的线程中创建,但请注意,在创建第二个窗口时,必须确保第一个窗口的OpenGL上下文不是最新的

#include <thread>
#include <mutex>
#include <condition_variable>

GLFWwindow *wnd = nullptr;
bool wnd2created = false;
std::mutex mtx;
std::condition_variable cv;

void wnd2func( void )
{
    GLFWwindow *wnd2 = glfwCreateWindow( 800, 600, "window 2", nullptr, wnd );

    {
        std::unique_lock<std::mutex> lck(mtx);
        wnd2created = true;
        cv.notify_one();
    }

    if (wnd2 == nullptr)
        return;

    glfwMakeContextCurrent(wnd2);

    // [...]
}

int main()
{
    // [...]

    wnd = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", 0, nullptr);
    if (wnd2 == nullptr)
        return -1;

    std::thread wnd2thread(wnd2func); 

    {
      std::unique_lock<std::mutex> lck(mtx);
      cv.wait(lck, []() -> bool { return wnd2created; });
    }

    glfwMakeContextCurrent(wnd);

    // [...]
}
#包括
#包括
#包括
GLFWwindow*wnd=nullptr;
bool wnd2created=false;
std::互斥mtx;
std::条件变量cv;
作废wnd2func(作废)
{
GLFWwindow*wnd2=glfwCreateWindow(800600,“窗口2”,空PTR,wnd);
{
标准:唯一锁定lck(mtx);
wnd2created=true;
cv.通知_one();
}
如果(wnd2==nullptr)
返回;
glfwMakeContextCurrent(wnd2);
// [...]
}
int main()
{
// [...]
wnd=glfwCreateWindow(SCR_宽度,SCR_高度,“LearnOpenGL”,0,nullptr);
如果(wnd2==nullptr)
返回-1;
std::线程wnd2thread(wnd2func);
{
标准:唯一锁定lck(mtx);
cv.wait(lck,[]()->bool{return wnd2created;});
}
glfwMakeContextCurrent(wnd);
// [...]
}

共享
是一个输入参数。请参阅。
创建第一个窗口,并为第二个窗口第二次调用
glfwCreateWindow

GLFWwindow*wnd=glfwCreateWindow(SCR_宽度,SCR_高度,“LearnOpenGL”,0,nullptr);
GLFWwindow*wnd2=glfwCreateWindow(SCR_宽度,SCR_高度,“窗口2”,0,窗口);
该窗口甚至可以在单独的线程中创建,但请注意,在创建第二个窗口时,必须确保第一个窗口的OpenGL上下文不是最新的

#include <thread>
#include <mutex>
#include <condition_variable>

GLFWwindow *wnd = nullptr;
bool wnd2created = false;
std::mutex mtx;
std::condition_variable cv;

void wnd2func( void )
{
    GLFWwindow *wnd2 = glfwCreateWindow( 800, 600, "window 2", nullptr, wnd );

    {
        std::unique_lock<std::mutex> lck(mtx);
        wnd2created = true;
        cv.notify_one();
    }

    if (wnd2 == nullptr)
        return;

    glfwMakeContextCurrent(wnd2);

    // [...]
}

int main()
{
    // [...]

    wnd = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", 0, nullptr);
    if (wnd2 == nullptr)
        return -1;

    std::thread wnd2thread(wnd2func); 

    {
      std::unique_lock<std::mutex> lck(mtx);
      cv.wait(lck, []() -> bool { return wnd2created; });
    }

    glfwMakeContextCurrent(wnd);

    // [...]
}
#包括
#包括
#包括
GLFWwindow*wnd=nullptr;
bool wnd2created=false;
std::互斥mtx;
std::条件变量cv;
作废wnd2func(作废)
{
GLFWwindow*wnd2=glfwCreateWindow(800600,“窗口2”,空PTR,wnd);
{
标准:唯一锁定lck(mtx);
wnd2created=true;
cv.通知_one();
}
如果(wnd2==nullptr)
返回;
glfwMakeContextCurrent(wnd2);
// [...]
}
int main()
{
// [...]
wnd=glfwCreateWindow(SCR_宽度,SCR_高度,“LearnOpenGL”,0,nullptr);
如果(wnd2==nullptr)
返回-1;
std::线程wnd2thread(wnd2func);
{
标准:唯一锁定lck(mtx);
cv.wait(lck,[]()->bool{return wnd2created;});
}
glfwMakeContextCurrent(wnd);
// [...]
}

非常感谢您继续回答,现在它很有魅力。非常感谢您继续回答,现在它很有魅力