home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / rayce27s / camera.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-02  |  2.4 KB  |  116 lines

  1. /*
  2.  * camera.c -- standard routines for camera
  3.  * 
  4.  * (c) Han-Wen Nienhuys, 1993 <hanwen@stack.urc.tue.nl>
  5.  * 
  6.  * This program is free software; you can redistribute it and/or modify it
  7.  * under the terms of the GNU General Public License as published by the
  8.  * Free Software Foundation;
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  * 
  19.  */
  20.  
  21. #include <math.h>
  22. #include "ray.h"
  23. #include "proto.h"
  24. #include "extern.h"
  25.  
  26. PUBLIC double
  27. to_fov_coeff(struct camera *c, double angle)
  28. {
  29.     return veclen(c->eye_dir) * tan(degtorad(angle) / 2);
  30. }
  31.  
  32. PUBLIC void
  33. copy_camera(struct camera *dst, struct camera *src)
  34. {
  35.     assert(src != NULL && dst != NULL);
  36.  
  37.     *dst = *src;
  38.  
  39. }
  40.  
  41. /* point the camera towards a particular point. */
  42. PUBLIC void
  43. point_camera(struct camera *c, vector lookat)
  44. {
  45.     double          dirlen;
  46.  
  47.     dirlen = veclen(c->eye_dir);
  48.     vsub(c->eye_dir, lookat, c->eye);
  49.     norm(c->eye_dir, c->eye_dir);
  50.     svproduct(c->eye_dir, dirlen, c->eye_dir);
  51. }
  52.  
  53. PUBLIC void
  54. init_camera(struct camera *p)
  55. {
  56.     setvector(p->eye_dir, 0, 0, 1);
  57.     setvector(p->eye, 0, 0, -10);
  58.     setvector(p->sky, 0, 1, 0);
  59.     p->fov = to_fov_coeff(p, 90);
  60.     p->aspect = 1.0;
  61. }
  62.  
  63. PUBLIC struct camera *
  64. get_new_camera(void)
  65. {
  66.     struct camera  *p;
  67.  
  68.     p = ALLOC(struct camera);
  69.  
  70.     CHECK_MEM(p, "camera");
  71.     init_camera(p);
  72.  
  73.     return p;
  74. }
  75.  
  76. PUBLIC void
  77. free_camera(struct camera *c)
  78. {
  79.     free((void *) c);
  80. }
  81.  
  82. PUBLIC void
  83. translate_camera(struct camera *c, vector t)
  84. {
  85.     vadd(c->eye, c->eye, t);
  86. }
  87.  
  88. PUBLIC void
  89. rotate_camera(struct camera *c, matrix rotmat)
  90. {
  91.     rotate_vector(&c->eye_dir, rotmat);
  92.     rotate_vector(&c->eye, rotmat);
  93.     rotate_vector(&c->sky, rotmat);
  94. }
  95.  
  96. PUBLIC void
  97. scale_camera(struct camera *c, vector s)
  98. {
  99.     vcproduct(c->eye_dir, c->eye_dir, s);
  100.     vcproduct(c->eye, c->eye, s);
  101.     vcproduct(c->sky, c->sky, s);
  102. }
  103.  
  104. PUBLIC void
  105. print_camera(struct camera *c)
  106. {
  107. #ifdef DEBUG
  108.     printf("camera:");
  109.     print_v("eye_dir", c->eye_dir);
  110.     print_v("eye", c->eye);
  111.     print_v("sky", c->sky);
  112.     printf("fov %lf\n", c->fov);
  113.     printf("aspect %lf\n", c->aspect);
  114. #endif
  115. }
  116.