资源描述:
《glut教程六movethecamera》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、KeyboardExample:MovingtheCameraPrev: KeyboardNext: AdvancedKeyboard OK,soletsseeamoreexcitinguseforthekeyboardusingGLUT.Inthissectionwe’regoingtogothroughthecodeofanapplicationthatwilldrawasmallworldpopulatedwithsnowmen,andwe’regoingtousethedirectionkeystomovethecamerainthisworld.Thelefta
2、ndrightkeyswillrotatethecameraaroundtheYaxis,i.e.intheXZplane,whereastheupanddownkeyswillmovethecameraforwardandbackwardsinthecurrentdirection.Thecodeforthissampleapplicationisnowpresentedwithcommentswhereappropriate.Firstweneedsomeglobalvariablestostorethecameraparameters.Thevariableswil
3、lstoreboththecamerapositionandthevectorthatgivesustheaimingdirection.Wewillalsostoretheangle.Thereisnoneedtostorethe y componentsinceitisconstant.Hence,weneed:·angle:theangleofrotationintheyaxis.thisvariablewillallowustorotatethecamera·x,z:ThecamerapositionintheXZplane·lx,lz:Avectordefini
4、ngourlineofsightLetsdealwiththevariabledeclarations://angleofrotationforthecameradirectionfloatangle=0.0;//actualvectorrepresentingthecamera'sdirectionfloatlx=0.0f,lz=-1.0f;//XZpositionofthecamerafloatx=0.0f,z=5.0f;Thecodetodrawasnowmanisnowpresented.TheresultlookslikethisvoiddrawSnowMan(
5、){glColor3f(1.0f,1.0f,1.0f);//DrawBodyglTranslatef(0.0f,0.75f,0.0f);glutSolidSphere(0.75f,20,20);//DrawHeadglTranslatef(0.0f,1.0f,0.0f);glutSolidSphere(0.25f,20,20);//DrawEyesglPushMatrix();glColor3f(0.0f,0.0f,0.0f);glTranslatef(0.05f,0.10f,0.18f);glutSolidSphere(0.05f,10,10);glTranslatef
6、(-0.1f,0.0f,0.0f);glutSolidSphere(0.05f,10,10);glPopMatrix();//DrawNoseglColor3f(1.0f,0.5f,0.5f);glRotatef(0.0f,1.0f,0.0f,0.0f);glutSolidCone(0.08f,0.5f,10,2);}Nextwehavethenewrenderfunction.Itcontainsallthecommandstodrawourlittleworld.Anotherchangeisinthe gluLookAt function.Theparameters
7、ofthe gluLookAt functionarenowvariablesinsteadoffixedvalues.Justincaseyouaren’tfamiliarwiththisfunction,heregoesabriefexplanation.The gluLookAt functionprovidesaneasyandintuitivewaytosetthecamerapositionandorientation.Basicallyithasthreegroupsofparameters,eachoneisc