计算机图形学实验报告资料_第1页
计算机图形学实验报告资料_第2页
计算机图形学实验报告资料_第3页
计算机图形学实验报告资料_第4页
计算机图形学实验报告资料_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验一:二维图形的绘制和变换1、 实验目的 掌握基本的图形学算法,熟悉VC下图形学的编程,初步了解并使用OpenGL绘制图形。2、 实验内容二维图形的绘制和变换,绘制包括直线、三角形、矩形,变换包括平移、旋转、缩放。3、 实验原理二维图形的绘制和变换:在图形系统中,矩阵是实现变换的标准方法。平移变换、旋转变换和缩放变换的矩阵表示形式如下。平移变换: P=P+T。旋转变换: P=R*P。缩放变换: P=S*P。引入齐次坐标后,平移、旋转和缩放变换的矩阵表示形式如下所示。(1) 平移变换: 1 0 0x, y, 1 = x, y, 1 0 1 0 tx ty 1(2) 旋转变换: cos sin

2、0x, y, 1 = x, y, 1 -sin cos 0 0 0 1(3) 缩放变换:sx 0 0x, y, 1 = x, y, 1 0 sy 0 0 0 1四、实验代码及结果1.编写对一个三角形分别实现平移、缩放、旋转等变化的源码及效果图。实验核心代码void display(void) glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); glColor3f (1.0, 1.0, 1.0); glTranslatef(-100.0,-50.0,1.0); draw_triangle ();

3、 glLoadIdentity (); glTranslatef (0.0, 100.0, 1.0); draw_triangle (); glLoadIdentity (); glRotatef (90.0, 0.0, 0.0, 1.0); draw_triangle (); glLoadIdentity (); glScalef (0.5, 0.5, 1.0); draw_triangle (); glFlush ();2. 实现如图功能#include#include #include void init(void) glClearColor (0.0, 0.0, 0.0, 0.0);

4、glShadeModel (GL_SMOOTH); void draw_triangle(void) glShadeModel(GL_SMOOTH); glColor3f(0.2,0.7,0.30); glBegin (GL_TRIANGLES);/画出三角形,为混合色填充方式 glVertex2f(50.0, 25.0); glColor3f(0.4,0.5,0.60); glVertex2f(150.0, 25.0); glColor3f(0.9,0.7,0.8); glVertex2f(100.0, 100.0); glEnd(); void display(void) glClear

5、(GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); glColor3f (1.0, 1.0, 1.0); glTranslatef(-100.0,-50.0,1.0); draw_triangle (); glLoadIdentity (); glTranslatef (0.0, 100.0, 1.0); glRotatef (90.0, 0.0, 0.0, 1.0); glScalef (0.5, 0.5, 1.0); draw_triangle ();/经过三种变换后画出图形 glFlush (); vo

6、id reshape (int w, int h) glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); if (w = h) gluOrtho2D (-200.0, 250.0, -100.0*(GLfloat)h/(GLfloat)w, 200.0*(GLfloat)h/(GLfloat)w);/调整裁剪窗口 else gluOrtho2D (-200.0*(GLfloat)w/(GLfloat)h, 250.0*(GLfloat)w/(GLfloat)h,

7、 -50.0, 200.0); glMatrixMode(GL_MODELVIEW);int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (600, 600); glutInitWindowPosition (100, 100); glutCreateWindow (argv0); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glut

8、MainLoop(); return 0;实验二:使用中点扫描算法绘制直线和圆一、实验目的 掌握基本的图形学算法,熟悉VC下图形学的编程,初步了解并使用OpenGL绘制图形。二、实验内容使用中点扫描算法绘制直线和圆。3、 实验原理(一)基本思想: 过各行各列象素中心构造一组虚拟网格线(与DDA算法与中点画线算法中的网格线是一样的),从起点到终点的直线L与各垂直网格线相交。假定Q为L与垂线x = xi的交点,则根据误差项 d 的大小可以确定该列象素中与Q点最近的象素。 设直线L的方程为: y=kx+b ,其中k是斜率,则 y = kx (这个式子中的y和x是表示增量)即x增加1,y增加斜率k。

9、因为直线的起始点在象素中心,所以误差项d的初值d00。x下标每增加1,d的值相应递增直线的斜率值k,即ddk。 (1)当 d0.5 时,最接近于当前象素的右上方象素(xi1,yi1) (2)当 d 0.5 时,更接近于右方象素(xi 1, yi) 为方便计算,令ed - 0.5,则e的初值为-0.5,增量为k。 (3)当 e0 时,取当前象素(xi , yi )的右上方象素(xi 1, yi 1),在考虑下一个象素以前,必须将误差项重新初始化,即将它减1; (4)当 e 0 时,更接近于右方象素(xi 1, yi)。(二)具体实验算法1. 使用中点扫描算法绘制直线和圆。中点画线法(|k|1)的

10、算法,如下所示:(1) 输入直线的起始和终止端点P0(x0, y0)和P1(x1, y1);(2) 计算初始值a=y0-y1, b=x1-x0, c=x0y1-x1y0, d=2a+b, x=x0, y=y0;(3) 绘制点(x,y);(4) 判断d的符号,若d0,则(x,y)更新为(x+1, y+1),d更新为d+2a+2b,否则(x,y)更新为(x+1, y),d更新为d+2a;(5) 当直线没有绘制完成时,跳转到步骤(3)执行,否则算法结束。2. 中点画圆法的步骤如下所示:(1) 输入圆的半径R;(2) 计算初始值d=1.25-R,x=0,y=R;(3) 绘制(x,y)及其另外7个对称点

11、;(4) 判断d的符号,若d0,则先将d更新为d+2x+3,再将(x,y)更新为(x+1, y),否则先将d更新为d+2(x-y)+5,再将(x,y)更新为(x+1, y-1);(5) 当xy时,重复步骤(3)和(4),否则算法结束。四、实验代码及实验结果#include stdio.h /用DDA法画直线,定义了直线始点(10,10*m+30)#include main() float m; printf(the m is ); /输入直线的斜率m scanf(%f,&m); if(abs(m)=1) int x; float y; int driver=DETECT,mode; initg

12、raph(&driver,&mode,c:WINLibTC); y=10*m+30; for(x=10;x=300;x+) y=y+m; putpixel(x,y,4); getch(); closegraph(); else int y; float x; int driver=DETECT,mode; initgraph(&driver,&mode,cWINLibTC); for(y=10;y=300;y+) x=x+1/m; putpixel(x,y,4); getch(); closegraph(); #include stdio.h#includevoid bresinham(int

13、 x0,int y0,int x1,int y1,int color) /*用户自定义的函数,用bresinham法画直线*/ int x=x0; int y=y0; float m=(y1-y0)/(x1-x0); /*m为直线斜率*/ float e=m-1/2; /*e是偏离值*/ while(x=0) /*如果偏离值非负,x,y都前进一个单位,画点*/ x+; y+; putpixel(x,y,color); e=e-1; else /*如果偏离值小于零,只有x前进一个单位,y不动,画点*/ x+; putpixel(x,y,color); e=e+m; main() int a,b,

14、c,d,e=4; int graphdriver=DETECT; /*初始化图形*/ int graphmode=0; initgraph(&graphdriver,&graphmode,); printf(input the x0:); /*输入直线的始点和终点 */ scanf(%d,&a); printf(input the y0:); scanf(%d,&b); printf(input the x1:); scanf(%d,&c); printf(input the y1:); scanf(%d,&d); bresinham(a,b,c,d,e); getch(); closegra

15、ph(); #include /*用bresenham法画圆*/void bresenham(int xc,int yc,int r,int color) int x,y,d; x=0; y=r; d=3-2*r; while(xy) putcircle(xc,yc,x,y,color); if(d0) d=d+4*x+6; else d=d+4*(x-y)+10; y-; x+; if(x=y) putcircle(xc,yc,x,y,color);putcircle(int xc,int yc,int x,int y,int color) putpixel(xc+x,yc+y,color)

16、; putpixel(xc-x,yc+y,color); putpixel(xc+x,yc-y,color); putpixel(xc-x,yc-y,color); putpixel(xc+y,yc+x,color); putpixel(xc-y,yc+x,color); putpixel(xc+y,yc-x,color); putpixel(xc-y,yc-x,color);main() int a,b,c,d; int graphdriver=DETECT; /*初始化图形*/ int graphmode=0; initgraph(&graphdriver,&graphmode,); cl

17、eardevice(); a=330; b=240; c=10; d=4; putpixel(a,b,d); bresenham(a,b,c,d); getch(); closegraph(); 实验三:绘制一个三维图形一、实验目的 掌握基本的图形学算法,熟悉VC下图形学的编程,初步了解并使用OpenGL绘制图形。二、实验内容绘制一个三维图形,使用OpenGL绘制三维画图3、 实验原理4、 实验代码及实验结果1. 绘制线框长方体#include #include#include#include#include/清除屏幕,设置当前颜色为白色/绘制一个线框模式的正方体void CALLBACK d

18、isplay(void) glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glLoadIdentity(); glTranslatef(0.0,0.0,-5.0); glScalef(1.0,2.0,1.0); auxWireCube(1.0); glFlush();/进行必要的初始化工作void myinit(void) glShadeModel(GL_FLAT);/在窗口第一次创建时以及每次改变窗口大小或移动位置时调用此函数void CALLBACK myReshape(int w,int h) glMatrixMode(GL_P

19、ROJECTION); glLoadIdentity(); glFrustum(-1.0,1.0,-1.0,1.0,1.5,20.0); glMatrixMode(GL_MODELVIEW); glViewport(0,0,w,h);/主循环/给定窗口的初始大小、标题打开一个窗口/设置为RGBA显示模式,并处理输入消息int main(int argc,char *argv) auxInitDisplayMode(AUX_SINGLE|AUX_RGB); auxInitPosition(0,0,500,500); auxInitWindow(argv0); myinit(); auxResha

20、peFunc(myReshape); auxMainLoop(display);2. 绘制立方体并实验其旋转/ DiffuseLight.cpp/ OpenGL SuperBible/ Demonstrates simple diffuse lighting/ Program by Richard S. Wright Jr.#include / OpenGL toolkit#include #include #include #include #include #include #ifdef _APPLE_#include #else#define FREEGLUT_STATIC#includ

21、e #endifGLFrame viewFrame;GLFrustum viewFrustum;GLBatch sphereBatch;GLMatrixStack modelViewMatrix;GLMatrixStack projectionMatrix;GLGeometryTransform transformPipeline;GLShaderManager shaderManager;GLuint diffuseLightShader; / The diffuse light shaderGLint locColor; / The location of the diffuse colo

22、rGLint locLight; / The location of the Light in eye coordinatesGLint locMVP; / The location of the ModelViewProjection matrix uniformGLint locMV; / The location of the ModelView matrix uniformGLint locNM; / The location of the Normal matrix uniform/ This function does any needed initialization on th

23、e rendering/ context. void SetupRC(void)/ BackgroundglClearColor(0.3f, 0.3f, 0.3f, 1.0f );glEnable(GL_DEPTH_TEST);glEnable(GL_CULL_FACE); shaderManager.InitializeStockShaders(); viewFrame.MoveForward(4.0f); / Make the sphere gltMakeCube(sphereBatch,0.5);diffuseLightShader = shaderManager.LoadShaderP

24、airWithAttributes(DiffuseLight.vp, DiffuseLight.fp, 2, GLT_ATTRIBUTE_VERTEX, vVertex,GLT_ATTRIBUTE_NORMAL, vNormal);printf(%d %d %d %d %dn,locColor,locLight,locMVP,locMV,locNM);locColor = glGetUniformLocation(diffuseLightShader, diffuseColor);locLight = glGetUniformLocation(diffuseLightShader, vLigh

25、tPosition);locMVP = glGetUniformLocation(diffuseLightShader, mvpMatrix);locMV = glGetUniformLocation(diffuseLightShader, mvMatrix);locNM = glGetUniformLocation(diffuseLightShader, normalMatrix);printf(%d %d %d %d %dn,locColor,locLight,locMVP,locMV,locNM);/ Cleanupvoid ShutdownRC(void)/ Called to dra

26、w scenevoid RenderScene(void)static CStopWatch rotTimer;/ Clear the window and the depth bufferglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(viewFrame);modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() *10.0f, 1.0f, 1.0f, 1.0f);GLfloat vEyeLight = -100.0f, 100.0f,

27、100.0f ;GLfloat vDiffuseColor = 0.0f, 0.0f, 1.0f, 1.0f ;glUseProgram(diffuseLightShader);glUniform4fv(locColor, 1, vDiffuseColor);glUniform3fv(locLight, 1, vEyeLight);glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix();glUniformMatrix4fv(locMV, 1, GL_FALSE, trans

28、formPipeline.GetModelViewMatrix();glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeline.GetNormalMatrix(); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); glutSwapBuffers();glutPostRedisplay();void ChangeSize(int w, int h)/ Prevent a divide by zeroif(h = 0)h = 1;/ Set Viewport to window dimensio

29、ns glViewport(0, 0, w, h); viewFrustum.SetPerspective(35.0f, float(w)/float(h), 1.0f, 100.0f); projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix(); transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);/ Main entry point for GLUT based programsint main(int argc, char* argv)

30、gltSetWorkingDirectory(argv0);glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);glutInitWindowSize(800, 600);glutCreateWindow(Simple Diffuse Lighting); glutReshapeFunc(ChangeSize); glutDisplayFunc(RenderScene);GLenum err = glewInit();if (GLEW_OK != err) f

31、printf(stderr, GLEW Error: %sn, glewGetErrorString(err);return 1; SetupRC(); glutMainLoop();ShutdownRC();return 0; 实验四:二维图形的绘制和变换一、实验目的 掌握基本的图形学算法,熟悉VC下图形学的编程,初步了解并使用OpenGL绘制图形。二、实验内容绘制bezier,hermite,b样条曲线3、 实验原理1、曲线段可以用端点、切向量和曲线段之间的连续性等约束条件来定义 2、两个端点和两端点处的切向量定义Hermite曲线; 3、两个端点和另外两个控制端点切向量的点定义的Bezi

32、er曲线;4、由四个控制顶点定义的样条曲线。四、实验代码及实验结果void CDrawYTQXView:Hermite()/绘制Hermite三次插值样条int a44 =2,-2,1,1,-3,3,-2,-1,0,0,1,0,1,0,0,0;/Mh矩阵系数int b42;/边界点 for(int i=0;i4;i+)b00=p1i0;b01=p1i1;/起点的坐标b10=p1i+10;b11=p1i+11;/终点的坐标b20=p2i0;b21=p2i1;/起点的导数b30=p2i+10;b31=p2i+11;/终点的导数Caculate(a,b);CClientDC dc(this);CPe

33、n MyPen,*pOldPen;MyPen.CreatePen(PS_SOLID,1,RGB(0,0,255);pOldPen=dc.SelectObject(&MyPen);dc.MoveTo(p1i0,p1i1);for(double t=0.0;t=1;t+=1.0/400)int x=ROUND(pow(t,3)*result00+pow(t,2)*result10+ t*result20+result30);int y=ROUND(pow(t,3)*result01+pow(t,2)*result11+ t*result21+result31);dc.LineTo(x,y);dc.

34、SelectObject(pOldPen);MyPen.DeleteObject();void CDrawYTQXView:Caculate(int a44,int b42)/矩阵相乘int i,j,k;for(i=0;i4;i+)for(j=0;j2;j+)resultij=0;/矩阵清零for(i=0;i2;i+)for(j=0;j4;j+)for(k=0;k4;k+)resultji+=ajk*bki; void CDrawYTQXView:DrawBezier()/绘制Bezier曲线CClientDC dc(this);double x,y;int rate=400,n;n=Ctrl

35、Point-1;for(double t=0;t=1;t+=1.0/rate)x=0;y=0;for(int i=0;i=n;i+)x+=pti.x*Cnk(n,i)*pow(t,i)*pow(1-t,n-i);y+=pti.y*Cnk(n,i)*pow(t,i)*pow(1-t,n-i);dc.SetPixel(ROUND(x),ROUND(y),RGB(0,0,255);/曲线颜色double CDrawYTQXView:Cnk(const int &n, const int &i)/Bernstein第一项return double(Factorial(n)/(Factorial(i)*

36、Factorial(n-i);int CDrawYTQXView:Factorial(int m)/阶乘函数int f=1;for(int i=1;i=m;i+)f*=i;return f;void CDrawYTQXView:DrawB3_curves()/绘制B样条曲线CClientDC dc(this);int i,rate=10,m;long lx,ly;m=CtrlPoint-(3+1);double F03,F13,F23,F33;lx=ROUND(pt0.x+4.0*pt1.x+pt2.x)/6.0);/t0的起点x坐标ly=ROUND(pt0.y+4.0*pt1.y+pt2.y)/6.0);/t0的起点y坐标dc.MoveTo(lx,ly);CPen MyPen2,*pOldPen2;MyPen2.CreatePen(PS_SOLID,2,RGB(0,0,255);/颜色设置pOldPen2=dc.SelectObject(&MyPen2);for(i=1;im+2;i+)/m+1段三次样条曲线for(double t=0;t=1;t+=1.0/rate)F03=(-t*t*t+3*t*t-3*t+1)/6;/计算F0,3(t

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论