2016-07-27 80 views
2

我的GLUT程序行爲非常出乎意料。它所做的一切(它應該做的)就是將屏幕清除爲藍色,並畫出一個遠離距離的紫色三角形。GLUT只運行一次顯示回調,不輸出到終端

首先,這裏是我的代碼:

#include <GL/glut.h> 
#include <iostream> 

class Ground{ 
private: 
    float zpos; 
public: 
    Ground(){ 
     zpos = -5.0f; 
    } 
    void Render(){ 
     glColor3f(1.0,0.0,1.0); 
     glBegin(GL_TRIANGLES); 
     glVertex3f(0,0,zpos); 
     glVertex3f(0,1,zpos); 
     glVertex3f(1,1,zpos); 
     glEnd(); 
     zpos -= 0.1f; 
    } 
}; 

class Skybox{ 
public: 
    Skybox(){ 

    } 
    void Render(){ 
     glClearColor(0.5, 0.8, 1.0, 1.0); 
     glClear(GL_COLOR_BUFFER_BIT); 
    } 
}; 

Skybox s; 
Ground g; 
void init(void) 
{ 
    s = Skybox(); 
    g = Ground(); 
    glShadeModel (GL_SMOOTH); 
    glMatrixMode(GL_MODELVIEW); 
    glDepthFunc(GL_LEQUAL); 
    glViewport(0,0,glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT)); 
    glEnable(GL_DEPTH_TEST); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(60,glutGet(GLUT_SCREEN_WIDTH)/glutGet(GLUT_SCREEN_HEIGHT),0.1,100); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
} 

void display(void) 
{ 
    std::cout << "Hi"; 
    glClearColor(0,0,0,0); 
    glClearDepth(1); 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    s.Render(); 
    g.Render(); 
    glLoadIdentity(); 
    glutSwapBuffers(); 
} 

int main(int argc, char** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); 
    glutInitWindowSize(800, 600); 
    glutCreateWindow ("Umm"); 
    init(); 
    glutDisplayFunc(display); 
    glutMainLoop(); 
    return 0; 
} 

但是,代碼沒有按照預期工作 - 紫色三角形不會退回到的距離,它保持靜止了,簡單cout沒有按」即使做任何事情! (但不知何故,所有的顯示功能後的代碼被執行,但只有一次?)

我在做什麼錯?

回答

1

將計時器回調後重新顯示現在每一次:

void timer(int value) 
{ 
    glutPostRedisplay(); 
    glutTimerFunc(16, timer, 0); 
} 

否則display()將只在操作系統感覺就像是,通常被稱爲after an expose/damage/paint event (like a window resize, or exposing more of the window by moving another window off of it)

一起:

#include <GL/glut.h> 
#include <iostream> 

class Ground{ 
private: 
    float zpos; 
public: 
    Ground(){ 
     zpos = -5.0f; 
    } 
    void Render(){ 
     glColor3f(1.0,0.0,1.0); 
     glBegin(GL_TRIANGLES); 
     glVertex3f(0,0,zpos); 
     glVertex3f(0,1,zpos); 
     glVertex3f(1,1,zpos); 
     glEnd(); 
     zpos -= 0.1f; 
    } 
}; 

class Skybox{ 
public: 
    Skybox(){ 

    } 
    void Render(){ 
     glClearColor(0.5, 0.8, 1.0, 1.0); 
     glClear(GL_COLOR_BUFFER_BIT); 
    } 
}; 

Skybox s; 
Ground g; 
void init(void) 
{ 
    s = Skybox(); 
    g = Ground(); 
    glShadeModel (GL_SMOOTH); 
    glMatrixMode(GL_MODELVIEW); 
    glDepthFunc(GL_LEQUAL); 
    glViewport(0,0,glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT)); 
    glEnable(GL_DEPTH_TEST); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(60,glutGet(GLUT_SCREEN_WIDTH)/glutGet(GLUT_SCREEN_HEIGHT),0.1,100); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
} 

void display(void) 
{ 
    std::cout << "Hi"; 
    glClearColor(0,0,0,0); 
    glClearDepth(1); 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    s.Render(); 
    g.Render(); 
    glLoadIdentity(); 
    glutSwapBuffers(); 
} 

void timer(int value) 
{ 
    glutTimerFunc(16, timer, 0); 
    glutPostRedisplay(); 
} 

int main(int argc, char** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); 
    glutInitWindowSize(800, 600); 
    glutCreateWindow ("Umm"); 
    init(); 
    glutTimerFunc(0, timer, 0); 
    glutDisplayFunc(display); 
    glutMainLoop(); 
    return 0; 
} 
+0

請問爲什麼 「喜」 永遠不會顯示?是因爲它不是主線程的一部分嗎? – acer

+0

@acer:[我的工作很好](http://i.stack.imgur.com/2HN03.png)。不知道你的設置有什麼不同。 – genpfault

相關問題