资源描述:
《图形学代码及截图》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验2:代码:#include#includeGLdoubleangle=10.0;voiddisplay(){glClear(GL_COLOR_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glBegin(GL_POLYGON);glVertex2d(-0.5,-0.5);glVertex2d(-0.5,0.5);glVertex2d(0.5,0.5);glVertex2d(0.5,-0.5);glEnd();glFlush();}voidmous
2、eFunc(intbutton,intstate,intx,inty){//旋转if(state==GLUT_DOWN&&button==GLUT_LEFT_BUTTON){glMatrixMode(GL_MODELVIEW);glLoadIdentity();if(angle<360)angle+=10;elseangle-=360;glRotatef(angle,1,1,2);glutPostRedisplay();}}voidkeyboard(unsignedcharkey,intx,inty){if(key==27
3、){exit(-1);}}voidinit(){glClearColor(0.0,0.0,0.0,0.0);glColor3f(1.0,0.0,0.0);}voidreshape(intw,inth){glViewport(0,0,w,h);}intmain(intargc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE
4、GLUT_RGB);glutInitWindowSize(500,500);glutInitWindowPosition(
5、100,100);glutCreateWindow("OpenGL中的建模与变换");init();glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutMouseFunc(mouseFunc);glutMainLoop();return0;}截图:点击旋转:体会:主要是我们在进行变换的时候,要注意glMatrixMode的设置,在进行投影变换的时候要设置为GL_PROJECTION,而当图形变换的时候要设置为GL_
6、MODELVIEW模式。实验3:代码:#include#includevoiddisplay(){glClear(GL_COLOR_BUFFER_BIT
7、GL_DEPTH_BUFFER_BIT);glColor3f(1.0,1.0,1.0);glEnable(GL_LIGHT0);glDisable(GL_LIGHT1);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslatef(-0.5,0,0);glutSolidSpher
8、e(0.3,20,22);glLoadIdentity();glDisable(GL_LIGHT0);glEnable(GL_LIGHT1);glTranslatef(0.5,0,0);glutSolidSphere(0.3,20,22);glutSwapBuffers();}voidkeyboard(unsignedcharkey,intx,inty){if(key==27){exit(-1);}}voidinit(){glClearColor(0.0,0.0,0.0,0.0);GLfloatposition0[4]={
9、-10,10,-10,1};GLfloatambient0[4]={1,0,0,1};glLightfv(GL_LIGHT0,GL_POSITION,position0);glLightfv(GL_LIGHT0,GL_AMBIENT,ambient0);GLfloatposition1[4]={10,10,-10,1};GLfloatambient1[4]={0,1,0,1};glLightfv(GL_LIGHT1,GL_POSITION,position1);glLightfv(GL_LIGHT1,GL_AMBIENT,
10、ambient1);glEnable(GL_LIGHTING);glEnable(GL_DEPTH_TEST);}voidreshape(intw,inth){glViewport(0,0,w,h);}intmain(intargc,char**argv){glutInit(&argc,argv);gl