简体   繁体   English

使用 GLFW 生成 window,背景颜色不变

[英]Generated window with GLFW, background color not changing

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.我正在尝试更改由 OpenGL/GLFW 生成的 window 中的背景颜色,并且我使用的代码与 GLFW 文档中的代码类似。 I'm on Ubuntu 20.04 and the window background is always black, no matter the parameters in the glClearColor() function.我在 Ubuntu 20.04 和 window 背景总是黑色的,无论 glClearColor() function 中的参数如何。

I tried this on Windows 10 using VS and it worked perfectly, but on Ubuntu it's not working at all.我在 Windows 10 上使用 VS 试过这个,它工作得很好,但在 Ubuntu 上它根本不起作用。 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.我还关注了 Cherno 的 Sparky游戏引擎系列,并尝试将 glClear() function 封装在 class 方法中,但这并没有改变任何东西。

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.我应该得到一个红色背景的 window,但它是黑色的。

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.作为附加元素,我还使用 CMake 来帮助配置和构建项目(我知道这太过分了),我相信 clang 是用于该项目的编译器。 Maybe that influences the result.也许这会影响结果。

You need an appropriate loader after glfwMakeContextCurrent(window) .glfwMakeContextCurrent(window)之后你需要一个合适的加载器。

If you are using glad, you should call gladLoadGL(glfwGetProcAddress) after glfwMakeContextCurrent(window) as the official doc suggests here .如果您正在使用 glad,您应该按照官方文档在此处建议的那样在glfwMakeContextCurrent(window) ) 之后调用gladLoadGL(glfwGetProcAddress)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM