Changeset 3746 in orxonox.OLD for orxonox/branches/levelloader/src/camera.cc
- Timestamp:
- Apr 7, 2005, 3:54:49 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/levelloader/src/camera.cc
r3605 r3746 17 17 18 18 #include "camera.h" 19 19 20 #include "world.h" 20 21 #include "world_entity.h" 22 #include "vector.h" 21 23 22 24 using namespace std; 23 25 26 //////////// 27 // CAMERA // 28 //////////// 29 24 30 /** 25 31 \brief creates a Camera 26 27 This standard constructor sets all parameters to zero28 32 */ 29 Camera::Camera (World* world)33 Camera::Camera(void) 30 34 { 31 this->world = world; 32 this->bound = NULL; 33 /* give it some physical live */ 34 this->m = 10; 35 this->a = new Vector(0.0, 0.0, 0.0); 36 this->v = new Vector(0.0, 0.0, 0.0); 37 this->fs = new Vector(0.0, 0.0, 0.0); 38 this->cameraMode = NORMAL; 39 this->deltaTime = 3000.0; 40 this->cameraOffset = 1.0; 41 this->cameraOffsetZ = 10.0; 42 this->t = 0.0; 35 this->setClassName("Camera"); 36 this->target = new CameraTarget(); 43 37 38 this->setFovy(90); 39 this->setAspectRatio(1.2f); 40 this->setClipRegion(.1, 2000); 44 41 45 this->set Drawable (false);42 this->setViewMode(VIEW_NORMAL); 46 43 } 47 44 … … 49 46 \brief default destructor 50 47 */ 51 Camera::~Camera ()48 Camera::~Camera(void) 52 49 { 53 this->bound = NULL;54 this->world = NULL;55 56 50 } 57 51 58 52 /** 59 \brief time based actualisation of camera parameters 60 \param deltaT: The amount of time that has passed in milliseconds 61 62 This is called by the World in every time_slice, use it to do fancy time dependant effects (such 63 as smooth camera movement or swaying). 53 \brief focuses the Camera onto a Target 54 \param target the new PNode the Camera should look at. 64 55 */ 65 void Camera:: tick (Uint32 deltaT)56 void Camera::lookAt(PNode* target) 66 57 { 67 if( this->t <= deltaTime) 68 {this->t += deltaT;} 69 //printf("time is: t=%f\n", t ); 70 updateDesiredPlace(); 71 //jump(NULL); 58 this->target->setParent(target); 72 59 } 73 60 74 61 /** 75 \brief this calculates the location where the track wants the camera to be 76 77 This refreshes the placement the camera should have according to the 78 bound entity's position on the track. 62 \returns The PNode of the Target (from there you can get position and so on 79 63 */ 80 void Camera::updateDesiredPlace ()64 PNode* Camera::getTarget(void) 81 65 { 82 switch(cameraMode) 83 { 84 85 case ELLIPTICAL: 86 { 87 /* 88 //r = actual_place.r 89 Orxonox *orx = Orxonox::getInstance(); 90 Location lookat; 91 Placement plFocus; 92 if( bound != NULL) 93 { 94 bound->getLookat (&lookat); 95 orx->getWorld()->calcCameraPos (&lookat, &plFocus); 96 Quaternion *fr; 97 if(t < 20.0) 98 { 99 Vector *start = new Vector(0.0, 1.0, 0.0); 100 r = *(new Vector(0.0, 5.0, 0.0)); 101 102 Vector up(0.0, 0.0, 1.0); 103 104 Vector op(1.0, 0.0, 0.0); 105 float angle = angleDeg(op, *start); 106 printf("angle is: %f\n", angle); 107 108 //if in one plane 109 from = new Quaternion(angle, up); 110 111 //from = new Quaternion(*start, *up); 112 //&from = &plFocus.w; 113 //fr = &plFocus.w; real quaternion use 114 66 return (PNode*)this->target; 67 } 115 68 116 69 117 Vector vDirection(1.0, 0.0, 0.0); 118 //vDirection = plFocus.w.apply(vDirection); 119 to = new Quaternion(vDirection, *start); 120 res = new Quaternion(); 121 } 122 //printf("vector r = %f, %f, %f\n",r.x, r.y, r.z ); 123 rAbs = r.len(); 124 if(t < 30) 125 { 126 ka = rAbs / deltaTime*deltaTime; 127 } 70 /** 71 \brief sets a new AspectRatio 72 \param aspectRatio the new aspect ratio to set (width / height) 73 */ 74 void Camera::setAspectRatio(float aspectRatio) 75 { 76 this->aspectRatio = aspectRatio; 77 } 128 78 129 res->quatSlerp(to, from, t/deltaTime, res); 79 /** 80 \brief sets the Field of View to fofy 81 \param fovy new field of view factor (in degrees) 82 */ 83 void Camera::setFovy(float fovy) 84 { 85 this->fovy = fovy; 86 } 130 87 131 Vector ursp(0.0, 0.0, 0.0); 132 desiredPlace.r = ursp - res->apply(r); 88 /** 89 \brief Sets a new clipping region 90 \param nearClip The near clip plane 91 \param farClip The far clip plane 92 */ 93 void Camera::setClipRegion(float nearClip, float farClip) 94 { 95 this->nearClip = nearClip; 96 this->farClip = farClip; 97 } 133 98 134 printf("desired place is: %f, %f, %f\n", desiredPlace.r.x, desiredPlace.r.y, desiredPlace.r.z); 135 //plLastBPlace = *bound->get_placement(); 136 137 } 138 */ 139 } 99 void Camera::setViewMode(ViewMode mode) 100 { 101 switch (mode) 102 { 103 default: 104 case VIEW_NORMAL: 105 this->toFovy = 60.0; 106 this->toRelCoor = Vector(-10, 5, 0); 140 107 break; 141 case SMOTH_FOLLOW: 142 { 143 /* 144 Placement *plBound = bound->getPlacement(); 145 Location lcBound; 146 if(bound != null) 147 { 148 bound->getLookat(&lcBound); 149 Vector vDirection(0.0, 0.0, 1.0); 150 vDirection = plBound->w.apply(vDirection); 151 desiredPlace.r = (vDirection * ((lcBound.dist-10.0))) + Vector(0,0,5.0); 152 } 153 */ 154 break; 155 } 156 /* this is a camera mode that tries just to follow the entity. */ 157 case STICKY: 158 { 159 /* 160 if(bound != null) 161 { 162 Placement *plBound = bound->getPlacement(); 163 Vector vDirection(0.0, 0.0, 1.0); 164 Vector eclipticOffset(0.0, 0.0, 5.0); 165 vDirection = plBound->w.apply(vDirection); 166 desiredPlace.r = plBound->r - vDirection*10 + eclipticOffset; 167 } 168 */ 169 break; 170 } 171 /* the camera is handled like an entity and rolls on the track */ 172 case NORMAL: 173 if( bound != NULL && world != NULL ) 174 { 175 //FIXME: camera should be made via relative coordinates 176 Vector* cameraOffset = new Vector (-10, 5, 0); 177 this->setRelCoor (cameraOffset); 178 } 179 else 180 { 181 /* 182 desiredPlace.r = Vector (0,0,0); 183 desiredPlace.w = Quaternion (); 184 */ 185 } 108 case VIEW_BEHIND: 109 this->toFovy = 120.0; 110 this->toRelCoor = Vector(-7, 0, 0); 186 111 break; 112 case VIEW_FRONT: 113 this->toFovy = 95.0; 114 this->toRelCoor = Vector(12, 5, 0); 115 break; 116 case VIEW_LEFT: 117 this->toFovy = 90; 118 this->toRelCoor = Vector(0, 2, -10); 119 break; 120 case VIEW_RIGHT: 121 this->toFovy = 90; 122 this->toRelCoor = Vector(0, 2, 10); 123 break; 124 case VIEW_TOP: 125 this->toFovy= 120; 126 this->toRelCoor = Vector(0, 4, 0); 187 127 } 188 128 } 129 130 131 /** 132 \brief Updates the position of the camera. 133 \param dt The time that elapsed. 134 */ 135 void Camera::tick(float dt) 136 { 137 dt /= 500; 138 float tmpFovy = (this->toFovy - this->fovy) * dt; 139 if (tmpFovy > .001) 140 this->fovy += (this->toFovy - this->fovy) * dt; 141 Vector tmpPos = (this->toRelCoor - *this->getRelCoor()) * dt; 142 if (tmpPos.len() >= .001) 143 { 144 tmpPos = tmpPos + *this->getRelCoor(); 145 this->setRelCoor(&tmpPos); 146 } 147 } 148 189 149 190 150 /** … … 196 156 void Camera::apply () 197 157 { 158 // switching to Projection Matrix 198 159 glMatrixMode (GL_PROJECTION); 199 160 glLoadIdentity (); 200 // view201 // TO DO: implement options for frustum generation202 //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0);203 gluPerspective(60, 1.2f, 0.1, 2000);204 205 //Vector up(0,0,1);206 //Vector dir(1,0,0);207 //Quaternion q(dir,up);208 //float matrix[4][4];209 //q.conjugate().matrix (matrix);210 //glMultMatrixf ((float*)matrix);211 //glTranslatef (10,0,-5);212 //213 //dir = Vector(-1,-1,0);214 //q = Quaternion( dir, up);215 //glMatrixMode (GL_MODELVIEW);216 //glLoadIdentity ();217 //q.matrix (matrix);218 //glMultMatrixf ((float*)matrix);219 //glTranslatef (2,2,0);220 //221 //glBegin(GL_TRIANGLES);222 //glColor3f(1,0,0);223 //glVertex3f(0,0,0.5);224 //glColor3f(0,1,0);225 //glVertex3f(-0.5,0,-1);226 //glColor3f(0,0,1);227 //glVertex3f(0.5,0,-1);228 //glEnd();229 161 230 // ===== first camera control calculation option 231 // rotation 232 float matrix[4][4]; 233 //this->absDirection.conjugate().matrix (matrix); 234 /* orientation and */ 235 //glMultMatrixf ((float*)matrix); 162 // setting up the perspective 163 gluPerspective(this->fovy, 164 this->aspectRatio, 165 this->nearClip, 166 this->farClip); 236 167 237 /* translation */ 238 //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z ); 168 // speed-up feature 169 Vector cameraPosition = this->getAbsCoor(); 170 Vector targetPosition = this->target->getAbsCoor(); 171 Vector up = Vector(0, 1, 0); 172 up = this->getAbsDir().apply(up); 239 173 174 // Setting the Camera Eye, lookAt and up Vectors 175 gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, 176 targetPosition.x, targetPosition.y, targetPosition.z, 177 up.x, up.y, up.z); 240 178 241 // ===== second camera control calculation option 242 243 gluLookAt(this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z, 244 this->parent->getAbsCoor ().x, this->parent->getAbsCoor ().y, this->parent->getAbsCoor ().z, 245 0.0, 1.0, 0.0); 246 247 179 // switching back to Modeling Matrix 248 180 glMatrixMode (GL_MODELVIEW); 249 glLoadIdentity ();250 181 } 251 182 252 183 253 184 254 /** 255 \brief bind the camera to an entity 256 \param entity: The enitity to bind the camera to 257 258 This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's 259 Location and get_lookat() to determine the viewpoint the camera will render from. 260 Note that you cannot bind a camera to a free entity. 261 */ 262 void Camera::bind (WorldEntity* entity) 185 /////////////////// 186 // CAMERA-TARGET // 187 /////////////////// 188 189 190 CameraTarget::CameraTarget() 263 191 { 264 if( entity != NULL) 265 { 266 if( entity->isFree()) printf("Cannot bind camera to free entity"); 267 else 268 { 269 this->bound = entity; 270 } 271 } 192 this->setClassName("CameraTarget"); 193 this->setMode(PNODE_MOVEMENT); 272 194 } 273 195 196 CameraTarget::~CameraTarget() 197 { 274 198 275 void Camera::setWorld(World* world)276 {277 this->world = world;278 199 } 279 280
Note: See TracChangeset
for help on using the changeset viewer.