OpenGL绘制旋转茶壶

来源:互联网 发布:崔永元转基因事件知乎 编辑:程序博客网 时间:2024/04/28 14:20

绘制一个茶壶,鼠标左键实现拖动旋转,

鼠标右键点击加速绕Y轴旋转:



弱鸡套了三四个版子才拼凑出来拿出来当明天的实验报告,

代码:

#include <stdlib.h>  #include <GL/glut.h>  #include<iostream>#pragma comment(lib, "glut32.lib")  using namespace std;GLfloat roate = 0.0;// set rote of roate ying yu bu hao  bu zhuang le 设置旋转速率GLfloat rote = 0.0;//shezhi旋转角度GLfloat anglex = 0.0;//X 轴旋转GLfloat angley = 0.0;//Y 轴旋转GLfloat anglez = 0.0;//Z 轴旋转GLint WinW = 400;GLint WinH = 400;GLfloat oldx;//当左键按下时记录鼠标坐标  GLfloat oldy;void init(void){glClearColor(1.0, 1.0, 1.0, 1.0); //背景黑色  }void display(void){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0, 0.0, 0.0); //画笔红色glLoadIdentity();  //加载单位矩阵  gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);glRotatef(rote, 0.0f, 1.0f, 0.0f);glRotatef(anglex,1.0,0.0,0.0);glRotatef(angley,0.0,1.0,0.0);glRotatef(anglez,0.0,0.0,1.0);glutWireTeapot(2);rote += roate;//glRotatef(angle, 0.0, 1.0, 0.0);//angle += 1.0f;glutSwapBuffers();}void reshape(int w, int h){glViewport(0, 0, (GLsizei)w, (GLsizei)h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);}void mouse(int button, int state, int x, int y){if (button == GLUT_LEFT_BUTTON){if (state == GLUT_DOWN){roate = 0;rote = 0;oldx = x;//当左键按下时记录鼠标坐标  oldy = y;cout << "left" << endl;}}if (button == GLUT_RIGHT_BUTTON){if (state == GLUT_DOWN){roate += 1.0f;cout << "right" << endl;}}}void motion(int x, int y){GLint deltax = oldx - x;GLint deltay = oldy - y;anglex  += 360 * (GLfloat)deltax / (GLfloat)WinW;//根据屏幕上鼠标滑动的距离来设置旋转的角度  angley += 360 * (GLfloat)deltay / (GLfloat)WinH;anglez += 360 * (GLfloat)deltay / (GLfloat)WinH;oldx = x;//记录此时的鼠标坐标,更新鼠标坐标  oldy = y;//若是没有这两句语句,滑动是旋转会变得不可控  glutPostRedisplay();glutPostRedisplay();}int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);glutInitWindowSize(600, 600);glutInitWindowPosition(100, 100);glutCreateWindow(argv[0]);init();glutDisplayFunc(display);glutReshapeFunc(reshape);glutMouseFunc(mouse);glutMotionFunc(motion);glutIdleFunc(display);glutMainLoop();return 0;}


1 0
原创粉丝点击