istereo

changeset 20:21d58290e634

forgot the cam files
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 10:15:35 +0300
parents ab4972098eb7
children 75a63f9ab7cc
files src/cam.c src/cam.h
diffstat 2 files changed, 213 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/cam.c	Wed Sep 07 10:15:35 2011 +0300
     1.3 @@ -0,0 +1,170 @@
     1.4 +#include <math.h>
     1.5 +#include "opengl.h"
     1.6 +#include "cam.h"
     1.7 +#include "sanegl.h"
     1.8 +
     1.9 +#define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
    1.10 +
    1.11 +typedef struct vec3 {
    1.12 +	float x, y, z;
    1.13 +} vec3_t;
    1.14 +
    1.15 +/* viewing parameters */
    1.16 +#define DEF_THETA	0
    1.17 +#define DEF_PHI		0
    1.18 +#define DEF_DIST	0
    1.19 +#define DEF_X		0
    1.20 +#define DEF_Y		0
    1.21 +#define DEF_Z		0
    1.22 +
    1.23 +static float cam_theta = DEF_THETA, cam_phi = DEF_PHI;
    1.24 +static float cam_dist = DEF_DIST;
    1.25 +static vec3_t cam_pos = {DEF_X, DEF_Y, DEF_Z};
    1.26 +static float cam_speed = 1.0;
    1.27 +
    1.28 +/* projection parameters */
    1.29 +#define DEF_VFOV	45.0
    1.30 +#define DEF_ASPECT	1.3333333
    1.31 +#define DEF_NEAR	1.0
    1.32 +#define DEF_FAR		1000.0
    1.33 +
    1.34 +static float vfov = DEF_VFOV;
    1.35 +static float aspect = DEF_ASPECT;
    1.36 +static float nearclip = DEF_NEAR, farclip = DEF_FAR;
    1.37 +
    1.38 +/* stereo parameters */
    1.39 +#define DEF_EYE_SEP		0.1
    1.40 +#define DEF_FOCUS_DIST	1.0
    1.41 +
    1.42 +static float eye_sep = DEF_EYE_SEP;
    1.43 +static float focus_dist = DEF_FOCUS_DIST;
    1.44 +
    1.45 +
    1.46 +static float rot_speed = 0.5;
    1.47 +static float zoom_speed = 0.1;
    1.48 +
    1.49 +void cam_reset(void)
    1.50 +{
    1.51 +	cam_reset_view();
    1.52 +	cam_reset_proj();
    1.53 +	cam_reset_stereo();
    1.54 +}
    1.55 +
    1.56 +void cam_reset_view(void)
    1.57 +{
    1.58 +	cam_theta = DEF_THETA;
    1.59 +	cam_phi = DEF_PHI;
    1.60 +	cam_dist = DEF_DIST;
    1.61 +	cam_pos.x = DEF_X;
    1.62 +	cam_pos.y = DEF_Y;
    1.63 +	cam_pos.z = DEF_Z;
    1.64 +}
    1.65 +
    1.66 +void cam_reset_proj(void)
    1.67 +{
    1.68 +	vfov = DEF_VFOV;
    1.69 +	aspect = DEF_ASPECT;
    1.70 +	nearclip = DEF_NEAR;
    1.71 +	farclip = DEF_FAR;
    1.72 +}
    1.73 +
    1.74 +void cam_reset_stereo(void)
    1.75 +{
    1.76 +	eye_sep = DEF_EYE_SEP;
    1.77 +	focus_dist = DEF_FOCUS_DIST;
    1.78 +}
    1.79 +
    1.80 +void cam_pan(int dx, int dy)
    1.81 +{
    1.82 +	float dxf = dx * cam_speed;
    1.83 +	float dyf = dy * cam_speed;
    1.84 +	float angle = -DEG_TO_RAD(cam_theta);
    1.85 +
    1.86 +	cam_pos.x += cos(angle) * dxf + sin(angle) * dyf;
    1.87 +	cam_pos.z += -sin(angle) * dxf + cos(angle) * dyf;
    1.88 +}
    1.89 +
    1.90 +void cam_height(int dh)
    1.91 +{
    1.92 +	cam_pos.y += dh * cam_speed;
    1.93 +}
    1.94 +
    1.95 +void cam_rotate(int dx, int dy)
    1.96 +{
    1.97 +	cam_theta += dx * rot_speed;
    1.98 +	cam_phi += dy * rot_speed;
    1.99 +
   1.100 +	if(cam_phi < -90) cam_phi = -90;
   1.101 +	if(cam_phi > 90) cam_phi = 90;
   1.102 +}
   1.103 +
   1.104 +void cam_zoom(int dz)
   1.105 +{
   1.106 +	cam_dist += dz * zoom_speed;
   1.107 +	if(cam_dist < 0.001) {
   1.108 +		cam_dist = 0.001;
   1.109 +	}
   1.110 +}
   1.111 +
   1.112 +void cam_clip(float n, float f)
   1.113 +{
   1.114 +	nearclip = n;
   1.115 +	farclip = f;
   1.116 +}
   1.117 +
   1.118 +void cam_fov(float f)
   1.119 +{
   1.120 +	vfov = f;
   1.121 +}
   1.122 +
   1.123 +void cam_aspect(float a)
   1.124 +{
   1.125 +	aspect = a;
   1.126 +}
   1.127 +
   1.128 +void cam_separation(float s)
   1.129 +{
   1.130 +	eye_sep = s;
   1.131 +}
   1.132 +
   1.133 +void cam_focus_dist(float d)
   1.134 +{
   1.135 +	focus_dist = d;
   1.136 +
   1.137 +	cam_separation(d / 30.0);
   1.138 +}
   1.139 +
   1.140 +void cam_view_matrix(void)
   1.141 +{
   1.142 +	cam_stereo_view_matrix(CAM_CENTER);
   1.143 +}
   1.144 +
   1.145 +void cam_stereo_view_matrix(int eye)
   1.146 +{
   1.147 +	static const float offs_sign[] = {0.0f, 0.5f, -0.5f};	/* center, left, right */
   1.148 +	float offs = eye_sep * offs_sign[eye];
   1.149 +
   1.150 +	gl_translatef(offs, 0, 0);
   1.151 +
   1.152 +	gl_translatef(0, 0, -cam_dist);
   1.153 +	gl_rotatef(cam_phi, 1, 0, 0);
   1.154 +	gl_rotatef(cam_theta, 0, 1, 0);
   1.155 +	gl_translatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
   1.156 +}
   1.157 +
   1.158 +void cam_proj_matrix(void)
   1.159 +{
   1.160 +	cam_stereo_proj_matrix(CAM_CENTER);
   1.161 +}
   1.162 +
   1.163 +void cam_stereo_proj_matrix(int eye)
   1.164 +{
   1.165 +	float vfov_rad = M_PI * vfov / 180.0;
   1.166 +	float top = nearclip * tan(vfov_rad * 0.5);
   1.167 +	float right = top * aspect;
   1.168 +
   1.169 +	static const float offs_sign[] = {0.0f, 1.0, -1.0};	/* center, left, right */
   1.170 +	float frust_shift = offs_sign[eye] * (eye_sep * 0.5 * nearclip / focus_dist);
   1.171 +
   1.172 +	gl_frustum(-right + frust_shift, right + frust_shift, -top, top, nearclip, farclip);
   1.173 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/cam.h	Wed Sep 07 10:15:35 2011 +0300
     2.3 @@ -0,0 +1,43 @@
     2.4 +#ifndef CAM_H_
     2.5 +#define CAM_H_
     2.6 +
     2.7 +enum {
     2.8 +	CAM_CENTER,
     2.9 +	CAM_LEFT,
    2.10 +	CAM_RIGHT
    2.11 +};
    2.12 +
    2.13 +/* reset to the initial state */
    2.14 +void cam_reset(void);			/* all */
    2.15 +void cam_reset_view(void);		/* view parameters */
    2.16 +void cam_reset_proj(void);		/* projection parameters */
    2.17 +void cam_reset_stereo(void);	/* stereo parameters */
    2.18 +
    2.19 +/* camera viewing parameters */
    2.20 +void cam_pan(int dx, int dy);		/* pan across X/Z plane */
    2.21 +void cam_height(int dh);			/* move verticaly */
    2.22 +void cam_rotate(int dx, int dy);	/* rotate around local Y and X axis */
    2.23 +void cam_zoom(int dz);				/* dolly the camera fwd/back */
    2.24 +
    2.25 +/* camera projection parameters */
    2.26 +void cam_clip(float n, float f);	/* set clipping planes */
    2.27 +void cam_fov(float f);				/* vertical field of view in degrees */
    2.28 +void cam_aspect(float a);			/* aspect ratio (width / height) */
    2.29 +
    2.30 +/* stereo parameters */
    2.31 +void cam_separation(float s);
    2.32 +void cam_focus_dist(float d);
    2.33 +
    2.34 +/* multiply the camera view matrix on top of the current matrix stack
    2.35 + * (which should be GL_MODELVIEW)
    2.36 + */
    2.37 +void cam_view_matrix(void);
    2.38 +void cam_stereo_view_matrix(int eye);
    2.39 +
    2.40 +/* multiply the camera projection matrix on top of the current matrix stack
    2.41 + * (which should be GL_PROJECTION)
    2.42 + */
    2.43 +void cam_proj_matrix(void);
    2.44 +void cam_stereo_proj_matrix(int eye);
    2.45 +
    2.46 +#endif	/* CAM_H_ */