《2023年计算机图形学大作业报告灯光纹理映射.pdf》由会员分享,可在线阅读,更多相关《2023年计算机图形学大作业报告灯光纹理映射.pdf(27页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、大 作 业 报 告实验课程名称:计算机图形学_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _学 生 姓 名:_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _班 级:_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _学 院(系):学生学号:指 导 教 师:成 绩:一、目的这次大作业是作为这学期的最后的一个考核,所以必须要用到所有的本学期学过的知识,比如如何画出
2、三维图形,如何在图像上在图像上添加纹理光照,如何使用鼠标和键盘进行人机交互等。二、重要功能模块设计1 矩阵运算模块的设计:功能描述:程序启动后,这部分功能模块会为整个应用程序提供算法支持,具体是矩阵直接的互相运算,在 2D 向 3D转化过程中会起到很重要的作用。代码设计:f l o a t v v (f l o a t *v 1 ,f 1 o a t *v 2)r e t u r n v 1 0 *v 2 0 +v 1 1 *v 2 1 H-v 1 2 *v 2 2 ;)v o i d v x v(f 1 o a t *n,f l o a t *v l,f l o a t *v 2)。n 0
3、=v l 1 *v 2 2 -v 1 2 *v 2 l ;n 1 =v 1 2 *v 2 0 -v l 0 *v 2 2 ;n 2 =v l 0 *v 2 1 -v 1 1 *v 2 0 ;v o i d l o a d I d e n t i t y (M a t r i x m)M a t r i x i d e n t i t y =1.0,0.0,0 .0,0.0 ),0.0 ,1.0,0 .0,0.0 ,0.0,0.0 ,1.0,0.0),。0.0,0.0,0.0,1.0 ;。f o r(i n t i =0;i 4;i+)。f o r(i n t j =0;j 4 ;j+)m i
4、j =i d e n t i t y i j ;v o i d p r e M u 1 t i p l y (M a t r i x p,M a t r i x m)i n t ij;M a t r i x t;。f o r (i =0;i 4;i +)f o r(j =0;j 4;j +)。t i j =p i 0 *m 0 j +p i 1 *m 1 j +p i 2 *m 2 j +p i 3 *m 3 j ;f o r (i =0;i 4;i +)3。f o r(j =0;j 4 ;j +)。m i j =t i j ;)2显示图形模块的设计:功能描述:该模块重要为所要画的图形进行描述
5、,是绘制图形的重要函数,通过调用这个功能模块就能画出用于显示给用户的图形。代码设计:/2 D图形的d i s p l a y函数v o i d d i s p 1 a y ()g l C 1 e a r (G L _ C O L O R _ B U F F E R _ B I T);。i f(n =1&t y p e =G L _ L I N E _ S T R I P)。g l B e g i n(G L _ P O I N T S);g l V e r t e x 2 i v (v e r t 0 );。g l C o l o r 3 f (0,0.5,0 .5);g l V e r t
6、e x 2 i (w i d t h-v e r t 0 0 1,v e r t 0 1 );。g 1 E n d O ;)g I C o 1 o r 3 f (0,0.5,0.5);g l B e g i n (G L _ L I N E S);。g 1 V e r t e x 2 i (w i d t h /2,0);g l V e r t e x 2 i (w i d th/2,h e i g h t-1);。g l E n d O ;。g l B e g i n (t y p e);。f o r (i n t i =0;i n;i +)g l V e r t e x 2 i (w i
7、d t h-v e r t i 0 -L v e r t i 1 );g l E n d ();。g l C o l o r 3 f(l,1,0);g 1 B e g i n(t y p e);f o r (i n t i =0 ;i n;i+)g l V e r t e x 2 i v (v e r t i );g l E n d();g l u t S w a p B u f f e r s ();i n i t Q M e s h ();。g l u t S e t W i n d o w(w i n i d):3 g 1 u t P o s t R e d i s p l a y();
8、)3 D图形的显示函数v o i d m o d e 1 D i s p 1 a y ()g l C 1 e a r (G L _ C 0L O R _ B U F F E R _ B I T I G L _ D E P T H _ B U F F E R _ B I T);g 1 P u s h M a t r i x();o f l o a t M 1 6 ;。f o r (i n t r =0;r 4;r+)f o r (i n t c =0;c 4 ;c +)M 4 *c +r C R M r c;。g 1 M u l t M a t r i x f (M);g 1 S c a l e
9、 f (z o o m,z o o m,z o o m);d r a w Q M e s h ();g l P o p M a t r i x ();g l P u s h M a t r i x ();g l L i g h t f v (G L _ L I G H T O,G L _ A M B I E N T,a m b);。g l L i g h t f v(G L _ L I G H T O ,G L _ P O S I T I O N,1 i g h t P o s i t i o n);g I P o p M a t r i x O ;。g l u t S w a p B u f
10、 f e r s ():3 键盘敲击模块功能描述:该功能重要为两个窗体添加键盘敲击事件,在 2D 窗体中重要添加了反走样效果,清除所画的图,点线转化功能,对 于 3D 窗体重要添加了投影方式的变换,光源移动的功能代码设计:/2D绘制窗体的键盘敲击解决函数v o i d k e y b o a r d (u n s i g n e d c h a r k e y,i n t x,i n t y)s w i t c h (k e y)。c a s e a :a n t i a 1 i a s i n g =!a n t i a 1 i a s i n g;反走样效果。i f (a n t i a
11、l i a s i n g)b。g l E n a b l e (G L _ B L E N D);。g l E n a b l e (G L _ L I N E _ S M O O T H);。e 1 s e t。g l D i s a b l e(G L _ B L E N D);。g l D i s a b I e (G L _ L I N E _ S M O O T H);。)。o b r e a k ;。c a s e c :n =0;b r e a k;/清除界面功能。a c a s e 1 1 :t y p e =G L _ L I N E _ S T R I P;b r e a
12、 k;/绘制线功能。c a s e v :t y p e =G L _P O I N T S;b r e a k;/绘制点功能0 g l u t P o s t R e d i s p l a y ();)/3 D显示窗体的键盘敲击事件解决函数:v o i d mode I K e y b o a r d (u n s i g n e d c h a r k e y,i n t x,i n t y )。s w i t c h (k e y )c a s e o :g l M a t r i x M o d e (G L _ P R 0 J E C T I O N);/正投影3g 1 L o a
13、 d I d e n t i t y ();。g 1 0 r t h o (-4,4,-4,4 ,6,1 4);。g I M a t r i x M o d e(G L _ M O D E L V I E W);o o o b r e a k;。c a s e :g l M a t r i x M o d e(G L _ P R O J E C T I O N);侧投影,但是是默认的。g l L o a d I d e n t i t y();。g l u P e r s p e c t i v e(4 5.0,1 .0,6.0 ,1 4.0);。g l M a t r i x M o d e
14、 (G L _ M O D E L V I E W);b r e a k ;。c a s e o f :l i g h t P o si t i o n 0 =1 i g h t P o s i t i o n 0 -0.5;。光源移动。b re a k ;c a se n 1:l i g h tP o si ti o n 1 =1 i g h tP o si ti o n l 0.5;s 。b re a k;。c a se z :1 i g h tP o si ti o n 2 =1 i g h tP o si ti o n 2 -0.5;。b r e a k;。c a s e ,k :g
15、1 D i sa b l e (G L_F O G);b r e a k ;)。g l utP o stR e d i sp 1 a y();)4鼠标点击模块功能描述:该功能模块重要实现了对两个窗体鼠标事件的解决,对于3 D窗体实现了鼠标左键旋转,由键放大缩小的效果,2 D窗体实现了左键画点,右键可以对已经存在的点改变它的位置。代码设计:3 D窗体的鼠标事件vo i d m o d e l M o u se (i n t b ut t o n,i n t sta te,i n t x,i n t y)s w i tc h (b utto n)。o c a se G L U T _ L E F
16、T _ B U T T O N:,i f (sta te =G L U T _ D 0W N )。C X =X;c y=h -1 -y;o d x=d y=0;s l e f t _b utto n _ d o w n =true;。e 1 se。l e f t_ b u t to n _ d o wn =f a l se;。b r e a k;。o c a se G L U T _ R I G H T _ B U T T O N:。i f(s t a te =G L U T _ D O W N)C X =X;。c y=h -1 y;。r i g h t_ b u tto n _ d o wn
17、 =true;。)e 1 s e。ri g h t _ b u t to n _ d o wn =f a I s e ;。b re a k ;)2D窗体鼠标事件vo i d m o u s e(i n t b u tto n ,i n t s t a te,i n t x,i n t y)。y 二 h e i g h t-1-y;。s w i tc h(b u t t o n)。c a se G L U T _ L E F T _ B U T T O N:。i f (sta te =G L U T _ D 0W N&!r ub b e r b a n d i n g )v=n+;。ve r t
18、 v 0 =(x =wi d th)?wi d t。v e r t v 1=(y =h e i g h t)?h e i g h t-1:y。ru b b e r b a n d i n g =t rue;g 1 u t P o stR e d i s pl a y();。)e 1 se r u b b e r b a n d i n g =f a 1 s e;。b re a k;。c a se G L U T R I G H T _ B U T T 0 N:。i f (st a te =G L U T _ D 0W N&!ru b b e rb a n d i n g&(v=f i n d
19、V e r-1)(。o i f(g l u tG e tM o d i f i e rs()=GLUT_ACT I V E _ C T R L)t e x(x,y)!。f o r(i n t i =v;i n 1;i+)ve rt v 0 =ve rt i+1 0 ;。ve rt i 1 =ve rt i+1 1;6 O n ;o e l s e 。ve r t v 0 =(x =wi d t h)?wi d t h 1 :x;。v e r t v l =(y =h e i g h t)?h e i g h t -1:y;rub b e r b a n d i n g =true;O 0 o
20、g l utP o s t R e d i spl a y();0 o e l se rub b e rb a n d i n g =f a l se;。b r e a k;)功 能 截 图:运用鼠标在界面上画出本来的图,截图如下:按 住“Z”后,上方的光照变化,截图如下:Display object按 住“Y”后,下方的光照效果发生变化,截图如下:使用鼠标实现图像方向的变化,截图如下:使用鼠标实现图像大小的变化,截图如下:放大:缩小:Ditplay object/叵区 Display object=叵区三、心得通过对本次大作业实验的编写,更进一步了解了 0 P enGL的用法,从计算机图形学
21、课程中也许收获最多的并不是图形学的一些基础知识,虽然这些很重要,但是真正有些成就感的还是用Ope nG L开发出来的这些程序。特别是这个项目,和前面几个只需抄抄代码就能出来的实验相比,是需要好好动动脑筋的。在这个项目中需要解决鼠标等设备的输入,然后再根据输入捕获相应的变化再进行解决。这一块对于我来说是比较困难的。在解决这个问题上花了不少的时间,在解决这个问题之后应当说后面的工作还是比较轻松的。以前觉得看到的游戏里面的画面很是完美,现在我知道了这写图的制作方法,并且我也学会了如何制作简朴的三维图片,加上对三维图形的一些修饰,使简朴的图形变得更好看更精致。完整代码如下:#in c lud e#in
22、cl u d e#def i ne QMeshSi z e 25 6int w i dth=4 0 0,h e i g ht=6 0 0,ve r t10 0 2,n=0,type=GL_L I N E _ S T RIP,v;boolr u bbe r banding=fals e,ant i al i a sing=fa 1 s e;f loatQMes h 1 0 0 QMeshS i ze+1 3;GLfloatlightPosi t i on=-4.0,4.0,-6.0,1.0 ;GLfloatamblJ=0.3 f,0.3f,0.3f,0.3f);GL f 1 o a t fron
23、tDi f fAmb=0.95f,0.3 f,0.1GLf 1 oat b ackDi f fAm b =0.1 f,0 3f,0 95f,1.0);GL f loat Specf=0.45f,0.4 5 f,0.45f,1.0);GLfloatSp e cExp=15.0;b o oil e ft_ b u t to n _d o wn=f al s e,r i g h t_butto n _down=f a 1 se;int w=6 0 0,h=600,w in id,c x,c y,d x=0,dy=0;f l o a t zo o m=1.0;typ e def f 1 oat Ma
24、t ri x 4 4;Matrix RM,CRM;voidi n i t QM e s h()f 1 oat r;for(in t i=0;in;i+)。r=v ert LiJ L 0 -wi d th/2.0;。f o r(int j=0;j Q Mesh S i z e;j+)o QMesh i j0=r*cos(3.14159*2*j/QMes h Si z e)/QM eshij l=(verti 1-he i ght/2.0)/100;QMeshi j 2=r*s i n(3.14159*2*j/QMeshS i ze)/100;)QMe s hi QMes h Size 0=Q M
25、 esh i 0J0;QMeshiQMeshSize 1 =QM e s hi0l;。QM eshiQM es h Siz e 2=QMesh i0 2;)v o id di s p 1 a y()g 1 Clear(GL_COLOR_BUFFER_BIT);if(n=1&typ e=G L _L IN E _S T R I P)“g 1 B e g i n(GL_PO I N T S);gglVe r t ex2i v(v er t 0);aoglCo 1 or3f(0,0.5,0.5);。gIVe r te x 2i(width-vert0 0-1,v ert0l );gl E nd();
26、)oglColo r3f(005,0.5);glBegin(GL_LINES);eg 1 Ve r tex2i(wi d t h Z2,0);10 0;gl V e rtex2i(w i d th/2,height-1);glEn d();glB e gin(typ e);fo r(int i=0;i n;i+)g IV e rtex2i(wid t h-verti 0 1,vertil);oglEnd();glCo 1 o r 3 f(1,1,0);glB e gi n(t ype);for(in t il=0;iln;i 1+)glVerte x 2 iv(ver t il);ogl En
27、 d();oglut S wa p B u ffer s();in i tQMesh();glu t SetW i ndo w(wi n i d);glutPos t R e d i sp 1 a y ();)v oid keybo a r d(unsigne d cha r k e y,i ntx,int y)swi t ch(key)ca s e 1 a*:a n ti a 1 iasing=!antia 1 ia s in g;“f(an t ia 1 i a sing)。oglEna b I e(GL_ BLEND);o ooglEn a b 1 e(G L _L I NE_SMOOT
28、H);elseglDisable(GL_BLEND);a gID i sab 1 e(G L_LINE_SMOOTH);OOO Ib break;ca s ec n=0;break;c a se*1 type=GL_LINE_ S TR I P;br e ak;ca s e v:type=GL_P O INTS;bre a k;)oglu t P o stRedisplay();)intf i n d V e r t ex(int x,i n t y)intdx,d y;ofor(in t i=0;i n;i+)d x=verti 0 x;o dy=ve r ti 1-y;“f(dx*d x+
29、d y*dy 16)re t u rn i;)return-1;)void mouse(ini button,int sta t e,i nt x,in t y)呼=h e i ght-1 -y;switch(b u t t o n)case GLUT_ L EFT_B U TTO N:if(s t a t e=GLUT.DOWN&!rubb e rbanding)?v-n+;covert v 0=(x=widt h)?wid t h-1 :x;v ert v l =(y=hei g ht)?h e i g ht-1:y;。rubberba n din g=t r u e;00g 1 utPo
30、s t Re d i spl a y();。e Iserubberbanding=false;的break;ocase G L U T_RIGH T_BUTTON:。i f(st a te=G L UT_DOWN&!r u bb e r banding&(v=fi n d Vertex(x,y)!=-l)。“if(g lu t Ge t Modifiers()=GL U T_ACTIVE_CTRL)08 ofbr(i n t i=v;i n-1;i+)ve r tvO=verti+lO;verti E1 =ve r t i+1 1;o e e g elseovertv 0 =(x=w id t
31、 h)?w i d th-1 :x;v ertvl=(y=h e ight)?height-1:y;阿 u b b erban d in g=tru e;d o|0g 1 u tPostRed i splay();0 0 Iaoe 1 seru b berbanding=f a Is e;。break;)v o id m o tion(i n t x,i nt y)“f(r u b berband i ng)ve r t v 0=(x=width)?width-1 :x;y=he i g h t-l-y;v e rtvl=(y=h e i g ht)?height-1:y;g lutPos t
32、 Redispla y();void n o rm a liz e(float*v)float m=s qrt(v0*v0+vl*v 1+v2*v 2);of o r(i n t i=0;i 3;i+)8V Li=v i/m;)f loa t v v(f 1 oat*v 1,float*v2)oretu r n vl0*v 2 0+v 1 1 *v 21+vl 2*v2 ;v o idvxv(float*n,f 1 oat*v 1,f 1 o at*v 2)on 0=v 1 1*v2 2-vl 2*v2 1;nl=vl2*v2 0 vl 0*v2 2;2=v l 0*v 2 l-v ll*v
33、2 0;voidl o adIdentity(Ma t ri x m)oM a trix identit y=1 .0,0.0,0.0,0.0),0.0,1.0,o.o,0.0 ,。(0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.01);ofo r(int i=0;i 4;i+)for(i n t j=0;j 4;j+)m i j=id e ntityi j ;)void p re Mult i p ly(Matrix p,M a t rix m)ftint i,j;Matr i x t;f or(i=0;i 4;i+)o r(j=0;j 4;j+)tij=p:i 0*m0 L
34、j +pil 1*m El j+pi2 *m2 j +p i3*m3 j;for(i=0;i 4;i+)for(j=0;j 4;j+)。mi j=t L i j;)v o id rota t e(float angle,f 1 oa t*axis)afloat v 3=a xis0,a x isi 3=1,0,0),业3=0,0,1);1 o adldentity(RM);n o r m a 1 i z e(v)float r 1 =(v 1 0)?a c os(v v(v,i):a c o s(v v(v,i);eMat r ix Rz=co s(rl),s i n(rl),0.0,0.0,
35、sin(r 1),cos(rl),0.0,0.0,o (0.0,0.0,1.0,0.0 ,。0.0,0.0,0.0,1.0;p r e M u It i ply(R z,RM);floa t r2=-a cos(vv(axi s,k);Mat r ix Ry=cos(r 2),0.0,sin(r 2),0.0,0.0,1.0,0.0,0.0,。-sin(r 2),0.0,co s(r 2),0.0,。0.0,0.0,0.0,1.0);opreM u 1 tiply(Ry,RM);Matr i x R=cos(angle),-s i n(angl e),0.0,0.0,s i n(a n g l
36、e),c o s(an g le),().0,0.0,。0.0,0.0,1.0,0.0,o (0.0,0.0,0.0,1.0);p r e Mu 1 t iply(R,R M);M atrix R_y=cos(-r2),0.0,sin(-r2),0.0),0.0,1.0,0.0,0.0,-sin(-r2),0.0,cos(-r2),0.0),o 0.0,0.0,0.0,1.0;preMu 1 tip 1 y(R_y,RM);Matri x R_z=c o s(-rl),-sin(-rl),0.0,0.0,si n(-r 1),c o s(-r 1),0.0,0.0,。0.0,0.0,1.0,0
37、.0 ,0.0,0.0,0.0,1.0);opr e Mu 1 tip 1 y(R_ z,RM);pre Mu 1 t i p ly(RM,CRM);voi d dra wQMesh()Mntij;f loat vl3,v 2 3 ,norm3 ;glBegin(G L _Q U A D S);f o r(i=0;i n-1;i+)for(j=0;jQMesh S ize;j+)。vl01=QM eshi+l|j+l 0 一 QMeshij0;vl 1 =QMe s h Ei+lj+l 1 一 QMesh 1;l2=QMes h i+1 j+l2-QMes h ij 2;v20=QM e s
38、h i+lj j 0-QMeshliJj+1 0 J;。v2l=QMeshi+1 j 1 一 QMe s h i j+l 1 ;8V2出=QMeshi+lj2-QMesh i j+l E 2;vx v(norm,v l,v2);glN o rm a 1 3 fv(norm);。g 1 Vertex3f v(QMes h i j);。glVe r t e x3f v(QMesh ij+1 );oglVe r t e x3fv(QMes h i+lj+l);gIVer t ex3fv(Q M e s h i+lj);glE n d();)voi d modelDisp 1 a y()g 1 C l
39、ear(G L _COLOR_BUFFE R_B IT|GL_DEPTH_ B UFF ER_BI T);oglPushMatr i x();afloat M 1 6 ;for(i n t r=0;r 4;r+)fo r(int c=0;c v 1 0=ex w/2;vl 1=cy-h/2;o3ov 1 2=300.0;。v2 0=x-w/2;。v 2 1=y-h/2;。v22=300.0;。vxv(a x i s,vl,v2);。angle=sqrt(dx*dx+d y*d y)/2 00;norm a lize(axis);。rotate(angl e,a x is);0 0 J。i f(
40、right_ b utt o n_d o wn)z oom=zo o m-zoom*(y-cy)/25 0;OCX=X;acy=y;g lutPostR e displa y();)v o idmodell d I e(void)if(d x*d x+d y*dy 9)pre Mui t i p ly(RM,CRM);oglutPo s tR ed isplayO;)void m a in(inta r g c,char*arg v)g 1 ut I nit(&arg c,arg v);og 1 utln i t D i s p layMode(GLUT_RGB|GLUT_DOUBL E);o
41、glutlni t Wi n d o w S i ze(wi d th,h e igh t);glutlnitWin d owPos i t i on(5 0,10 0);ogl u tCrea t eWindow(u E d it p olygons*);glClearColor(0.0,0.0,0.0,00);oglColor3f(1,1,0);g IMa t rixMode(GL_PROJ E C T IO N);ogl L o a diden t i ty();gluO r t ho2D(0,wi d th,O,h e i ght);glB 1 endF u nc(GL_SRC_ALP
42、HA,G L_ONE_MI N U S _ S RC_ALPHA);。g lutDisp 1 ayFunc(d i s p 1 a y);g 1 u tKe y b oardFunc(k e yb o ard);叫 u tMouseFunc(m o u s e);o g 1 u t MotionFun c(mot i on);og 1 ut I nitDi s pl a yMod e(GLUT.RG B|GLUT_DEPTH|GLUT_DOUBLE);唱 u tlnitWindo wS i ze(w,h);glutlnitWindowPosition(5 0+width,1 0 0);owin
43、id=g lutCreateWindow(Dis p 1 ay o b j e c tH);唱 Enab 1 e(GL _DE P TH_TES T);g 1 Clear Color(0.0,0.0,0.0,0.0);g 1 Mat e r i alf v(G L_FRONT,G L_AM BIEN T _ A ND_DIFFUSE,f ronlDif f Am b);-glM a te r ia 1 f v(GL_BACK,GL_A M BIEN T _AND_DI F FU S E,backD i f f Amb);og 1 M a te r ialf v(G L _F RONT_AND_
44、BACK,GL_SPECULAR,S p ec);glM a te r i al f(GL_FRONT_AND_BACK,GL_SHIN I NESS,SpecEx p);glLi g htfv(GL_ L IG HTO,G L_AMB I ENT,amb);glLightfv(GL_LIGH T 0,G L_ P O S IT ION,1 i ghtP o sitio n);g 1 LightModeli(GL_LIGHT_MODEL_T WO_SID E,GL_T RUE);oglEnab 1 e(G L_N O R M A LIZE);glEnab 1 e(G L _L I GHTING
45、);glEn a ble(GL_L I GHTO);oglMatrixM o de(GL_ PRO JECTIO N);。g 1 Lo a d Ident i t y();gl u Perspe c t i v e(45.0,1.0,6,0,14.0);oglM a trixMod e(GL_MODELV I E W);g 1 L oadldentit y();g 1 Transla t ed(0,0,-1 0);ogl u tD i s pl a y F unc(mo d elDisplay);o g lutK e ybo a r d Fu n c(mo d elKeyb o a rd);glutM o u seFunc(mo d elMou s e);glutMotionFunc(mod elMotion);oglu t Id 1 eFunc(mod elldle);01 o adlde n t i ty(CRM);1 oad I de n t i ty(R M);g I u tMa i n Loop();