stereoview

annotate src/cam.cc @ 2:30c7a5df0523

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