2014-09-19 16 views
0

我想爲特定的OpenGL項目使用GLUT。我已經把glut32.dll/glut.h/glut32.lib放在他們需要的目錄中。將源文件添加到Visual Studio中的項目後,當我點擊debug/run時,它不會顯示任何錯誤。我使用的源代碼是旋轉的彩色立方體。現在,在進行調試後,輸出控制檯確實顯示了彩色立方體,但僅在一瞬間發生,這不應該發生。在Visual Studio中使用GLUT庫,沒有錯誤,但輸出控制檯不起作用

的代碼我使用:

#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文件已經正確放置。那麼,什麼可能導致控制檯在一秒之內離開?

回答

0

有了供應過剩,您不應該從附加到glutIdleFunc的功能中抽取東西,而應該從glutDisplayFunc中抽取。

使用glutDisplayFunc(main_loop_function);並創建一個新的計時器功能做angle += 0.25;glutTimerFunc(...)連接回調的定時方式,而不是在每個重繪旋轉,這可能不是在定期發生。

相關問題