2011-12-22 128 views
2

爲什麼這段代碼會產生一個黑屏(沒有畫......)?OpenGL黑色屏幕/沒有繪製?

我創建了Pong克隆,但是我無法繼續工作。

#include <GL/glut.h> 

struct Rectangle { 
    int x; 
    int y; 
    int width; 
    int height; 
}; 

struct Ball { 
    int x; 
    int y; 
    int radius; 
}; 

typedef struct Rectangle Rectangle; 
typedef struct Ball Ball; 

Rectangle rOne, rTwo; 
Ball ball; 

void display(void); 
void reshape(int w, int h); 
void drawRectangle(Rectangle *r); 

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

    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); 
    glutInitWindowSize(800,600); 
    glutCreateWindow("Pong"); 
    gluOrtho2D(0,0,800.0,600.0); 

    rOne.x = 100; 
    rOne.y = 100; 
    rOne.width = 100; 
    rOne.height = 50; 

    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 
    glutMainLoop(); 

    return 0; 

} 

void display(void) { 
    glClear(GL_COLOR_BUFFER_BIT); 

    drawRectangle(&rOne); 

    glFlush(); 
    glutSwapBuffers(); 
} 

void reshape(int w, int h) { 
    glViewport(0,0,w,h); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0,0,(GLfloat)w,(GLfloat)h); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
} 

void drawRectangle(Rectangle *r) { 
    glBegin(GL_QUADS); 
     glVertex2i(r->x,r->y); 
    glVertex2i(r->x+(r->width-1),r->y); 
    glVertex2i(r->x+(r->width-1),r->y+(r->height-1)); 
    glVertex2i(r->x,r->y+(r->height-1)); 
    glEnd(); 
} 
+2

首先,你大概的意思:'gluOrtho2D(0,800,0,600);'。在'reshape'函數中調用此函數後,不要加載標識模型視圖矩陣。 – 2011-12-22 21:02:42

+2

你爲什麼接受這個答案?一般來說,你不只是接受某人迴應的第一件事。習慣性地等待幾個更多的答案並選擇* best *。你知道,解決你的問題的那個。 – 2011-12-22 21:26:25

回答

5

你請求鄰投影零寬度:

gluOrtho2D(0,0,(GLfloat)w,(GLfloat)h); 

這可能是你的意思:

gluOrtho2D(0,(GLfloat)w,0,(GLfloat)h); 

例子:

#include <GL/glut.h> 

typedef struct { 
    int x; 
    int y; 
    int width; 
    int height; 
} Rect; 

typedef struct { 
    int x; 
    int y; 
    int radius; 
} Ball; 

Rect rOne, rTwo; 
Ball ball; 

void display(void); 
void reshape(int w, int h); 
void drawRectangle(Rect *r); 

int main(int argc, char* argv[]) 
{ 
    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
    glutInitWindowSize(800,600); 
    glutCreateWindow("Pong"); 

    rOne.x = 100; 
    rOne.y = 100; 
    rOne.width = 100; 
    rOne.height = 50; 

    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 
    glutMainLoop(); 

    return 0; 
} 

void display(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 

    drawRectangle(&rOne); 

    glFlush(); 
    glutSwapBuffers(); 
} 

void reshape(int w, int h) 
{ 
    glViewport(0,0,w,h); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0,(GLfloat)w,0,(GLfloat)h); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
} 

void drawRectangle(Rect *r) 
{ 
    glBegin(GL_QUADS); 
    glVertex2i(r->x,r->y); 
    glVertex2i(r->x+(r->width-1),r->y); 
    glVertex2i(r->x+(r->width-1),r->y+(r->height-1)); 
    glVertex2i(r->x,r->y+(r->height-1)); 
    glEnd(); 
} 
3

你的問題謊言在這裏:

gluOrtho2D(0,0,800.0,600.0); 

gluOrtho2D不像glViewport。 gluOrtho2D的參數是:

gluOrtho2D(left, right, bottom, top); 

所以,你必須把它像

gluOrtho(0, w, 0, h);