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

下载本文档

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

文档简介

1、计算机图形学实验报告实验九 二维图形变换一、实验教学目标与基本要求1.掌握图形变换的基本算法原理;2.实现若干典型二维图形变换算法。二理论基础1.生成前几次实验中的基本图形;2.对生成的基本图形进行平移、旋转、放缩、对称等变换。3. 对计算机绘图的原理有一定的认识。三算法设计与分析1二维变换1. 平移变换2.比例变换 sx sy 1 等比例变换 sx sy 1 放大 sx sy 1 缩小 sx sy 13.对称变换当b=d=0, a=-1, e=1时关于y轴对称当b=d=0, a=1, e=-1时关于x轴对称当b=d=0, a=-1, e=-1时关于原点对称当b=d=1, a=e=0时关于直线

2、y=x对称当b=d=-1, a=e=0时关于直线y=-x对称4.旋转变换绕原点逆时针旋转5.错切变换 当d=0时,x*=x+by,y*=y,沿x方向错切位移 当b=0时,x*=x,y*=dx+y, 沿y方向错切位移 当b0时,当d0时,x*=x+by,y=dx+y 6.复合变换-复合平移 对同一图形做两次平移相当于将两次的平移两加起来:复合变换-复合缩放 复合变换-复合旋转复合变换-关于f (xf,yf)点的缩放变换先把坐标系平移到(xf,yf),在新的坐标系下做比例变换,然后再将坐标原点平移回去。复合变换-绕f(xf,yf)点的旋转变换先把坐标系平移到f(xf,yf),在新的坐标系下做旋转变

3、换,然后再将坐标原点平移回去四、程序调试及结果的分析本程序运行环境为vc下的mfc环境。实验代码:void czuobiaobianhuanview:oncgpingyi() /平移/ todo: add your command handler code herefor(int i=0;i<3;i+) pti.x+=5; / pti.y+=5;redrawwindow();void czuobiaobianhuanview:oncgxuanzhuan() /旋转变换float dangle=15.0*pi/180.0;for(int i=0;i<3;i+)pti.x=(pti.x

4、-500)*cos(dangle)-(pti.y-240)*sin(dangle)+500;pti.y=(pti.x-500)*sin(dangle)+(pti.y-240)*cos(dangle)+240;/ todo: add your command handler code hereredrawwindow();void czuobiaobianhuanview:oncgduicheng() /对称变换/ todo: add your command handler code herefor(int i=0;i<3;i+)pti.x=2*500-pti.x;pti.y=pti.y

5、;redrawwindow();void czuobiaobianhuanview:oncgshuofang() /缩放变换/ todo: add your command handler code herefloat dscalex=1.1;float dscaley=1.1;for(int i=0;i<3;i+)pti.x*=dscalex;pti.y*=dscaley;redrawwindow();void czuobiaobianhuanview:oncgcuoqiebianhuan() /错切变换/ todo: add your command handler code her

6、eint b=0,d=2;for(int i=0;i<3;i+)pti.x=pti.x-500+b*(pti.y-240)+500;pti.y=pti.y-240+d*(pti.x-500)+240;redrawwindow();void czuobiaobianhuanview:oncgjianpan() / todo: add your command handler code here/onkeydown(nchar,nrepcnt,nflags);void czuobiaobianhuanview:onkeydown(uint nchar, uint nrepcnt, uint

7、nflags) / todo: add your message handler code here and/or call defaultint i=0;cpoint temp=pt0;switch(nchar)case vk_up:for(i=0;i<3;i+)pti.y-=5;break;case vk_down:for(i=0;i<3;i+)pti.y+=5;break;case vk_left:for(i=0;i<3;i+)pti.x-=5;break;case vk_right:for(i=0;i<3;i+)pti.x+=5;break;case 0x5a:

8、for(i=0;i<3;i+)pti.x=(pti.x-500)*0.8+500;pti.y=(pti.y-240)*0.8+240;break;case 0x58:for(i=0;i<3;i+)pti.x=(pti.x-500)*1.1+500;pti.y=(pti.y-240)*1.1+240;break;case 0x52:float dangle=15.0*pi/180.0;for(int i=0;i<3;i+)pti.x=(pti.x-500)*cos(dangle)-(pti.y-240)*sin(dangle)+500;pti.y=(pti.x-500)*sin

9、(dangle)+(pti.y-240)*cos(dangle)+240;break;redrawwindow();cview:onkeydown(nchar,nrepcnt,nflags);void czuobiaobianhuanview:ondraw(cdc* pdc)czuobiaobianhuandoc* pdoc = getdocument();assert_valid(pdoc);/ todo: add draw code for native data herecpen penred(ps_solid,3,rgb(255,0,0);/定义红色笔 pdc->selectob

10、ject(&penred);pdc->moveto(pt0);pdc->lineto(pt1);pdc->lineto(pt2);pdc->lineto(pt0); pdc->moveto(100,240); pdc->lineto(900,240); pdc->moveto(500,5); pdc->lineto(500,400); /yuandian(500,240) pdc->textout(15,10,"平移变换,键盘方向键"); pdc->textout(15,28,"缩放变换,z缩小,

11、x放大"); pdc->textout(15,46,"旋转变换,键盘r键"); pdc->textout(15,66,"还可以根据上面的菜单栏中的图形变换进行选择"); /onkeydown(nchar,nrepcnt,nflags);运行结果展示:原图:旋转变换后:对称变换后:错切变换后:实验十 三维图形变换一、实验教学目标与基本要求1实习三维图形的坐标系之间的变换;2三维图形几何变换;3掌握三维图形的坐标系之间的变换算法及三维图形几何变换的原理和实现。二理论基础实现三维图形的坐标系之间的变换(世界坐标、物坐标、屏幕坐标)以及三维

12、图形几何变换。三算法设计与分析三维变换变换矩阵比例、错切、旋转变换1.平移变换 若点(x*, y*, z*)是由点(x,y,z)在x,y和z轴方向分别移动距离tx, ty和tz得到的,则这两点间的坐标关系为: x* = x + tx y* = y + ty z* = z + tz 该式的矩阵形式为:2.比例变换设点(x,y,z)经过缩放变换后得到点(x*,y*,z*),这两点坐标间的关系为: x* = sx x ; y* = sy y ; z* = sz z ; 其中,sx,sy,sz 分别为沿x轴,y轴和z轴方向放大或缩小的比例。它们可以相当,也可以不相等。上式的矩阵形式如下所示:3. 旋转

13、变换在右手坐标系下,设给定点的坐标为(x,y,z) = (rcos,rsin,z),则它绕z轴旋转角后,得点(x,y,z),则 x = rcos(+) = xcos ysin ; y = rsin(+) = xsin+ ycos ; z = z ; 所以,旋转变换对应的变换矩阵为:4.对称变换n 相对于xy平面对称:坐标变换后有 x = x, y = y, z = -z; 则变换矩阵为:参数图形的几何变换n 对于一般的线框图,可以利用基于点的几何变换(前面所讲),但对于参数表示的点、曲线、曲面图形,通过基于点的几何变换进行变换,则计算的工作量和存储空间都会非常大,所以一般对其方程式直接进行参数

14、变换(基于数学运算)。n 其变换依然是通过变换矩阵进行变换。2.参数曲线、曲面的几何变换(3) 比例变换比例系数为s,对参数曲线作变比例变换,只要对其几何系数矩阵b作变比例变换就可以了。结果:b*= sb(4) 对称反射变换 b*=brf四、程序调试及结果的分析实验代码:bool cshiwutuxingview:setwindowpixelformat(hdc hdc)/定义窗口的像素格式 pixelformatdescriptor pixeldesc= sizeof(pixelformatdescriptor),1,pfd_draw_to_window|pfd_support_opengl

15、|pfd_doublebuffer|pfd_support_gdi,pfd_type_rgba,24,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,pfd_main_plane,0,0,0,0 ; this->m_glpixelindex = choosepixelformat(hdc,&pixeldesc); if(this->m_glpixelindex=0) this->m_glpixelindex = 1; if(describepixelformat(hdc,this->m_glpixelindex,sizeof(pixelform

16、atdescriptor),&pixeldesc)=0) return false; if(setpixelformat(hdc,this->m_glpixelindex,&pixeldesc)=false) return false; return true;bool cshiwutuxingview:createviewglcontext(hdc hdc) this->m_hglcontext = wglcreatecontext(hdc); if(this->m_hglcontext=null) /创建失败 return false; if(wglmak

17、ecurrent(hdc,this->m_hglcontext)=false) /选为当前rc失败 return false; return true; void cshiwutuxingview:ondestroy() cview:ondestroy();cview:ondestroy(); / todo: add your message handler code here if(wglgetcurrentcontext()!=null) wglmakecurrent(null,null); if(this->m_hglcontext!=null) wgldeleteconte

18、xt(this->m_hglcontext); this->m_hglcontext = null; / todo: add your message handler code hereint cshiwutuxingview:oncreate(lpcreatestruct lpcreatestruct) if (cview:oncreate(lpcreatestruct) = -1) return -1; / todo: add your specialized creation code here hwnd hwnd = this->getsafehwnd(); hdc

19、hdc = :getdc(hwnd); if(this->setwindowpixelformat(hdc)=false) return 0; if(this->createviewglcontext(hdc)=false) return 0; return 0;/ todo: add your specialized creation code herereturn 0;void cshiwutuxingview:onpaint() /cpaintdc dc(this); / device context for painting/ todo: add your message

20、handler code here/ do not call cview:onpaint() for painting messagescpaintdc dc(this); / device context for painting / todo: add your message handler code here / do not call cview:onpaint() for painting messages glloadidentity(); glclear(gl_color_buffer_bit); glbegin(gl_polygon);glcolor4f(1.0f,0.0f,

21、0.0f,1.0f);/glvertex2f(100.0f,50.0f);glvertex2f(pt0.x,pt0.y);glcolor4f(0.0f,1.0f,0.0f,1.0f);/glvertex2f(450.0f,400.0f);glvertex2f(pt1.x,pt1.y);glcolor4f(0.0f,0.0f,1.0f,1.0f);/glvertex2f(450.0f,50.0f);glvertex2f(pt2.x,pt2.y); glend();/auxwirebox(100,100,100); glflush();void cshiwutuxingview:onsize(ui

22、nt ntype, int cx, int cy) /cview:onsize(ntype, cx, cy);/ todo: add your message handler code herecview:onsize(ntype, cx, cy); / todo: add your message handler code here glsizei width,height; gldouble aspect; width = cx; height = cy; if(cy=0) aspect = (gldouble)width; else aspect = (gldouble)width/(g

23、ldouble)height; glviewport(0,0,width,height); glmatrixmode(gl_projection); glloadidentity(); gluortho2d(0.0,500.0*aspect,0.0,500.0); glmatrixmode(gl_modelview); glloadidentity();void cshiwutuxingview:onkeydown(uint nchar, uint nrepcnt, uint nflags) / todo: add your message handler code here and/or c

24、all defaultcview:onkeydown(nchar, nrepcnt, nflags);int i;/glloadidentity();switch(nchar) case vk_up:/*for(i=0;i<3;i+) pti.y+=5;*/glloadidentity ();gltranslatef (0.0, -5.0, 0.0);draw_triangle();break;case vk_down:/*for(i=0;i<3;i+)pti.y-=5;*/gltranslatef (0.0, 5.0, 0.0);draw_triangle();break;case vk_left:/*for(i=0;i<3;i+)

温馨提示

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

评论

0/150

提交评论