1 | #ifdef HAVE_CONFIG_H |
---|
2 | # include "config.h" |
---|
3 | #endif |
---|
4 | |
---|
5 | #ifdef HAVE_GL_GL_H |
---|
6 | # include <GL/gl.h> |
---|
7 | #elif HAVE_OPENGL_GL_H |
---|
8 | # include <OpenGL/gl.h> |
---|
9 | #endif |
---|
10 | #include <math.h> |
---|
11 | #include "camera.h" |
---|
12 | #include "globals.h" |
---|
13 | #include "vector.h" |
---|
14 | |
---|
15 | static const vector up = {0.0, 0.0, 1.0}; |
---|
16 | |
---|
17 | void camera_home(void){ |
---|
18 | vector releye; |
---|
19 | anugavis.eye[0] = anugavis.minX; |
---|
20 | anugavis.eye[1] = anugavis.minY; |
---|
21 | anugavis.eye[2] = 0; |
---|
22 | anugavis.focus[0] = anugavis.minX + (anugavis.maxX - anugavis.minX) / 2; |
---|
23 | anugavis.focus[1] = anugavis.minY + (anugavis.maxY - anugavis.minY) / 2; |
---|
24 | anugavis.focus[2] = 0; |
---|
25 | vsub(anugavis.eye, anugavis.focus, releye); |
---|
26 | anugavis.eye[2] = vlen(releye); |
---|
27 | } |
---|
28 | |
---|
29 | void camera_pitch(float theta){ |
---|
30 | float c = cos(theta); |
---|
31 | float s = sin(theta); |
---|
32 | float x; |
---|
33 | float y; |
---|
34 | float z; |
---|
35 | vector releye; |
---|
36 | vector newreleye; |
---|
37 | vector axis; |
---|
38 | float dot; |
---|
39 | float len; |
---|
40 | vsub(anugavis.eye, anugavis.focus, releye); |
---|
41 | /* Check we're not too near the vertical */ |
---|
42 | dot = vdot(up, releye); len = vlen(releye); |
---|
43 | if(((dot > 0.99 * len) && (theta > 0)) || |
---|
44 | ((dot < -0.99 * len) && (theta < 0))) return; |
---|
45 | /* Calculation from glRotatef manpage. */ |
---|
46 | vcross(releye, up, axis); |
---|
47 | vscale(axis, 1.0/vlen(axis), axis); |
---|
48 | x = axis[0]; y = axis[1]; z = axis[2]; |
---|
49 | newreleye[0] = |
---|
50 | releye[0] * (x*x*(1-c)+c) + |
---|
51 | releye[1] * (x*y*(1-c)-z*s) + |
---|
52 | releye[2] * (x*z*(1-c)+y*s); |
---|
53 | newreleye[1] = |
---|
54 | releye[0] * (y*x*(1-c)+z*s) + |
---|
55 | releye[1] * (y*y*(1-c)+c) + |
---|
56 | releye[2] * (y*z*(1-c)-x*s); |
---|
57 | newreleye[2] = |
---|
58 | releye[0] * (z*x*(1-c)-y*s) + |
---|
59 | releye[1] * (z*y*(1-c)+x*s) + |
---|
60 | releye[2] * (z*z*(1-c)+c); |
---|
61 | vadd(newreleye, anugavis.focus, anugavis.eye); |
---|
62 | } |
---|
63 | |
---|
64 | void camera_strafe(float dist){ |
---|
65 | vector releye; |
---|
66 | vector dir; |
---|
67 | vsub(anugavis.eye, anugavis.focus, releye); |
---|
68 | vcross(releye, up, dir); |
---|
69 | vscale(dir, dist/vlen(dir), dir); |
---|
70 | vadd(anugavis.eye, dir, anugavis.eye); |
---|
71 | vadd(anugavis.focus, dir, anugavis.focus); |
---|
72 | } |
---|
73 | |
---|
74 | void camera_track(float dist){ |
---|
75 | vector dir = {0, 0, 0}; |
---|
76 | vsub(anugavis.eye, anugavis.focus, dir); |
---|
77 | dir[2] = 0; |
---|
78 | vscale(dir, dist/vlen(dir), dir); |
---|
79 | vadd(anugavis.eye, dir, anugavis.eye); |
---|
80 | vadd(anugavis.focus, dir, anugavis.focus); |
---|
81 | } |
---|
82 | |
---|
83 | void camera_yaw(float theta){ |
---|
84 | float c = cos(theta); |
---|
85 | float s = sin(theta); |
---|
86 | vector releye; |
---|
87 | vector newreleye; |
---|
88 | vsub(anugavis.eye, anugavis.focus, releye); |
---|
89 | newreleye[0] = releye[0] * c + releye[1] * s; |
---|
90 | newreleye[1] = releye[0] * -1 * s + releye[1] * c; |
---|
91 | newreleye[2] = releye[2]; |
---|
92 | vadd(newreleye, anugavis.focus, anugavis.eye); |
---|
93 | } |
---|
94 | |
---|
95 | void camera_zoom(float dist){ |
---|
96 | vector releye; |
---|
97 | float len; |
---|
98 | vsub(anugavis.focus, anugavis.eye, releye); |
---|
99 | if((len = vlen(releye)) < dist) return; |
---|
100 | vscale(releye, dist/len, releye); |
---|
101 | vadd(anugavis.eye, releye, anugavis.eye); |
---|
102 | } |
---|