我最近将我的电脑升级到了Windows 10,并安装了Visual Studio 2015。我尝试在Visual Studio2015中编写一个"Hello OpenGL“程序,项目构建成功,但它以代码1退出。我得到的只是创建的窗口很快就出现和消失了。下面是我的代码:
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello OpenGL");
glutMainLoopEvent();
return 0;
}如上所述,项目成功构建,下面是构建重用:
1>------ Build started: Project: HelloGL, Configuration: Debug Win32 ------
1> main.cpp
1> HelloGL.vcxproj -> D:\OpenGL Projects\HelloGL\Debug\HelloGL.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========但是当我按F5调试程序时,它的结果让我灰心丧气:
The thread 0x23d4 has exited with code 1 (0x1).
The thread 0x20b8 has exited with code 1 (0x1).
The thread 0x10d0 has exited with code 1 (0x1).
The program '[7040] HelloGL.exe' has exited with code 1 (0x1).发布于 2015-08-06 20:51:26
首先,感谢给我回复的人。我已经找出了问题所在,我所需要做的就是为窗口注册一个回调函数,下面是运行代码:
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
// myDisplay
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello OpenGL");
// Register a callback function for the window's repainting event
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}发布于 2015-08-06 16:39:14
调用glutMainLoop而不是glutMainLoopEvent。
后面的glutMainLoopEvent是一个特定于FreeGLUT的函数,它允许将GLUT事件分派放在自定义编写的循环中;因此,必须从循环中调用它,并由您决定何时退出程序。
glutMainLoop实现自己的主循环,并在最后一个窗口关闭时退出程序。
https://stackoverflow.com/questions/31845764
复制相似问题