簡體   English   中英

使用 GLFW 生成 window,背景顏色不變

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

我正在嘗試更改由 OpenGL/GLFW 生成的 window 中的背景顏色,並且我使用的代碼與 GLFW 文檔中的代碼類似。 我在 Ubuntu 20.04 和 window 背景總是黑色的,無論 glClearColor() function 中的參數如何。

我在 Windows 10 上使用 VS 試過這個,它工作得很好,但在 Ubuntu 上它根本不起作用。 不會生成任何錯誤消息。

我還關注了 Cherno 的 Sparky游戲引擎系列,並嘗試將 glClear() function 封裝在 class 方法中,但這並沒有改變任何東西。

這是整個代碼:

#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;
}

我應該得到一個紅色背景的 window,但它是黑色的。

作為附加元素,我還使用 CMake 來幫助配置和構建項目(我知道這太過分了),我相信 clang 是用於該項目的編譯器。 也許這會影響結果。

glfwMakeContextCurrent(window)之后你需要一個合適的加載器。

如果您正在使用 glad,您應該按照官方文檔在此處建議的那樣在glfwMakeContextCurrent(window) ) 之后調用gladLoadGL(glfwGetProcAddress)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM