istereo
view src/cam.c @ 26:862a3329a8f0
wohooo, added a shitload of code from zlib/libpng/libjpeg. When the good lord was raining shared libraries the iphone held a fucking umbrella...
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Thu, 08 Sep 2011 06:28:38 +0300 |
parents | |
children | ff055bff6a15 |
line source
1 #include <math.h>
2 #include "opengl.h"
3 #include "cam.h"
4 #include "sanegl.h"
6 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
8 typedef struct vec3 {
9 float x, y, z;
10 } vec3_t;
12 /* viewing parameters */
13 #define DEF_THETA 0
14 #define DEF_PHI 0
15 #define DEF_DIST 0
16 #define DEF_X 0
17 #define DEF_Y 0
18 #define DEF_Z 0
20 static float cam_theta = DEF_THETA, cam_phi = DEF_PHI;
21 static float cam_dist = DEF_DIST;
22 static vec3_t cam_pos = {DEF_X, DEF_Y, DEF_Z};
23 static float cam_speed = 1.0;
25 /* projection parameters */
26 #define DEF_VFOV 45.0
27 #define DEF_ASPECT 1.3333333
28 #define DEF_NEAR 1.0
29 #define DEF_FAR 1000.0
31 static float vfov = DEF_VFOV;
32 static float aspect = DEF_ASPECT;
33 static float nearclip = DEF_NEAR, farclip = DEF_FAR;
35 /* stereo parameters */
36 #define DEF_EYE_SEP 0.1
37 #define DEF_FOCUS_DIST 1.0
39 static float eye_sep = DEF_EYE_SEP;
40 static float focus_dist = DEF_FOCUS_DIST;
43 static float rot_speed = 0.5;
44 static float zoom_speed = 0.1;
46 void cam_reset(void)
47 {
48 cam_reset_view();
49 cam_reset_proj();
50 cam_reset_stereo();
51 }
53 void cam_reset_view(void)
54 {
55 cam_theta = DEF_THETA;
56 cam_phi = DEF_PHI;
57 cam_dist = DEF_DIST;
58 cam_pos.x = DEF_X;
59 cam_pos.y = DEF_Y;
60 cam_pos.z = DEF_Z;
61 }
63 void cam_reset_proj(void)
64 {
65 vfov = DEF_VFOV;
66 aspect = DEF_ASPECT;
67 nearclip = DEF_NEAR;
68 farclip = DEF_FAR;
69 }
71 void cam_reset_stereo(void)
72 {
73 eye_sep = DEF_EYE_SEP;
74 focus_dist = DEF_FOCUS_DIST;
75 }
77 void cam_pan(int dx, int dy)
78 {
79 float dxf = dx * cam_speed;
80 float dyf = dy * cam_speed;
81 float angle = -DEG_TO_RAD(cam_theta);
83 cam_pos.x += cos(angle) * dxf + sin(angle) * dyf;
84 cam_pos.z += -sin(angle) * dxf + cos(angle) * dyf;
85 }
87 void cam_height(int dh)
88 {
89 cam_pos.y += dh * cam_speed;
90 }
92 void cam_rotate(int dx, int dy)
93 {
94 cam_theta += dx * rot_speed;
95 cam_phi += dy * rot_speed;
97 if(cam_phi < -90) cam_phi = -90;
98 if(cam_phi > 90) cam_phi = 90;
99 }
101 void cam_zoom(int dz)
102 {
103 cam_dist += dz * zoom_speed;
104 if(cam_dist < 0.001) {
105 cam_dist = 0.001;
106 }
107 }
109 void cam_clip(float n, float f)
110 {
111 nearclip = n;
112 farclip = f;
113 }
115 void cam_fov(float f)
116 {
117 vfov = f;
118 }
120 void cam_aspect(float a)
121 {
122 aspect = a;
123 }
125 void cam_separation(float s)
126 {
127 eye_sep = s;
128 }
130 void cam_focus_dist(float d)
131 {
132 focus_dist = d;
134 cam_separation(d / 30.0);
135 }
137 void cam_view_matrix(void)
138 {
139 cam_stereo_view_matrix(CAM_CENTER);
140 }
142 void cam_stereo_view_matrix(int eye)
143 {
144 static const float offs_sign[] = {0.0f, 0.5f, -0.5f}; /* center, left, right */
145 float offs = eye_sep * offs_sign[eye];
147 gl_translatef(offs, 0, 0);
149 gl_translatef(0, 0, -cam_dist);
150 gl_rotatef(cam_phi, 1, 0, 0);
151 gl_rotatef(cam_theta, 0, 1, 0);
152 gl_translatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
153 }
155 void cam_proj_matrix(void)
156 {
157 cam_stereo_proj_matrix(CAM_CENTER);
158 }
160 void cam_stereo_proj_matrix(int eye)
161 {
162 float vfov_rad = M_PI * vfov / 180.0;
163 float top = nearclip * tan(vfov_rad * 0.5);
164 float right = top * aspect;
166 static const float offs_sign[] = {0.0f, 1.0, -1.0}; /* center, left, right */
167 float frust_shift = offs_sign[eye] * (eye_sep * 0.5 * nearclip / focus_dist);
169 gl_frustum(-right + frust_shift, right + frust_shift, -top, top, nearclip, farclip);
170 }