带有GLFW的生成窗口,背景颜色不更改

发布于 2025-01-20 23:45:37 字数 1464 浏览 2 评论 0原文

我正在尝试更改 OpenGL/GLFW 生成的窗口中的背景颜色,并且我使用的代码与 GLFW 文档中的代码类似。我使用的是 Ubuntu 20.04,无论 glClearColor() 函数中的参数如何,窗口背景始终为黑色。

我在 Windows 10 上使用 VS 尝试过,它运行得很好,但在 Ubuntu 上它根本不起作用。不会生成错误消息。

我还关注了The Cherno's Sparky游戏引擎系列,并尝试将glClear()函数封装在类方法中,但这并没有改变任何东西。

这是整个代码:

#include <iostream>
#include <GL/gl.h>
#include <GLFW/glfw3.h>

int main(int argc, char *argv[])
{
    std::cout << "Hello world!" << std::endl;
    if (!glfwInit())
    {
        // Initialization failed
        exit(EXIT_FAILURE);
    }

    GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
    if (!window)
    {
        // Window or OpenGL context creation failed
        std::cout << "Error creating window!" << std::endl;
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

    while (!glfwWindowShouldClose(window)) 
    {
        int width, height;

        glfwGetFramebufferSize(window, &width, &height);

        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);



        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
    exit(EXIT_SUCCESS);

    return 0;
}

我应该得到一个红色背景的窗口,但它是黑色的。

作为附加元素,我还使用 CMake 来帮助配置和构建项目(我知道这相当过分),并且我相信 clang 是用于该项目的编译器。也许这会影响结果。

I'm trying to change the background color in a window generated by OpenGL/GLFW and I'm using a similar code than the one in the GLFW docs. I'm on Ubuntu 20.04 and the window background is always black, no matter the parameters in the glClearColor() function.

I tried this on Windows 10 using VS and it worked perfectly, but on Ubuntu it's not working at all. No error messages are generated.

I also followed The Cherno's Sparky game engine series, and tried encapsulating the glClear() function in a class method, but this didn't change anything.

This is the whole code:

#include <iostream>
#include <GL/gl.h>
#include <GLFW/glfw3.h>

int main(int argc, char *argv[])
{
    std::cout << "Hello world!" << std::endl;
    if (!glfwInit())
    {
        // Initialization failed
        exit(EXIT_FAILURE);
    }

    GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
    if (!window)
    {
        // Window or OpenGL context creation failed
        std::cout << "Error creating window!" << std::endl;
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

    while (!glfwWindowShouldClose(window)) 
    {
        int width, height;

        glfwGetFramebufferSize(window, &width, &height);

        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);



        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
    exit(EXIT_SUCCESS);

    return 0;
}

I'm supposed to get a window with a red background but it's black.

As additional elements, I'm also using CMake to help configuring and building the project (quite overkill I know), and I believe clang is the compiler used for the project. Maybe that influences the result.

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

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

发布评论

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

评论(1

蓝眼泪 2025-01-27 23:45:37

glfwmakecontextcurrent(window)之后,您需要适当的加载程序。

如果您使用的是GLAIN,则应调用gladloadgl(glfwgetProcAddress) glfwmakecontextcurrent(window),因为官方文档建议在这里

You need an appropriate loader after glfwMakeContextCurrent(window).

If you are using glad, you should call gladLoadGL(glfwGetProcAddress) after glfwMakeContextCurrent(window) as the official doc suggests here.

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