在Visual Studio中使用GLUT库,没有错误,但输出控制台不起作用
Using GLUT library in Visual Studio, no error, but output console doesn't work
我正试图将GLUT用于特定的openGL项目。我已经把glut32.dll/glut.h/glut32.lib放在他们需要的目录中。在Visual Studio中将源文件添加到项目后,当我点击debug/run时,它不会显示任何错误。我使用的源代码是一个旋转的彩色立方体。现在,在进行调试之后,输出控制台确实显示了彩色立方体,但只显示了一瞬间,这是不应该发生的。
我使用的代码:
#include <GL/glut.h>
#define window_width 640
#define window_height 480
// Main loop
void main_loop_function() {
// Z angle
static float angle;
// Clear color (screen)
// And depth (used internally to block obstructed objects)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Load identity matrix
glLoadIdentity();
// Multiply in translation matrix
glTranslatef(0, 0, -10);
// Multiply in rotation matrix
glRotatef(angle, 0, 0, 1);
// Render colored quad
glBegin(GL_QUADS);
glColor3ub(255, 000, 000);
glVertex2f(-1, 1);
glColor3ub(000, 255, 000);
glVertex2f(1, 1);
glColor3ub(000, 000, 255);
glVertex2f(1, -1);
glColor3ub(255, 255, 000);
glVertex2f(-1, -1);
glEnd();
// Swap buffers (color buffers, makes previous render visible)
glutSwapBuffers();
// Increase angle to rotate
angle += 0.25;
}
// Initialze OpenGL perspective matrix
void GL_Setup(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
gluPerspective(45, (float) width / height, .1, 100);
glMatrixMode(GL_MODELVIEW);
}
// Initialize GLUT and start main loop
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("GLUT Example!!!");
glutIdleFunc(main_loop_function);
GL_Setup(window_width, window_height);
glutMainLoop();
}
有人能告诉我是什么原因造成的吗?代码没有任何错误。由于输出只显示了半秒钟,我假设GLUT文件已经正确放置。那么,是什么原因导致控制台在一秒钟内消失?
使用glut时,不应该从附加到glutIdleFunc
的函数中提取内容,而应该从glutDisplayFunc
中提取内容。
使用glutDisplayFunc(main_loop_function);
并创建一个新的定时器函数来执行angle += 0.25;
,并将回调与glutTimerFunc(...)
连接起来,以定时方式旋转,而不是在每次重绘时旋转,这可能不会定期发生。
相关文章:
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 显示错误输出的简单数组排序程序
- 我的代码中有错误,未声明的标识符
- Dijkstra 中的错误输出
- C++ LeetCode #377 的 DP 解决方案中,此代码是否有错误?
- 自定义runtime_error,如果我在 #what 中使用#c_str(),则错误输出始终为空
- 流字符串错误输出
- 试图建立银行管理系统,但有错误
- 我试图创建临时对象的方式有错误吗
- 我的随机生成器是否不工作,或者我决定人/骨架是否击中对手的方式是否有错误
- 我在OpenCV中有错误的"approxPolyDP(ROI_Vertices, ROI_Poly, 1.0, true)"
- C++ 异常处理错误输出
- 我在这个 C++ 代码上有错误吗?
- 是视觉C++优化器错误还是我的代码中有错误?
- C ++中的错误输出,i = 65530,当我们打印i的值时,它给出-6状态65530
- SSE和iostream:浮点类型的错误输出
- C++将 popen 错误输出读取为字符串
- 为什么一个简单的C++程序会有错误的结果?是 #define 引起的吗?
- 对此有错误
- C :数据文件有错误:预期的无限制ID