版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
计算机图形学实验内容PAGEPAGE21计算机图形学实验肖加清实验一图形学实验基础实验目的掌握VC++绘图的一般步骤;掌握OpenGL软件包的安装方法;掌握OpenGL绘图的一般步骤;掌握OpenGL的主要功能与基本语法。二、实验内容1、VC++绘图实验(1)实验内容:以下是绘制金刚石图案。已给出VC++参考程序,但里面有部分错误,请改正,实现以下图案。N=3//视图类的一个成员函数,这个成员函数可以放在OnDraw函数里调用。//在视图类的头文件(.h)里定义此函数voidDiamond();//在视图类的实现文件(.cpp)里实现此函数voidCTestView::Diamond(){ CP2*P; intN; doubleR;R=300;N=10;P=newCP2[N]; CClientDCdc(this); CRectRect; GetClientRect(&Rect); doubletheta; theta=2*PI/N; for(inti=0;i<N;i++) { P[i].x=R*cos(i*theta); P[i].y=R*sin(i*theta); } for(i=0;i<=N-2;i++) { for(intj=i+1;j<=N-1;j++) {//其中ROUND函数需要自己实现,实现四舍五入的功能。 dc.MoveTo(ROUND(P[i].x+Rect.right/2),ROUND(P[i].y+Rect.bottom/2)); dc.LineTo(ROUND(P[j].x+Rect.right/2),ROUND(P[j].y+Rect.bottom/2)); } } delete[]P; }2、OpenGL绘图(1)以下是用OpenGL绘制茶壶的代码,请在OpenGL环境下运行,分析OpenGL程序的结构#include<windows.h>#include<GL/gl.h>#include<GL/glu.h>#include<GL/glaux.h>//定义输出窗口的大小#defineWINDOW_HEIGHT300#defineWINDOW_WIDTH500//用户初始化函数voidmyninit(void);//窗口大小变化时的回调函数voidCALLBACKmyReshape(GLsizeiw,GLsizeih);//每帧OpenGL都会调用这个函数,应该把显示代码放在这个函数中voidCALLBACKdisplay(void);intwindow_width=WINDOW_WIDTH;intwindow_height=WINDOW_HEIGHT;//视点离物体的距离floatdistance=3.6f;//初始化,此时为空,可以在这里进行初始化操作voidmyinit(void){}voidCALLBACKdisplay(void){ //设置清屏的颜色,并清屏和深度缓冲 glClearColor(0.0f,0.0f,0.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //设置成模型矩阵模式 glMatrixMode(GL_MODELVIEW); //载入单位化矩阵 glLoadIdentity(); //坐标中心向Z轴平移-distance,这样使坐标中心位于视点前方 glTranslatef(0.0,0.0,-distance); //在坐标中心显示一个茶壶 auxWireTeapot(1.0); //等待现有的OpenGL命令执行完成 glFlush(); //交换前后缓冲区 auxSwapBuffers();}voidCALLBACKmyReshape(GLsizeiw,GLsizeih){ if(!h)return; //这定视区 glViewport(0,0,w,h); //设定透视方式 glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0,1.0*(GLfloat)w/(GLfloat)h,1.0,30.0); window_width=w; window_height=h;}//移近移远的回调函数voidCALLBACKMoveNear(void){ distance-=0.3f;}voidCALLBACKMoveFar(void){ distance+=0.3f;}//主函数intmain(intargc,char**argv){ //初始化OpenGL的显示方式 auxInitDisplayMode(AUX_DOUBLE|AUX_RGB|AUX_DEPTH16); //设定OpenGL窗口位置和大小 auxInitPosition(0,0,WINDOW_WIDTH,WINDOW_HEIGHT); //打开窗口 auxInitWindow("OpenGL的一个简单的例子!"); //调用初始化函数 myinit(); //设定窗口大小变化的回调函数 auxReshapeFunc(myReshape); //设定移动视点的回调函数 auxKeyFunc('A',MoveNear);//按A键变大 auxKeyFunc('a',MoveFar);//按a键变小 //使display函数一直被调用 auxIdleFunc(display); //开始OpenGL的循环 auxMainLoop(display); //结束程序 return(0);}其运行结果如图所示:(2)OPENGL绘制矩形的简单例子参考程序:#include<gl/glut.h>voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f);//设置窗口背景颜色为白色 glMatrixMode(GL_PROJECTION);//设置投影参数 gluOrtho2D(0.0,200.0,0.0,150.0);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT);//用当前背景色填充窗口 glColor3f(1.0f,0.0f,0.0f);//设置当前的绘图颜色为红色 glRectf(50.0f,100.0f,150.0f,50.0f);//绘制一个矩形glFlush();//处理所有的OpenGL程序}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//初始化窗口的显示模式 glutInitWindowSize(400,300);//设置窗口的尺寸 glutInitWindowPosition(100,120);//设置窗口的位置 glutCreateWindow("矩形");//创建一个名为矩形的窗口 glutDisplayFunc(Display);//设置当前窗口的显示回调函数 Initial();//完成窗口初始化 glutMainLoop();//启动主GLUT事件处理循环 return0;}三、实验结果分析实验二直线生成算法与用户接口与交互式技术一、实验目的1、掌握直线生成算法(1)DDA算法(2)Bresenham算法掌握交互式技术(1)鼠标(2)键盘数字键、字母键、功能键和特殊键3、实现拾取操作二、实验内容1、以下给出了DDA算法的C++代码,请参考用VisualC++实现Bresenham算法。实现DDA画线程序实验步骤:1.建立一个DDALine的工程文件;2.添加ddaline()成员函数方法:在工作区中选择CLASSVIEW类窗口,右击CDDAlineView类,选择“addmemberfunction…”,定义如下的成员函数:voidddaline(CDC*pDC,intx0,inty0,intx1,inty1,COLORREFcolor);3.编写自定义的成员函数ddaline()程序voidCDDALineView::ddaline(CDC*pDC,intx0,inty0,intx1,inty1,COLORREFcolor){intlength,i;floatx,y,dx,dy;length=abs(x1-x0);if(abs(y1-y0)>length)length=abs(y1-y0);dx=(x1-x0)/length;dy=(y1-y0)/length;x=x0+0.5;y=y0+0.5;for(i=1;i<=length;i++){pDC->SetPixel((int)x,(int)y,color);x=x+dx;y=y+dy;}}4.编写OnDraw()函数voidCDDALineView::OnDraw(CDC*pDC){CDDALineDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahereddaline(pDC,100,100,400,100,RGB(255,0,0));ddaline(pDC,400,100,400,400,RGB(0,255,0));ddaline(pDC,400,400,100,400,RGB(0,0,255));ddaline(pDC,100,400,100,100,RGB(255,255,0));ddaline(pDC,100,100,400,400,RGB(255,0,255));ddaline(pDC,100,400,400,100,RGB(0,255,255));}}5.编译、调试和运行程序,查看程序结果。2、以下是利于鼠标和字母键实现用户接口与交互式技术,请参照利用特殊键实现,写出其代码。(1)OPENGL中利用鼠标实现橡皮筋技术的例子#include<gl/glut.h>intiPointNum=0;//已确定点的数目intx1=0,x2=0,y1=0,y2=0;//确定的点坐标intwinWidth=400,winHeight=300;//窗口的宽度和高度voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f);}voidChangeSize(intw,inth){ winWidth=w; winHeight=h; glViewport(0,0,w,h);//指定窗口显示区域 glMatrixMode(GL_PROJECTION);//设置投影参数 glLoadIdentity(); gluOrtho2D(0.0,winWidth,0.0,winHeight);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); if(iPointNum>=1) { glBegin(GL_LINES);//绘制直线段 glVertex2i(x1,y1); glVertex2i(x2,y2); glEnd(); } glutSwapBuffers();//交换缓冲区}voidMousePlot(GLintbutton,GLintaction,GLintxMouse,GLintyMouse){ if(button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN) { if(iPointNum==0||iPointNum==2){ iPointNum=1; x1=xMouse; y1=winHeight-yMouse; } else{ iPointNum=2; x2=xMouse; y2=winHeight-yMouse; glutPostRedisplay();//指定窗口重新绘制 } } if(button==GLUT_RIGHT_BUTTON&&action==GLUT_DOWN){ iPointNum=0; glutPostRedisplay(); }}voidPassiveMouseMove(GLintxMouse,GLintyMouse){ if(iPointNum==1) { x2=xMouse; y2=winHeight-yMouse; glutPostRedisplay(); } }intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//使用双缓存及RGB模型 glutInitWindowSize(400,300); glutInitWindowPosition(100,100); glutCreateWindow("橡皮筋技术"); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize);//指定窗口在整形回调函数 glutMouseFunc(MousePlot);//指定鼠标响应函数 glutPassiveMotionFunc(PassiveMouseMove);//指定鼠标移动响应函数 Initial(); glutMainLoop(); return0;}(2)OPENGL中利用键盘实现橡皮筋技术的例子#include<gl/glut.h>intiPointNum=0;//已确定点的数目intx1=0,x2=0,y1=0,y2=0;//确定的点坐标intwinWidth=400,winHeight=300;//窗口的宽度和高度voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f);}voidChangeSize(intw,inth){ winWidth=w; winHeight=h; glViewport(0,0,w,h);//指定窗口显示区域 glMatrixMode(GL_PROJECTION);//设置投影参数 glLoadIdentity(); gluOrtho2D(0.0,winWidth,0.0,winHeight);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); if(iPointNum>=1) { glBegin(GL_LINES);//绘制直线段 glVertex2i(x1,y1); glVertex2i(x2,y2); glEnd(); } glutSwapBuffers();//交换缓冲区}voidPassiveMouseMove(GLintxMouse,GLintyMouse){ if(iPointNum==1) { x2=xMouse; y2=winHeight-yMouse; glutPostRedisplay(); } }voidKey(unsignedcharkey,intx,inty){ switch(key){ case'p': if(iPointNum==0||iPointNum==2) { iPointNum=1; x1=x; y1=winHeight-y; } else { iPointNum=2; x2=x; y2=winHeight-y; glutPostRedisplay(); } break; default:break; } }intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//使用双缓存及RGB模型 glutInitWindowSize(400,300); glutInitWindowPosition(100,100); glutCreateWindow("橡皮筋技术"); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize);//指定窗口在整形回调函数 glutKeyboardFunc(Key);//指定键盘响应函数 glutPassiveMotionFunc(PassiveMouseMove);//指定鼠标移动响应函数 Initial(); glutMainLoop(); return0;}3、以下OPENGL实现拾取操作的例子,请掌握实现拾取操作的方法。#include<gl/glut.h>#include"stdio.h"constGLintpickSize=32;intwinWidth=400,winHeight=300;voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f);}voidDrawRect(GLenummode){ if(mode==GL_SELECT)glPushName(1);//压入堆栈 glColor3f(1.0f,0.0f,0.0f); glRectf(60.0f,50.0f,150.0f,150.0f); if(mode==GL_SELECT)glPushName(2);//压入堆栈glColor3f(0.0f,1.0f,0.0f);glRectf(230.0f,50.0f,330.0f,150.0f);if(mode==GL_SELECT)glPushName(3);//压入堆栈glColor3f(0.0f,0.0f,1.0f);glRectf(140.0f,140.0f,240.0f,240.0f);}voidProcessPicks(GLintnPicks,GLuintpickBuffer[]){ GLinti; GLuintname,*ptr;printf("选中的数目为%d个\n",nPicks);ptr=pickBuffer;for(i=0;i<nPicks;i++){ name=*ptr;//选中图元在堆栈中的位置 ptr+=3;//跳过名字和深度信息 ptr+=name-1;//根据位置信息获得选中的图元名字 if(*ptr==1)printf("你选择了红色图元\n"); if(*ptr==2)printf("你选择了绿色图元\n"); if(*ptr==3)printf("你选择了蓝色图元\n"); ptr++;} printf("\n\n");}voidChangeSize(intw,inth){winWidth=w;winHeight=h;glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,winWidth,0.0,winHeight);}voidDisplay(void){glClear(GL_COLOR_BUFFER_BIT);DrawRect(GL_RENDER);glFlush();}voidMousePlot(GLintbutton,GLintaction,GLintxMouse,GLintyMouse){GLuintpickBuffer[pickSize];GLintnPicks,vp[4];if(button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN){ glSelectBuffer(pickSize,pickBuffer);//设置选择缓冲区glRenderMode(GL_SELECT);//激活选择模式glInitNames();//初始化名字堆栈glMatrixMode(GL_PROJECTION);glPushMatrix();glLoadIdentity();glGetIntegerv(GL_VIEWPORT,vp);//定义一个10×10的选择区域gluPickMatrix(GLdouble(xMouse),GLdouble(vp[3]-yMouse),10.0,10.0,vp);gluOrtho2D(0.0,winWidth,0.0,winHeight);DrawRect(GL_SELECT);//恢复投影变换glMatrixMode(GL_PROJECTION);glPopMatrix();glFlush();//获得选择集并输出nPicks=glRenderMode(GL_RENDER);ProcessPicks(nPicks,pickBuffer);glutPostRedisplay();}}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(400,300);glutInitWindowPosition(100,100);glutCreateWindow("拾取操作");glutDisplayFunc(Display);glutReshapeFunc(ChangeSize);glutMouseFunc(MousePlot);Initial();glutMainLoop();return0;}三、实验结果分析实验三圆和椭圆及其它基本图形的生成算法一、实验目的(1)掌握和利用VC++实现Bresenham算法;(2)掌握利用OPENGL绘制简单多面体、二次及三次曲线的方法;(3)掌握利用OPENGL层次建模的方法。二、实验内容(1)用VisualC++实现圆和椭圆的Bresenham算法;(2)利用OPENGL绘制简单多面体、二次及三次曲线;(3)利用OPENGL层次建模1、VisualC++实现圆的Bresenham生成算法的参考程序如下,请在MFC环境下实现程序实现步骤:(1)建立MidPointCircle工程文件;(2)右击CMidPointCircleView类,建立成员函数voidMidpointCircle(CDC*pDC,intx0,inty0,intr,COLORREFcolor)intCirPot(CDC*pDC,intx0,inty0,intx,inty,COLORREFcolor)(3)编写成员函数代码,程序如下:voidCMidPointCircleView::MidpointCircle(CDC*pDC,intx0,inty0,intr,COLORREFcolor){intx,y;floatd;x=0;y=r;d=1.25-r;CirPot(pDC,x0,y0,x,y,color);while(x<=y){if(d<0){d+=2*x+3;x++;}else{d+=2*(x-y)+5;x++;y--;}CirPot(pDC,x0,y0,x,y,color);}/*while*/}intCMidPointCircleView::CirPot(CDC*pDC,intx0,inty0,intx,inty,COLORREFcolor){pDC->SetPixel((x0+x),(y0+y),color);pDC->SetPixel((x0+y),(y0+x),color);pDC->SetPixel((x0+y),(y0-x),color);pDC->SetPixel((x0+x),(y0-y),color);pDC->SetPixel((x0-x),(y0-y),color);pDC->SetPixel((x0-y),(y0-x),color);pDC->SetPixel((x0-y),(y0+x),color);pDC->SetPixel((x0-x),(y0+y),color);return0;}(4)编写OnDraw(CDC*pDC)函数,程序如下:voidCMidPointCircleView::OnDraw(CDC*pDC){CMidPointCircleDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahereMidpointCircle(pDC,100,100,10,RGB(255,0,0));MidpointCircle(pDC,500,300,60,RGB(255,255,0));}(6)编译、运行程序,查看结果。2、编写画椭圆法的扫描转换程序,请在MFC环境下实现程序实现步骤:(1)建立MidPointEllise工程文件;(2)右击CMidPointElliseView类,建立成员函数voidMidpointEllise(CDC*pDC,intx0,inty0,inta,intb,COLORREFcolor)(3)编写成员函数代码,程序如下:voidCMidPointEllipseView::MidpointEllise(CDC*pDC,intx0,inty0,inta,intb,COLORREFcolor){intx,y;floatd1,d2;x=0;y=b;d1=b*b+a*a*(-b+0.25);pDC->SetPixel(x+x0,y+y0,color);while(b*b*(x+1)<a*a*(y-0.5)){if(d1<0){d1+=b*b*(2*x+3);x++;}else{d1+=(b*b*(2*x+3)+a*a*(-2*y+2));x++;y--;}pDC->SetPixel(x0+x,y0+y,color);pDC->SetPixel(x0+x,y0-y,color);pDC->SetPixel(x0-x,y0+y,color);pDC->SetPixel(x0-x,y0-y,color);}//上半部分d2=(b*(x+0.5))*(b*(x+0.5))+(a*(y-1))*(a*(y-1))-(a*b)*(a*b);while(y>0){if(d2<0){d2+=b*b*(2*x+2)+a*a*(-2*y+3);x++;y--;}else{d2+=a*a*(-2*y+3);y--;}pDC->SetPixel(x0+x,y0+y,color);pDC->SetPixel(x0+x,y0-y,color);pDC->SetPixel(x0-x,y0+y,color);pDC->SetPixel(x0-x,y0-y,color);}//下半部分}(4)编写OnDraw()函数voidCMidPointEllipseView::OnDraw(CDC*pDC){CMidPointEllipseDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahereMidpointEllise(pDC,300,200,50,20,RGB(255,0,0));}编译、运行程序。3、用OPENGL绘制简单多面体、二次及三次曲线的例子#include<gl/glut.h>staticGLsizeiiMode=1;staticGLfloatxRot=0.0f;//x方向旋转参数staticGLfloatyRot=0.0f;//y方向旋转参数GLUquadricObj*obj;//二次曲面对象voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f); glColor3f(0.0f,0.0f,0.0f); obj=gluNewQuadric(); gluQuadricDrawStyle(obj,GLU_LINE);//以线框方式绘制二次曲面对象}voidChangeSize(intw,inth){glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1.5f,1.5f,-1.5f,1.5f);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(xRot,1.0f,0.0f,0.0f);//旋转图形 glRotatef(yRot,0.0f,1.0f,0.0f);//旋转图形 //指定需要绘制的图元 switch(iMode) { case1: glutWireTetrahedron(); break; case2: glutSolidTetrahedron(); break; case3: glutWireOctahedron(); break; case4: glutSolidOctahedron(); break; case5: glutWireSphere(1.0f,15,15); break; case6: glutSolidSphere(1.0f,15,15);break; case7: glutWireTeapot(1.0f);break; case8: glutSolidTeapot(1.0f);break; case9: gluSphere(obj,1.0f,15,15); break; case10: gluCylinder(obj,1.0f,0.0f,1.0f,15,15); break; case11: gluPartialDisk(obj,0.3f,0.8f,15,15,30.0f,260.0f); break; default:break; } glFlush();}voidProcessMenu(intvalue){ iMode=value; glutPostRedisplay();}voidSpecialKeys(intkey,intx,inty){ if(key==GLUT_KEY_UP) xRot-=5.0f; if(key==GLUT_KEY_DOWN) xRot+=5.0f; if(key==GLUT_KEY_LEFT) yRot-=5.0f; if(key==GLUT_KEY_RIGHT) yRot+=5.0f; if(xRot>356.0f) xRot=0.0f; if(xRot<-1.0f) xRot=355.0f; if(yRot>356.0f) yRot=0.0f; if(yRot<-1.0f) yRot=355.0f; glutPostRedisplay();}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow("OpenGL模型绘制函数示例"); //创建菜单并定义菜单回调函数 intnGlutPolyMenu=glutCreateMenu(ProcessMenu); glutAddMenuEntry("线框正四面体",1);//创建GLUT多面体绘制菜单 glutAddMenuEntry("实体正四面体",2); glutAddMenuEntry("线框正八面体",3); glutAddMenuEntry("实体正八面体",4); intnGlutCurveMenu=glutCreateMenu(ProcessMenu);//创建GLUT曲面绘制菜单 glutAddMenuEntry("线框球面",5); glutAddMenuEntry("实体球面",6); glutAddMenuEntry("线框茶壶",7); glutAddMenuEntry("实体茶壶",8); intnGluCurveMenu=glutCreateMenu(ProcessMenu);//创建GLU曲面绘制菜单 glutAddMenuEntry("线框球面",9); glutAddMenuEntry("线框圆锥面",10); glutAddMenuEntry("线框圆环面",11); intnMainMenu=glutCreateMenu(ProcessMenu);//创建主菜单 glutAddSubMenu("GLUT多面体",nGlutPolyMenu); glutAddSubMenu("GLUT曲面",nGlutCurveMenu); glutAddSubMenu("GLU曲面",nGluCurveMenu); glutAttachMenu(GLUT_RIGHT_BUTTON); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); Initial(); glutMainLoop(); return0;}4、用OPENGL实现绘制奥运五环标志#include<gl/glut.h>GLuintOlympicRings;voidInitial(void){ glClearColor(1.0f,1.0f,1.0f,1.0f); OlympicRings=glGenLists(1); glNewList(OlympicRings,GL_COMPILE); glColor3f(1.0,1.0,0.0); glTranslatef(-22.0,0.0,0.0); glutSolidTorus(0.5,20.0,15,50);//绘制黄色环 glColor3f(0.0,1.0,0.0); glTranslatef(44.0,0.0,0.0); glutSolidTorus(0.5,20.0,15,50);//绘制绿色环 glColor3f(0.0,0.0,0.0); glTranslatef(-22.0,30.0,0.0); glutSolidTorus(0.5,20.0,15,50);//绘制黑色环 glColor3f(0.0,0.0,1.0); glTranslatef(-42.0,0.0,0.0); glutSolidTorus(0.5,20.0,15,50);//绘制蓝色环 glColor3f(1.0,0.0,0.0); glTranslatef(84.0,0.0,0.0); glutSolidTorus(0.5,20.0,15,50);//绘制红色环 glEndList();}voidChangeSize(intw,inth){ glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-70.0f,70.0f,-70.0f,70.0f);}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glCallList(OlympicRings);//调用显示列表 glFlush();}intmain(intargc,char*argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow("OpenGL模型绘制函数示例"); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize); Initial(); glutMainLoop(); return0;}实验结果分析实验四裁剪算法一、实验目的(1)掌握Cohen-Sutherland线段裁剪算法,中点分割线段裁剪算法,Liang-Barsky裁剪算法。(2)掌握反走样技术二、实验内容1)实现Cohen-Sutherland线段裁剪算法;2)实现中点分割线段裁剪算法;3)实现Liang-Barsky裁剪算法;4)实现反走样技术。1、Cohen-Sutherland编码裁剪算法的程序设计,请在MFC环境下实现,并理解代码。实验环境:VisualC++步骤1:创建Code_Clip工程文件;步骤2:在主程序的程序头部定义符号常量#defineLEFT1#defineRIGHT2#defineBOTTOM4#defineTOP8步骤3:定义成员变量和成员函数intWT;intWB;intWR;intWL;intC_S_Line(CDC*pDC,intx1,inty1,intx2,inty2);voidencode(intx,inty,int*code);步骤4:在构造函数中为窗口边界变量赋初值CCode_ClipView::CCode_ClipView(){//TODO:addconstructioncodehereWL=100;WR=400;WB=100;WT=300;}步骤5:编写成员函数程序voidCCode_ClipView::encode(intx,inty,int*code){intc=0;if(x<WL)c=c|LEFT;elseif(x>WR)c=c|RIGHT;if(y<WB)c=c|BOTTOM;elseif(y>WT)c=c|TOP;*code=c;}intCCode_ClipView::C_S_Line(CDC*pDC,intx1,inty1,intx2,inty2){//CDC*pDC=GetDC();intcode1,code2,code,x,y;encode(x1,y1,&code1);//(x1,y1)处的编码encode(x2,y2,&code2);//(x1,y1)处的编码while(code1!=0||code2!=0)//当code1不等于0或code2不等于0{if((code1&code2)!=0)return0;//当code1与code2不等于0,在同侧;code=code1;if(code1==0)code=code2;if((LEFT&code)!=0)//求交点{x=WL;y=y1+(y2-y1)*(WL-x1)/(x2-x1);}elseif((RIGHT&code)!=0){x=WR;y=y1+(y2-y1)*(WR-x1)/(x2-x1);}elseif((BOTTOM&code)!=0){y=WB;x=x1+(x2-x1)*(WB-y1)/(y2-y1);}elseif((TOP&code)!=0){y=WT;x=x1+(x2-x1)*(WT-y1)/(y2-y1);}if(code==code1){x1=x;y1=y;encode(x,y,&code1);}else{x2=x;y2=y;encode(x,y,&code2);}}//endwhile,表示code1,code2都为0,其中的线段为可视部分pDC->MoveTo(x1,y1);pDC->LineTo(x2,y2);}步骤6:编写OnDraw()程序voidCCode_ClipView::OnDraw(CDC*pDC){CCode_ClipDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahere//定义三条直线的坐标intx11,y11,x21,y21,x12,y12,x22,y22,x13,y13,x23,y23;x11=50;y11=150;x21=450;y21=250;x12=150;y12=150;x22=350;y22=240;x13=50;y13=400;x23=500;y23=350;//定义画笔CPenPenRed(PS_SOLID,1,RGB(255,0,0));//定义红色笔CPenPenBlue(PS_SOLID,1,RGB(0,0,255));//定义蓝色笔//先画出窗口,用蓝色pDC->SelectObject(&PenBlue);pDC->Rectangle(WL,WB,WR,WT);//先画出三条直线,用红线pDC->SelectObject(&PenRed);pDC->MoveTo(x11,y11);pDC->LineTo(x21,y21);pDC->MoveTo(x12,y12);pDC->LineTo(x22,y22);pDC->MoveTo(x13,y13);pDC->LineTo(x23,y23);//用蓝线,画出裁剪三条线pDC->SelectObject(&PenBlue);C_S_Line(pDC,x11,y11,x21,y21);C_S_Line(pDC,x12,y12,x22,y22);C_S_Line(pDC,x13,y13,x23,y23);}步骤7:编译、调试,查看运行结果。2、多边形裁剪实验环境:VisualC++和OpenGL/*代码*/#include<windows.h>#include<GL/gl.h>#include<GL/glu.h>#include<GL/glaux.h>voidmyinit(void);voidCALLBACKmyReshape(GLsizeiw,GLsizeih);voidCALLBACKdisplay(void);voidCALLBACKdisplay(void){GLdoubleeqn[4]={1.0,0.0,0.0,0.0};glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,0.0,1.0);glPushMatrix();glTranslatef(0.0,0.0,-5.0);/*cliptheleftpartofwire_sphere:x<0*/glClipPlane(GL_CLIP_PLANE0,eqn);glEnable(GL_CLIP_PLANE0);glRotatef(-90.0,1.0,0.0,0.0);auxWireSphere(1.0);glPopMatrix();glFlush();}voidmyinit(void){glShadeModel(GL_FLAT);}voidCALLBACKmyReshape(GLsizeiw,GLsizeih){ glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,20.0);glMatrixMode(GL_MODELVIEW);}voidmain(void){auxInitDisplayMode(AUX_SINGLE|AUX_RGB);auxInitPosition(0,0,500,500);auxInitWindow("裁剪例子");myinit();auxReshapeFunc(myReshape);auxMainLoop(display);}运行结果:3、OPENGL实现直线段的反走样#include<gl/glut.h>GLuintlineList;//指定显示列表IDvoidInitial(){ glClearColor(1.0f,1.0f,1.0f,0.0f); glLineWidth(12.0f); glColor4f(0.0,0.6,1.0,1.0); lineList=glGenLists(1); glNewList(lineList,GL_COMPILE);//定义显示列表 glBegin(GL_LINE_LOOP); glVertex2f(1.0f,1.0f); glVertex2f(4.0f,2.0f); glVertex2f(2.0f,5.0f); glEnd(); glEndList();}voidChangeSize(GLsizeiw,GLsizeih){ if(h==0) h=1; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) gluOrtho2D(0.0,5.0,0.0,6.0*(GLfloat)h/(GLfloat)w); else gluOrtho2D(0.0,5.0*(GLfloat)w/(GLfloat)h,0.0,6.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}voidDisplayt(void){ glClear(GL_COLOR_BUFFER_BIT); glCallList(lineList);glFlush();}voidDisplayw(void){ glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_LINE_SMOOTH);//使用反走样 glEnable(GL_BLEND);//启用混合函数 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);//指定混合函数 glCallList(lineList);glFlush();}voidmain(void){ glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(300,300); glutCreateWindow("原始图形"); glutDisplayFunc(Displayt);glutReshapeFunc(ChangeSize); Initial(); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(300,300); glutInitWindowSize(300,300); glutCreateWindow("反走样图形"); glutDisplayFunc(Displayw);glutReshapeFunc(ChangeSize); Initial(); glutMainLoop();}三、实验结果分析实验五二维变换一、实验目的(1)二维平移变换原理(2)二维缩放变换(3)二维对称变换(4)二维旋转变换(5)二维错切变换二、实验内容1、实现一个三角形的基本几何变换。 实验环境:OpenGL和VisualC++。/*代码*/#include<windows.h>#include<GL/gl.h>#include<GL/glu.h>#include<GL/glaux.h>voidmyinit(void);voiddraw_triangle(void);voidCALLBACKdisplay(void);voidCALLBACKmyReshape(GLsizeiw,GLsizeih);voiddraw_triangle(void){glBegin(GL_LINE_LOOP);glVertex2f(0.0,25.0);glVertex2f(25.0,-25.0);glVertex2f(-25.0,-25.0);glEnd();}voidCALLBACKdisplay(void){glClearColor(0.0,0.0,0.0,1.0);glClear(GL_COLOR_BUFFER_BIT);/*drawanoriginaltriangle*/glLoadIdentity();glColor3f(1.0,1.0,1.0);/*white*/draw_triangle(); /*translatingatrianglealongX_axis*/ glLoadIdentity(); glTranslatef(-20.0,0.0,0.0);glColor3f(1.0,0.0,0.0);/*red*/draw_triangle(); /*scalingatrianglealongX_axisby1.5andalongY_axisby0.5*/glLoadIdentity();glScalef(1.5,0.5,1.0);glColor3f(0.0,1.0,0.0);/*green*/draw_triangle(); /*rotatingatriangleinacounterclockwisedirectionaboutZ_axis*/glLoadIdentity();glRotatef(90.0,0.0,0.0,1.0);glColor3f(0.0,0.0,1.0);/*blue*/draw_triangle(); /*scalingatrianglealongY_axisandreflectingitaboutY_axis*/glLoadIdentity();glScalef(1.0,-0.5,1.0);glColor3f(1.0,1.0,0.0);/*yellow*/draw_triangle();glFlush();}voidmyinit(void){ glShadeModel(GL_FLAT);}voidCALLBACKmyReshape(GLsizeiw,GLsizeih){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w<=h)glOrtho(-50.0,50.0,-50.0*(GLfloat)h/(GLfloat)w,50.0*(GLfloat)h/(GLfloat)w,-1.0,1.0);elseglOrtho(-50.0*(GLfloat)w/(GLfloat)h,50.0*(GLfloat)w/(GLfloat)h,-50.0,50.0,-1.0,1.0);glMatrixMode(GL_MODELVIEW);}voidmain(void){auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);auxInitPosition(0,0,500,500);auxInitWindow("三角形的基本几何变换"); myinit();auxReshapeFunc(myReshape);auxMainLoop(display);}运行结果:2、参考例子2 #include<gl/glut.h>voidinitial(void){ glClearColor(1.0,1.0,1.0,1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-10.0,10.0,-10.0,10.0);//指定二维裁剪窗口}voidtriangle(GLsizeimode){ if(mode==1) glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//多边形模式为线框 else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);//多边形模式为填充多边形 glBegin(GL_TRIANGLES); glVertex2f(0.0,5.0); glVertex2f(5.0,-5.0); glVertex2f(-5.0,-5.0); glEnd();}voidDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); glViewport(0,0,200,200);//指定从0,0开始长宽均为200的 triangle(1); glColor3f(0.0,0.0,1.0); glViewport(200,0,200,200); triangle(2); glFlush();}voidmain(void){ glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(100,100); glutInitWindowSize(400,200); glutCreateWindow("多视区"); initial(); glutDisplayFunc(Display); glutMainLoop();}三、实验结果分析实验六三维变换一、实验目的掌握三维平移变换,三维缩放变换,三维旋转变换,三维平行投影。二、实验内容1)利用三维变换原理实现一个三维长方体物体的平移,旋转和缩放变换。2)利用三维透视原理实现平行投影和透视投影。1、绘制一个三维的正面透视立方体。采用OpenGL进行编程。实验环境:OpenGL和VisualC++。#include<windows.h>#include<GL/gl.h>#include<GL/glu.h>#include<GL/glaux.h>voidmyinit(void);voidCALLBACKmyReshape(GLsizeiw,GLsizeih);voidCALLBACKdisplay(void);voidCALLBACKdisplay(void){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,1.0,1.0);glLoadIdentity();/*clearthematrix*/glTranslatef(0.0,0.0,-5.0);/*viewingtransformation*/glScalef(1.0,2.0,1.0);/*modelingtransformation*/auxWireCube(1.0);/*drawthecube*/glFlush();}voidmyinit(void){glShadeModel(GL_FLAT);}voidCALLBACKmyReshape(GLsizeiw,GLsizeih){ glMatrixMode(GL_PROJECTION);/*prepareforandthen*/glLoadIdentity();/*definetheprojection*/glFrustum(-1.0,1.0,-1.0,1.0,1.5,20.0);/*transformation*/glMatrixMode(GL_MODELVIEW);/*backtomodelviewmatrix*/glViewport(0,0,w,h);/*definetheviewport*/}voidmain(void){auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);auxInitPosition(0,0,500,500);auxInitWindow("三维的正面透视立方体");myinit();auxReshapeFunc(myReshape);auxMainLoop(display);}2、利用OPENGL建立分子模型#include<gl/glut.h>voidInitial(){ glEnable(GL_DEPTH_TEST);//启用深度测试 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);//指定透视投影的观察空间 glMatrixMode(GL_MODELVIEW); glLoadIdentity();}voidDisplay(void){ staticflo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 铜山区幼儿园劳务合同
- 提前还贷后贷款合同处理
- 《坚持依法行政》课件
- 《血压测量》课件
- 2025年岳阳货运资格证题库在线练习
- 2025年来宾货运资格证模拟考试题库下载
- 保障性住房交易附加条款
- 零售行业文员聘用合同样本
- 增资扩股协议书
- 花店摊位租赁合同
- 2024年廉洁经营承诺书2篇
- 《如何培养良好心态》课件
- 《中医养生肾》课件
- 2024年二级建造师考试建筑工程管理与实务试题及解答参考
- 乡镇(街道)和村(社区)应急预案编制管理百问百答
- 医院培训课件:《乳腺癌解读》
- 中国高血压防治指南(2024年修订版)核心要点解读
- 2024年人教版八年级语文上册期末考试卷(附答案)
- 辽宁省大连市2023-2024学年高三上学期双基测试(期末考试) 物理 含解析
- 2024网络数据安全管理条例全文解读课件
- 2024年刑法知识考试题库含答案(综合卷)
评论
0/150
提交评论