camera.cpp camera.h OpenArena // //Module:camera.cpp //Author:TomHicks //Creation:09-01-2003 //LastEdit:10-09-2003 //Editors:TomHicks // //Purpose: //ToimplementanOpenGLCamera // //SummaryofMethods: //Camera //-Camera(); //InitalizePDM's.Setspositionto0,0,0upto0,1,0andviewto0,0,-1 //-Vec3fPosition(); //Returnsacopyofthepositionvector. //-Vec3fView(); //Returnsacopyoftheviewvector. //-Vec3fUpVector(); //Returnsacopyoftheupvector. //-Vec3fStrafe(); //Returnsacopyofthestrafevector. //-voidPositionCamera(doublexpos,doubleypos,doublezpos, //doublexview,doubleyview,doublezview, //doublexup,doubleyup,doublezup); //Setstheposition,up,andviewvectorstothosecreatedfromtherespectiveinputvalues. //-voidPositionCamera(Vec3fpos,Vec3fview,Vec3fup); //Setstheposition,up,andviewvectorstothosepassedin. //-voidRotateView(doubleangle,doubleX,doubleY,doubleZ); //RotatestheviewangledegreesontheaxisspecifiedbyX,Y,andZ. //-voidSetViewByMouse(Screeng_Screen); //Rotatesthepitchandyawoftheviewbasedonthemouse. //-voidRotateAroundPoint(doubleangle,doubleX,doubleY,doubleZ,Vec3fvCenter); //RotatestheviewangledegreesaroundapointvCenterontheaxisspecifiedbyX,Y,Z. //-voidStrafeCamera(doublespeed); //Movesthecameraalonganaxisperpendiculartotheplanespecifiedbytheupandviewvectorsadistancespecifiedbyspeed. //-voidMoveCamera(doublespeed); //Movesthecameraalongtheaxisspecifiedbyit'sviewvectoradistanceequaltospeed. //-voidUpdate(); //Updatetheinternalstrafevector. //-voidLook(); //Positionsthecurrentviewportatpointofthecameraandfacingthesamedirection. // #include"camera.h" namespaceOpenArena { Camera::Camera() { m_vPosition=Vec3f(0,0,0); m_vView=Vec3f(0,0,-1); m_vUpVector=Vec3f(0,1,0); } voidCamera::PositionCamera(doublexpos,doubleypos,doublezpos, doublexview,doubleyview,doublezview, doublexup,doubleyup,doublezup) { m_vPosition=Vec3f(xpos,ypos,zpos); m_vView=Vec3f(xview,yview,zview); m_vUpVector=Vec3f(xup,yup,zup); UpdateVectors(); } voidCamera::PositionCamera(Vec3fpos,Vec3fview,Vec3fup) { m_vPosition=pos; m_vView=view; m_vUpVector=up; UpdateVectors(); } voidCamera::SetViewByMouse(Windowwindow) { //Removetheifdefandusethelinuxsectionforbothlinuxandwindows #ifdef__linux staticdoublecurrentRotX=0.0f; Vec2ipos; Vec2imiddle; doubleangleZ; middle.x=window.GetWidth()/2; middle.y=window.GetHeight()/2; pos=window.GetMousePosition(); if(pos!=middle) { window.SetMousePosition(middle); angleZ=(middle.y-pos.y)/1000.0; currentRotX-=angleZ; if(currentRotX>1.0) { currentRotX=1.0; } elseif(currentRotX<-1.0) { currentRotX=-1.0; } else { Vec3daxis=(m_vView-m_vPosition).cross(m_vUpVector); axis.normalize(); RotateView(angleZ,axis.x,axis.y,axis.z); RotateView((middle.x-pos.x)/1000.0,0,1,0); //RotateView((middle.x-pos.x)/1000.0,m_vUpVector.x,m_vUpVector.y,m_vUpVector.z); } } #endif #ifdef__APPLE__ staticdoublecurrentRotX=0.0f; Vec2ipos; Vec2imiddle; doubleangleZ; middle.x=window.GetWidth()/2; middle.y=window.GetHeight()/2; pos=window.GetMousePosition(); if(pos!=middle) { window.SetMousePosition(middle); angleZ=(middle.y-pos.y)/1000.0; currentRotX-=angleZ; if(currentRotX>1.0) { currentRotX=1.0; } elseif(currentRotX<-1.0) { currentRotX=-1.0; } else { Vec3daxis=(m_vView-m_vPosition).cross(m_vUpVector); axis.normalize(); RotateView(angleZ,axis.x,axis.y,axis.z); RotateView((middle.x-pos.x)/1000.0,0,1,0); //RotateView((middle.x-pos.x)/1000.0,m_vUpVector.x,m_vUpVector.y,m_vUpVector.z); } } #endif #ifdefWIN32 staticdoublecurrentRotX=0.0f; POINTmpos; POINTmiddle; doubleangleZ; middle.x=window.GetWidth()/2; middle.y=window.GetHeight()/2; GetCursorPos(&mpos); SetCursorPos(middle.x,middle.y); if(mpos.x!=middle.x||mpos.y!=middle.y) { angleZ=double(middle.y-mpos.y)/1000.0f; currentRotX-=angleZ; if(currentRotX>1.0f) currentRotX=1.0f; elseif(currentRotX<-1.0f) currentRotX=-1.0f; else { Vec3faxis=(m_vView-m_vPosition).cross(m_vUpVector); axis.normalize(); RotateView(angleZ,axis.x,axis.y,axis.z); //needtoswitchthesetwowhenIfigureoutstuffforflight //tillthenIthinkthefirstisfaster RotateView(double(middle.x-mpos.x)/1000.0f,0,1,0); //RotateView(double(middle.x-mpos.x)/1000.0f,m_vUpVector.x,m_vUpVector.y,m_vUpVector.z); } } #endif } voidCamera::MoveCamera(doublespeed) { Vec3fheading=(m_vView-m_vPosition).normalized(); //m_vPosition=heading*speed m_vPosition.x+=heading.x*speed; m_vPosition.z+=heading.z*speed; m_vView.x+=heading.x*speed; m_vView.z+=heading.z*speed; UpdateVectors(); } voidCamera::RotateView(doubleangle,Vec3daxis) { //MaybemakethisnotcalltheotherRotateViewlater axis.normalize(); RotateView(angle,axis.x,axis.y,axis.z); } voidCamera::RotateView(doubleangle,doublex,doubley,doublez) { Vec3fnView; Vec3fcView; cView=m_vView-m_vPosition; doublecosTheta=cos(angle); doublesinTheta=sin(angle); nView.x=(cosTheta+(1-cosTheta)*x*x)*cView.x+ ((1-cosTheta)*x*y-z*sinTheta)*cView.y+ ((1-cosTheta)*x*z+y*sinTheta)*cView.z; nView.y=(cosTheta+(1-cosTheta)*y*y)*cView.y+ ((1-cosTheta)*x*y+z*sinTheta)*cView.x+ ((1-cosTheta)*y*z-x*sinTheta)*cView.z; nView.z=(cosTheta+(1-cosTheta)*z*z)*cView.z+ ((1-cosTheta)*x*z-y*sinTheta)*cView.x+ ((1-cosTheta)*y*z+x*sinTheta)*cView.y; m_vView.x=m_vPosition.x+nView.x; m_vView.y=m_vPosition.y+nView.y; m_vView.z=m_vPosition.z+nView.z; } voidCamera::StrafeCamera(doublespeed) { m_vPosition.x+=m_vStrafe.x*speed; m_vPosition.z+=m_vStrafe.z*speed; m_vView.x+=m_vStrafe.x*speed; m_vView.z+=m_vStrafe.z*speed; } voidCamera::Update() { m_vStrafe=((m_vView-m_vPosition).cross(m_vUpVector)).normalized(); //SetViewByMouse();//TODOtakethiswholefunctionoutasap } voidCamera::Look() { gluLookAt(m_vPosition.x,m_vPosition.y,m_vPosition.z, m_vView.x,m_vView.y,m_vView.z, m_vUpVector.x,m_vUpVector.y,m_vUpVector.z); } Vec3fCamera::Position() { returnm_vPosition; } Vec3fCamera::Strafe() { returnm_vStrafe; } Vec3fCamera::UpVector() { returnm_vUpVector; } Vec3fCamera::View() { returnm_vView; } voidCamera::RotateHorizontal(floatangle) { RotateView(angle,GetUpVector()); } voidCamera::RotateVertical(floatangle) { RotateView(angle,GetRightVector()); } Vec3fCamera::GetUpVector() { returnm_vUpVector; } Vec3fCamera::GetRightVector() { returnm_vStrafe; } Vec3fCamera::GetForwardVector() { returnm_vView; } voidCamera::UpdateVectors() { } };