《计算机图形学实验报告》由会员分享,可在线阅读,更多相关《计算机图形学实验报告(35页珍藏版)》请在人人文库网上搜索。
1、计 算 机 图 形 学课程实验报告姓名:学号:目 录实验一 直线的DDA算法一、【实验目的】1.掌握DDA算法的基本原理。2.掌握DDA直线扫描转换算法。3.深入了解直线扫描转换的编程思想。二、【实验内容】1.利用DDA的算法原理,编程实现对直线的扫描转换。2.加强对DDA算法的理解和掌握。三、【测试数据及其结果】四、【实验源代码】#include#include#include#includeGLsizei winWidth=500;GLsizei winHeight=500;void Initial(void)glClearColor(1.0f,1.0f,1.0f,1.0f);glMatr。
2、ixMode(GL_PROJECTION);gluOrtho2D(0.0,200.0,0.0,150.0);void DDALine(int x0,int y0,int x1,int y1)glColor3f(1.0,0.0,0.0);int dx,dy,epsl,k;float x,y,xIncre,yIncre;dx=x1-x0; dy=y1-y0;x=x0; y=y0;if(abs(dx)abs(dy) epsl=abs(dx);else epsl=abs(dy);xIncre=(float)dx/(float)epsl;yIncre=(float)dy/(float)epsl;for(。
3、k=0;k#include#include#includeGLsizei winWidth=500;GLsizei winHeight=500;void lineBres(int x0, int y0, int xEnd, int yEnd) glColor3f(0.0, 0.0, 1.0);int dx=fabs(xEnd-x0), dy=fabs(yEnd-y0);int p=2*dy-dx;int twoDy=2*dy, twoDyMinusDx=2*(dy-dx);int x, y;if (x0xEnd) x=xEnd;y=yEnd;xEnd=x0;elsex=x0;y=y0;glPo。
4、intSize(6);glBegin(GL_POINTS);glVertex2i(x, y);glEnd();while (xvoid init()glClearColor(0,0,0,0);void MidBresenhamCircle(int r)int x,y,d;x=0;y=r;d=1-r;glBegin(GL_LINE_STRIP);while(x#pragma comment(linker,/subsystem:windows /entry:mainCRTStartup)GLuint lineList; /指定显示列表void Initial()glClearColor(1.0f,。
5、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();void ChangeSize(GLsizei w,GLsizei h)if(h=0) h=1;glViewpo。
6、rt(0,0,w,h);glMatrixMode(GL_PROJECTION); /指定设置投影参数glLoadIdentity();if(w#include #pragma comment(linker,/subsystem:windows /entry:mainCRTStartup)const float PI=3.1415;void DrawCircle(GLfloat radius)GLfloat x,y,z;glBegin(GL_LINE_LOOP);for (int alpha=0;alpha#includeconst float PI=3.1415;void initial(vo。
7、id)glClearColor(1.0,1.0,1.0,1.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(-10.0,10.0,-10.0,10.0);void triangle(GLsizei mode)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);g。
8、lVertex2f(-5.0,-5.0);glEnd();void polygon(GLsizei mode)if(mode=1)glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);glBegin(GL_POLYGON);glVertex2f(2.0,7.0);glVertex2f(5.0,3.0);glVertex2f(4.0,0.0);glVertex2f(0.0,0.0);glVertex2f(1.0,4.0);glEnd();void DrawCircle(GLfl。
9、oat r)GLfloat x,y,z;glBegin(GL_LINE_LOOP);for (int alpha=0;alphaGLint angleSelf=0;void Initial()glEnable(GL_DEPTH_TEST);glClearColor(1.0f,1.0f,1.0f,1.0f);void ChangeSize(int w,int h)if(h=0) h=1;glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();GLfloat fAspect;fAspect=(float)w/(float)h。
10、;gluPerspective(45.0,fAspect,1,500.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();void Display(void)static float fElect1=0.0f;glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslatef(0.0f,0.0f,-250.0f);glColor3f(1.0f,0.0f,0.0f);glutWireSphere(12.0f,15。
11、,15);glColor3f(0.0f,1.0f,0.0f);glPushMatrix();glRotatef(fElect1,0.0f,1.0f,0.0f);glTranslatef(90.0f,0.0f,0.0f);glRotatef(angleSelf,0,1,0);glutWireSphere(6.0f,15,15);glPopMatrix();glPushMatrix();glRotatef(45.0f,0.0f,0.0f,1.0f);glRotatef(fElect1,0.0f,1.0f,0.0f);glTranslatef(-70.0f,0.0f,0.0f);glRotatef(。
12、angleSelf,0,1,0);glutWireSphere(6.0f,15,15);glPopMatrix();glPushMatrix();glRotatef(-45.0f,0.0f,0.0f,1.0f);glRotatef(fElect1,0.0f,1.0f,0.0);glTranslatef(0.0f,0.0f,60.0f);glRotatef(angleSelf,0,1,0);glutWireSphere(6.0f,15,15);glPopMatrix();fElect1 +=5.0f;if(fElect1360.0f) fElect1=10.0f;glutSwapBuffers(。
13、);void RotateSelf(int value)if(value=1)angleSelf+=5;angleSelf%=360;glutPostRedisplay();glutTimerFunc(100,RotateSelf,1);void TimerFunc(int value)glutPostRedisplay();glutTimerFunc(100,TimerFunc,1);int main(int argc,char*argv)glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);glu。
14、tCreateWindow(分子动画示例);glutReshapeFunc(ChangeSize);glutDisplayFunc(Display);glutTimerFunc(500,TimerFunc,1);glutTimerFunc(100,RotateSelf,1);Initial();glutMainLoop();return 0;实验六 Bezier曲线一、【实验目的】1.掌握Bezire曲线定义。2.掌握设计绘制一次、二次和三次Bezier曲线算法。二、【实验内容】1.绘制NURBS曲面。2.基于Bezier定义根据控制多边形的阶次绘制Bezier曲线。三、【测试数据及其结果】四。
15、、【实验源代码】原实验代码:#include#include#includeclass Pt3Dpublic:GLfloat x,y,z;void GetCnk(GLint n,GLint *c)GLint i,k;for(k=0;k=k+1;i-)ck=ck*i;for(i=n-k;i=2;i-)ck=ck/i;void GetPointPr(GLint *c,GLfloat t,Pt3D*Pt,int ControlN,Pt3D*ControlP)GLint k,n=ControlN-1;GLfloat Bernstein;Pt-x=0.0;Pt-y=0.0;Pt-z=0.0;for(k=。
16、0;kx+=ControlPk.x*Bernstein;Pt-y+=ControlPk.y*Bernstein;Pt-z+=ControlPk.z*Bernstein;void BezierCurve(GLint m,GLint ControlN,Pt3D *ControlP)GLint *C,i;Pt3D CurvePt;C=new GLintControlN;GetCnk(ControlN-1,C);glBegin(GL_POINTS);for(i=0;ivoid initial(void)glClearColor(1.0,1.0,1.0,1.0);glLineWidth(4.0);GLf。
17、loat ControlP43=-80.0,40.0,0.0,-10.0,90.0,0.0,10.0,-90.0,0.0,80.0,40.0,0.0;glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,3,4,*ControlP);glEnable(GL_MAP1_VERTEX_3);void Display(void)glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,0.0,0.0);glMapGrid1f(100,0.0,1.0);glEvalMesh1(GL_LINE,0,100);glFlush();void Reshape(GLint。
18、 newWidth,GLint newHeight)glViewport(0,0,newWidth,newHeight);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(-100.0,100.0,-100.0,100.0);void main(void)glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(100,100);glutInitWindowSize(400,400);glutCreateWindow(Bezier曲线);initial();gl。
19、utDisplayFunc(Display);glutReshapeFunc(Reshape);glutMainLoop();实验七 NURBS曲面和Bezier曲面一、【实验目的】1.掌握NURBS曲线定义。2.掌握设计绘制一次、二次和三次NURBS曲面算法。二、【实验内容】1.在屏幕上单击鼠标左键绘制控制多边形,基于NURBS定义根据控制多边形的阶次绘制NURBS曲面。2.绘制的曲面中,红色的点表示曲面的控制点,并增加了光标键控制旋转的交互式方式,以获得更好的显示效果。三、【测试数据及其结果】四、【实验源代码】NURBS曲面:#include#include#includeGLUnurbs。
20、Obj*pNurb=NULL;GLint nNumPoints=4;GLfloat ctrlPoints443= -6.0f,-6.0f,0.0f,-6.0f,-2.0f,0.0f,-6.0f,2.0f,0.0f,-6.0f,6.0f,0.0f,-2.0f,-6.0f,0.0f,-2.0f,-2.0f,8.0f,-2.0f,2.0f,8.0f,-2.0f,6.0f,0.0f,2.0f,-6.0f,0.0f,2.0f,-2.0f,8.0f,2.0f,2.0f,8.0f,2.0f,6.0f,0.0f,6.0f,-6.0f,0.0f,6.0f,-2.0f,0.0f,6.0f,2.0f,0.0f,6.。
21、0f,6.0f,0.0f;GLfloat Knots8=0.0f,0.0f,0.0f,0.0f,1.0f,1.0f,1.0f,1.0f;static GLfloat xRot=0.0f;static GLfloat yRot=0.0f;void DrawPoints(void)int i,j;glPointSize(5.0f);glColor3ub(255,0,0);glBegin(GL_POINTS);for(i=0;i356.0f) xRot=0.0f;if(xRot356.0) yRot=0.0f;if(yRot#include #include GLUnurbsObj*pNurb=NU。
22、LL;GLint nNumPoints=4;GLfloat Knots8=0.0f,0.0f,0.0f,0.0f,1.0f,1.0f,1.0f,1.0f;GLfloat ControlP443=-1.5,-1.5,4.0,-0.5,-1.5,2.0,-0.5,-1.5,-1.0,1.5,-1.5,2.0,-1.5,-0.5,1.0,-0.5,-0.5,3.0,-0.5,-0.5,0.0,1.5,-0.5,-1.0,-1.5,0.5,4.0,-0.5,0.5,0.0,0.5,0.5,3.0,1.5,0.5,4.0,-1.5,1.5,-2.0,-0.5,1.5,-2.0,0.5,1.5,0.0,1。
23、.5,1.5,-1.0;void DrawPoints(void)int i,j;glPointSize(5.0f);glColor3ub(255,0,0);glBegin(GL_POINTS);for(i=0;i#includevoid Initial(void)GLfloat mat_ambient=0.2f,0.2f,0.2f,1.0f;GLfloat mat_diffuse=0.8f,0.8f,0.8f,1.0f;GLfloat mat_specular=1.0f,1.0f,1.0f,1.0f;GLfloat mat_shininess=50.0f;GLfloat light0_dif。
24、fuse=0.0f,0.0f,1.0f,1.0f;GLfloat light0_position=1.0f,1.0f,1.0f,0.0f;GLfloat light1_ambient=0.2f,0.2f,0.2f,1.0f;GLfloat light1_diffuse=1.0f,0.0f,0.0f,1.0f;GLfloat light1_specular=1.0f,0.6f,0.6f,1.0f;GLfloat light1_position=-3.0f,-3.0f,3.0f,1.0f;GLfloat spot_direction=1.0f,1.0f,-1.0f;glMaterialfv(GL_。
25、FRONT,GL_AMBIENT,mat_ambient);glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse);glLightfv(GL_LIGHT0,GL_POSITION,light0_position);glLightfv(GL_LIGHT1,GL_AMBIENT,light1_diffuse);glLightfv(GL_LIGHT1,GL_DIFFUSE,light1_diffuse);glLightfv(GL_LIGHT1,GL_SPECULAR,light1_specular);glLightfv(GL_LIGHT1,GL_POSITION。