istereo2

diff src/cam.c @ 2:81d35769f546

added the tunnel effect source
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 19 Sep 2015 05:51:51 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/cam.c	Sat Sep 19 05:51:51 2015 +0300
     1.3 @@ -0,0 +1,188 @@
     1.4 +/*
     1.5 +Stereoscopic tunnel for iOS.
     1.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU General Public License as published by
    1.10 +the Free Software Foundation, either version 3 of the License, or
    1.11 +(at your option) any later version.
    1.12 +
    1.13 +This program is distributed in the hope that it will be useful,
    1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +GNU General Public License for more details.
    1.17 +
    1.18 +You should have received a copy of the GNU General Public License
    1.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 +*/
    1.21 +
    1.22 +#include <math.h>
    1.23 +#include "opengl.h"
    1.24 +#include "cam.h"
    1.25 +#include "sanegl.h"
    1.26 +
    1.27 +#define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
    1.28 +
    1.29 +typedef struct vec3 {
    1.30 +	float x, y, z;
    1.31 +} vec3_t;
    1.32 +
    1.33 +/* viewing parameters */
    1.34 +#define DEF_THETA	0
    1.35 +#define DEF_PHI		0
    1.36 +#define DEF_DIST	0
    1.37 +#define DEF_X		0
    1.38 +#define DEF_Y		0
    1.39 +#define DEF_Z		0
    1.40 +
    1.41 +static float cam_theta = DEF_THETA, cam_phi = DEF_PHI;
    1.42 +static float cam_dist = DEF_DIST;
    1.43 +static vec3_t cam_pos = {DEF_X, DEF_Y, DEF_Z};
    1.44 +static float cam_speed = 1.0;
    1.45 +
    1.46 +/* projection parameters */
    1.47 +#define DEF_VFOV	45.0
    1.48 +#define DEF_ASPECT	1.3333333
    1.49 +#define DEF_NEAR	1.0
    1.50 +#define DEF_FAR		1000.0
    1.51 +
    1.52 +static float vfov = DEF_VFOV;
    1.53 +static float aspect = DEF_ASPECT;
    1.54 +static float nearclip = DEF_NEAR, farclip = DEF_FAR;
    1.55 +
    1.56 +/* stereo parameters */
    1.57 +#define DEF_EYE_SEP		0.1
    1.58 +#define DEF_FOCUS_DIST	1.0
    1.59 +
    1.60 +static float eye_sep = DEF_EYE_SEP;
    1.61 +static float focus_dist = DEF_FOCUS_DIST;
    1.62 +
    1.63 +
    1.64 +static float rot_speed = 0.5;
    1.65 +static float zoom_speed = 0.1;
    1.66 +
    1.67 +void cam_reset(void)
    1.68 +{
    1.69 +	cam_reset_view();
    1.70 +	cam_reset_proj();
    1.71 +	cam_reset_stereo();
    1.72 +}
    1.73 +
    1.74 +void cam_reset_view(void)
    1.75 +{
    1.76 +	cam_theta = DEF_THETA;
    1.77 +	cam_phi = DEF_PHI;
    1.78 +	cam_dist = DEF_DIST;
    1.79 +	cam_pos.x = DEF_X;
    1.80 +	cam_pos.y = DEF_Y;
    1.81 +	cam_pos.z = DEF_Z;
    1.82 +}
    1.83 +
    1.84 +void cam_reset_proj(void)
    1.85 +{
    1.86 +	vfov = DEF_VFOV;
    1.87 +	aspect = DEF_ASPECT;
    1.88 +	nearclip = DEF_NEAR;
    1.89 +	farclip = DEF_FAR;
    1.90 +}
    1.91 +
    1.92 +void cam_reset_stereo(void)
    1.93 +{
    1.94 +	eye_sep = DEF_EYE_SEP;
    1.95 +	focus_dist = DEF_FOCUS_DIST;
    1.96 +}
    1.97 +
    1.98 +void cam_pan(int dx, int dy)
    1.99 +{
   1.100 +	float dxf = dx * cam_speed;
   1.101 +	float dyf = dy * cam_speed;
   1.102 +	float angle = -DEG_TO_RAD(cam_theta);
   1.103 +
   1.104 +	cam_pos.x += cos(angle) * dxf + sin(angle) * dyf;
   1.105 +	cam_pos.z += -sin(angle) * dxf + cos(angle) * dyf;
   1.106 +}
   1.107 +
   1.108 +void cam_height(int dh)
   1.109 +{
   1.110 +	cam_pos.y += dh * cam_speed;
   1.111 +}
   1.112 +
   1.113 +void cam_rotate(int dx, int dy)
   1.114 +{
   1.115 +	cam_theta += dx * rot_speed;
   1.116 +	cam_phi += dy * rot_speed;
   1.117 +
   1.118 +	if(cam_phi < -90) cam_phi = -90;
   1.119 +	if(cam_phi > 90) cam_phi = 90;
   1.120 +}
   1.121 +
   1.122 +void cam_zoom(int dz)
   1.123 +{
   1.124 +	cam_dist += dz * zoom_speed;
   1.125 +	if(cam_dist < 0.001) {
   1.126 +		cam_dist = 0.001;
   1.127 +	}
   1.128 +}
   1.129 +
   1.130 +void cam_clip(float n, float f)
   1.131 +{
   1.132 +	nearclip = n;
   1.133 +	farclip = f;
   1.134 +}
   1.135 +
   1.136 +void cam_fov(float f)
   1.137 +{
   1.138 +	vfov = f;
   1.139 +}
   1.140 +
   1.141 +void cam_aspect(float a)
   1.142 +{
   1.143 +	aspect = a;
   1.144 +}
   1.145 +
   1.146 +void cam_separation(float s)
   1.147 +{
   1.148 +	eye_sep = s;
   1.149 +}
   1.150 +
   1.151 +void cam_focus_dist(float d)
   1.152 +{
   1.153 +	focus_dist = d;
   1.154 +
   1.155 +	cam_separation(d / 30.0);
   1.156 +}
   1.157 +
   1.158 +void cam_view_matrix(void)
   1.159 +{
   1.160 +	cam_stereo_view_matrix(CAM_CENTER);
   1.161 +}
   1.162 +
   1.163 +void cam_stereo_view_matrix(int eye)
   1.164 +{
   1.165 +	static const float offs_sign[] = {0.0f, 0.5f, -0.5f};	/* center, left, right */
   1.166 +	float offs = eye_sep * offs_sign[eye];
   1.167 +
   1.168 +	gl_translatef(offs, 0, 0);
   1.169 +
   1.170 +	gl_translatef(0, 0, -cam_dist);
   1.171 +	gl_rotatef(cam_phi, 1, 0, 0);
   1.172 +	gl_rotatef(cam_theta, 0, 1, 0);
   1.173 +	gl_translatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
   1.174 +}
   1.175 +
   1.176 +void cam_proj_matrix(void)
   1.177 +{
   1.178 +	cam_stereo_proj_matrix(CAM_CENTER);
   1.179 +}
   1.180 +
   1.181 +void cam_stereo_proj_matrix(int eye)
   1.182 +{
   1.183 +	float vfov_rad = M_PI * vfov / 180.0;
   1.184 +	float top = nearclip * tan(vfov_rad * 0.5);
   1.185 +	float right = top * aspect;
   1.186 +
   1.187 +	static const float offs_sign[] = {0.0f, 1.0, -1.0};	/* center, left, right */
   1.188 +	float frust_shift = offs_sign[eye] * (eye_sep * 0.5 * nearclip / focus_dist);
   1.189 +
   1.190 +	gl_frustum(-right + frust_shift, right + frust_shift, -top, top, nearclip, farclip);
   1.191 +}