metasurf

diff examples/metaballs/src/cam.c @ 0:7aa4627e492b

first commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Oct 2011 07:34:31 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/metaballs/src/cam.c	Tue Oct 25 07:34:31 2011 +0300
     1.3 @@ -0,0 +1,216 @@
     1.4 +#include <math.h>
     1.5 +#include "cam.h"
     1.6 +
     1.7 +#if defined(__APPLE__) && defined(__MACH__)
     1.8 +#include <OpenGL/gl.h>
     1.9 +#else
    1.10 +#include <GL/gl.h>
    1.11 +#endif
    1.12 +
    1.13 +#define DR				(M_PI / 180.0)
    1.14 +#define DEG_TO_RAD(x)	((x) * DR)
    1.15 +
    1.16 +typedef struct vec3 {
    1.17 +	float x, y, z;
    1.18 +} vec3_t;
    1.19 +
    1.20 +/* viewing parameters */
    1.21 +#define DEF_THETA	0
    1.22 +#define DEF_PHI		0
    1.23 +#define DEF_DIST	0
    1.24 +#define DEF_X		0
    1.25 +#define DEF_Y		0
    1.26 +#define DEF_Z		0
    1.27 +
    1.28 +static float cam_theta = DEF_THETA, cam_phi = DEF_PHI;
    1.29 +static float cam_dist = DEF_DIST;
    1.30 +static vec3_t cam_pos = {DEF_X, DEF_Y, DEF_Z};
    1.31 +
    1.32 +/* projection parameters */
    1.33 +#define DEF_VFOV	45.0
    1.34 +#define DEF_ASPECT	1.3333333
    1.35 +#define DEF_NEAR	1.0
    1.36 +#define DEF_FAR		1000.0
    1.37 +
    1.38 +static float vfov = DEF_VFOV;
    1.39 +static float aspect = DEF_ASPECT;
    1.40 +static float nearclip = DEF_NEAR, farclip = DEF_FAR;
    1.41 +
    1.42 +/* stereo parameters */
    1.43 +#define DEF_EYE_SEP		0.1
    1.44 +#define DEF_FOCUS_DIST	1.0
    1.45 +
    1.46 +static float eye_sep = DEF_EYE_SEP;
    1.47 +static float focus_dist = DEF_FOCUS_DIST;
    1.48 +
    1.49 +static float pan_speed = 0.001;
    1.50 +static float rot_speed = 0.5;
    1.51 +static float zoom_speed = 0.1;
    1.52 +
    1.53 +static float vmin_deg = -90.0, vmax_deg = 90.0;
    1.54 +
    1.55 +void cam_reset(void)
    1.56 +{
    1.57 +	cam_reset_view();
    1.58 +	cam_reset_proj();
    1.59 +	cam_reset_stereo();
    1.60 +}
    1.61 +
    1.62 +void cam_reset_view(void)
    1.63 +{
    1.64 +	cam_theta = DEF_THETA;
    1.65 +	cam_phi = DEF_PHI;
    1.66 +	cam_dist = DEF_DIST;
    1.67 +	cam_pos.x = DEF_X;
    1.68 +	cam_pos.y = DEF_Y;
    1.69 +	cam_pos.z = DEF_Z;
    1.70 +}
    1.71 +
    1.72 +void cam_reset_proj(void)
    1.73 +{
    1.74 +	vfov = DEF_VFOV;
    1.75 +	aspect = DEF_ASPECT;
    1.76 +	nearclip = DEF_NEAR;
    1.77 +	farclip = DEF_FAR;
    1.78 +}
    1.79 +
    1.80 +void cam_reset_stereo(void)
    1.81 +{
    1.82 +	eye_sep = DEF_EYE_SEP;
    1.83 +	focus_dist = DEF_FOCUS_DIST;
    1.84 +}
    1.85 +
    1.86 +void cam_set_vrange(float min_deg, float max_deg)
    1.87 +{
    1.88 +	vmin_deg = min_deg;
    1.89 +	vmax_deg = max_deg;
    1.90 +}
    1.91 +
    1.92 +void cam_move(float x, float y, float z)
    1.93 +{
    1.94 +	cam_pos.x += x;
    1.95 +	cam_pos.y += y;
    1.96 +	cam_pos.z += z;
    1.97 +}
    1.98 +
    1.99 +void cam_rotate(float theta, float phi)
   1.100 +{
   1.101 +	cam_phi += phi;
   1.102 +	cam_theta += theta;
   1.103 +}
   1.104 +
   1.105 +void cam_dolly(float dist)
   1.106 +{
   1.107 +	cam_dist += dist;
   1.108 +}
   1.109 +
   1.110 +void cam_inp_pan_speed(float speed)
   1.111 +{
   1.112 +	pan_speed = speed;
   1.113 +}
   1.114 +
   1.115 +void cam_inp_rotate_speed(float speed)
   1.116 +{
   1.117 +	rot_speed = speed;
   1.118 +}
   1.119 +
   1.120 +void cam_inp_zoom_speed(float speed)
   1.121 +{
   1.122 +	zoom_speed = speed;
   1.123 +}
   1.124 +
   1.125 +
   1.126 +void cam_inp_pan(int dx, int dy)
   1.127 +{
   1.128 +	float dxf = dx * pan_speed;
   1.129 +	float dyf = dy * pan_speed;
   1.130 +	float angle = -DEG_TO_RAD(cam_theta);
   1.131 +
   1.132 +	cam_pos.x += cos(angle) * dxf + sin(angle) * dyf;
   1.133 +	cam_pos.z += -sin(angle) * dxf + cos(angle) * dyf;
   1.134 +}
   1.135 +
   1.136 +void cam_inp_height(int dh)
   1.137 +{
   1.138 +	cam_pos.y += dh * pan_speed;
   1.139 +}
   1.140 +
   1.141 +void cam_inp_rotate(int dx, int dy)
   1.142 +{
   1.143 +	cam_theta += dx * rot_speed;
   1.144 +	cam_phi += dy * rot_speed;
   1.145 +
   1.146 +	if(cam_phi < vmin_deg) cam_phi = vmin_deg;
   1.147 +	if(cam_phi > vmax_deg) cam_phi = vmax_deg;
   1.148 +}
   1.149 +
   1.150 +void cam_inp_zoom(int dz)
   1.151 +{
   1.152 +	cam_dist += dz * zoom_speed;
   1.153 +	if(cam_dist < 0.001) {
   1.154 +		cam_dist = 0.001;
   1.155 +	}
   1.156 +}
   1.157 +
   1.158 +void cam_clip(float n, float f)
   1.159 +{
   1.160 +	nearclip = n;
   1.161 +	farclip = f;
   1.162 +}
   1.163 +
   1.164 +void cam_fov(float f)
   1.165 +{
   1.166 +	vfov = f;
   1.167 +}
   1.168 +
   1.169 +void cam_aspect(float a)
   1.170 +{
   1.171 +	aspect = a;
   1.172 +}
   1.173 +
   1.174 +void cam_separation(float s)
   1.175 +{
   1.176 +	eye_sep = s;
   1.177 +}
   1.178 +
   1.179 +void cam_focus_dist(float d)
   1.180 +{
   1.181 +	focus_dist = d;
   1.182 +
   1.183 +	cam_separation(d / 30.0);
   1.184 +}
   1.185 +
   1.186 +void cam_view_matrix(void)
   1.187 +{
   1.188 +	cam_stereo_view_matrix(CAM_CENTER);
   1.189 +}
   1.190 +
   1.191 +void cam_stereo_view_matrix(int eye)
   1.192 +{
   1.193 +	static const float offs_sign[] = {0.0f, 0.5f, -0.5f};	/* center, left, right */
   1.194 +	float offs = eye_sep * offs_sign[eye];
   1.195 +
   1.196 +	glTranslatef(offs, 0, 0);
   1.197 +
   1.198 +	glTranslatef(0, 0, -cam_dist);
   1.199 +	glRotatef(cam_phi, 1, 0, 0);
   1.200 +	glRotatef(cam_theta, 0, 1, 0);
   1.201 +	glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
   1.202 +}
   1.203 +
   1.204 +void cam_proj_matrix(void)
   1.205 +{
   1.206 +	cam_stereo_proj_matrix(CAM_CENTER);
   1.207 +}
   1.208 +
   1.209 +void cam_stereo_proj_matrix(int eye)
   1.210 +{
   1.211 +	float vfov_rad = M_PI * vfov / 180.0;
   1.212 +	float top = nearclip * tan(vfov_rad * 0.5);
   1.213 +	float right = top * aspect;
   1.214 +
   1.215 +	static const float offs_sign[] = {0.0f, 1.0, -1.0};	/* center, left, right */
   1.216 +	float frust_shift = offs_sign[eye] * (eye_sep * 0.5 * nearclip / focus_dist);
   1.217 +
   1.218 +	glFrustum(-right + frust_shift, right + frust_shift, -top, top, nearclip, farclip);
   1.219 +}