istereo2

view src/cam.c @ 28:74b50b538858

added nexus7 device id for ad testing
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 05 Oct 2015 17:16:31 +0300
parents
children
line source
1 /*
2 Stereoscopic tunnel for iOS.
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 #include <math.h>
20 #include "opengl.h"
21 #include "cam.h"
22 #include "sanegl.h"
24 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
26 typedef struct vec3 {
27 float x, y, z;
28 } vec3_t;
30 /* viewing parameters */
31 #define DEF_THETA 0
32 #define DEF_PHI 0
33 #define DEF_DIST 0
34 #define DEF_X 0
35 #define DEF_Y 0
36 #define DEF_Z 0
38 static float cam_theta = DEF_THETA, cam_phi = DEF_PHI;
39 static float cam_dist = DEF_DIST;
40 static vec3_t cam_pos = {DEF_X, DEF_Y, DEF_Z};
41 static float cam_speed = 1.0;
43 /* projection parameters */
44 #define DEF_VFOV 45.0
45 #define DEF_ASPECT 1.3333333
46 #define DEF_NEAR 1.0
47 #define DEF_FAR 1000.0
49 static float vfov = DEF_VFOV;
50 static float aspect = DEF_ASPECT;
51 static float nearclip = DEF_NEAR, farclip = DEF_FAR;
53 /* stereo parameters */
54 #define DEF_EYE_SEP 0.1
55 #define DEF_FOCUS_DIST 1.0
57 static float eye_sep = DEF_EYE_SEP;
58 static float focus_dist = DEF_FOCUS_DIST;
61 static float rot_speed = 0.5;
62 static float zoom_speed = 0.1;
64 void cam_reset(void)
65 {
66 cam_reset_view();
67 cam_reset_proj();
68 cam_reset_stereo();
69 }
71 void cam_reset_view(void)
72 {
73 cam_theta = DEF_THETA;
74 cam_phi = DEF_PHI;
75 cam_dist = DEF_DIST;
76 cam_pos.x = DEF_X;
77 cam_pos.y = DEF_Y;
78 cam_pos.z = DEF_Z;
79 }
81 void cam_reset_proj(void)
82 {
83 vfov = DEF_VFOV;
84 aspect = DEF_ASPECT;
85 nearclip = DEF_NEAR;
86 farclip = DEF_FAR;
87 }
89 void cam_reset_stereo(void)
90 {
91 eye_sep = DEF_EYE_SEP;
92 focus_dist = DEF_FOCUS_DIST;
93 }
95 void cam_pan(int dx, int dy)
96 {
97 float dxf = dx * cam_speed;
98 float dyf = dy * cam_speed;
99 float angle = -DEG_TO_RAD(cam_theta);
101 cam_pos.x += cos(angle) * dxf + sin(angle) * dyf;
102 cam_pos.z += -sin(angle) * dxf + cos(angle) * dyf;
103 }
105 void cam_height(int dh)
106 {
107 cam_pos.y += dh * cam_speed;
108 }
110 void cam_rotate(int dx, int dy)
111 {
112 cam_theta += dx * rot_speed;
113 cam_phi += dy * rot_speed;
115 if(cam_phi < -90) cam_phi = -90;
116 if(cam_phi > 90) cam_phi = 90;
117 }
119 void cam_zoom(int dz)
120 {
121 cam_dist += dz * zoom_speed;
122 if(cam_dist < 0.001) {
123 cam_dist = 0.001;
124 }
125 }
127 void cam_clip(float n, float f)
128 {
129 nearclip = n;
130 farclip = f;
131 }
133 void cam_fov(float f)
134 {
135 vfov = f;
136 }
138 void cam_aspect(float a)
139 {
140 aspect = a;
141 }
143 void cam_separation(float s)
144 {
145 eye_sep = s;
146 }
148 void cam_focus_dist(float d)
149 {
150 focus_dist = d;
152 cam_separation(d / 30.0);
153 }
155 void cam_view_matrix(void)
156 {
157 cam_stereo_view_matrix(CAM_CENTER);
158 }
160 void cam_stereo_view_matrix(int eye)
161 {
162 static const float offs_sign[] = {0.0f, 0.5f, -0.5f}; /* center, left, right */
163 float offs = eye_sep * offs_sign[eye];
165 gl_translatef(offs, 0, 0);
167 gl_translatef(0, 0, -cam_dist);
168 gl_rotatef(cam_phi, 1, 0, 0);
169 gl_rotatef(cam_theta, 0, 1, 0);
170 gl_translatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
171 }
173 void cam_proj_matrix(void)
174 {
175 cam_stereo_proj_matrix(CAM_CENTER);
176 }
178 void cam_stereo_proj_matrix(int eye)
179 {
180 float vfov_rad = M_PI * vfov / 180.0;
181 float top = nearclip * tan(vfov_rad * 0.5);
182 float right = top * aspect;
184 static const float offs_sign[] = {0.0f, 1.0, -1.0}; /* center, left, right */
185 float frust_shift = offs_sign[eye] * (eye_sep * 0.5 * nearclip / focus_dist);
187 gl_frustum(-right + frust_shift, right + frust_shift, -top, top, nearclip, farclip);
188 }