-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcamera.c
More file actions
245 lines (214 loc) · 5.85 KB
/
camera.c
File metadata and controls
245 lines (214 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// This is a hack of the f00Dave's CameraB.cpp Descent style camera demo.
// I converted it to C and removed cut it down to just the camera routines.
//
// System includes.
#include <math.h>
#include "platform.h"
// Local includes.
#include "f00QuatC.h"
#define bool int
#define true 1
#define false 0
//
// Some local settings, play with them at your own peril....
//
#define DEF_keyboardAngleStep 5.0
#define DEF_truckStepSize 100
//
// Icky camera globals. :-/
//
// Position
GLfloat camX = 0.0;
GLfloat camY = 0.0;
GLfloat camZ = 0.0;
// Orientation
// Note: We will be storing this 'pre-inverted'. This means the quat is always ready for
// application, and does not need to be inverted, like the position variables....
// This *does* require us to invert the directions used to modify the quat, however.
// Win some, lose some....
f00Quat camOrient;
/***************************************************************/
void
getCamera(float m[4][4], float v[3])
{
GLfloat rotMatrix[ 16 ];
int i, j, k;
// Apply the current orientation....
f00Quat_getMatrix( &camOrient, rotMatrix );
for (j = 0, k = 0; j < 4; j++)
{
for (i = 0; i < 4; i++)
m[i][j] = rotMatrix[k++];
}
v[0] = -camX;
v[1] = -camY;
v[2] = -camZ;
}
/***************************************************************/
// This is called when we wish to apply the viewpoint transformations.
void
applyCamera()
{
GLfloat rotMatrix[ 16 ];
// Apply the current orientation....
f00Quat_getMatrix( &camOrient, rotMatrix );
glMultMatrixf( rotMatrix );
// ... and translation.
glTranslatef( -camX, -camY, -camZ );
}
/***************************************************************/
// This is called when we wish to reset the viewpoint.
void
resetCamera()
{
// Reset the position
camX = 0.0;
camY = 0.0;
camZ = 0.0;
// and the orientation
f00Quat_reset(&camOrient);
}
/***************************************************************/
// This is called when we want to 'truck' the camera (ie: move it, relative to it's
// current orientation).
void
truckCamera( GLfloat truckBy, bool truckX, bool truckY, bool truckZ )
{
GLfloat dirX;
GLfloat dirY;
GLfloat dirZ;
f00Quat truckQuat;
f00Quat_copy( &truckQuat, &camOrient );
if( truckZ )
{
; // Don't do anything....
}
else if( truckX )
{
f00Quat_postMult_3( &truckQuat, 0.0, -90.0, 0.0 );
}
else if( truckY )
{
f00Quat_postMult_3( &truckQuat, 90.0, 0.0, 0.0 );
}
// Ask the Quat class for a unit vector, aligned with the orientation.
f00Quat_getDirectionVector( &truckQuat, &dirX, &dirY, &dirZ );
// Use the values to update the position....
camX += dirX * truckBy;
camY += dirY * truckBy;
camZ += dirZ * truckBy;
}
/***************************************************************/
// This is called when we want to turn the camera.
void
turnCamera( GLfloat turnX, GLfloat turnY, GLfloat turnZ )
{
f00Quat_postMult_3( &camOrient, turnX, turnY, turnZ );
}
/***************************************************************/
#pragma argsused
// We trap 'special' keystrokes here.
int
/***************************************************************/
specialFunc( int key, int x, int y )
{
int retval = 1;
int glutModifiers = glutGetModifiers();
if ((glutModifiers & GLUT_ACTIVE_CTRL) == 0)
return 0;
// Ctrl-Alt Arrow keys roll left, right, up, down.
if (glutModifiers & GLUT_ACTIVE_ALT)
switch( key )
{
// Move forwards.
case GLUT_KEY_PAGE_UP:
truckCamera( -DEF_truckStepSize, false, false, true );
break;
// Move backwards.
case GLUT_KEY_PAGE_DOWN:
truckCamera( DEF_truckStepSize, false, false, true );
break;
// Roll to the left (counterclockwise).
case GLUT_KEY_LEFT:
f00Quat_postMult_3( &camOrient, 0.0, 0.0, -DEF_keyboardAngleStep );
break;
// Roll to the right (clockwise).
case GLUT_KEY_RIGHT:
f00Quat_postMult_3( &camOrient, 0.0, 0.0, DEF_keyboardAngleStep );
break;
// Look up.
case GLUT_KEY_UP:
f00Quat_postMult_3( &camOrient, DEF_keyboardAngleStep, 0.0, 0.0 );
break;
// Look down.
case GLUT_KEY_DOWN:
f00Quat_postMult_3( &camOrient, -DEF_keyboardAngleStep, 0.0, 0.0 );
break;
default:
retval = 0; // Didn't find a key
}
// Ctrl-Shift Arrow keys turn left, right, up, down.
else if (glutModifiers & GLUT_ACTIVE_SHIFT)
switch( key )
{
// Move forwards.
case GLUT_KEY_PAGE_UP:
truckCamera( -DEF_truckStepSize, false, false, true );
break;
// Move backwards.
case GLUT_KEY_PAGE_DOWN:
truckCamera( DEF_truckStepSize, false, false, true );
break;
// Turn left.
case GLUT_KEY_LEFT:
f00Quat_postMult_3( &camOrient, 0.0, -DEF_keyboardAngleStep, 0.0 );
break;
// Turn right.
case GLUT_KEY_RIGHT:
f00Quat_postMult_3( &camOrient, 0.0, DEF_keyboardAngleStep, 0.0 );
break;
// Look up.
case GLUT_KEY_UP:
f00Quat_postMult_3( &camOrient, DEF_keyboardAngleStep, 0.0, 0.0 );
break;
// Look down.
case GLUT_KEY_DOWN:
f00Quat_postMult_3( &camOrient, -DEF_keyboardAngleStep, 0.0, 0.0 );
break;
default:
retval = 0; // Didn't find a key
}
// Ctrl Arrow keys truck left, right, up, down.
else
switch( key )
{
// Move forwards.
case GLUT_KEY_PAGE_UP:
truckCamera( -DEF_truckStepSize, false, false, true );
break;
// Move backwards.
case GLUT_KEY_PAGE_DOWN:
truckCamera( DEF_truckStepSize, false, false, true );
break;
// Strafe left.
case GLUT_KEY_LEFT:
truckCamera( -DEF_truckStepSize, true, false, false );
break;
// Strafe right.
case GLUT_KEY_RIGHT:
truckCamera( DEF_truckStepSize, true, false, false );
break;
// Strafe up.
case GLUT_KEY_UP:
truckCamera( DEF_truckStepSize, false, true, false );
break;
// Strafe down.
case GLUT_KEY_DOWN:
truckCamera( -DEF_truckStepSize, false, true, false );
break;
default:
retval = 0; // Didn't find a key
}
return retval;
}