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