Sei sulla pagina 1di 4

CS408, Lab 3: OpenGL for 3D Objects

Setting up an OpenGL Project


1. Start Microsoft Visual Studio 2010. Select File / New Project. Under Project Ty e!" c#oose Visual $%% / &in'2 $onsole ( lication. $#an)e t#e na*e to +oc,et or so*e suita-le na*e and c#an)e t#e location to a #ard dri.e located on t#e co* uter /ty ically $ or 01. 2ou will need to re*e*-er t#is location for ste s ' and 3. $lic, 45. 2. $lic, Ne6t7 or ( lication Settin)s /t#ey )o to t#e sa*e lace1. $#ec, t#e 8* ty Project! -o6. $lic, Finis#. '. 9o to #tt ://www2.cs.ure)ina.ca/;ani*a/<0=/Notes/4 en9>/4 en9>.#t* and download files fro* t#e Second 4 en9> section: *ain2.c " Slee .#" and Slee .c to your ro)ra* directory. <. ?n Visual $%%" )o to Project/(dd 86istin) ?te*@ and select *ain2.c . 3. 0ownload 9>UT for &indows fro* #tt ://www.6*ission.co*/;nate/)lut.#t*l to your roject directory. $#oose t#e glut-3.7.6-bin.zip (117 KB) file. 86tract t#e files fro* t#e Ai file and lace t#e* in your roject folder. Note: you just want t#e fi.e actual files fro* t#e inner*ost folder of t#e )lut Ai file. B. 8nsure t#at Solution 86 lorer is .isi-le /View / Solution 86 lorer1. Select *ain2.c . C. $o* ile and run t#e ori)inal ro)ra* /usin) t#e )reen trian)le on t#e t#ird line just -efore 0e-u)!1. 2ou s#ould see 2 windows: a te6t console and a )ra #ical window la-eled Second 4 en9> Pro)ra*!. T#ere is a wireDfra*e cu-e in t#e *iddle of t#is second window. 86it t#e windows. $losin) t#e te6t window will close t#e )ra #ical window" -ut not .ice .ersa.

Playing it! "o#els


=. ?n t#e dis lay function" c#an)e t#e osition of t#e wireDfra*e cu-e -y adjustin) t#e ara*eters to )lTranslate. Mo.e it to D2 in t#e E di*ension and D1 in t#e F di*ension. glTranslatef(-2.0, 0.0, -1.0); G. $#an)e t#e rotation of t#e wireDfra*e cu-e -y rotatin) it -y 10 de)rees around t#e E a6is. Note: t#e E a6is corres onds to /1" 0" 01: glRotatef(10.0, 1.0, 0.0, 0.0); 10. Try so*e ot#er rotations" suc# as D10 and G0. 11. Ma,e t#e wireDfra*e cu-e t#ree ti*es as tall -y scalin) t#e 2 di*ension -y a factor of ': glScalef(1.0, 3.0, 1.0); 12. (dd a co y of t#e wireDfra*e cu-e at %2 in t#e E di*ension and D1 in t#e F di*ension. To do so" co y all t#e code fro* )lPus#Matri6 to )lPo Matri6 inclusi.e and t#en adjust t#e ara*eters to glTranslatef. 1'. $#an)e t#e first cu-e to a solid cu-e instead of a wireDfra*e cu-e -y callin) function glutSolidCube instead of glutWireCube

1<. $#an)e t#e color of t#e cu-e to -lue /0" 0" 11 -y addin) at )l$olor' line after its )lPus#Matri6 line. glColor3f(0.0, 0.0, 1.0); 13. (dd a wireDfra*e s #ere at /0" 0" D21. 9i.e it a radius of 0.G wit# 20 slices /lines of lon)itude1 and <0 stac,s /lines of latitude1: glutWireS !ere(0.", 20, #0); Note: To do so" you s#ould co y all t#e code for one cu-e fro* gl$us!%atri& to gl$o %atri& inclusi.e" and re lace crucial line wit# t#e glutWireS !ere line. 1B. (dd a wireDfra*e cone at t#e ori)in. 9i.e it a radius of 0.G wit# 20 slices /lines of lon)itude1 and <0 stac,s /lines of latitude1:
glutWireCone(1.0, 1.', 20, 1); (( radius is 1, !eig!t is 1.'

()ain" you s#ould co y all t#e code for one cu-e fro* gl$us!%atri& to gl$o %atri& inclusi.e" and re lace t#e crucial line wit# t#e glutWireCone line.

$##ing t!e %, &, an# ' $(es


1C. To dis lay t#e E a6is" draw a red line fro* D100 to %100 on t#e EDa6is" i.e." fro* /D 100" 0" 01 to /100" 0" 01. (dd t#is drawin) code in t#e dis lay function after t#e )lu>oo,(t lines: gl)egin(*+,+-./S); glColor3f(1.0, 0.0, 0.0); gl0erte&3f(-100.0, 0.0, 0.0); gl0erte&3f( 100.0, 0.0, 0.0); gl/nd(); 1=. To dis lay t#e 2 a6is" draw a )reen line fro* D100 to %100 on t#e 2Da6is" i.e." fro* /0" D100" 01 to /0" D100" 01. (dd t#is drawin) code: gl)egin(*+,+-./S); glColor3f(0.0, 1.0, 0.0); gl0erte&3f(0.0, -100.0, 0.0); gl0erte&3f(0.0, 100.0, 0.0); gl/nd(); 1G. (dd code to draw a -lue line for t#e FDa6is. 20. 4 tional: $ut/ aste t#e code for drawin) t#e a6es to a new .oid function called a&es,dis la1 and in t#e dis la1 function just ut a call to a&es,dis la1( );

"a)ing a *oc)et
21. 2ou s#ould -e a-le to see t#at t#e wireDfra*e cone is oriented alon) t#e F a6is. T#e s*all end oints towards ositi.e F. ?n t#e dis la1 function" to *o.e t#e ca*era fart#er away fro* t#e scene" c#an)e t#e first line of nu*-ers for t#e glu+oo23t function fro* 1.0" 2.0" 2.0" to 1.0" 20.0" 20.0" 22. ?n t#e dis la1 function" rotate t#e cone -y 2C0 de)rees around t#e E a6is so t#at it is ointin) u alon) t#e 2 a6is. glRotatef(240, 1, 0, 0); 2'. +aise t#e cone u -y < units" -y translatin) -y < alon) t#e 2 a6is: glTranslatef(0.0, #.0, 0.0);

2<. Ma,e a -ody for t#e roc,et -y usin) a )lu cylinder /no to or -otto*1 and a )lu dis, for t#e -otto*. 5ind of co* licated" sorry:
*+56uadric7b89 ,6uadric : glu.e;<uadric(); gl$us!%atri&(); glColor3f(0.', 0.', 0.0); glRotatef(240, 1, 0, 0); gluC1linder( ,6uadric, 1.0, 1.0, #.0, 20, 1); glColor3f(0.', 0.', 0.'); glu=is2( ,6uadric, 0.0, 1.0, 20, #); gl$o %atri&(); glu=elete<uadric( ,6uadric);

23. 4 tional: a. $ut/ aste t#e code for drawin) t#e roc,et" includin) t#e outer )lPus#Matri6 and )lPo Matri6 calls" to a new .oid function called roc2et,dis la1 and in t#e dis la1 function just ut a call to roc2et,dis la1( ); -. To *a,e t#e roc,et red in color" in t#e roc,et dis lay function" ut a call to glColorH -efore t#e gl$us!%atri& and re*o.e t#e ot#er )l$olorH lines. c. To rotate or scale t#e roc,et as a w#ole" ut glRotatef and glScalef lines after t#e glTranslatef line t#at is just after t#e gl$us!%atri&() line.

Launc!ing t!e *oc)et


2B. ?n t#e *ain function" add a call to glut-dle>unc after t#e call to glut=is la1>unc. glut-dle>unc(idle); (dd function called idle to your ro)ra* a-o.e dis la1: ?oid idle () @ (( tell 7 en*+ to redis la1 as often as ossible glut$ostRedis la1(); A (dd a rototy e for t#is function wit# t#e rototy e for t#e dis lay function. $o* ile and run your ro)ra*. ?t s#ould a ear t#e sa*e -ut #o) all a.aila-le $PU ti*e. 2ou will need to close t#e te6t window to sto it. 2C. $reate a new )lo-al floatin) oint .aria-le" na*ed 1$osition wit# an initial .alue of 0.0. float 1$osition : 0.0; ?n t#e idle function" insert t#e followin) after t#e code for t#eta: 1$osition B: 0.1; if(1$osition C 10) 1$osition : 0.0; 2=. (dd t#e lines gl$us!%atri&(); glTranslatef(0.0, 1$osition, 0.0f); i**ediately -efore t#e code to draw t#e roc,et. ?ndent t#e code for drawin) t#e roc,et. (fter t#e code to draw t#e roc,et" add t#e line gl$o %atri&();

'

()ain" all lines -etween gl$us!%atri& and gl$o %atri& s#ould -e indented for reada-ility reasons. 2G. $o* ile and run t#e ro)ra*. T#e roc,et s#ould fly out of si)#t" rea ear at t#e ori)in" and t#en re eat. ?t s#ould #a en so fast t#at you can #ardly see it. '0. &e will slow t#e ani*ation down -y *a,in) 4 en9> wait 1/100 of a second -etween t#e dis la1 of eac# fra*e. 9o to Project / (dd 86istin) ?te*@ and add Slee .! and Slee .c to your roject. (t t#e to of your Dain2.c file" add Einclude FSlee .!F ?n your idle function" i**ediately -efore glut$ostRedis la1" add t#e line slee (0.01); (( ;ait for 0.01 seconds $o* ile and run t#e ro)ra*. T#e roc,et s#ould fly at a sensi-le s eed and t#e E -utton in t#e corner s#ould wor, a)ain.

<

Potrebbero piacerti anche