OpenGL, 一个旋转的正方体,为什么红色的面没有显示。

#include<gl/glut.h>GLfloatvertices[][3]={{-1.0,-1.0,1.0},{-1.0,1.0,1.0},{1.0,1.0,1.0}... #include <gl/glut.h>

GLfloat vertices[][3] = {{-1.0, -1.0, 1.0}, {-1.0, 1.0, 1.0},
{1.0, 1.0, 1.0}, {1.0, -1.0, 1.0}, {-1.0, -1.0, -1.0},
{-1.0, 1.0, -1.0}, {1.0, 1.0, -1.0}, {1.0, -1.0, -1.0}};

GLfloat colors[][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 1.0},
{1.0, 1.0, 0.0}, {0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}};

int axis;
float theta[3];

void polygon(int a, int b, int c, int d)
{
glBegin(GL_POLYGON);
glVertex3fv(vertices[a]);
glVertex3fv(vertices[b]);
glVertex3fv(vertices[c]);
glVertex3fv(vertices[d]);
glEnd();
}

void colorcube()
{
glColor3fv(colors[0]);
polygon(0, 3, 2, 1);
glColor3fv(colors[1]);
polygon(2, 3, 7, 6);
glColor3fv(colors[2]);
polygon(3, 0, 4, 7);
glColor3fv(colors[3]);
polygon(1, 2, 6, 5);
glColor3fv(colors[4]);
polygon(4, 5, 6, 7);
glColor3fv(colors[5]);
polygon(5, 4, 0, 1);
}

void spinCube()
{
theta[axis] += 1.0;
if (theta[axis] > 360.0) theta[axis] -= 360.0;
glutPostRedisplay();
}

void mouse(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)axis = 1;
if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}

void init()
{
//glClearColor(0.0, 0.0, 0.0, 0.0);
//glColor3f(1.0, 1.0, 1.0);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glFlush();
glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0, 4.0, -4.0, 4.0, -4.0, 4.0);
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow("OpenGL");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
init();
glutMainLoop();
}
展开
842787863
2012-02-15 · 超过15用户采纳过TA的回答
知道答主
回答量:100
采纳率:0%
帮助的人:38.1万
展开全部
#include "stdafx.h"
#include <gl/glut.h>
#include<Windows.h>

GLfloat vertices[][3] = {{-1.0, -1.0, 1.0}, {-1.0, 1.0, 1.0},
{1.0, 1.0, 1.0}, {1.0, -1.0, 1.0}, {-1.0, -1.0, -1.0},
{-1.0, 1.0, -1.0}, {1.0, 1.0, -1.0}, {1.0, -1.0, -1.0}};

GLfloat colors[][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 1.0},
{1.0, 1.0, 0.0}, {0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}};

int axis;
float theta[3];

void polygon(int a, int b, int c, int d)
{
glBegin(GL_POLYGON);
glVertex3fv(vertices[a]);
glVertex3fv(vertices[b]);
glVertex3fv(vertices[c]);
glVertex3fv(vertices[d]);
glEnd();
}

void colorcube()
{
glColor3fv(colors[0]);
polygon(0, 3, 2, 1);
glColor3fv(colors[1]);
polygon(2, 3, 7, 6);
glColor3fv(colors[2]);
polygon(3, 0, 4, 7);
glColor3fv(colors[3]);
polygon(1, 2, 6, 5);
glColor3fv(colors[4]);
polygon(4, 5, 6, 7);
glColor3fv(colors[5]);
polygon(5, 4, 0, 1);
}

void spinCube()
{
theta[axis] += 1.0;
if (theta[axis] > 360.0) theta[axis] -= 360.0;
glutPostRedisplay();
Sleep(90);

}

void mouse(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)axis = 1;
if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}

void init()
{
//glClearColor(0.0, 0.0, 0.0, 0.0);
//glColor3f(1.0, 1.0, 1.0);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glLoadIdentity();
glRotatef(theta[0], 1.0, 1.0, 0.0);
colorcube();
glFlush();
glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0, 4.0, -4.0, 4.0, -4.0, 4.0);
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow("OpenGL");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
init();
glutMainLoop();
}

改下旋转轴就 看到啦
追问
仔细看它的旋转,只有五种颜色,
追答
眼睛都看花了,数据不好检查啊,你有一个面的坐标错了,和其他面混合在一起了,所以你永远都看不到那个面
winorlose0083
2012-02-14
知道答主
回答量:1
采纳率:0%
帮助的人:1663
展开全部
你的程序在我的电脑上跑过了,看到立方体的旋转后我觉得应该是display设置的有问题,因为立方体是旋转绕的轴和你的视线是垂直的,你只能看到4个面,两个侧面你永远看不到。
追问
void mouse(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)axis = 1;
if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}
可以旋转的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式