erebus

view liberebus/src/camera.cc @ 0:4abdce1361b9

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 27 Apr 2014 16:02:47 +0300
parents
children 9621beb22694
line source
1 #include <stdio.h>
2 #include <math.h>
3 #include "camera.h"
5 static void calc_sample_pos_rec(int sidx, float xsz, float ysz, float *pos);
7 Camera::Camera()
8 {
9 vfov = M_PI / 4.0;
10 cached_matrix_valid = false;
12 rdir_cache_width = rdir_cache_height = 0;
13 rdir_cache = 0;
14 }
16 Camera::Camera(const Vector3 &p)
17 : pos(p)
18 {
19 vfov = M_PI / 4.0;
20 cached_matrix_valid = false;
22 rdir_cache_width = rdir_cache_height = 0;
23 rdir_cache = 0;
24 }
26 Camera::~Camera()
27 {
28 delete [] rdir_cache;
29 }
31 void Camera::set_fov(float vfov)
32 {
33 this->vfov = vfov;
35 // invalidate the dir cache
36 delete [] rdir_cache;
37 }
39 float Camera::get_fov() const
40 {
41 return vfov;
42 }
44 void Camera::set_position(const Vector3 &pos)
45 {
46 this->pos = pos;
47 cached_matrix_valid = false; // invalidate the cached matrix
48 }
50 const Vector3 &Camera::get_position() const
51 {
52 return pos;
53 }
55 const Matrix4x4 &Camera::get_matrix() const
56 {
57 if(!cached_matrix_valid) {
58 calc_matrix(&cached_matrix);
59 cached_matrix_valid = true;
60 }
61 return cached_matrix;
62 }
64 Vector2 Camera::calc_sample_pos(int x, int y, int xsz, int ysz, int sample) const
65 {
66 float ppos[2];
67 float aspect = (float)xsz / (float)ysz;
69 float pwidth = 2.0 * aspect / (float)xsz;
70 float pheight = 2.0 / (float)ysz;
72 ppos[0] = (float)x * pwidth - aspect;
73 ppos[1] = 1.0 - (float)y * pheight;
75 calc_sample_pos_rec(sample, pwidth, pheight, ppos);
76 return Vector2(ppos[0], ppos[1]);
77 }
79 Ray Camera::get_primary_ray(int x, int y, int xsz, int ysz, int sample) const
80 {
81 #pragma omp single
82 {
83 if(!rdir_cache || rdir_cache_width != xsz || rdir_cache_height != ysz) {
84 printf("calculating primary ray direction cache\n");
86 delete [] rdir_cache;
87 rdir_cache = new Vector3[xsz * ysz];
89 #pragma omp parallel for
90 for(int i=0; i<ysz; i++) {
91 Vector3 *rdir = rdir_cache + i * xsz;
92 for(int j=0; j<xsz; j++) {
93 Vector2 ppos = calc_sample_pos(j, i, xsz, ysz, 0);
95 rdir->x = ppos.x;
96 rdir->y = ppos.y;
97 rdir->z = 1.0 / tan(vfov / 2.0);
98 rdir->normalize();
100 rdir++;
101 }
102 }
103 rdir_cache_width = xsz;
104 rdir_cache_height = ysz;
105 }
106 }
108 Ray ray;
109 ray.origin = pos;
110 ray.dir = rdir_cache[y * xsz + x];
112 // transform the ray direction with the camera matrix
113 Matrix4x4 mat = get_matrix();
114 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;
115 mat.m[3][3] = 1.0;
117 ray.dir = ray.dir.transformed(mat);
118 return ray;
119 }
121 TargetCamera::TargetCamera() {}
123 TargetCamera::TargetCamera(const Vector3 &pos, const Vector3 &targ)
124 : Camera(pos), target(targ)
125 {
126 }
128 void TargetCamera::set_target(const Vector3 &targ)
129 {
130 target = targ;
131 cached_matrix_valid = false; // invalidate the cached matrix
132 }
134 const Vector3 &TargetCamera::get_target() const
135 {
136 return target;
137 }
139 void TargetCamera::calc_matrix(Matrix4x4 *mat) const
140 {
141 Vector3 up(0, 1, 0);
142 Vector3 dir = (target - pos).normalized();
143 Vector3 right = cross_product(up, dir);
144 up = cross_product(dir, right);
146 *mat = Matrix4x4(
147 right.x, up.x, dir.x, pos.x,
148 right.y, up.y, dir.y, pos.y,
149 right.z, up.z, dir.z, pos.z,
150 0.0, 0.0, 0.0, 1.0);
151 }
153 void FlyCamera::input_move(float x, float y, float z)
154 {
155 static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
157 Vector3 k = vfwd.transformed(rot);
158 Vector3 i = vright.transformed(rot);
159 Vector3 j = cross_product(k, i);
161 pos += i * x + j * y + k * z;
162 cached_matrix_valid = false;
163 }
165 void FlyCamera::input_rotate(float x, float y, float z)
166 {
167 Vector3 axis(x, y, z);
168 float axis_len = axis.length();
169 if(fabs(axis_len) < 1e-5) {
170 return;
171 }
172 rot.rotate(axis / axis_len, -axis_len);
173 rot.normalize();
175 cached_matrix_valid = false;
176 }
178 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
179 {
180 Matrix4x4 tmat;
181 tmat.set_translation(pos);
183 Matrix3x3 rmat = rot.get_rotation_matrix();
185 *mat = tmat * Matrix4x4(rmat);
186 }
188 /* generates a sample position for sample number sidx, in the unit square
189 * by recursive subdivision and jittering
190 */
191 static void calc_sample_pos_rec(int sidx, float xsz, float ysz, float *pos)
192 {
193 static const float subpt[4][2] = {
194 {-0.25, -0.25}, {0.25, -0.25}, {-0.25, 0.25}, {0.25, 0.25}
195 };
197 if(!sidx) {
198 return;
199 }
201 /* determine which quadrant to recurse into */
202 int quadrant = ((sidx - 1) % 4);
203 pos[0] += subpt[quadrant][0] * xsz;
204 pos[1] += subpt[quadrant][1] * ysz;
206 calc_sample_pos_rec((sidx - 1) / 4, xsz / 2, ysz / 2, pos);
207 }