19,472
社区成员




//glut_loop.cpp
//
#include <GL/freeglut.h>
//
void render(void);
void keyFunc(unsigned char, int, int);
int window_id;
bool keep_running = true;
//
// A barebones GLUT application
//
int main()
{
// Create our OpenGL Window
int fake_argc = 1;
char* fake_argv;
glutInit(&fake_argc, &fake_argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(640, 480);
window_id = glutCreateWindow("GLUT Loop");
// Set the function to handle normal key presses
glutKeyboardFunc(keyFunc);
// Begin the loop
while(keep_running)
{
glutMainLoopEvent();
render();
glutSwapBuffers();
}
// Exit gracefully
glutDestroyWindow(window_id);
//退出函数处理,,,
return 0;
}
//
// This is where you'd draw a frame of your 3D application
//
void render()
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Look on my works, ye Mighty, and despair!
glutWireTeapot( 0.5 );
}
//
// Handler for normal keypressed
//
void keyFunc(unsigned char key, int x, int y)
{
switch (key)
{
case 113: // 'q',
case 81: // 'Q',
case 27: // 'ESC'
keep_running = false;
break;
}
}
lz可以使用freeglut库
和glut基本差不多,不同之处在于:
freeglut和glut类似,一个比较重要的不同点在于freeglut库中的glutMainLoopEvent函数执行一次循环以后会返回控制权,
glut中的glutMainLoop()一旦进入循环就再也不返回