stereoview
view src/cam.cc @ 1:59fd3f6948fe
fixed makefile for macosx
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 04 Mar 2011 07:15:42 +0200 |
parents | |
children |
line source
1 #include <henge.h>
2 #include "cam.h"
4 /* viewing parameters */
5 #define DEF_THETA 0
6 #define DEF_PHI 25
7 #define DEF_DIST 5
8 #define DEF_X 0
9 #define DEF_Y 0.25
10 #define DEF_Z 0
12 static float cam_theta = DEF_THETA, cam_phi = DEF_PHI;
13 static float cam_dist = DEF_DIST;
14 static Vector3 cam_pos(DEF_X, DEF_Y, DEF_Z);
16 /* projection parameters */
17 #define DEF_VFOV 45.0
18 #define DEF_ASPECT 1.3333333
19 #define DEF_NEAR 1.0
20 #define DEF_FAR 1000.0
22 static float vfov = DEF_VFOV;
23 static float aspect = DEF_ASPECT;
24 static float nearclip = DEF_NEAR, farclip = DEF_FAR;
26 /* stereo parameters */
27 #define DEF_EYE_SEP 0.1
28 #define DEF_FOCUS_DIST 1.0
30 static float eye_sep = DEF_EYE_SEP;
31 static float focus_dist = DEF_FOCUS_DIST;
34 static float rot_speed = 0.5;
35 static float zoom_speed = 0.1;
37 static float cam_speed = 1.0;
40 void cam_reset(void)
41 {
42 cam_reset_view();
43 cam_reset_proj();
44 cam_reset_stereo();
45 }
47 void cam_reset_view(void)
48 {
49 cam_theta = DEF_THETA;
50 cam_phi = DEF_PHI;
51 cam_dist = DEF_DIST;
52 cam_pos = Vector3(DEF_X, DEF_Y, DEF_Z);
53 }
55 void cam_reset_proj(void)
56 {
57 vfov = DEF_VFOV;
58 aspect = DEF_ASPECT;
59 nearclip = DEF_NEAR;
60 farclip = DEF_FAR;
61 }
63 void cam_reset_stereo(void)
64 {
65 eye_sep = DEF_EYE_SEP;
66 focus_dist = DEF_FOCUS_DIST;
67 }
69 void cam_pan(int dx, int dy)
70 {
71 float dxf = dx * cam_speed;// * cam_dist;
72 float dyf = dy * cam_speed;// * cam_dist;
73 float angle = -DEG_TO_RAD(cam_theta);
75 cam_pos.x += cos(angle) * dxf + sin(angle) * dyf;
76 cam_pos.z += -sin(angle) * dxf + cos(angle) * dyf;
77 }
79 void cam_height(int dh)
80 {
81 cam_pos.y += dh * cam_speed;// * cam_dist;
82 }
84 void cam_rotate(int dx, int dy)
85 {
86 cam_theta += dx * rot_speed;
87 cam_phi += dy * rot_speed;
89 if(cam_phi < -90) cam_phi = -90;
90 if(cam_phi > 90) cam_phi = 90;
91 }
93 void cam_zoom(int dz)
94 {
95 cam_dist += dz * zoom_speed;
96 if(cam_dist < 0.001) {
97 cam_dist = 0.001;
98 }
99 }
101 void cam_clip(float n, float f)
102 {
103 nearclip = n;
104 farclip = f;
105 }
107 void cam_fov(float f)
108 {
109 vfov = f;
110 }
112 void cam_aspect(float a)
113 {
114 aspect = a;
115 }
117 void cam_separation(float s)
118 {
119 eye_sep = s;
120 }
122 void cam_focus_dist(float d)
123 {
124 focus_dist = d;
126 cam_separation(d / 30.0);
127 }
129 void cam_view_matrix(void)
130 {
131 cam_stereo_view_matrix(CAM_CENTER);
132 }
134 void cam_stereo_view_matrix(int eye)
135 {
136 static const float offs_sign[] = {0.0f, 0.5f, -0.5f}; /* center, left, right */
137 float offs = eye_sep * offs_sign[eye];
139 glTranslatef(offs, 0, 0);
141 glTranslatef(0, 0, -cam_dist);
142 glRotatef(cam_phi, 1, 0, 0);
143 glRotatef(cam_theta, 0, 1, 0);
144 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
145 }
147 void cam_proj_matrix(void)
148 {
149 cam_stereo_proj_matrix(CAM_CENTER);
150 }
152 void cam_stereo_proj_matrix(int eye)
153 {
154 float vfov_rad = M_PI * vfov / 180.0;
155 float top = nearclip * tan(vfov_rad * 0.5);
156 float right = top * aspect;
158 static const float offs_sign[] = {0.0f, 1.0, -1.0}; /* center, left, right */
159 float frust_shift = offs_sign[eye] * (eye_sep * 0.5 * nearclip / focus_dist);
161 glFrustum(-right + frust_shift, right + frust_shift, -top, top, nearclip, farclip);
162 }