|
|
我在进行长方体的旋转时,在旋转的过程长方体发生了变形我的程序如下:请各位高手指点一下:
#include <GL/glut.h>
#define high 0.25
#define longth 2.5
#define wide 1.0
GLfloat angle=0;
void init(void)
{
glClearColor(1.0,1.0,1.0,1.0);
glShadeModel(GL_FLAT);
}
void colorcube()
{
static GLfloat vd[]=
{
-wide,-longth,-high,
wide,-longth,-high,
wide,longth,-high,
-wide,longth,-high,
-wide,-longth,high,
wide,-longth,high,
-wide,longth,high,
0.0,longth,high
};
static GLubyte fside[]={5,1,2,6};
static GLubyte bside[]={4,7,3,0};
static GLubyte rside[]={6,7,3,2};
static GLubyte lside[]={4,5,1,0};
static GLubyte aside[]={4,5,6,7};
static GLubyte uside[]={0,3,2,1};
glEnableClientState(GL_VERTEX_ARRAY);//开启顶点向量
glVertexPointer(3,GL_FLOAT,0,vd);
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,fside);//前面
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,bside);//后面
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,rside);//右面
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,lside);//左面
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,aside);//上面
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,uside);//下面
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //使用RGB颜色和深度缓存
glLoadIdentity();
gluLookAt(10.0,10.0,2.0,0,0,0,0.0,0.0,1.0);//设置视点,相机位于(-1.5,2.0,7.0),对准(0.0,0.0,0.0)
glRotatef(angle,0,0,1);//旋转调试
colorcube();//画下层立方体
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0.0,0.0,(GLfloat)w,(GLfloat)h);//定义视口大小
glMatrixMode(GL_PROJECTION);//选择更改投影矩阵
glLoadIdentity();//修改之前矩阵为单位矩阵
//定义投影矩阵
if(w<=h)
glOrtho(-5.0,5.0,-5.0*(GLfloat)h/(GLfloat)w,5.0*(GLfloat)h/(GLfloat)w,-45.0,45.0);
else
glOrtho(-5.0*(GLfloat)h/(GLfloat)w,5.0*(GLfloat)h/(GLfloat)w,-5.0,5.0,-45.0,45.0);
glMatrixMode(GL_MODELVIEW);//更改为模型视点矩阵
glLoadIdentity();
}
void keyboard(unsigned char key,int x,int y)
{
switch(key){
case 'q':
angle+=5;
if(angle>=360)
angle=0;
glutPostRedisplay();
break;
case 'Q':
angle-=5;
glutPostRedisplay();
break;
default:
break;
}
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH|GLUT_MULTISAMPLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("cube");
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
} |
|