OpenGL旋转立方体的函数,鼠标控制旋转轴,但生成的图形有问题
下面是代码:#include<gl/glut.h>intaxis;floattheta[3];voidInit(){glClearColor(0.0,0.0,0.0,0....
下面是代码:
#include<gl/glut.h>
int axis;
float theta[3];
void Init()
{
glClearColor(0.0,0.0,0.0,0.0);//将背景色置为黑色
}
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}
};//定义定点数组
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 cube()
{
glColor3f(1.0,0.0,0.0);
Polygon(0,3,2,1);
glColor3f(0.0,1.0,0.0);
Polygon(2,3,7,6);
glColor3f(0.0,0.0,1.0);
Polygon(0,3,7,4);
glColor3f(1.0,1.0,1.0);
Polygon(1,2,6,5);
glColor3f(0.5,0.5,0.5);
Polygon(4,7,6,5);
glColor3f(0.5,1.0,1.0);
Polygon(5,4,0,1);
}//绘制彩色立方体
void Display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除颜色及深度缓存
glMatrixMode(GL_MODELVIEW);
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);
cube();
glutSwapBuffers();
}
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 spincube()
{
theta[axis]+=0.1;
if (theta[axis]>360.0) theta[axis]-=360.0;
glutPostRedisplay();
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0*(GLfloat)w/(GLfloat)h,4.0*(GLfloat)w/(GLfloat)h,-4.0*(GLfloat)h/(GLfloat)w,4.0*(GLfloat)h/(GLfloat)w,-6.0,6.0);
}
void main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,100);
glutCreateWindow("cube");
Init();
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutIdleFunc(spincube);
glutMouseFunc(mouse);
glutMainLoop();
}
问题:1.第一个面我设置的是红色,但执行后没有红色的面,怎么回事?
2.旋转时任然能看到后面的面,如何消隐?
3.我改变窗口大小时如何让旋转的立方体每个面都不变形?
4.用gluLookAt函数能否产生运动效果?
我刚开始接触Opengl,这些问题对大家相信不是难事,请大侠帮忙解决一下。NeHe的教程我有,就不用介绍了哈。.另外,我财富只有19.只能拿出这么多,请见谅。谢谢 展开
#include<gl/glut.h>
int axis;
float theta[3];
void Init()
{
glClearColor(0.0,0.0,0.0,0.0);//将背景色置为黑色
}
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}
};//定义定点数组
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 cube()
{
glColor3f(1.0,0.0,0.0);
Polygon(0,3,2,1);
glColor3f(0.0,1.0,0.0);
Polygon(2,3,7,6);
glColor3f(0.0,0.0,1.0);
Polygon(0,3,7,4);
glColor3f(1.0,1.0,1.0);
Polygon(1,2,6,5);
glColor3f(0.5,0.5,0.5);
Polygon(4,7,6,5);
glColor3f(0.5,1.0,1.0);
Polygon(5,4,0,1);
}//绘制彩色立方体
void Display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除颜色及深度缓存
glMatrixMode(GL_MODELVIEW);
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);
cube();
glutSwapBuffers();
}
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 spincube()
{
theta[axis]+=0.1;
if (theta[axis]>360.0) theta[axis]-=360.0;
glutPostRedisplay();
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0*(GLfloat)w/(GLfloat)h,4.0*(GLfloat)w/(GLfloat)h,-4.0*(GLfloat)h/(GLfloat)w,4.0*(GLfloat)h/(GLfloat)w,-6.0,6.0);
}
void main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,100);
glutCreateWindow("cube");
Init();
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutIdleFunc(spincube);
glutMouseFunc(mouse);
glutMainLoop();
}
问题:1.第一个面我设置的是红色,但执行后没有红色的面,怎么回事?
2.旋转时任然能看到后面的面,如何消隐?
3.我改变窗口大小时如何让旋转的立方体每个面都不变形?
4.用gluLookAt函数能否产生运动效果?
我刚开始接触Opengl,这些问题对大家相信不是难事,请大侠帮忙解决一下。NeHe的教程我有,就不用介绍了哈。.另外,我财富只有19.只能拿出这么多,请见谅。谢谢 展开
1个回答
展开全部
程序里有点面有问题,逆时针画一个面,这个面才是面向你的;有点面你顺时针画了。正确顺序:
glColor3f(1.0,0.0,0.0);
Polygon(0,1,2,3);
glColor3f(0.0,1.0,0.0);
Polygon(2,6,7,3);
glColor3f(0.0,0.0,1.0);
Polygon(0,3,7,4);
glColor3f(1.0,1.0,1.0);
Polygon(1,5,6,2);
glColor3f(0.5,0.5,0.5);
Polygon(4,7,6,5);
glColor3f(0.5,1.0,1.0);
Polygon(5,1,0,4);
glEnable(GL_CULL_FACE);剔除反面。
按你这个投影,想固定窗口只能:
glViewport(0,0,w,w);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,-6.0,6.0);
gluLookAt改变摄像机的,可以产生动画
glColor3f(1.0,0.0,0.0);
Polygon(0,1,2,3);
glColor3f(0.0,1.0,0.0);
Polygon(2,6,7,3);
glColor3f(0.0,0.0,1.0);
Polygon(0,3,7,4);
glColor3f(1.0,1.0,1.0);
Polygon(1,5,6,2);
glColor3f(0.5,0.5,0.5);
Polygon(4,7,6,5);
glColor3f(0.5,1.0,1.0);
Polygon(5,1,0,4);
glEnable(GL_CULL_FACE);剔除反面。
按你这个投影,想固定窗口只能:
glViewport(0,0,w,w);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,-6.0,6.0);
gluLookAt改变摄像机的,可以产生动画
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询