istereo

annotate src/cam.c @ 36:834503dcb486

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