opengl画立方体无法显示,怎么回事啊,就显示一个黑框,没有我要现实的立方体,谢谢各位解答啊,我是菜鸟

#include<GL/glut.h>voidinit(void){glutInitWindowSize(200,150);glClearColor(0.0,0.0,0.... #include<GL/glut.h>

void init(void){
glutInitWindowSize(200, 150);
glClearColor(0.0,0.0,0.0,0.0);
glViewport(0,0,200,150);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f,(GLfloat)200/(GLfloat)150,1.0f,1000.0f);
}

void rect(void){
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0f, 0.0f, 0.0f); // top face
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // front face
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // right face
glVertex3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, 0.0f); // left face
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // bottom face
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // back face
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glEnd();
glFlush();
}
void main(int argc,char** argv){
glutInit(&argc,argv);
//glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,200);
glutCreateWindow("Draw Rect");
init();
glutDisplayFunc(rect);
glutMainLoop();
}
展开
 我来答
吞薯条
2011-10-22 · 超过13用户采纳过TA的回答
知道答主
回答量:34
采纳率:0%
帮助的人:34.5万
展开全部
gluPerspective(90.0f,(GLfloat)200/(GLfloat)150,1.0f,1000.0f);
定义的可视范围是-1.0~-1000.0,
glBegin(GL_POLYGON);
这个是画一个多边形,就是说下边全部点都将成为多边形的一个点。
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
定义的正方形的深度在0.0~-1.0,不在可视范围内。

另外正方形的顶点有错误。
追问
gluPerspective(90.0f,(GLfloat)200/(GLfloat)150,1.0f,1000.0f);
后两个参数一定要是整数才行啊,我的立方体没有问题的哈
追答
gluPerspective(90.0f,(GLfloat)200/(GLfloat)150,1.0f,1000.0f);
的最后两个参数表示Z方向上的可视范围,是浮点数,必须为正数且>0(0会出错误)。你所设定的是1.0跟1000,表示只有Z坐标在(-1.0 到 -1000)的点才能被绘制。而你下边绘制的点Z坐标都在(0 到 -1.0)所以全部看不到。另外glVertex3f(0.0f, 0.0f, 0.0f); // back face 这个真的是画错了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式