glutDisplayFunc(display);
glutMainLoop();
glut库中的这两行代码无法无限循环,display函数只执行了一次。glutMainLoop()函数,只有改变窗体大小才能触发glutDisplayFunc(display);
我运行程序之后,任意敲击键盘,开启无限循环

#include <stdlib.h>
#include<iostream>
#include<GL/glew.h>
#include<GL/glut.h>
void userInit(); //自定义初始化
void display(void);
GLuint VAO;
GLuint VBO;
unsigned int vertexShader;
unsigned int fragmentShader;
unsigned int shaderProgram;
float vertices[] = {
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
0.0f, 0.5f, 0.0f // top
};
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"uniform vec4 ourColor;\n"
"void main()\n"
"{\n"
" FragColor = ourColor;\n"
/*here we have the input Color. We just write it out to FragColor. FragColor is our own output variable which GL automatically bind to output 0, therefore we don't need to set it up from our C++ side of the code.*/
"}\n\0";
void keyboardFunc(unsigned
无限循环困境: glutDisplayFunc vs. 键盘触发循环

本文探讨了如何在使用GLUT库进行OpenGL编程时,实现一个基于键盘输入而非默认的glutDisplayFunc无限循环,并解释了为何仅通过glutDisplayFunc不能实现无限循环,以及如何通过键盘事件触发自定义的display函数。
最低0.47元/天 解锁文章

1645

被折叠的 条评论
为什么被折叠?



