我正在尝试更改OpenGL/GLFW生成的窗口中的背景颜色,我使用的代码与GLFW文档中的代码类似。我使用的是Ubuntu 20.04,窗口背景始终为黑色,无论glClearColor()函数中的参数如何。
我用VS在Windows10上试过了,效果很好,但是在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是用于项目的编译器。
1条答案
按热度按时间n3ipq98p1#
在
glfwMakeContextCurrent(window)
之后,您需要一个适当的加载程序。如果您使用glad,则应该按照这里的官方文档建议,在
glfwMakeContextCurrent(window)
之后调用gladLoadGL(glfwGetProcAddress)
。