2011-12-22 14 views
2

なぜこのコードは黒い画面を生成しますか(何も描画されません...)?OpenGL黒画面/何も描画されていない?

私はポンクローンを作成していますが、これを動作させることなく進めることはできません。あなたはゼロ幅のオルト投影を要求している

#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(left, right, bottom, top); 

、おそらく意味: 'gluOrtho2D(0、800、0、600);'。 'reshape'関数でこの呼び出しの後にアイデンティティモデルビュー行列をロードしないでください。 –

+2

なぜあなたはその答えを受け入れましたか?一般的に、誰かが最初に応答したことを受け入れるだけではありません。いくつかの答えを待ち、*ベスト*を選ぶのが通例です。あなたが知っているのは、あなたの問題を解決するものです。 –

答えて

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のパラメータは以下のとおりです。あなたはまず

gluOrtho(0, w, 0, h); 
関連する問題