File: camera.c

package info (click to toggle)
xinv3d 1.31-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: ansic: 3,669; makefile: 69
file content (138 lines) | stat: -rw-r--r-- 3,964 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*------------------------------------------------------------------
  camera.c:

    XINVADERS 3D - 3d Shoot'em up
    Copyright (C) 2000 Don Llopis

    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; either version 2 of the License, or
    (at your option) any later version.

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "mat4x4.h"
#include "vec4x1.h"
#include "camera.h"

/* local variables */
static float hfov, vfov, hpc, vpc;
static float screen_width, screen_height, half_screen_width,
             half_screen_height;
static float aspect_ratio;
static float hadjust, vadjust;
static MAT r_2_l; 


void Camera_init ( unsigned int width, unsigned int height, float fov )
{
  screen_width       = (float) width;
  screen_height      = (float) height;
  half_screen_width  = screen_width / 2.0f;
  half_screen_height = screen_height / 2.0f;
  
  /* calculate horizontal & vertical field of view */

  /* horizontal compensate = HALF_SCREEN_WIDTH / tan ( degrees / 2 )
     vertical compensate = HALF_SCREEN_HEIGHT / tan ( degrees / 2 )

     see "Building 3d Game Engine" by Brain Hook
  */

  hfov = vfov = fov;

  hpc = half_screen_width / tan ( hfov/1.5f );
  vpc = half_screen_height / tan ( vfov/1.5f );

  /* adjust aspect ratio if screen not square ( ie. 4/3 ratio )*/
  aspect_ratio = screen_height / screen_height;


  /* calculate perspective adjustment values */
  hadjust = hpc;
  vadjust = (vpc * aspect_ratio);

  /* initialize coordinate system conversion mat */
  Matrix_id ( r_2_l );
  r_2_l[2][2] = -1.0f;
  
}

void Camera_project_point ( VEC a, int b[2] )
{
   if ( a[2] < 0.0 ) {
      b[0] = b[1] = 0;
      return;
   }
  b[0] = (int) ( a[0] * hadjust / a[2] + half_screen_width );
  b[1] = (int) ( -a[1] * vadjust / a[2] + half_screen_height );
}

void Camera_project_points ( VEC a[], int b[], int n )
{
   int i, j;
  
   for ( i=0, j=0; i<n; i++, j+=2 )
   {
     b[j] = (int) ( a[i][0] * hadjust / a[i][2] + half_screen_width );
     b[j+1] = (int) ( -a[i][1] * vadjust / a[i][2] + half_screen_height );
   }
}

void Camera_transform ( MAT r, VEC up, VEC from, VEC at )
{
  VEC x, y, z, tmp, pos;
  MAT tmp_mat;

  /* initialize transformation mat */
  Matrix_id ( tmp_mat );

  /* calculate view plane normal -- or view-z-axis */
  Vector_sub ( from, at, z );
  Vector_norm ( z );

  /* calculate view-x-axis */
  Vector_cross ( up, z, x );
  Vector_norm ( x );

  /* calculate view-y-axis */
  Vector_cross ( z, x, y );

  /* place axis-vecs into transformation mat
     To understand how this works see chapter 5
     and 6 of "Computer Graphics Principles & Practice"
   */
  Matrix_set_xrow ( tmp_mat, x );
  Matrix_set_yrow ( tmp_mat, y );
  Matrix_set_zrow ( tmp_mat, z );

  /* get camera's world position and rotate it
     into camera coordinate system. This becomes
     the new translation which is to be applied
     to an object's vertices inorder to make its
     position relative to the camera coordinate system
     see chapter 6
  */
  Vector_copy ( from, tmp );
  Vector_negate ( tmp );
  Matrix_vec_mult ( tmp_mat, tmp, pos );
  Matrix_set_trans ( tmp_mat, pos );

  /* now change from right hand coordinate system
     to left hand coordinate system
     leaving the result in 'r'
  */
  Matrix_mult ( r_2_l, tmp_mat, r );
}