2011-02-06 18 views
1

問題,當我嘗試將其編譯後運行這段代碼,我只拿到了沒有內容的窗口邊框,所以哪裏是錯誤?與GLUT

注:我使用Ubuntu和gcc編譯

gcc -lglut Simple.c -o Simple 

The output

#include <GL/glut.h> // Header File For The GLUT Library 
#include <GL/gl.h> // Header File For The OpenGL Library 
#include <GL/glu.h> // Header File For The GLu Library 

void SetupRC(void); 
void RenderScene(void); 
void ChangeSize(GLsizei w, GLsizei h); 

// Called to draw scene 
void RenderScene(void) 
{ 
    // Clear the window with current clearing color 
    glClear(GL_COLOR_BUFFER_BIT); 

    // set the color to these values 
    //   R G  B 
    glColor3f(56.0f, 19.0f,68.0f); 

    // Draw a filled rectangle with current color 
    glRectf(-25.0f, 50.0f, 50.0f, -25.0f); 

    // Flush drawing commands 
    glFlush(); 
} 

int main(int argc, char* argv[]) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA); 
    glutInitWindowPosition(400,200); 
    glutInitWindowSize(640,468); 
    glutCreateWindow("Simple"); 
    glutDisplayFunc(RenderScene); 
    glutReshapeFunc(ChangeSize); 

    SetupRC(); 
    glutMainLoop(); 

    return 0; 
} 

// Setup the rendering state 
void SetupRC(void) 
{ 
    glClearColor(0.0f, 2.0f, 1.0f, 0.0f); 
} 

// Handling window resizing 
void ChangeSize(GLsizei w, GLsizei h) 
{ 
    GLfloat aspectRatio; 

    // Prevent divide by zero 
    if (h == 0) 
     h = 1; 

    // Set Viewport to window dimensions 
    glViewport(0, 0, w, h); 

    // Reset coordinate system 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    // Establish clipping volume (left, right, bottom, top, near, far) 
    aspectRatio = (GLfloat) w/(GLfloat) h; 
    if (w <= h) { 
     glOrtho(-100.0, 100.0, -100/aspectRatio, 100.0/aspectRatio, 1.0, -1.0); 
    } 
    else 
     glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
} 
+3

浮法顏色去從0.0到1.0。無符號字符255映射到浮點數1.0。這可能會幫助你一些。 – 2011-02-06 16:30:40

+0

謝謝你,我改變了值,但它並沒有幫助 – 2011-02-06 16:39:36

回答

2

嘗試添加glutSwapBuffers()RenderScene()結束。

0

一兩件事。您有一個深度緩衝區,所以您還應該將GL_DEPTH_BUFFER_BIT添加到清除呼叫。

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);