home *** CD-ROM | disk | FTP | other *** search
- /*
- * camera.c -- standard routines for camera
- *
- * (c) Han-Wen Nienhuys, 1993 <hanwen@stack.urc.tue.nl>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- */
-
- #include <math.h>
- #include "ray.h"
- #include "proto.h"
- #include "extern.h"
-
- PUBLIC double
- to_fov_coeff(struct camera *c, double angle)
- {
- return veclen(c->eye_dir) * tan(degtorad(angle) / 2);
- }
-
- PUBLIC void
- copy_camera(struct camera *dst, struct camera *src)
- {
- assert(src != NULL && dst != NULL);
-
- *dst = *src;
-
- }
-
- /* point the camera towards a particular point. */
- PUBLIC void
- point_camera(struct camera *c, vector lookat)
- {
- double dirlen;
-
- dirlen = veclen(c->eye_dir);
- vsub(c->eye_dir, lookat, c->eye);
- norm(c->eye_dir, c->eye_dir);
- svproduct(c->eye_dir, dirlen, c->eye_dir);
- }
-
- PUBLIC void
- init_camera(struct camera *p)
- {
- setvector(p->eye_dir, 0, 0, 1);
- setvector(p->eye, 0, 0, -10);
- setvector(p->sky, 0, 1, 0);
- p->fov = to_fov_coeff(p, 90);
- p->aspect = 1.0;
- }
-
- PUBLIC struct camera *
- get_new_camera(void)
- {
- struct camera *p;
-
- p = ALLOC(struct camera);
-
- CHECK_MEM(p, "camera");
- init_camera(p);
-
- return p;
- }
-
- PUBLIC void
- free_camera(struct camera *c)
- {
- free((void *) c);
- }
-
- PUBLIC void
- translate_camera(struct camera *c, vector t)
- {
- vadd(c->eye, c->eye, t);
- }
-
- PUBLIC void
- rotate_camera(struct camera *c, matrix rotmat)
- {
- rotate_vector(&c->eye_dir, rotmat);
- rotate_vector(&c->eye, rotmat);
- rotate_vector(&c->sky, rotmat);
- }
-
- PUBLIC void
- scale_camera(struct camera *c, vector s)
- {
- vcproduct(c->eye_dir, c->eye_dir, s);
- vcproduct(c->eye, c->eye, s);
- vcproduct(c->sky, c->sky, s);
- }
-
- PUBLIC void
- print_camera(struct camera *c)
- {
- #ifdef DEBUG
- printf("camera:");
- print_v("eye_dir", c->eye_dir);
- print_v("eye", c->eye);
- print_v("sky", c->sky);
- printf("fov %lf\n", c->fov);
- printf("aspect %lf\n", c->aspect);
- #endif
- }
-