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();
}
Why does this piece of code produce a black screen (nothing is drawn...)?
I'm creating a Pong clone but I can't proceed without getting this to work.
#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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题就在这里:
gluOrtho2D 不像 glViewport。 gluOrtho2D 的参数是:
所以你必须像这样调用它
Your problem lies here:
gluOrtho2D is not like glViewport. The parameter of gluOrtho2D are :
So you must call it like
您请求宽度为零的正射投影:
这可能就是您的意思:
示例:
You're requesting an ortho projection with zero width:
This is probably what you meant:
Example: