]> git.tdb.fi Git - libs/gl.git/blob - source/camera.cpp
Add a Camera class
[libs/gl.git] / source / camera.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <cmath>
9 #include "camera.h"
10 #include "matrix.h"
11 #include "projection.h"
12
13 namespace Msp {
14 namespace GL {
15
16 Camera::Camera():
17         fov(M_PI/4),
18         aspect(4.0/3.0),
19         clip_near(0.1),
20         clip_far(10),
21         position(0, 0, 0),
22         look_dir(0, 0, -1),
23         up_dir(0, 1, 0)
24 {
25         for(unsigned i=0; i<16; ++i)
26                 matrix[i] = (i%5 ? 0 : 1);
27         matrix[10] = -1;
28 }
29
30 void Camera::set_field_of_view(float f)
31 {
32         fov = f;
33 }
34
35 void Camera::set_aspect(float a)
36 {
37         aspect = a;
38 }
39
40 void Camera::set_depth_clip(float n, float f)
41 {
42         clip_near = n;
43         clip_far = f;
44 }
45
46 void Camera::set_position(const Vector3 &p)
47 {
48         position = p;
49         compute_matrix();
50 }
51
52 void Camera::set_up_direction(const Vector3 &u)
53 {
54         float len = sqrt(u.x*u.x+u.y*u.y+u.z*u.z);
55
56         up_dir.x = u.x/len;
57         up_dir.y = u.y/len;
58         up_dir.z = u.z/len;
59
60         compute_matrix();
61 }
62
63 void Camera::set_look_direction(const Vector3 &l)
64 {
65         float len = sqrt(l.x*l.x+l.y*l.y+l.z*l.z);
66
67         look_dir.x = l.x/len;
68         look_dir.y = l.y/len;
69         look_dir.z = l.z/len;
70
71         compute_matrix();
72 }
73
74 void Camera::look_at(const Vector3 &p)
75 {
76         set_look_direction(Vector3(p.x-position.x, p.y-position.y, p.z-position.z));
77 }
78
79 Vector3 Camera::project(const Vector4 &p) const
80 {
81         float near_h = tan(fov/2)*clip_near;
82         float near_w = near_h*aspect;
83         float z_range = clip_far-clip_near;
84
85         float eye_x = matrix[0]*p.x+matrix[4]*p.y+matrix[8]*p.z+matrix[12]*p.w;
86         float eye_y = matrix[1]*p.x+matrix[5]*p.y+matrix[9]*p.z+matrix[13]*p.w;
87         float eye_z = matrix[2]*p.x+matrix[6]*p.y+matrix[10]*p.z+matrix[14]*p.w;
88
89         return Vector3(eye_x/near_w/-eye_z, eye_y/near_h/-eye_z,
90                 (clip_far+clip_near)/z_range+2*clip_far*clip_near/(eye_z*z_range));
91 }
92
93 Vector4 Camera::unproject(const Vector4 &p) const
94 {
95         float near_h = tan(fov/2)*clip_near;
96         float near_w = near_h*aspect;
97         float z_range = clip_far-clip_near;
98
99         float z = (2*clip_far*clip_near)/(p.z*z_range-(clip_far+clip_near))-matrix[14]*p.w;
100         float x = p.x*-z*near_w-matrix[12]*p.w;
101         float y = p.y*-z*near_h-matrix[13]*p.w;
102
103         return Vector4(matrix[0]*x+matrix[1]*y+matrix[2]*z,
104                 matrix[4]*x+matrix[5]*y+matrix[6]*z,
105                 matrix[8]*x+matrix[9]*y+matrix[10]*z,
106                 p.w);
107 }
108
109 void Camera::apply() const
110 {
111         float h = tan(fov/2)*2*clip_near;
112
113         matrix_mode(PROJECTION);
114         load_identity();
115         frustum_centered(h*aspect, h, clip_near, clip_far);
116
117         matrix_mode(MODELVIEW);
118         load_matrix(matrix);
119 }
120
121 void Camera::compute_matrix()
122 {
123         float x = look_dir.y*up_dir.z-look_dir.z*up_dir.y;
124         float y = look_dir.z*up_dir.x-look_dir.x*up_dir.z;
125         float z = look_dir.x*up_dir.y-look_dir.y*up_dir.x;
126         float len = sqrt(x*x+y*y+z*z);
127
128         matrix[0] = x/len;
129         matrix[4] = y/len;
130         matrix[8] = z/len;
131
132         matrix[1] = matrix[4]*look_dir.z-matrix[8]*look_dir.y;
133         matrix[5] = matrix[8]*look_dir.x-matrix[0]*look_dir.z;
134         matrix[9] = matrix[0]*look_dir.y-matrix[4]*look_dir.x;
135
136         matrix[2] = -look_dir.x;
137         matrix[6] = -look_dir.y;
138         matrix[10] = -look_dir.z;
139
140         matrix[12] = -position.x;
141         matrix[13] = -position.y;
142         matrix[14] = -position.z;
143 }
144
145 } // namespace GL
146 } // namespace Msp