文档详情

计算机图形学实验代码--2D小动画.doc

发布:2017-12-13约1.95千字共7页下载文档
文本预览下载声明
计算机图形学实验 —— 2D动画代码 转动的茶壶 #include stdafx.h #include glut.h int flag=0; static GLfloat theta[]={0.0,0.0,0.0}; static GLint axis=2; void reshape(int w,int h); void init(); void display(); void mouse(int btn, int state,int x, int y); void spinCube(); int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); char *argv[] = {hello , }; int argc = 2; glutInit(argc, argv); //初始化GLUT库; glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(800,800); glutCreateWindow(colorcube); init(); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glutMainLoop(); return 0; } void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(0,0,5,0,0,0,0,1,0); 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); glColor3f(1,1,1); glutWireTeapot(1); glPushMatrix(); glTranslatef(0,-1.1,0); glRotatef(90,1,0,0); glColor3f(0,0,1); glutWireTorus(0.4, 1.3,60,60); glPopMatrix(); glPushMatrix(); glTranslatef(0,-0.5,0); glColor3f(1,1,0.2); glutWireSphere(3,4,2); glPopMatrix(); glutSwapBuffers(); } void init() { glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glEnable(GL_DEPTH_TEST); } 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 reshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(120,w/h,1,60); //定义透视投影投影观察体 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void spinCube() { theta[axis]+=1.0; if (theta[axis]360.0) theta[axis]-=360.0; glutPostRedisplay(); }
显示全部
相似文档