2013-07-18 49 views
-3

當我編譯它時,它會創建一個空白的blac屏幕,如果我從任一位置取出大量交換緩衝區,或者同時出現空白屏幕,那麼問題是什麼?這只是顯示一個空白的黑屏,如果我不把glutSwapBuffers()放在兩個地方,它顯示一個空白的白屏?

#ifdef __APPLE__ 
#include <GLUT/glut.h> 
#else 
#include <GL/glut.h> 
#endif 

#include <stdlib.h> 



void ProcessSpecialKeys(int key, int x, int y){ 

    switch(key){ 

     case GLUT_KEY_RIGHT: 

exit(0); 

     case GLUT_KEY_LEFT: 

exit(0); 

     case GLUT_KEY_UP: 

exit(0); 

     case GLUT_KEY_DOWN: 

exit(0); 

     default: 

exit(0); 

    } 

} 




void renderPrimitive(void){ 

    glBegin(GL_QUADS); 
    glVertex3f(-1.0f,-1.0f,0.0f); 
    glVertex3f(-1.0f,1.0f,0.0f); 
    glVertex3f(1.0f,1.0f,0.0f); 
    glVertex3f(1.0f,-1.0f,0.0f); 
    glEnd(); 
} 

void display(void){ 

    glTranslatef(0.0f,0.0f,-0.5f); 
    renderPrimitive(); 
    glutSwapBuffers(); 
} 

int main(int argc, char **argv) { 

glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); 
glutInitWindowPosition(100,100); 
glutInitWindowSize(320,320); 
glutCreateWindow("Dimension"); 
glutDisplayFunc(display); 

    glLoadIdentity(); 

    glutSpecialFunc(ProcessSpecialKeys); 

glutMainLoop(); 

return 0; 
} 
+4

[這個問題與你的其他問題有什麼不同?](http://stackoverflow.com/questions/17730333/i-am-new-to-glut-and-it-is-just-rendering-a- blank-white-screen-help-please) – Borgleader

+0

我只看到一個'glutSwapBuffers()'調用。 。 。 –

+0

我看不到模型視圖和投影矩陣的任何設置。這很可能是四分之一像素在相機後面。使用例如相機設置您的相機和投影。 'gluLookAt()'和'gluPerspective()'。 – Angew

回答

0

在開始新繪製之前,您需要清除緩衝區。換句話說,在你的顯示功能做到這一點:

void display(void){ 
    glClear(GL_COLOR_BUFFER_BIT); // <----- 
    glTranslatef(0.0f,0.0f,-0.5f); 
    renderPrimitive(); 
    glutSwapBuffers(); 
} 

和閱讀功能glClearColor

EDITED

這是不正確的地方,但要改變背景顏色,做這樣的:

void display(void){ 
    glClearColor(0.5, 0.5, 0.5, 1); 
    glClear(GL_COLOR_BUFFER_BIT); // <----- 
    glScalef(0.5f,0.5f,0.5f); // Your Quad is taking all the framebuffer 
    renderPrimitive(); 
    glutSwapBuffers(); 
} 
+0

但它仍然不會工作... – Josh

-2

試試這個:

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

void ProcessSpecialKeys(int key, int x, int y) 
{ 
    switch(key) 
    { 
    case GLUT_KEY_RIGHT: 
    case GLUT_KEY_LEFT: 
    case GLUT_KEY_UP: 
    case GLUT_KEY_DOWN: 
     exit(0); 
     break; 
    default: 
     exit(0); 
     break; 
    } 
} 

void renderPrimitive() 
{ 
    glBegin(GL_QUADS); 
    glVertex3f(-1.0f,-1.0f,0.0f); 
    glVertex3f(-1.0f,1.0f,0.0f); 
    glVertex3f(1.0f,1.0f,0.0f); 
    glVertex3f(1.0f,-1.0f,0.0f); 
    glEnd(); 
} 

void display() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(-2, 2, -2, 2, -1, 1); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glPushMatrix(); 
    glTranslatef(0.0f,0.0f,-0.5f); 
    renderPrimitive(); 
    glPopMatrix(); 

    glutSwapBuffers(); 
} 

int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); 
    glutInitWindowPosition(100,100); 
    glutInitWindowSize(320,320); 
    glutCreateWindow("Dimension"); 
    glutDisplayFunc(display); 
    glutSpecialFunc(ProcessSpecialKeys); 
    glutMainLoop(); 
    return 0; 
} 
+0

它的工作表示感謝,我覺得這麼愚蠢,雖然我只是一個初學者。 C: – Josh

0

試試glColor3f(0.5f,0.5f,0.5f);並刪除glClearColor(0.5,0.5,0.5,1);