-
Notifications
You must be signed in to change notification settings - Fork 2
/
camera.cpp
executable file
·223 lines (190 loc) · 5.84 KB
/
camera.cpp
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
/* Author: Samuel Yung
* Source: camera
* Description: Contains functions that controls the camera.
*/
#include "render.h"
#include "camera.h"
/* Function: camInit
* Description: Initializes the camera control variables.
* Input: None
* Output: None
*/
void camInit()
{
Theta = 0; // Z-X plane
Phi = PI + PI / 2; // Y-X plane
R = 4;
lineOfSight.x = 0.0;
lineOfSight.y = 0.0;
lineOfSight.z = 0.0;
cameraPos.x = lineOfSight.x + (R * cos(Phi) * cos(Theta));
cameraPos.y = lineOfSight.y + (R * sin(Theta));
cameraPos.z = lineOfSight.z + (-R * sin(Phi) * cos(Theta));
viewVector.x = 0.0;
viewVector.y = 1.0;
viewVector.z = 0.0;
camFol = 0;
} //end camInit
/* Function: setCamera()
* Description: Sets the camera position and viewing.
* Input: None
* Output: None
*/
void setCamera()
{
glLoadIdentity();
gluLookAt(cameraPos.x, cameraPos.y, cameraPos.z, lineOfSight.x, lineOfSight.y, lineOfSight.z, viewVector.x, viewVector.y, viewVector.z);
} //end setCamera
/* Function: zoom
* Description: Zooms the camera in and out.
* Input: dir - Zoom direction. (Zoom In = 1, Zoom Out = -1)
* Output: None
*/
void zoom(int dir, int follow)
{
R += dir*ZOOM; // Increment/decrement zoom distance
if (R < 0.2) // Zoom In Limit
R = 0.2;
if (follow == 0)
{
// Recompute camera position
cameraPos.x = lineOfSight.x + (R * cos(Phi) * cos(Theta));
cameraPos.y = lineOfSight.y + (R * sin(Theta));
cameraPos.z = lineOfSight.z + (-R * sin(Phi) * cos(Theta));
} //end if
else if (follow == 1)
cameraPos.z = lineOfSight.z + R;
setCamera();
} //end zoom
/* Function: cameraOrbit
* Description: Rotates the entire camera about a point using the mouse.
* Input: x - X Coordinate of the mouse
* y - Y Coordinate of the mouse
* Output: None
*/
void cameraOrbit(int x, int y)
{
point mouse;
mouse.x = x - mousePos.x;
mouse.y = y - mousePos.y;
Phi += mouse.x * 0.01;
Theta += mouse.y * 0.01;
if (Phi > (2 * PI))
Phi -= 2 * PI;
if (Phi < 0)
Phi += 2 * PI;
if (Theta > (PI / 2 - 0.01)) // dont let the point enter the north pole
Theta = PI / 2 - 0.01;
if (Theta < (-PI / 2 + 0.01))
Theta = -PI / 2 + 0.01;
// Recompute camera position
cameraPos.x = lineOfSight.x + (R * cos(Phi) * cos(Theta));
cameraPos.y = lineOfSight.y + (R * sin(Theta));
cameraPos.z = lineOfSight.z + (-R * sin(Phi) * cos(Theta));
setCamera();
} //end cameraOrbit
/* Function: cameraFreeMove
* Description: Free movement camera computations. Allow camera to move
* forward, backword, up, down, strafe left, strafe right,
* tilt up/down and turn right/left.
* Input: dir - Direction of movement
* 0. Move Camera Forward
* 1. Move Camera Backward
* 2. Move Camera Up
* 3. Move Camera Down
* 4. Move Camera Right
* 5. Move Camera Left
* 6. Tilt Camera Up (Not Implemented)
* 7. Tilt Camera Down (Not Implemented)
* 8. Turn Camera Right (Not Implemented)
* 9. Turn Camera Left (Not Implemented)
* Output: None
*/
void cameraFreeMove(int dir)
{
point mouse, vec, pVec, cVec, cPos;
double ang, length;
pDIFFERENCE(lineOfSight, cameraPos, vec);
pNORMALIZE(vec);
if (dir == 0) // Move Camera Forward
{
pMULTIPLY(vec, CAMSPEED, vec);
pSUM(cameraPos, vec, cameraPos);
pSUM(lineOfSight, vec, lineOfSight);
} //end if
else if (dir == 1) // Move Camera Backward
{
pMULTIPLY(vec, CAMSPEED, vec);
pDIFFERENCE(cameraPos, vec, cameraPos);
pDIFFERENCE(lineOfSight, vec, lineOfSight);
} //end else
else if (dir == 2) // Move Camera Up
{
ang = Phi + CAMROT;
cPos.x = lineOfSight.x + (R * cos(ang) * cos(Theta));
cPos.y = lineOfSight.y + (R * sin(Theta));
cPos.z = lineOfSight.z + (-R * sin(ang) * cos(Theta));
pDIFFERENCE(cPos, cameraPos, pVec);
pNORMALIZE(pVec);
CROSSPRODUCTp(pVec, vec, cVec);
pMULTIPLY(cVec, CAMSPEED, cVec);
pSUM(cameraPos, cVec, cameraPos);
pSUM(lineOfSight, cVec, lineOfSight);
} //end else
else if (dir == 3) // Move Camera Down
{
ang = Phi + CAMROT;
cPos.x = lineOfSight.x + (R * cos(ang) * cos(Theta));
cPos.y = lineOfSight.y + (R * sin(Theta));
cPos.z = lineOfSight.z + (-R * sin(ang) * cos(Theta));
pDIFFERENCE(cPos, cameraPos, pVec);
pNORMALIZE(pVec);
CROSSPRODUCTp(pVec, vec, cVec);
pMULTIPLY(cVec, CAMSPEED, cVec);
pDIFFERENCE(cameraPos, cVec, cameraPos);
pDIFFERENCE(lineOfSight, cVec, lineOfSight);
} //end else
else if (dir == 4) // Move Camera Strafe Right
{
ang = Theta + CAMROT;
cPos.x = lineOfSight.x + (R * cos(Phi) * cos(ang));
cPos.y = lineOfSight.y + (R * sin(ang));
cPos.z = lineOfSight.z + (-R * sin(Phi) * cos(ang));
pDIFFERENCE(cPos, cameraPos, pVec);
pNORMALIZE(pVec);
CROSSPRODUCTp(vec, pVec, cVec);
pMULTIPLY(cVec, CAMSPEED, cVec);
pSUM(cameraPos, cVec, cameraPos);
pSUM(lineOfSight, cVec, lineOfSight);
} //end else
else if (dir == 5) // Move Camera Strafe Left
{
ang = Theta + CAMROT;
cPos.x = lineOfSight.x + (R * cos(Phi) * cos(ang));
cPos.y = lineOfSight.y + (R * sin(ang));
cPos.z = lineOfSight.z + (-R * sin(Phi) * cos(ang));
pDIFFERENCE(cPos, cameraPos, pVec);
pNORMALIZE(pVec);
CROSSPRODUCTp(vec, pVec, cVec);
pMULTIPLY(cVec, CAMSPEED, cVec);
pDIFFERENCE(cameraPos, cVec, cameraPos);
pDIFFERENCE(lineOfSight, cVec, lineOfSight);
} //end else
} //end cameraFreeMove
/* Function: cameraFollow
* Description: Fixates the camera focus on an obj and follows the object.
* Input: model - Model information
* Output: None
*/
void cameraFollow(GLMmodel *model)
{
point obj;
double objRadius;
Theta = 0; // Z-X plane
Phi = PI + PI / 2; // Y-X plane
glmMeshGeometricParameters(model, &obj.x, &obj.y, &obj.z, &objRadius);
pCPY(obj, lineOfSight);
cameraPos.x = lineOfSight.x;
cameraPos.y = lineOfSight.y;
cameraPos.z = lineOfSight.z + R;
} //end cameraFollow