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();
}
首先,你大概的意思:'gluOrtho2D(0,800,0,600);'。在'reshape'函數中調用此函數後,不要加載標識模型視圖矩陣。 – 2011-12-22 21:02:42
你爲什麼接受這個答案?一般來說,你不只是接受某人迴應的第一件事。習慣性地等待幾個更多的答案並選擇* best *。你知道,解決你的問題的那個。 – 2011-12-22 21:26:25