2011-12-22 95 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);