[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 | */ |
---|
[2068] | 29 | Camera::Camera () |
---|
| 30 | { |
---|
| 31 | bound = NULL; |
---|
| 32 | desired_place.r = Vector (0,0,0); |
---|
| 33 | desired_place.w = Rotation (0,0,0); |
---|
| 34 | actual_place.r = Vector (0,0,0); |
---|
| 35 | actual_place.w = Rotation (0,0,0); |
---|
| 36 | } |
---|
| 37 | |
---|
[2096] | 38 | /** |
---|
| 39 | \brief default destructor |
---|
| 40 | */ |
---|
[2068] | 41 | Camera::~Camera () |
---|
| 42 | { |
---|
| 43 | } |
---|
| 44 | |
---|
[2096] | 45 | /** |
---|
| 46 | \brief time based actualisation of camera parameters |
---|
| 47 | \param deltaT: The amount of time that has passed in milliseconds |
---|
| 48 | |
---|
| 49 | This is called by the World in every time_slice, use it to do fancy time dependant effects (such |
---|
| 50 | as smooth camera movement or swaying). |
---|
| 51 | */ |
---|
[2068] | 52 | void Camera::time_slice (Uint32 deltaT) |
---|
| 53 | { |
---|
| 54 | update_desired_place (); |
---|
| 55 | } |
---|
| 56 | |
---|
[2096] | 57 | /** |
---|
| 58 | \brief this calculates the location where the track wants the camera to be |
---|
| 59 | |
---|
| 60 | This refreshes the placement the camera should have according to the bound entity's position on the track. |
---|
| 61 | */ |
---|
[2068] | 62 | void Camera::update_desired_place () |
---|
| 63 | { |
---|
| 64 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 65 | Location lookat; |
---|
| 66 | |
---|
| 67 | if( bound != NULL) |
---|
| 68 | { |
---|
| 69 | bound->get_lookat (&lookat); |
---|
| 70 | orx->get_world()->calc_camera_pos (&lookat, &desired_place); |
---|
| 71 | } |
---|
| 72 | else |
---|
| 73 | { |
---|
| 74 | desired_place.r = Vector (0,0,0); |
---|
| 75 | desired_place.w = Rotation (0,0,0); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
[2096] | 79 | /** |
---|
| 80 | \brief initialize rendering perspective according to this camera |
---|
| 81 | |
---|
| 82 | This is called immediately before the a rendering cycle starts, it sets all global rendering options as |
---|
| 83 | well as the GL_PROJECTION matrix according to the camera. |
---|
| 84 | */ |
---|
[2068] | 85 | void Camera::apply () |
---|
| 86 | { |
---|
| 87 | glMatrixMode (GL_PROJECTION); |
---|
| 88 | // view |
---|
| 89 | // TO DO: implement options for frustum generation |
---|
| 90 | glFrustum( -1.0,1.0,-1.0,1.0,1.5,20.0); |
---|
| 91 | // rotation |
---|
| 92 | float matrix[16]; |
---|
| 93 | actual_place.w.glmatrix (matrix); |
---|
| 94 | glLoadMatrixf (matrix); |
---|
| 95 | // translation |
---|
[2100] | 96 | glTranslatef (actual_place.r.x, actual_place.r.y, actual_place.r.z); |
---|
[2068] | 97 | |
---|
| 98 | glMatrixMode (GL_MODELVIEW); |
---|
[2080] | 99 | glLoadIdentity (); |
---|
[2068] | 100 | } |
---|
| 101 | |
---|
[2096] | 102 | /** |
---|
| 103 | \brief set the camera position |
---|
| 104 | \param plc: The Placement to set the camera to |
---|
| 105 | |
---|
| 106 | This will set the actual and desired placement of the camera to plc |
---|
| 107 | */ |
---|
[2068] | 108 | void Camera::jump (Placement* plc = NULL) |
---|
| 109 | { |
---|
| 110 | if( plc == NULL) |
---|
| 111 | { |
---|
| 112 | actual_place = desired_place; |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
| 116 | desired_place = *plc; |
---|
| 117 | actual_place = *plc; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
[2096] | 121 | /** |
---|
| 122 | \brief bind the camera to an entity |
---|
| 123 | \param entity: The enitity to bind the camera to |
---|
| 124 | |
---|
| 125 | This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's |
---|
| 126 | Location and get_lookat() to determine the viewpoint the camera will render from. |
---|
| 127 | Note that you cannot bind a camera to a free entity. |
---|
| 128 | */ |
---|
[2068] | 129 | void Camera::bind (WorldEntity* entity) |
---|
| 130 | { |
---|
[2080] | 131 | if( entity != NULL) |
---|
| 132 | { |
---|
[2100] | 133 | if( entity->isFree ()) printf("Cannot bind camera to free entity"); |
---|
[2080] | 134 | else bound = entity; |
---|
| 135 | } |
---|
[2068] | 136 | } |
---|