erebus

view liberebus/src/camera.cc @ 8:e2d9bf168a41

semi-works ...
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 May 2014 06:12:57 +0300
parents 9621beb22694
children d15ee526daa6
line source
1 #include <stdio.h>
2 #include <math.h>
3 #include "camera.h"
5 #define DEFAULT_FOV DEG_TO_RAD(50)
7 static void calc_sample_pos_rec(int sidx, float xsz, float ysz, float *pos);
9 Camera::Camera()
10 {
11 vfov = DEFAULT_FOV;
12 cached_matrix_valid = false;
13 }
15 Camera::Camera(const Vector3 &p)
16 : pos(p)
17 {
18 vfov = DEFAULT_FOV;
19 cached_matrix_valid = false;
20 }
22 Camera::~Camera()
23 {
24 }
26 void Camera::set_fov(float vfov)
27 {
28 this->vfov = vfov;
29 }
31 float Camera::get_fov() const
32 {
33 return vfov;
34 }
36 void Camera::set_position(const Vector3 &pos)
37 {
38 this->pos = pos;
39 cached_matrix_valid = false; // invalidate the cached matrix
40 }
42 const Vector3 &Camera::get_position() const
43 {
44 return pos;
45 }
47 const Matrix4x4 &Camera::get_matrix() const
48 {
49 if(!cached_matrix_valid) {
50 calc_matrix(&cached_matrix);
51 cached_matrix_valid = true;
52 }
53 return cached_matrix;
54 }
56 Vector2 Camera::calc_sample_pos(int x, int y, int xsz, int ysz, int sample) const
57 {
58 float ppos[2];
59 float aspect = (float)xsz / (float)ysz;
61 float pwidth = 2.0 * aspect / (float)xsz;
62 float pheight = 2.0 / (float)ysz;
64 ppos[0] = (float)x * pwidth - aspect;
65 ppos[1] = 1.0 - (float)y * pheight;
67 calc_sample_pos_rec(sample, pwidth, pheight, ppos);
68 return Vector2(ppos[0], ppos[1]);
69 }
71 Ray Camera::get_primary_ray(int x, int y, int xsz, int ysz, int sample) const
72 {
73 Vector2 ppos = calc_sample_pos(x, y, xsz, ysz, sample);
75 Ray ray;
76 ray.origin = pos;
77 ray.dir.x = ppos.x;
78 ray.dir.y = ppos.y;
79 ray.dir.z = 1.0 / tan(vfov / 2.0);
80 ray.dir.normalize();
82 // transform the ray direction with the camera matrix
83 Matrix4x4 mat = get_matrix();
84 mat.m[0][3] = mat.m[1][3] = mat.m[2][3] = mat.m[3][0] = mat.m[3][1] = mat.m[3][2] = 0.0;
85 mat.m[3][3] = 1.0;
87 ray.dir = ray.dir.transformed(mat);
88 return ray;
89 }
91 TargetCamera::TargetCamera() {}
93 TargetCamera::TargetCamera(const Vector3 &pos, const Vector3 &targ)
94 : Camera(pos), target(targ)
95 {
96 }
98 void TargetCamera::set_target(const Vector3 &targ)
99 {
100 target = targ;
101 cached_matrix_valid = false; // invalidate the cached matrix
102 }
104 const Vector3 &TargetCamera::get_target() const
105 {
106 return target;
107 }
109 void TargetCamera::calc_matrix(Matrix4x4 *mat) const
110 {
111 Vector3 up(0, 1, 0);
112 Vector3 dir = (target - pos).normalized();
113 Vector3 right = cross_product(up, dir);
114 up = cross_product(dir, right);
116 *mat = Matrix4x4(
117 right.x, up.x, dir.x, pos.x,
118 right.y, up.y, dir.y, pos.y,
119 right.z, up.z, dir.z, pos.z,
120 0.0, 0.0, 0.0, 1.0);
121 }
123 void FlyCamera::input_move(float x, float y, float z)
124 {
125 static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
127 Vector3 k = vfwd.transformed(rot);
128 Vector3 i = vright.transformed(rot);
129 Vector3 j = cross_product(k, i);
131 pos += i * x + j * y + k * z;
132 cached_matrix_valid = false;
133 }
135 void FlyCamera::input_rotate(float x, float y, float z)
136 {
137 Vector3 axis(x, y, z);
138 float axis_len = axis.length();
139 if(fabs(axis_len) < 1e-5) {
140 return;
141 }
142 rot.rotate(axis / axis_len, -axis_len);
143 rot.normalize();
145 cached_matrix_valid = false;
146 }
148 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
149 {
150 Matrix4x4 tmat;
151 tmat.set_translation(pos);
153 Matrix3x3 rmat = rot.get_rotation_matrix();
155 *mat = tmat * Matrix4x4(rmat);
156 }
158 /* generates a sample position for sample number sidx, in the unit square
159 * by recursive subdivision and jittering
160 */
161 static void calc_sample_pos_rec(int sidx, float xsz, float ysz, float *pos)
162 {
163 static const float subpt[4][2] = {
164 {-0.25, -0.25}, {0.25, -0.25}, {-0.25, 0.25}, {0.25, 0.25}
165 };
167 if(!sidx) {
168 return;
169 }
171 /* determine which quadrant to recurse into */
172 int quadrant = ((sidx - 1) % 4);
173 pos[0] += subpt[quadrant][0] * xsz;
174 pos[1] += subpt[quadrant][1] * ysz;
176 calc_sample_pos_rec((sidx - 1) / 4, xsz / 2, ysz / 2, pos);
177 }