Planeshift

pscamera.h

Go to the documentation of this file.
00001 /*
00002  * Author: Andrew Robberts
00003  *
00004  * Copyright (C) 2003 Atomic Blue ([email protected], http://www.atomicblue.org)
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation (version 2 of the License)
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 
00020 #ifndef PS_CAMERA_HEADER
00021 #define PS_CAMERA_HEADER
00022 //=============================================================================
00023 // Crystal Space Includes
00024 //=============================================================================
00025 #include <csutil/csstring.h>
00026 #include <csutil/ref.h>
00027 #include <iutil/virtclk.h>
00028 
00029 //=============================================================================
00030 // Project Includes
00031 //=============================================================================
00032 #include "net/cmdbase.h"
00033 
00034 //=============================================================================
00035 // Local Includes
00036 //=============================================================================
00037 #include "psoptions.h"
00038 
00039 #define NPC_MODE_DISTANCE 5.0f
00040 
00041 struct RaceDefinition;
00042 struct iView;
00043 struct iPerspectiveCamera;
00044 struct iSector;
00045 struct iCollideSystem;
00046 struct iMeshWrapper;
00047 
00048 class GEMClientActor;
00049 class GEMClientObject;
00050 
00051 class psCamera : public iOptionsClass, public psCmdBase
00052 {
00053 public:
00054 
00055     psCamera();
00056     virtual ~psCamera();
00057 
00058     void LoadOptions();
00059     void SaveOptions();
00060 
00061     // the different camera modes
00062     enum CAMERA_MODES
00063     {
00064         CAMERA_FIRST_PERSON = 0,
00065         CAMERA_THIRD_PERSON,
00066         CAMERA_M64_THIRD_PERSON,
00067         CAMERA_LARA_THIRD_PERSON,
00068         CAMERA_FREE,
00069 
00070         // a special hidden camera mode that contains the actual data to use (all other camera modes set this one accordingly)
00071         CAMERA_ACTUAL_DATA, // this should be the first entry here after the real camera modes
00072         CAMERA_LAST_ACTUAL, // keep reference to what the actual data was last frame
00073 
00074         // This is a camera mode that occurs in the transition phase in between normal camera modes.
00075         CAMERA_TRANSITION,
00076 
00077         // camera mode for talking to an NPC
00078         CAMERA_NPCTALK,
00079 
00080         // A camera mode that is used when a collision occurs.
00081         // It allows for collisions to be rectified faster than normal
00082         CAMERA_COLLISION,
00083         CAMERA_ERR, // this is the error between the actual camera pos and its ideal
00084 
00085         CAMERA_MODES_COUNT
00086     };
00087 
00088     struct DistanceCfg
00089     {
00090         bool adaptive;      // is distance adapted automatically ?
00091         int dist;           // fixed distance (used when adaptive=false)
00092         int minFPS;         // acceptable FPS interval (used when adaptive=true)
00093         int maxFPS;
00094         int minDist;        // minimum acceptable distance (used when adaptive=true)
00095     };
00096 
00097     const char *HandleCommand(const char *cmd);
00098     void HandleMessage(MsgEntry *msg);
00099 
00100 
00105     bool InitializeView(GEMClientActor* entity);
00106 
00110     void SetActor(GEMClientActor* entity);
00111 
00120     void npcTargetReplaceIfEqual(GEMClientObject *currentEntity, GEMClientObject* replacementEntity);
00121 
00127     bool LoadFromFile(bool useDefault=false, bool overrideCurrent=true);
00128 
00132     bool SaveToFile();
00133 
00137     bool Draw();
00138 
00143     void SetPosition(const csVector3& pos, int mode = -1);
00144 
00149     csVector3 GetPosition(int mode = -1) const;
00150 
00157     void SetTarget(const csVector3& tar, int mode = -1);
00158 
00163     csVector3 GetTarget(int mode = -1) const;
00164 
00171     void SetUp(const csVector3& up, int mode = -1);
00172 
00179     csVector3 GetUp(int mode = -1) const;
00180 
00184     void LockCameraMode(bool state);
00185 
00189     void SetCameraMode(int mode);
00190 
00193     void NextCameraMode();
00194 
00198     int GetCameraMode() const;
00199 
00203     csString GetCameraModeVerbose() const;
00204 
00208     iPerspectiveCamera *GetICamera();
00209 
00213     iView *GetView();
00214 
00222     iMeshWrapper * Get3DPointFrom2D(int x, int y, csVector3 * worldCoord=0, csVector3 * untransfCoord=0);
00223 
00233     iMeshWrapper* FindMeshUnder2D(int x, int y, csVector3  * pos = NULL, int *poly = NULL);
00234 
00239     void SetPitch(float pitch, int mode = -1);
00240 
00245     void MovePitch(float deltaPitch, int mode = -1);
00246 
00251     float GetPitch(int mode = -1) const;
00252 
00256     void SetPitchVelocity(float pitchVel);
00257 
00261     float GetPitchVelocity() const;
00262 
00267     void SetYaw(float yaw, int mode = -1);
00268 
00273     void MoveYaw(float deltaYaw, int mode = -1);
00274 
00279     float GetYaw(int mode = -1) const;
00280 
00284     void SetYawVelocity(float yawVel);
00285 
00289     float GetYawVelocity() const;
00290 
00295     void SetDistance(float distance, int mode = -1);
00296 
00301     void MoveDistance(float deltaDistance, int mode = -1);
00302 
00307     float GetDistance(int mode = -1) const;
00308 
00311     void ResetActualCameraData();
00312 
00315     void ResetCameraPositioning();
00316 
00320     bool RotateCameraWithPlayer() const;
00321 
00326     csVector3 GetForwardVector(int mode = -1) const;
00327 
00332     csVector3 GetRightVector(int mode = -1) const;
00333 
00338     float GetMinDistance(int mode = -1) const;
00339 
00344     void SetMinDistance(float dist, int mode = -1);
00345 
00350     float GetMaxDistance(int mode = -1) const;
00351 
00356     void SetMaxDistance(float dist, int mode = -1);
00357 
00362     float GetTurnSpeed(int mode = -1) const;
00363 
00368     void SetTurnSpeed(float speed, int mode = -1);
00369 
00374     float GetSpringCoef(int mode = -1) const;
00375 
00380     void SetSpringCoef(float coef, int mode = -1);
00381 
00386     float GetDampeningCoef(int mode = -1) const;
00387 
00392     void SetDampeningCoef(float coef, int mode = -1);
00393 
00398     float GetSpringLength(int mode = -1) const;
00399 
00404     void SetSpringLength(float length, int mode = -1);
00405 
00409     bool CheckCameraCD() const;
00410 
00414     void SetCameraCD(bool useCD);
00415 
00419     bool GetUseNPCCam();
00420 
00424     void SetUseNPCCam(bool useNPCCam);
00425 
00429     float GetTransitionThreshold() const;
00430 
00434     void SetTransitionThreshold(float threshold);
00435 
00440     float GetDefaultPitch(int mode = -1) const;
00441 
00446     void SetDefaultPitch(float pitch, int mode = -1);
00447 
00452     float GetDefaultYaw(int mode = -1) const;
00453 
00458     void SetDefaultYaw(float yaw, int mode = -1);
00459 
00464     float GetSwingCoef(int mode = -1) const;
00465 
00470     void SetSwingCoef(float swingCoef, int mode = -1);
00471 
00475     void UseFixedDistanceClipping(float dist);
00476 
00482     void UseAdaptiveDistanceClipping(int minFPS, int maxFPS, int minDist);
00483 
00487     int GetLastCameraMode();
00488 
00489     DistanceCfg GetDistanceCfg();
00490     void SetDistanceCfg(DistanceCfg newcfg);
00491 
00492     int GetFixedDistClip() { return fixedDistClip; }
00493 
00494     bool IsInitialized() { return cameraInitialized; }
00495 
00496 private:
00497 
00498     struct CameraData
00499     {
00500         csVector3 worldPos;
00501         csVector3 worldTar;
00502         csVector3 worldUp;
00503 
00504         float pitch;
00505         float yaw;
00506         float roll; // not implemented yet (no need atm)
00507 
00508         float defaultPitch;
00509         float defaultYaw;
00510         float defaultRoll;
00511 
00512         float distance; // the distance between position and target
00513         float minDistance;
00514         float maxDistance;
00515 
00516         float turnSpeed;
00517 
00518         float springCoef;
00519         float InertialDampeningCoef;
00520         float springLength;
00521 
00522         float swingCoef;
00523 
00524         //initialize all here:
00525         CameraData()
00526         {
00527             worldPos.Set(0.0f,0.0f,0.0f);
00528             worldTar.Set(0.0f,0.0f,0.0f);
00529             worldUp.Set(0.0f,0.0f,0.0f);
00530             pitch = yaw = roll = 0;
00531             defaultPitch = defaultYaw = defaultRoll = 0.0f;
00532             distance = minDistance = maxDistance = 0.0f;
00533             turnSpeed  = 0.0f;
00534             springCoef = InertialDampeningCoef = springLength = 0.0f;
00535             swingCoef=0.0f;
00536         }
00537     };
00538 
00539     float CalcSpringCoef(float springyness, float scale) const;
00540     float CalcInertialDampeningCoef(float springyness, float scale) const;
00541     float EstimateSpringyness(size_t camMode) const;
00542 
00549     void DoCameraIdealCalcs(const csTicks elapsedTicks, const csVector3& actorPos, const csVector3& actorEye, const float actorYRot);
00550 
00558     csVector3 CalcCollisionPos(const csVector3& pseudoTarget, const csVector3& pseudoPosition, iSector*& sector);
00559 
00562     void DoCameraTransition();
00563 
00572     void DoElasticPhysics(bool isElastic, const csTicks elapsedTicks, const CameraData& deltaIdeal, iSector* sector);
00573 
00577     void EnsureActorVisibility();
00578 
00592     csVector3 CalcElasticPos(csVector3 currPos, csVector3 idealPos, csVector3 deltaIdealPos,
00593                              float deltaTime, float springCoef, float dampCoef, float springLength) const;
00594 
00595 
00609     float CalcElasticFloat(float curr, float ideal, float deltaIdeal,
00610                            float deltaTime, float springCoef, float dampCoef, float springLength) const;
00611 
00612 
00616     void CalculateFromYawPitchRoll(int mode = -1);
00617 
00621     void CalculateNewYawPitchRoll(int mode = -1);
00622 
00626     void CalculatePositionFromYawPitchRoll(int mode = -1);
00627 
00632     float CalculateNewYaw(csVector3 dir);
00633 
00637     void EnsureCameraDistance(int mode = -1);
00638 
00643     bool IsActorVisible(int mode = -1) const;
00644 
00649     float SaturateAngle(float angle) const;
00650 
00656     bool CloneCameraModeData(int fromMode, int toMode = -1);
00657 
00661     void SetDistanceClipping(float dist);
00662     float GetDistanceClipping();
00663 
00667     void AdaptDistanceClipping();
00668 
00669     // each mode's camera data has to be handled seperately
00670     CameraData camData[CAMERA_MODES_COUNT];
00671 
00672     int lastCameraMode;
00673     int currCameraMode;
00674 
00675     // velocity stuff
00676     float pitchVelocity;
00677     float yawVelocity;
00678 
00679     // some camera settings
00680     // NOTE: the offsets are not in the format you think they are, due to speed issues the offset is restricted to being directly behind the actor (no yaw).
00681     csVector3 firstPersonPositionOffset;
00682     csVector3 thirdPersonPositionOffset;
00683 
00684     /* if the squared difference between the actual camera position and the ideal position is less than this,
00685      * then we're no longer in the transition phase.
00686      */
00687     float transitionThresholdSquared;
00688     bool inTransitionPhase;
00689 
00691     bool hasCollision;
00692 
00694     bool cameraHasBeenPositioned;
00695 
00697     bool useCameraCD;
00698 
00700     bool useNPCCam;
00701 
00703     bool cameraInitialized;
00704 
00706     bool lockedCameraMode;
00707 
00708     csWeakRef<iSector> lastTargetSector;
00709 
00710     GEMClientActor* actor;
00711 
00713     csRef<iView> view;
00714 
00716     csRef<iVirtualClock> vc;
00717 
00719     csRef<iCollideSystem> cdsys;
00720 
00722     csTicks prevTicks;
00723 
00725     RaceDefinition *race;
00726 
00727     DistanceCfg distanceCfg;
00728 
00733     GEMClientActor* npcModeTarget;
00734 
00736     csVector3 npcModePosition;
00737 
00739     float npcOldRot;
00740     float vel;
00741     int fixedDistClip;
00742 };
00743 
00744 #endif