无法在 OpenGL 中随时间更改颜色
Failed to change color over time in OpenGL
我正在使用OpenGL超级圣经第7版学习OpenGL。但是,当我尝试运行第 2 章中的示例时,我发现了一个问题,即颜色没有按预期随时间变化。
这是主要程序:
int main(int argc, char** argv) {
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFWn");
return 1;
}
GLFWwindow* window;
window = glfwCreateWindow(800, 600, "My First OpenGL Project", NULL, NULL);
if (!window)
{
fprintf(stderr, "Failed to open windown");
return 1;
}
glfwMakeContextCurrent(window);
gl3wInit();
bool running = true;
do
{
double current_time = glfwGetTime();
static const GLfloat color[] = {
(float)sin(current_time) * 0.5f + 0.5f,
(float)cos(current_time) * 0.5f + 0.5f,
0.0f,
1.0f };
std::cout << current_time << std::endl;
std::cout << color[0] << std::endl;
glClearBufferfv(GL_COLOR, 0, color);
glfwSwapBuffers(window);
glfwPollEvents();
running &= (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_RELEASE);
running &= (glfwWindowShouldClose(window) != GL_TRUE);
} while (running);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
看起来current_time的值会随着时间的推移而变化,但颜色[0]的值却没有。为什么?
发生这种情况是因为静态变量的初始值设定项仅在初始化变量时调用一次。此外,const 是一个很好的提示,表明这个变量永远不会改变。
因此,如果您希望每个帧都有不同的颜色,请删除static const
并坚持使用
GLfloat color[] = {
(float)sin(current_time) * 0.5f + 0.5f,
(float)cos(current_time) * 0.5f + 0.5f,
0.0f,
1.0f };
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 从持续时间构造std::chrono::system_clock::time_point
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 使用简单类型列表实现的指数编译时间.为什么
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 在已经使用Git的情况下减少编译时间
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 从文本文件中读取时钟时间和事件时间并进行处理
- 具有未知值时的时间复杂性
- 如何减少花费的时间
- 如何在内核C++中使用1920x1080x16M图形或类似的16M颜色?(VGA)
- 如何在24位SDL_Surface上设置像素的颜色
- C++在变量给定的指定时间内关闭电脑
- rcpp函数中的清理时间很长
- C++:floor unix时间戳到UTC月份
- 在特定时间内更改 QLabel 的背景颜色
- OpenGL颜色缓冲每帧添加太多的帧时间