[2068] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific: |
---|
[2080] | 14 | main-programmer: Christian Meyer |
---|
[2068] | 15 | co-programmer: ... |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | #include "camera.h" |
---|
[2100] | 19 | #include "world.h" |
---|
| 20 | #include "world_entity.h" |
---|
[2068] | 21 | |
---|
| 22 | using namespace std; |
---|
| 23 | |
---|
[2096] | 24 | /** |
---|
| 25 | \brief creates a Camera |
---|
| 26 | |
---|
| 27 | This standard constructor sets all parameters to zero |
---|
| 28 | */ |
---|
[2636] | 29 | Camera::Camera (World* world) |
---|
[2068] | 30 | { |
---|
[2636] | 31 | this->world = world; |
---|
[3039] | 32 | coord = new Coordinate(); |
---|
| 33 | |
---|
[2551] | 34 | bound = NULL; |
---|
| 35 | /* give it some physical live */ |
---|
[3023] | 36 | mass = 10; |
---|
| 37 | acceleration = new Vector(0.0, 0.0, 0.0); |
---|
| 38 | velocity = new Vector(0.0, 0.0, 0.0); |
---|
[2551] | 39 | cameraMode = NORMAL; |
---|
| 40 | deltaTime = 3000.0; |
---|
| 41 | cameraOffset = 1.0; |
---|
| 42 | cameraOffsetZ = 10.0; |
---|
| 43 | t = 0.0; |
---|
| 44 | |
---|
[3013] | 45 | actual_place.pos.x = 0.0; |
---|
| 46 | actual_place.pos.y = 10.0; |
---|
| 47 | actual_place.pos.z = -5.0; |
---|
[3028] | 48 | |
---|
| 49 | // Initializing Target |
---|
| 50 | target = new CameraTarget (Vector (0,0,0)); |
---|
| 51 | |
---|
[2068] | 52 | } |
---|
| 53 | |
---|
[2096] | 54 | /** |
---|
| 55 | \brief default destructor |
---|
| 56 | */ |
---|
[2068] | 57 | Camera::~Camera () |
---|
| 58 | { |
---|
| 59 | } |
---|
| 60 | |
---|
[2096] | 61 | /** |
---|
| 62 | \brief time based actualisation of camera parameters |
---|
| 63 | \param deltaT: The amount of time that has passed in milliseconds |
---|
| 64 | |
---|
| 65 | This is called by the World in every time_slice, use it to do fancy time dependant effects (such |
---|
| 66 | as smooth camera movement or swaying). |
---|
| 67 | */ |
---|
[2068] | 68 | void Camera::time_slice (Uint32 deltaT) |
---|
| 69 | { |
---|
[2551] | 70 | if(t <= deltaTime) |
---|
| 71 | {t += deltaT;} |
---|
| 72 | //printf("time is: t=%f\n", t ); |
---|
| 73 | update_desired_place (); |
---|
| 74 | jump (NULL); |
---|
[2068] | 75 | } |
---|
| 76 | |
---|
[2096] | 77 | /** |
---|
[2551] | 78 | \brief this calculates the location where the track wants the camera to be |
---|
[2096] | 79 | |
---|
[2551] | 80 | This refreshes the placement the camera should have according to the |
---|
| 81 | bound entity's position on the track. |
---|
[2096] | 82 | */ |
---|
[2068] | 83 | void Camera::update_desired_place () |
---|
| 84 | { |
---|
[2551] | 85 | switch(cameraMode) |
---|
| 86 | { |
---|
| 87 | |
---|
[3028] | 88 | case NORMAL: |
---|
[3039] | 89 | coord->setPosition(world->track->getPos() - world->track->getDir() *10 +Vector (0,0,5), WORLD); |
---|
| 90 | target->coord->setPosition(world->track->getPos(), WORLD); |
---|
[2551] | 91 | |
---|
[3028] | 92 | Location lookat; |
---|
[2636] | 93 | if( bound != NULL && world != NULL ) |
---|
[2551] | 94 | { |
---|
| 95 | bound->get_lookat (&lookat); |
---|
[2636] | 96 | world->calc_camera_pos (&lookat, &desired_place); |
---|
[2551] | 97 | } |
---|
| 98 | else |
---|
| 99 | { |
---|
[3013] | 100 | desired_place.pos = Vector (0,0,0); |
---|
| 101 | desired_place.rot = Quaternion (); |
---|
[2551] | 102 | } |
---|
| 103 | break; |
---|
| 104 | } |
---|
[2068] | 105 | } |
---|
| 106 | |
---|
[2096] | 107 | /** |
---|
[2551] | 108 | \brief initialize rendering perspective according to this camera |
---|
[2096] | 109 | |
---|
[2551] | 110 | This is called immediately before the rendering cycle starts, it sets all global |
---|
| 111 | rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
[2096] | 112 | */ |
---|
[2068] | 113 | void Camera::apply () |
---|
| 114 | { |
---|
[2551] | 115 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 116 | glLoadIdentity (); |
---|
[2551] | 117 | // view |
---|
| 118 | // TO DO: implement options for frustum generation |
---|
| 119 | glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0); |
---|
[2115] | 120 | |
---|
[2551] | 121 | // ===== first camera control calculation option |
---|
| 122 | // rotation |
---|
[3039] | 123 | Vector eye = coord->getPosition(WORLD); |
---|
| 124 | Vector targetV = target->coord->getPosition(WORLD); |
---|
[3028] | 125 | |
---|
| 126 | gluLookAt (eye.x, eye.y, eye.z, |
---|
| 127 | targetV.x, targetV.y, targetV.z, |
---|
| 128 | 0,0,1); |
---|
| 129 | |
---|
[2112] | 130 | float matrix[4][4]; |
---|
[3013] | 131 | actual_place.rot.conjugate().matrix (matrix); |
---|
[2551] | 132 | /* orientation and */ |
---|
[3028] | 133 | // glMultMatrixf ((float*)matrix); |
---|
[2551] | 134 | /* translation */ |
---|
[3028] | 135 | // glTranslatef (-actual_place.pos.x, -actual_place.pos.y,- actual_place.pos.z); |
---|
[2551] | 136 | //Placement *plBound = bound->get_placement(); |
---|
| 137 | |
---|
| 138 | // ===== second camera control calculation option |
---|
| 139 | /* |
---|
[3023] | 140 | gluLookAt(actual_place.pos.x, actual_place.pos.y, actual_place.pos.z, |
---|
| 141 | bound->pos.x, bound->pos.y, bound->pos.z, |
---|
[2551] | 142 | 0.0, 0.0, 1.0); |
---|
[3023] | 143 | */ |
---|
[2551] | 144 | |
---|
[2068] | 145 | glMatrixMode (GL_MODELVIEW); |
---|
[2080] | 146 | glLoadIdentity (); |
---|
[2068] | 147 | } |
---|
| 148 | |
---|
[2096] | 149 | /** |
---|
| 150 | \brief set the camera position |
---|
| 151 | \param plc: The Placement to set the camera to |
---|
| 152 | |
---|
| 153 | This will set the actual and desired placement of the camera to plc |
---|
| 154 | */ |
---|
[2068] | 155 | void Camera::jump (Placement* plc = NULL) |
---|
| 156 | { |
---|
[2551] | 157 | if( plc == NULL) |
---|
| 158 | { |
---|
| 159 | actual_place = desired_place; |
---|
| 160 | //printf("Camera|jump: camer@ %f, %f, %f\n\n", actual_place.r.x, actual_place.r.y, actual_place.r.z); |
---|
| 161 | } |
---|
| 162 | else |
---|
| 163 | { |
---|
| 164 | desired_place = *plc; |
---|
| 165 | actual_place = *plc; |
---|
| 166 | } |
---|
[2068] | 167 | } |
---|
| 168 | |
---|
[2096] | 169 | /** |
---|
| 170 | \brief bind the camera to an entity |
---|
| 171 | \param entity: The enitity to bind the camera to |
---|
| 172 | |
---|
| 173 | This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's |
---|
| 174 | Location and get_lookat() to determine the viewpoint the camera will render from. |
---|
| 175 | Note that you cannot bind a camera to a free entity. |
---|
| 176 | */ |
---|
[2068] | 177 | void Camera::bind (WorldEntity* entity) |
---|
| 178 | { |
---|
[2551] | 179 | if( entity != NULL) |
---|
| 180 | { |
---|
| 181 | if( entity->isFree ()) printf("Cannot bind camera to free entity"); |
---|
| 182 | else |
---|
[2080] | 183 | { |
---|
[2551] | 184 | bound = entity; |
---|
| 185 | } |
---|
| 186 | } |
---|
[2068] | 187 | } |
---|
[2636] | 188 | |
---|
| 189 | |
---|
| 190 | void Camera::setWorld(World* world) |
---|
| 191 | { |
---|
| 192 | this->world = world; |
---|
| 193 | } |
---|
[3028] | 194 | |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | CameraTarget::CameraTarget () |
---|
| 198 | { |
---|
[3039] | 199 | coord = new Coordinate(); |
---|
[3028] | 200 | |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | CameraTarget::CameraTarget (Vector pos) |
---|
| 204 | { |
---|
[3039] | 205 | coord->setPosition (pos, WORLD); |
---|
[3028] | 206 | } |
---|