您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > 计算机图形学上机报告
计算机图形学上机实验报告计算机科学与技术学院班级:学号:姓名:指导教师:完成日期:实验一:基本图元绘制一.实验目的1.了解如何利用OpenGL库绘制图形2.理解glut程序框架3.理解窗口到视区的变换4.理解OpenGL实现动画的原理二.实验内容1.实现中点Bresenham算法画直线2.实现改进Bresenham算法画直线3.实现圆的绘制三.实验结果1.DDA算法画直线2.中点Bresenham算法画直线3.改进Bresenham算法画直线4.Bresenham算法画圆四.源程序#includewindows.h#includegl/glut.h#includestdio.hintm_PointNumber=0;//动画时绘制点的数目intm_DrawMode=4;//绘制模式1DDA算法画直线//2中点Bresenham算法画直线//3改进Bresenham算法画直线//4八分法绘制圆//5四分法绘制椭圆//绘制坐标线voidDrawCordinateLine(void){inti=0;//坐标线为黑色glColor3f(0.0f,0.0f,0.0f);glBegin(GL_LINES);for(i=10;i=250;i=i+10){glVertex2f((float)(i),0.0f);glVertex2f((float)(i),250.0f);glVertex2f(0.0f,(float)(i));glVertex2f(250.0f,(float)(i));}glEnd();}//绘制一个点,这里用一个正方形表示一个点。voidputpixel(GLsizeix,GLsizeiy){glRectf(10*x,10*y,10*x+10,10*y+10);}voidDDACreateLine(GLsizeix0,GLsizeiy0,GLsizeix1,GLsizeiy1,GLsizeinum){//设置颜色glColor3f(1.0f,0.0f,0.0f);//对画线动画进行控制if(num==1)printf(DDA画线算法:各点坐标\n);elseif(num==0)return;//画线算法的实现GLsizeidx,dy,epsl,k;GLfloatx,y,xIncre,yIncre;dx=x1-x0;dy=y1-y0;x=x0;y=y0;if(abs(dx)abs(dy))epsl=abs(dx);elseepsl=abs(dy);xIncre=(float)dx/epsl;yIncre=(float)dy/epsl;for(k=0;k=epsl;k++){putpixel((int)(x+0.5),(int)(y+0.5));if(k=num-1){printf(x=%f,y=%f,取整后x=%d,y=%d\n,x,y,(int)(x+0.5),(int)(y+0.5));break;}x+=xIncre;y+=yIncre;if(x=25||y=25)break;}}voidBresenhamLine(GLsizeix0,GLsizeiy0,GLsizeix1,GLsizeiy1,GLsizeinum){glColor3f(1.0f,0.0f,0.0f);if(num==1)printf(中点Bresenham算法画直线:各点坐标及判别式的值\n);elseif(num==0)return;//画线算法的实现GLsizeidx,dy,d,UpIncre,DownIncre,x,y,xend=0;if(x0x1){x=x1;x1=x0;x0=x;y=y1;y1=y0;y0=y;}x=x0;y=y0;dx=x1-x0;dy=y1-y0;d=dx-2*dy;UpIncre=2*dx-2*dy;DownIncre=-2*dy;while(x=x1){putpixel(x,y);if(x=num-1){break;}x++;if(d0){y++;d+=UpIncre;}elsed+=DownIncre;if(x=50||y=50)break;}}voidBresenham2Line(GLsizeix0,GLsizeiy0,GLsizeix1,GLsizeiy1,GLsizeinum){glColor3f(1.0f,0.0f,0.0f);if(num==1)printf(改进的Bresenham算法画直线:各点坐标及判别式的值\n);elseif(num==0)return;//画线算法的实现GLsizeix,y,dx,dy,e;dx=x1-x0;dy=y1-y0;e=-dx;x=x0;y=y0;while(x=x1){putpixel(x,y);if(x=num-1){break;}x++;e=e+2*dy;if(e0){y++;e=e-2*dx;}if(x=50||y=50)break;}}voidBresenhamCircle(GLsizeix,GLsizeiy,GLsizeir,GLsizeinum){glColor3f(1.0f,0.0f,0.0f);if(num==1)printf(Bresenham算法画圆:各点坐标及判别式的值\n);x=0,y=r;GLsizeid=1-r;while(xy){putpixel(x,y);if(x=num){break;}putpixel(y,x);if(x=num){break;}putpixel(-y,x);if(x=num){break;}putpixel(-x,y);if(x=num){break;}putpixel(-x,-y);if(x=num){break;}putpixel(-y,-x);if(x=num){break;}putpixel(y,-x);if(x=num){break;}putpixel(x,-y);if(d0)d+=2*x+3;else{d+=2*(x-y)+5;y--;}x++;}}//初始化窗口voidInitial(void){//设置窗口颜色为蓝色glClearColor(1.0f,1.0f,1.0f,1.0f);}//窗口大小改变时调用的登记函数voidChangeSize(GLsizeiw,GLsizeih){if(h==0)h=1;//设置视区尺寸glViewport(0,0,w,h);//重置坐标系统glMatrixMode(GL_PROJECTION);glLoadIdentity();//建立修剪空间的范围if(w=h)glOrtho(0.0f,250.0f,0.0f,250.0f*h/w,1.0,-1.0);elseglOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0,-1.0);}//在窗口中绘制图形voidReDraw(void){//用当前背景色填充窗口glClear(GL_COLOR_BUFFER_BIT);//画出坐标线DrawCordinateLine();switch(m_DrawMode){case1:DDACreateLine(0,0,20,15,m_PointNumber);break;case2:BresenhamLine(0,0,20,15,m_PointNumber);break;case3:Bresenham2Line(1,1,8,6,m_PointNumber);break;case4:BresenhamCircle(5,5,18,m_PointNumber);break;default:break;}glFlush();}//设置时间回调函数voidTimerFunc(intvalue){if(m_PointNumber==0)value=1;m_PointNumber=value;glutPostRedisplay();glutTimerFunc(500,TimerFunc,value+1);}//设置键盘回调函数voidKeyboard(unsignedcharkey,intx,inty){if(key=='1')m_DrawMode=1;if(key=='2')m_DrawMode=2;if(key=='3')m_DrawMode=3;if(key=='4')m_DrawMode=4;m_PointNumber=0;glutPostRedisplay();}//voidmain(void)intmain(intargc,char*argv[]){glutInit(&argc,argv);//初始化GLUT库OpenGL窗口的显示模式glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(800,600);glutInitWindowPosition(100,100);glutCreateWindow(基本图元绘制程序);glutDisplayFunc(ReDraw);glutReshapeFunc(ChangeSize);glutKeyboardFunc(Keyboard);//键盘响应回调函数glutTimerFunc(500,TimerFunc,1);//窗口初始化Initial();glutMainLoop();//启动主GLUT事件处理循环return0;}实验二:日地月模型一.实验目的1.理解OpenGL中的变换过程2.理解透视投影与平行投影的不同3.了解深度测试二.实验内容1.通过变换调整观察的位置与方向2.实现太阳、地球和月亮的运动模型三.实验结果太阳、地球和月亮的运动模型图1.图2.图3.四.源程序#includewindows.h#includegl/gl.h#includegl/glu.h#includegl/glut.hvoidInitial(){glEnable(GL_DEPTH_TEST);//启用深度测试glFrontFace(GL_CCW);//指定逆时针绕法表示多边形正面glClearColor(1.0f,1.0f,1.0f,1.0f);//背景为白色}voidChangeSize(intw,inth){if(h==0)h=1;//设置视区尺寸glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();//设置修剪空间GLfloatfAspect;fAspect=(float)w/(float)h;gluPerspective(45.0,fAspect,1.0,500.0);/*if(w=h)glOrtho(-nRange,nRange,nRange*h/w,-nRange*h/w,-nRange*2.0f,nRange*2.0f);elseglOrtho(-nRange*w/h,nRange*w/h,nRange,-nRange,-nRange*2.0f,nRange*2.0f);*/glMatrixMode(GL_MODELVIEW);glLoadIdentity();}voidRenderScene(void){//绕原子核旋转的角度staticfloatfElect1=0.0f;staticfloatf2=0.0f;glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//重置模型视图矩阵glMatrixMode(GL_MODELVIEW);glLoadIdentity();//将图形沿z轴负向移动glTranslatef(0.0f,0.0f,-250.0f);//绘制红色的原子核glColor3f(1.0f,0.0f,0.0f);glutSolidSphere(50.0f,15,15);//当前绘制颜色变为黄色glColor3f(0.0f,0.0f,0.0f);//绘制第一个电子//保存当前的模型视图矩阵glRotatef(fElect1,0.
本文标题:计算机图形学上机报告
链接地址:https://www.777doc.com/doc-7165953 .html