author | Radek Brich <radek.brich@devl.cz> |
Thu, 13 Dec 2007 00:08:11 +0100 | |
branch | pyrit |
changeset 36 | b490093b0ac3 |
parent 34 | 28f6e8b9d5d1 |
child 40 | 929aad02c5f2 |
permissions | -rw-r--r-- |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
/* |
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
2 |
* Pyrit Ray Tracer |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
* file: raytracer.cc |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
* |
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
5 |
* Radek Brich, 2006-2007 |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
*/ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
|
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
8 |
#ifdef PTHREADS |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
9 |
#include <pthread.h> |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
10 |
#endif |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
11 |
|
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
#include <stdio.h> |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
#include <malloc.h> |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
#include "raytracer.h" |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
|
30
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
16 |
int pyrit_verbosity = 2; |
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
17 |
|
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
// Hammersley spherical point distribution |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
// http://www.cse.cuhk.edu.hk/~ttwong/papers/udpoint/udpoints.html |
22 | 20 |
Vector3 Raytracer::SphereDistribute(int i, int n, Float extent, Vector3 &normal) |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
{ |
22 | 22 |
Float p, t, st, phi, phirad; |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
int kk; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
t = 0; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
for (p=0.5, kk=i; kk; p*=0.5, kk>>=1) |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
if (kk & 1) |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
t += p; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
t = 1.0 + (t - 1.0)*extent; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
phi = (i + 0.5) / n; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
phirad = phi * 2.0 * M_PI; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
st = sqrt(1.0 - t*t); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
|
22 | 36 |
Float x, y, z, xx, yy, zz, q; |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
x = st * cos(phirad); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
y = st * sin(phirad); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
z = t; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
// rotate against Y axis |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
q = acos(normal.z); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
zz = z*cos(q) - x*sin(q); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
xx = z*sin(q) + x*cos(q); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
yy = y; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
47 |
// rotate against Z axis |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
q = atan2f(normal.y, normal.x); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
x = xx*cos(q) - yy*sin(q); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
y = xx*sin(q) + yy*cos(q); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
z = zz; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
return Vector3(x, y, z); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
// ---- tyto dve funkce budou v budouci verzi metody objektu PhongShader |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
// calculate shader function |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
// P is point of intersection, N normal in this point |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
Colour PhongShader_ambient(Material &mat, Vector3 &P) |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
62 |
Colour col = mat.texture.colour; //mat.texture.evaluate(P); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
// ambient |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
return mat.ambient * col; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
66 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
/* |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
P is point of intersection, |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
70 |
N normal in this point, |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
71 |
R direction of reflected ray, |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
72 |
V direction to the viewer |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
73 |
*/ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
Colour PhongShader_calculate(Material &mat, Vector3 &P, Vector3 &N, Vector3 &R, Vector3 &V, |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
75 |
Light &light) |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
76 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
Colour I = Colour(); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
Vector3 L = light.pos - P; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
L.normalize(); |
22 | 80 |
Float L_dot_N = dot(L, N); |
81 |
Float R_dot_V = dot(R, V); |
|
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
82 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
83 |
Colour col = mat.texture.colour; //mat.texture.evaluate(P); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
84 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
85 |
// diffuse |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
86 |
I = mat.diffuse * col * light.colour * L_dot_N; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
87 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
88 |
// specular |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
89 |
if (R_dot_V > 0) |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
I += mat.specular * light.colour * powf(R_dot_V, mat.shininess); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
91 |
return I; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
92 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
93 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
94 |
Colour Raytracer::raytrace(Ray &ray, int depth, Shape *origin_shape) |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
95 |
{ |
22 | 96 |
Float nearest_distance = Inf; |
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
97 |
Shape *nearest_shape = top->nearest_intersection(origin_shape, ray, nearest_distance); |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
98 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
99 |
if (nearest_shape == NULL) { |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
100 |
return bg_colour; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
101 |
} else { |
31 | 102 |
Colour col = Colour(); |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
103 |
Vector3 P = ray.o + ray.dir * nearest_distance; // point of intersection |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
104 |
Vector3 normal = nearest_shape->normal(P); |
31 | 105 |
bool from_inside = false; |
106 |
||
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
22
diff
changeset
|
107 |
// make shapes double sided |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
22
diff
changeset
|
108 |
if (dot(normal, ray.dir) > 0.0) |
31 | 109 |
{ |
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
22
diff
changeset
|
110 |
normal = - normal; |
31 | 111 |
from_inside = true; |
112 |
} |
|
113 |
||
114 |
col = PhongShader_ambient(*nearest_shape->material, P); |
|
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
115 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
116 |
vector<Light*>::iterator light; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
117 |
for (light = lights.begin(); light != lights.end(); light++) { |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
118 |
Vector3 jo, L = (*light)->pos - P; // direction vector to light |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
119 |
L.normalize(); |
22 | 120 |
Float L_dot_N = dot(L, normal); |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
121 |
if (L_dot_N > 0) { |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
122 |
// test if this light is occluded (sharp shadows) |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
123 |
if ((*light)->cast_shadows) { |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
124 |
Ray shadow_ray = Ray(P, L); |
22 | 125 |
Float dist = FLT_MAX; |
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
126 |
if (top->nearest_intersection(nearest_shape, shadow_ray, dist)) |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
127 |
continue; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
128 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
129 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
130 |
// shading function |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
131 |
Vector3 R = L - 2.0 * L_dot_N * normal; |
31 | 132 |
col += PhongShader_calculate(*nearest_shape->material, |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
133 |
P, normal, R, ray.dir, **light); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
134 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
135 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
136 |
|
31 | 137 |
if (depth < max_depth) |
138 |
{ |
|
139 |
Colour trans_col, refl_col; |
|
140 |
Float trans = nearest_shape->material->transmissivity; |
|
141 |
Float refl = nearest_shape->material->reflectivity; |
|
142 |
const Float cos_i = - dot(normal, ray.dir); |
|
143 |
||
144 |
// reflection |
|
145 |
if (refl > 0.01) |
|
146 |
{ |
|
147 |
Vector3 newdir = ray.dir + 2.0 * cos_i * normal; |
|
148 |
Ray newray = Ray(P, newdir); |
|
149 |
refl_col = raytrace(newray, depth + 1, nearest_shape); |
|
150 |
} |
|
151 |
||
152 |
// refraction |
|
153 |
if (trans > 0.01) |
|
154 |
{ |
|
155 |
Float n, n1, n2; |
|
156 |
if (from_inside) |
|
157 |
{ |
|
158 |
n1 = nearest_shape->material->refract_index; |
|
159 |
n2 = 1.0; |
|
160 |
n = n1; |
|
161 |
} |
|
162 |
else |
|
163 |
{ |
|
164 |
n1 = 1.0; |
|
165 |
n2 = nearest_shape->material->refract_index; |
|
166 |
n = 1.0 / n2; |
|
167 |
} |
|
168 |
const Float sin2_t = n*n * (1 - cos_i*cos_i); |
|
169 |
if (sin2_t >= 1.0) |
|
170 |
{ |
|
171 |
// totally reflected |
|
172 |
refl += trans; |
|
173 |
trans = 0; |
|
174 |
} |
|
175 |
else |
|
176 |
{ |
|
177 |
const Float cos_t = sqrtf(1 - sin2_t); |
|
178 |
const Float Rdiv = 1.0/(n1*cos_i + n2*cos_t); |
|
179 |
const Float Rper = (n1*cos_i - n2*cos_t)*Rdiv; |
|
180 |
const Float Rpar = (n2*cos_i - n1*cos_t)*Rdiv; |
|
181 |
const Float R = (Rper*Rper + Rpar*Rpar)/2; |
|
182 |
refl += R*trans; |
|
183 |
trans = (1-R)*trans; |
|
184 |
Vector3 newdir = n * ray.dir + (n*cos_i - cos_t) * normal; |
|
185 |
Ray newray = Ray(P, newdir); |
|
186 |
trans_col = raytrace(newray, depth + 1, nearest_shape); |
|
187 |
} |
|
188 |
} |
|
189 |
col = (1-refl-trans)*col + refl*refl_col + trans*trans_col; |
|
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
190 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
191 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
192 |
// ambient occlusion |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
193 |
if (ao_samples) |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
194 |
{ |
22 | 195 |
Float miss = 0; |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
196 |
for (int i = 0; i < ao_samples; i++) { |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
197 |
Vector3 dir = SphereDistribute(i, ao_samples, ao_angle, normal); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
198 |
Ray ao_ray = Ray(P, dir); |
22 | 199 |
Float dist = ao_distance; |
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
200 |
Shape *shape_in_way = top->nearest_intersection(nearest_shape, ao_ray, dist); |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
201 |
if (shape_in_way == NULL) |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
202 |
miss += 1.0; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
203 |
else |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
204 |
miss += dist / ao_distance; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
205 |
} |
22 | 206 |
Float ao_intensity = miss / ao_samples; |
31 | 207 |
col = col * ao_intensity; |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
208 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
209 |
|
31 | 210 |
return col; |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
211 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
212 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
213 |
|
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
214 |
static inline void samplepixel(Colour &c, Vector3 &dir, RenderrowData* d, const int &oversample) |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
215 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
216 |
if (oversample <= 0) |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
217 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
218 |
// no oversampling |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
219 |
dir.normalize(); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
220 |
Ray ray(d->eye, dir); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
221 |
c = d->rt->raytrace(ray, 0, NULL); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
222 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
223 |
else |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
224 |
if (oversample <= 3) |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
225 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
226 |
// grid oversampling |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
227 |
c = Colour(0,0,0); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
228 |
static const int gridsamples[] = {5,9,16}; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
229 |
static const Float osa5x[] = {0.0, -0.4, +0.4, +0.4, -0.4}; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
230 |
static const Float osa5y[] = {0.0, -0.4, -0.4, +0.4, +0.4}; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
231 |
static const Float osa9x[] = {-0.34, 0.00, +0.34, |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
232 |
-0.34, 0.00, +0.34, -0.34, 0.00, +0.34}; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
233 |
static const Float osa9y[] = {-0.34, -0.34, -0.34, |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
234 |
0.00, 0.00, 0.00, +0.34, +0.34, +0.34}; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
235 |
static const Float osa16x[] = {-0.375, -0.125, +0.125, +0.375, |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
236 |
-0.375, -0.125, +0.125, +0.375, -0.375, -0.125, +0.125, +0.375, |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
237 |
-0.375, -0.125, +0.125, +0.375}; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
238 |
static const Float osa16y[] = {-0.375, -0.375, -0.375, -0.375, |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
239 |
-0.125, -0.125, -0.125, -0.125, +0.125, +0.125, +0.125, +0.125, |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
240 |
+0.375, +0.375, +0.375, +0.375}; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
241 |
static const Float *osaSx[] = {osa5x, osa9x, osa16x}; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
242 |
static const Float *osaSy[] = {osa5y, osa9y, osa16y}; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
243 |
const int samples = gridsamples[oversample-1]; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
244 |
const Float *osax = osaSx[oversample-1]; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
245 |
const Float *osay = osaSy[oversample-1]; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
246 |
for (int i = 0; i < samples; i++) |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
247 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
248 |
Vector3 tmpdir = dir + osax[i]*d->dx + osay[i]*d->dy; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
249 |
tmpdir.normalize(); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
250 |
Ray ray(d->eye, tmpdir); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
251 |
c += d->rt->raytrace(ray, 0, NULL); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
252 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
253 |
c = c * (1.0/samples); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
254 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
255 |
else |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
256 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
257 |
// stochastic oversampling |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
258 |
// ...todo |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
259 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
260 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
261 |
|
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
262 |
static void *renderrow(void *data) |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
263 |
{ |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
264 |
RenderrowData *d = (RenderrowData*) data; |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
265 |
const int subsample = d->rt->getSubsample(); |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
266 |
const Float subsample2 = 1.0/(subsample*subsample); |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
267 |
const int oversample = d->rt->getOversample(); |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
268 |
const int ww = d->w*3; |
19
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
269 |
Vector3 dir = d->dfix; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
270 |
for (int x = 0; x < d->w; x += subsample) { |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
271 |
// generate a ray from eye passing through this pixel |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
272 |
if (subsample > 1) |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
273 |
{ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
274 |
Colour ic; |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
275 |
// top-left |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
276 |
dir.normalize(); |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
277 |
Ray ray(d->eye, dir); |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
278 |
Colour c1 = d->rt->raytrace(ray, 0, NULL); |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
279 |
// top-right |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
280 |
Vector3 tmpdir = dir + (subsample-1)*d->dx; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
281 |
tmpdir.normalize(); |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
282 |
ray.dir = tmpdir; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
283 |
Colour c2 = d->rt->raytrace(ray, 0, NULL); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
284 |
// bottom right |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
285 |
tmpdir += (subsample-1)*d->dy; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
286 |
tmpdir.normalize(); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
287 |
ray.dir = tmpdir; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
288 |
Colour c4 = d->rt->raytrace(ray, 0, NULL); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
289 |
// bottom left |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
290 |
tmpdir = dir + (subsample-1)*d->dy; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
291 |
tmpdir.normalize(); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
292 |
ray.dir = tmpdir; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
293 |
Colour c3 = d->rt->raytrace(ray, 0, NULL); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
294 |
// are the colors similar? |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
295 |
Float m = (c1-c2).mag2(); |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
296 |
m = max(m, (c2-c3).mag2()); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
297 |
m = max(m, (c3-c4).mag2()); |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
298 |
m = max(m, (c4-c1).mag2()); |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
299 |
if (m < 0.001) |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
300 |
{ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
301 |
// interpolate |
22 | 302 |
Float *i = d->iter; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
303 |
for (int x = 0; x < subsample; x++) |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
304 |
{ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
305 |
for (int y = 0; y < subsample; y++) |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
306 |
{ |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
307 |
ic = c1*(subsample-x)*(subsample-y)*subsample2 |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
308 |
+ c2*(x)*(subsample-y)*subsample2 |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
309 |
+ c3*(subsample-x)*(y)*subsample2 |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
310 |
+ c4*(x)*(y)*subsample2; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
311 |
*(i + ww*y) = ic.r; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
312 |
*(i + ww*y + 1) = ic.g; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
313 |
*(i + ww*y + 2) = ic.b; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
314 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
315 |
i += 3; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
316 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
317 |
d->iter = i; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
318 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
319 |
else |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
320 |
{ |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
321 |
// render all pixels |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
322 |
Vector3 tmpdir = dir; |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
323 |
|
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
324 |
if (oversample) |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
325 |
{ |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
326 |
for (int x = 0; x < subsample; x++) |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
327 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
328 |
for (int y = 0; y < subsample; y++) |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
329 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
330 |
Vector3 tmp2dir = tmpdir + y*d->dy; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
331 |
samplepixel(ic, tmp2dir, d, oversample); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
332 |
*(d->iter + ww*y) = ic.r; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
333 |
*(d->iter + ww*y + 1) = ic.g; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
334 |
*(d->iter + ww*y + 2) = ic.b; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
335 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
336 |
d->iter += 3; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
337 |
tmpdir += d->dx; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
338 |
} |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
339 |
} |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
340 |
else |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
341 |
{ |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
342 |
/* this is so complex because it tries to reuse |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
343 |
already computed corner pixels |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
344 |
though, above code will also work for non-oversampling... */ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
345 |
// first column |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
346 |
*(d->iter) = c1.r; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
347 |
*(d->iter + 1) = c1.g; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
348 |
*(d->iter + 2) = c1.b; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
349 |
for (int y = 1; y < subsample-1; y++) |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
350 |
{ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
351 |
Vector3 tmp2dir = tmpdir + y*d->dy; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
352 |
tmp2dir.normalize(); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
353 |
ray.dir = tmp2dir; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
354 |
ic = d->rt->raytrace(ray, 0, NULL); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
355 |
*(d->iter + ww*y) = ic.r; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
356 |
*(d->iter + ww*y + 1) = ic.g; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
357 |
*(d->iter + ww*y + 2) = ic.b; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
358 |
} |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
359 |
*(d->iter + ww*(subsample-1)) = c3.r; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
360 |
*(d->iter + ww*(subsample-1) + 1) = c3.g; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
361 |
*(d->iter + ww*(subsample-1) + 2) = c3.b; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
362 |
d->iter += 3; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
363 |
tmpdir += d->dx; |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
364 |
// middle |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
365 |
for (int x = 1; x < subsample-1; x++) |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
366 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
367 |
for (int y = 0; y < subsample; y++) |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
368 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
369 |
Vector3 tmp2dir = tmpdir + y*d->dy; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
370 |
tmp2dir.normalize(); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
371 |
ray.dir = tmp2dir; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
372 |
ic = d->rt->raytrace(ray, 0, NULL); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
373 |
*(d->iter + ww*y) = ic.r; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
374 |
*(d->iter + ww*y + 1) = ic.g; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
375 |
*(d->iter + ww*y + 2) = ic.b; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
376 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
377 |
d->iter += 3; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
378 |
tmpdir += d->dx; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
379 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
380 |
// last column |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
381 |
*(d->iter) = c2.r; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
382 |
*(d->iter + 1) = c2.g; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
383 |
*(d->iter + 2) = c2.b; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
384 |
for (int y = 1; y < subsample-1; y++) |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
385 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
386 |
Vector3 tmp2dir = tmpdir + y*d->dy; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
387 |
tmp2dir.normalize(); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
388 |
ray.dir = tmp2dir; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
389 |
ic = d->rt->raytrace(ray, 0, NULL); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
390 |
*(d->iter + ww*y) = ic.r; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
391 |
*(d->iter + ww*y + 1) = ic.g; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
392 |
*(d->iter + ww*y + 2) = ic.b; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
393 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
394 |
*(d->iter + ww*(subsample-1)) = c4.r; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
395 |
*(d->iter + ww*(subsample-1) + 1) = c4.g; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
396 |
*(d->iter + ww*(subsample-1) + 2) = c4.b; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
397 |
d->iter += 3; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
398 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
399 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
400 |
} |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
401 |
else // subsample <= 1 |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
402 |
{ |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
403 |
Colour c; |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
404 |
samplepixel(c, dir, d, oversample); |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
405 |
// write color to buffer |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
406 |
*d->iter++ = c.r; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
407 |
*d->iter++ = c.g; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
408 |
*d->iter++ = c.b; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
409 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
410 |
dir += d->dx*subsample; |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
411 |
} |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
412 |
#ifdef PTHREADS |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
413 |
pthread_exit((void *)d); |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
414 |
#endif |
6
d8d596d26f25
pthreads and other fixes for Windows
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
415 |
return (void *)d; |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
416 |
} |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
417 |
|
22 | 418 |
void Raytracer::render(int w, int h, Float *buffer) |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
419 |
{ |
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
420 |
if (!camera || !top || !buffer) |
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
421 |
return; |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
422 |
|
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
423 |
RenderrowData *d; |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
424 |
|
22 | 425 |
Float S = 0.5/w; |
19
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
426 |
Vector3 dfix = camera->u*(-w/2.0*S/camera->f) |
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
427 |
+ camera->v*(h/2.0*S/camera->f) + camera->p; |
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
428 |
Vector3 dx = camera->u * (S/camera->f); |
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
429 |
Vector3 dy = camera->v * (-S/camera->f); |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
430 |
|
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
431 |
#ifdef PTHREADS |
30
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
432 |
dbgmsg(1, "* pthreads enabled, using %d threads\n", num_threads); |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
433 |
pthread_t threads[num_threads]; |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
434 |
for (int t = 0; t < num_threads; t++) |
6
d8d596d26f25
pthreads and other fixes for Windows
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
435 |
threads[t] = pthread_self(); |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
436 |
int t = 0; |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
437 |
#endif |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
438 |
|
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
439 |
/* for each pixel... */ |
30
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
440 |
dbgmsg(1, "* raytracing...\n"); |
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
441 |
dbgmsg(2, "- row 0 ( 0%% done)"); |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
442 |
for (int y = 0; y < h; y += subsample) |
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
443 |
{ |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
444 |
d = (RenderrowData*) malloc(sizeof(RenderrowData)); |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
445 |
d->rt = this; |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
446 |
d->w = w; |
19
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
447 |
d->eye = camera->eye; |
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
448 |
d->dfix = dfix; |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
449 |
d->dx = dx; |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
450 |
d->dy = dy; |
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
451 |
d->iter = buffer + y*3*w; |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
452 |
#ifdef PTHREADS |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
453 |
/* create new thread and increase 't' */ |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
454 |
int rc = pthread_create(&threads[t++], NULL, renderrow, (void *)d); |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
455 |
if (rc) { |
30
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
456 |
dbgmsg(0, "\nE pthread_create unsuccessful, return code was %d\n", rc); |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
457 |
exit(1); |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
458 |
} |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
459 |
/* when 't' owerflows, reset it */ |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
460 |
if (t >= num_threads) |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
461 |
t = 0; |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
462 |
/* wait for next thread in fifo queue, so the descriptor can be reused; |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
463 |
this also limits number of running threads */ |
6
d8d596d26f25
pthreads and other fixes for Windows
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
464 |
if (!pthread_equal(threads[t], pthread_self())) |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
465 |
if (pthread_join(threads[t], (void**)&d) == 0) |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
466 |
free(d); |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
467 |
#else |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
468 |
renderrow((void *)d); |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
469 |
free(d); |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
470 |
#endif |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
471 |
dfix += dy*subsample; |
30
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
472 |
dbgmsg(2, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%4d (%2d%% done)", y, y*100/(h-1)); |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
473 |
} |
30
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
474 |
dbgmsg(2, "\n"); |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
475 |
|
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
476 |
#ifdef PTHREADS |
30
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
477 |
dbgmsg(2, "- waiting for threads to finish\n"); |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
478 |
for (t = 0; t < num_threads; t++) |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
479 |
if (pthread_join(threads[t], (void**)&d) == 0) |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
480 |
free(d); |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
481 |
#endif |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
482 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
483 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
484 |
void Raytracer::addlight(Light *light) |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
485 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
486 |
lights.push_back(light); |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
487 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
488 |
|
22 | 489 |
void Raytracer::ambientocclusion(int samples, Float distance, Float angle) |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
490 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
491 |
ao_samples = samples; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
492 |
ao_distance = distance; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
493 |
ao_angle = angle; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
494 |
if (ao_distance == 0) |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
495 |
/* 0 ==> Inf */ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
496 |
ao_distance = FLT_MAX; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
497 |
} |