计算机图形学-旋转彩色正方体

来源:互联网 发布:php开发工程师简历 编辑:程序博客网 时间:2024/05/24 07:23




功能:1:鼠标可拖拉正方体做任意旋转

2:键盘输入x ,X,y,Y,z,Z可使正方体绕指定方向旋转

#include <GL/glut.h>#include <cstdlib>const float vertex[] = {-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};const float color[] = {0,0,0,1.0,0,0,1.0,1.0,0,0,1.0,0,                        0,0,1.0,1.0,0,1.0,1.0,1.0,1.0,0,1.0,1.0};const GLubyte index[] = {0,3,2,1,2,3,7,6,0,4,7,3,0,1,5,4,1,2,6,5,4,5,6,7};float theta[] = {0,0,0,0};int axis = 3;float step = 2.0;int win_w,win_h,mx,my;void display(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(theta[0],1.0,0,0); glRotatef(theta[1],0,1.0,0); glRotatef(theta[2],0,0,1.0); glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,index); glutSwapBuffers();}void keyboard(unsigned char key,int x,int y){ switch(key) { case 'x':  axis = 0;  step = 2.0;  break; case 'X':  axis = 0;  step = -2.0;  break; case 'y':  axis = 1;  step = 2.0;  break; case 'Y':  axis = 1;  step = -2.0;  break; case 'z':  axis = 2;  step = 2.0;  break; case 'Z':  axis = 2;  step = -2.0;  break; case 27:  exit(0); }}void idle(){ theta[axis] += step; if(theta[axis] >= 360)  theta[axis] -= 360; if(theta[axis] < 0)  theta[axis] += 360; glutPostRedisplay();}void move(int x,int y){ if(x < mx)  theta[1] += 2.0; else if(x > mx)  theta[1] -= 2.0; if(y < my)  theta[0] -= 2.0; else if(y > my)  theta[0] += 2.0; mx = x , my = y; glutPostRedisplay();}void reshape(int w,int h){ glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w <= h)  glOrtho(-2.0,2.0,-2.0*h/w,2.0*h/w,-2.0,2.0); else  glOrtho(-2.0*w/h,2.0*w/h,-2.0,2.0,-2.0,2.0); glViewport(0,0,w,h); win_w = w , win_h = h; glMatrixMode(GL_MODELVIEW);}void myinit(){ glClearColor(1.0,1.0,1.0,1.0); glEnable(GL_DEPTH_TEST); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3,GL_FLOAT,0,vertex); glColorPointer(3,GL_FLOAT,0,color); mx = win_w / 2; my = win_h / 2;}int main(int argc, char *argv[]){    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowPosition(100,100);    glutInitWindowSize(640, 480);    glutCreateWindow("Cube"); myinit();    glutDisplayFunc(display);    glutReshapeFunc(reshape); glutIdleFunc(idle); glutKeyboardFunc(keyboard); glutMotionFunc(move);       glutMainLoop();    return 0;}

将以上程序做进一步修改,实现以下功能,

正方体初始状态为绕X轴旋转,然后点击鼠标左键,正方体绕X轴旋转,点击鼠标右键,正方体绕y轴旋转,点击鼠标中间滑轮,正方体绕z轴旋转

#include <GL/glut.h>#include <cstdlib>const float vertex[] = { -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 };const float color[] = { 0, 0, 0, 1.0, 0, 0, 1.0, 1.0, 0, 0, 1.0, 0,0, 0, 1.0, 1.0, 0, 1.0, 1.0, 1.0, 1.0, 0, 1.0, 1.0 };const GLubyte index[] = { 0, 3, 2, 1, 2, 3, 7, 6, 0, 4, 7, 3, 0, 1, 5, 4, 1, 2, 6, 5, 4, 5, 6, 7 };float theta[] = { 0, 0, 0, 0 };int axis = 3;float step = 2.0;int win_w, win_h, mx, my;GLfloat rtri;GLfloat vx = 1.0, vy = 0.0, vz = 0.0;int flag1 = 0, flag2 = 0, flag3 = 0;void display(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotated(rtri, 0, 0, 1);glRotatef(theta[0], 1.0, 0, 0);glRotatef(theta[1], 0, 1.0, 0);glRotatef(theta[2], 0, 0, 1.0);glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, index);if (flag1==0 && flag2==0 && flag3==0)     rtri += 0.1f;if (flag1)theta[0] += 0.1f;if (flag2)theta[1] += 0.1f;if (flag3)theta[2] += 0.1f;glutSwapBuffers();//双缓存 }void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse){if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) {flag1 = 1;flag2 = 0;flag3 = 0;}if (button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN){flag1 = 0;flag2 = 1;flag3 = 0;}if (button == GLUT_MIDDLE_BUTTON && action == GLUT_DOWN){flag1 = 0;flag2 = 0;flag3 = 1;}glutPostRedisplay(); // 刷新窗口}void reshape(int w, int h){glMatrixMode(GL_PROJECTION);glLoadIdentity();if (w <= h)glOrtho(-2.0, 2.0, -2.0*h / w, 2.0*h / w, -2.0, 2.0);elseglOrtho(-2.0*w / h, 2.0*w / h, -2.0, 2.0, -2.0, 2.0);glViewport(0, 0, w, h);win_w = w, win_h = h;glMatrixMode(GL_MODELVIEW);}void idle(){theta[axis] += step;if (theta[axis] >= 360)theta[axis] -= 360;if (theta[axis] < 0)theta[axis] += 360;glutPostRedisplay();}void myinit(){glClearColor(1.0, 1.0, 1.0, 1.0);glEnable(GL_DEPTH_TEST);glEnableClientState(GL_COLOR_ARRAY);glEnableClientState(GL_VERTEX_ARRAY);glVertexPointer(3, GL_FLOAT, 0, vertex);glColorPointer(3, GL_FLOAT, 0, color);mx = win_w / 2;my = win_h / 2;}int main(int argc, char *argv[]){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowPosition(100, 100);glutInitWindowSize(640, 480);glutCreateWindow("Cube");myinit();glutDisplayFunc(display);glutReshapeFunc(reshape);glutIdleFunc(idle);glutMouseFunc(MousePlot);glutMainLoop();return 0;}