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