author | Radek Brich <radek.brich@devl.cz> |
Sat, 29 Mar 2008 12:09:50 +0100 | |
branch | pyrit |
changeset 55 | f6d75ae82c88 |
parent 54 | dbe3c7a4e0f0 |
child 56 | d4481fc43952 |
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 |
/* |
44 | 2 |
* raytracer.cc: Raytracer class |
3 |
* |
|
4 |
* This file is part of Pyrit Ray Tracer. |
|
5 |
* |
|
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
6 |
* Copyright 2006, 2007, 2008 Radek Brich |
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
|
7 |
* |
44 | 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9 |
* of this software and associated documentation files (the "Software"), to deal |
|
10 |
* in the Software without restriction, including without limitation the rights |
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12 |
* copies of the Software, and to permit persons to whom the Software is |
|
13 |
* furnished to do so, subject to the following conditions: |
|
14 |
* |
|
15 |
* The above copyright notice and this permission notice shall be included in |
|
16 |
* all copies or substantial portions of the Software. |
|
17 |
* |
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24 |
* THE SOFTWARE. |
|
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
|
25 |
*/ |
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 |
|
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
27 |
#include <pthread.h> |
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
|
28 |
#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
|
29 |
#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
|
30 |
#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
|
31 |
|
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
32 |
int pyrit_verbosity = 2; |
30
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
33 |
|
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
|
34 |
// 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
|
35 |
// http://www.cse.cuhk.edu.hk/~ttwong/papers/udpoint/udpoints.html |
22 | 36 |
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
|
37 |
{ |
22 | 38 |
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
|
39 |
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
|
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 |
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
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
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
|
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 |
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
|
48 |
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
|
49 |
|
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 |
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
|
51 |
|
22 | 52 |
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
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
|
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 |
// 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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
|
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 |
// 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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
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 |
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
|
70 |
} |
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 |
|
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 |
// ---- 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
|
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 |
// 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
|
75 |
// 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
|
76 |
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
|
77 |
{ |
42 | 78 |
Colour col; |
79 |
if (mat.texture) |
|
80 |
col = mat.texture->evaluate(P); |
|
81 |
else |
|
82 |
col = mat.colour; |
|
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
|
83 |
|
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 |
// 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
|
85 |
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
|
86 |
} |
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 |
/* |
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 |
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
|
90 |
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
|
91 |
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
|
92 |
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
|
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 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
|
95 |
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
|
96 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
L.normalize(); |
22 | 100 |
Float L_dot_N = dot(L, N); |
101 |
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
|
102 |
|
42 | 103 |
Colour col; |
104 |
if (mat.texture) |
|
105 |
col = mat.texture->evaluate(P); |
|
106 |
else |
|
107 |
col = mat.colour; |
|
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
|
108 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
109 |
// 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
|
110 |
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
|
111 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
112 |
// 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
|
113 |
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
|
114 |
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
|
115 |
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
|
116 |
} |
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 |
|
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 |
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
|
119 |
{ |
22 | 120 |
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
|
121 |
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
|
122 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
123 |
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
|
124 |
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
|
125 |
} else { |
31 | 126 |
Colour col = Colour(); |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
127 |
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
|
128 |
Vector3 normal = nearest_shape->normal(P); |
31 | 129 |
bool from_inside = false; |
130 |
||
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
22
diff
changeset
|
131 |
// 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
|
132 |
if (dot(normal, ray.dir) > 0.0) |
31 | 133 |
{ |
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
22
diff
changeset
|
134 |
normal = - normal; |
31 | 135 |
from_inside = true; |
136 |
} |
|
137 |
||
138 |
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
|
139 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
L.normalize(); |
22 | 144 |
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
|
145 |
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
|
146 |
// 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
|
147 |
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
|
148 |
Ray shadow_ray = Ray(P, L); |
22 | 149 |
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
|
150 |
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
|
151 |
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
|
152 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
153 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
154 |
// 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
|
155 |
Vector3 R = L - 2.0 * L_dot_N * normal; |
31 | 156 |
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
|
157 |
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
|
158 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
159 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
160 |
|
31 | 161 |
if (depth < max_depth) |
162 |
{ |
|
163 |
Colour trans_col, refl_col; |
|
164 |
Float trans = nearest_shape->material->transmissivity; |
|
165 |
Float refl = nearest_shape->material->reflectivity; |
|
166 |
const Float cos_i = - dot(normal, ray.dir); |
|
167 |
||
168 |
// reflection |
|
169 |
if (refl > 0.01) |
|
170 |
{ |
|
171 |
Vector3 newdir = ray.dir + 2.0 * cos_i * normal; |
|
172 |
Ray newray = Ray(P, newdir); |
|
173 |
refl_col = raytrace(newray, depth + 1, nearest_shape); |
|
174 |
} |
|
175 |
||
176 |
// refraction |
|
177 |
if (trans > 0.01) |
|
178 |
{ |
|
179 |
Float n, n1, n2; |
|
180 |
if (from_inside) |
|
181 |
{ |
|
182 |
n1 = nearest_shape->material->refract_index; |
|
183 |
n2 = 1.0; |
|
184 |
n = n1; |
|
185 |
} |
|
186 |
else |
|
187 |
{ |
|
188 |
n1 = 1.0; |
|
189 |
n2 = nearest_shape->material->refract_index; |
|
190 |
n = 1.0 / n2; |
|
191 |
} |
|
192 |
const Float sin2_t = n*n * (1 - cos_i*cos_i); |
|
193 |
if (sin2_t >= 1.0) |
|
194 |
{ |
|
195 |
// totally reflected |
|
196 |
refl += trans; |
|
197 |
trans = 0; |
|
198 |
} |
|
199 |
else |
|
200 |
{ |
|
201 |
const Float cos_t = sqrtf(1 - sin2_t); |
|
202 |
const Float Rdiv = 1.0/(n1*cos_i + n2*cos_t); |
|
203 |
const Float Rper = (n1*cos_i - n2*cos_t)*Rdiv; |
|
204 |
const Float Rpar = (n2*cos_i - n1*cos_t)*Rdiv; |
|
205 |
const Float R = (Rper*Rper + Rpar*Rpar)/2; |
|
206 |
refl += R*trans; |
|
207 |
trans = (1-R)*trans; |
|
208 |
Vector3 newdir = n * ray.dir + (n*cos_i - cos_t) * normal; |
|
209 |
Ray newray = Ray(P, newdir); |
|
210 |
trans_col = raytrace(newray, depth + 1, nearest_shape); |
|
211 |
} |
|
212 |
} |
|
213 |
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
|
214 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
215 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
216 |
// ambient occlusion |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
217 |
if (!from_inside && ao_samples) |
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
|
218 |
{ |
22 | 219 |
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
|
220 |
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
|
221 |
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
|
222 |
Ray ao_ray = Ray(P, dir); |
22 | 223 |
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
|
224 |
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
|
225 |
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
|
226 |
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
|
227 |
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
|
228 |
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
|
229 |
} |
22 | 230 |
Float ao_intensity = miss / ao_samples; |
31 | 231 |
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
|
232 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
233 |
|
31 | 234 |
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
|
235 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
236 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
237 |
|
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
238 |
#if 0 |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
239 |
static void *renderrow(void *data) |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
240 |
{ |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
241 |
RenderrowData *d = (RenderrowData*) data; |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
242 |
const int subsample = d->rt->getSubsample(); |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
243 |
const Float subsample2 = 1.0/(subsample*subsample); |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
244 |
const int oversample = d->rt->getOversample(); |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
245 |
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
|
246 |
Vector3 dir = d->dfix; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
247 |
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
|
248 |
// 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
|
249 |
if (subsample > 1) |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
250 |
{ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
251 |
Colour ic; |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
252 |
// top-left |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
253 |
dir.normalize(); |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
254 |
Ray ray(d->eye, dir); |
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
255 |
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
|
256 |
// top-right |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
257 |
Vector3 tmpdir = dir + (subsample-1)*d->dx; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
258 |
tmpdir.normalize(); |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
259 |
ray.dir = tmpdir; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
260 |
Colour c2 = d->rt->raytrace(ray, 0, NULL); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
261 |
// bottom right |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
262 |
tmpdir += (subsample-1)*d->dy; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
263 |
tmpdir.normalize(); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
264 |
ray.dir = tmpdir; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
265 |
Colour c4 = d->rt->raytrace(ray, 0, NULL); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
266 |
// bottom left |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
267 |
tmpdir = dir + (subsample-1)*d->dy; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
268 |
tmpdir.normalize(); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
269 |
ray.dir = tmpdir; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
270 |
Colour c3 = d->rt->raytrace(ray, 0, NULL); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
271 |
// are the colors similar? |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
272 |
Float m = (c1-c2).mag2(); |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
273 |
m = max(m, (c2-c3).mag2()); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
274 |
m = max(m, (c3-c4).mag2()); |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
275 |
m = max(m, (c4-c1).mag2()); |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
276 |
if (m < 0.001) |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
277 |
{ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
278 |
// interpolate |
22 | 279 |
Float *i = d->iter; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
280 |
for (int x = 0; x < subsample; x++) |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
281 |
{ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
282 |
for (int y = 0; y < subsample; y++) |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
283 |
{ |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
284 |
ic = c1*(subsample-x)*(subsample-y)*subsample2 |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
285 |
+ c2*(x)*(subsample-y)*subsample2 |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
286 |
+ c3*(subsample-x)*(y)*subsample2 |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
287 |
+ c4*(x)*(y)*subsample2; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
288 |
*(i + ww*y) = ic.r; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
289 |
*(i + ww*y + 1) = ic.g; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
290 |
*(i + ww*y + 2) = ic.b; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
291 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
292 |
i += 3; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
293 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
294 |
d->iter = i; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
295 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
296 |
else |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
297 |
{ |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
298 |
// render all pixels |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
299 |
Vector3 tmpdir = dir; |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
300 |
|
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
301 |
if (oversample) |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
302 |
{ |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
303 |
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
|
304 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
305 |
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
|
306 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
307 |
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
|
308 |
samplepixel(ic, tmp2dir, d, oversample); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
309 |
*(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
|
310 |
*(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
|
311 |
*(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
|
312 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
313 |
d->iter += 3; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
314 |
tmpdir += d->dx; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
315 |
} |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
316 |
} |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
317 |
else |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
318 |
{ |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
319 |
/* 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
|
320 |
already computed corner pixels |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
321 |
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
|
322 |
// first column |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
323 |
*(d->iter) = c1.r; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
324 |
*(d->iter + 1) = c1.g; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
325 |
*(d->iter + 2) = c1.b; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
326 |
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
|
327 |
{ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
328 |
Vector3 tmp2dir = tmpdir + y*d->dy; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
329 |
tmp2dir.normalize(); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
330 |
ray.dir = tmp2dir; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
331 |
ic = d->rt->raytrace(ray, 0, NULL); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
332 |
*(d->iter + ww*y) = ic.r; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
333 |
*(d->iter + ww*y + 1) = ic.g; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
334 |
*(d->iter + ww*y + 2) = ic.b; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
335 |
} |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
336 |
*(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
|
337 |
*(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
|
338 |
*(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
|
339 |
d->iter += 3; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
340 |
tmpdir += d->dx; |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
341 |
// middle |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
342 |
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
|
343 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
344 |
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
|
345 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
346 |
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
|
347 |
tmp2dir.normalize(); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
348 |
ray.dir = tmp2dir; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
349 |
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
|
350 |
*(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
|
351 |
*(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
|
352 |
*(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
|
353 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
354 |
d->iter += 3; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
355 |
tmpdir += d->dx; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
356 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
357 |
// last column |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
358 |
*(d->iter) = c2.r; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
359 |
*(d->iter + 1) = c2.g; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
360 |
*(d->iter + 2) = c2.b; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
361 |
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
|
362 |
{ |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
363 |
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
|
364 |
tmp2dir.normalize(); |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
365 |
ray.dir = tmp2dir; |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
366 |
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
|
367 |
*(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
|
368 |
*(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
|
369 |
*(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
|
370 |
} |
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
371 |
*(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
|
372 |
*(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
|
373 |
*(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
|
374 |
d->iter += 3; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
375 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
376 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
377 |
} |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
378 |
else // subsample <= 1 |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
379 |
{ |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
380 |
Colour c; |
33
83d0200d4c09
make over-sampling work together with sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
381 |
samplepixel(c, dir, d, oversample); |
32
8af5c17d368b
new Raytracer option: oversampling
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
382 |
// write color to buffer |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
383 |
*d->iter++ = c.r; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
384 |
*d->iter++ = c.g; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
385 |
*d->iter++ = c.b; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
386 |
} |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
387 |
dir += d->dx*subsample; |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
388 |
} |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
389 |
#ifdef PTHREADS |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
390 |
pthread_exit((void *)d); |
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
391 |
#endif |
6
d8d596d26f25
pthreads and other fixes for Windows
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
392 |
return (void *)d; |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
393 |
} |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
394 |
#endif |
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
395 |
|
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
396 |
void *Raytracer::raytrace_worker(void *d) |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
397 |
{ |
55 | 398 |
static const int my_queue_size = 256; |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
399 |
Raytracer *rt = (Raytracer*)d; |
54
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
400 |
Sample my_queue[my_queue_size]; |
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
401 |
Colour my_colours[my_queue_size]; |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
402 |
int my_count; |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
403 |
Ray ray; |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
404 |
for (;;) |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
405 |
{ |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
406 |
pthread_mutex_lock(&rt->sample_queue_mutex); |
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
407 |
while (rt->sample_queue_count == 0) |
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
408 |
{ |
54
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
409 |
if (rt->end_of_samples) |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
410 |
{ |
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
411 |
dbgmsg(4, "T thread [%d] exiting\n", pthread_self()); |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
412 |
pthread_mutex_unlock(&rt->sample_queue_mutex); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
413 |
pthread_exit(NULL); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
414 |
} |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
415 |
pthread_cond_wait(&rt->sample_queue_cond, &rt->sample_queue_mutex); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
416 |
} |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
417 |
|
54
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
418 |
if (rt->sample_queue_count >= my_queue_size) |
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
419 |
my_count = my_queue_size; |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
420 |
else |
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
421 |
my_count = rt->sample_queue_count; |
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
422 |
rt->sample_queue_count -= my_count; |
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
423 |
|
54
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
424 |
// copy samples to local queue |
55 | 425 |
if (rt->sample_queue_pos + my_count >= rt->sample_queue_size) |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
426 |
{ |
55 | 427 |
register int c = rt->sample_queue_size - rt->sample_queue_pos; |
428 |
memcpy(my_queue, rt->sample_queue + rt->sample_queue_pos, c*sizeof(Sample)); |
|
429 |
memcpy(my_queue + c, rt->sample_queue, (my_count - c)*sizeof(Sample)); |
|
430 |
rt->sample_queue_pos = my_count - c; |
|
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
431 |
} |
55 | 432 |
else |
433 |
{ |
|
434 |
memcpy(my_queue, rt->sample_queue + rt->sample_queue_pos, |
|
435 |
my_count*sizeof(Sample)); |
|
436 |
rt->sample_queue_pos += my_count; |
|
437 |
} |
|
438 |
if (rt->sample_queue_count <= my_queue_size*2) |
|
439 |
pthread_cond_signal(&rt->worker_ready_cond); |
|
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
440 |
pthread_mutex_unlock(&rt->sample_queue_mutex); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
441 |
|
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
442 |
// do the work |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
443 |
for (int i = 0; i < my_count; i++) |
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
444 |
{ |
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
445 |
ray = rt->camera->makeRay(my_queue[i]); |
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
446 |
my_colours[i] = rt->raytrace(ray, 0, NULL); |
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
447 |
} |
51
89fec8668768
remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
448 |
|
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
449 |
// save the results |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
450 |
pthread_mutex_lock(&rt->sampler_mutex); |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
451 |
for (int i = 0; i < my_count; i++) |
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
452 |
rt->sampler->saveSample(my_queue[i], my_colours[i]); |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
453 |
pthread_mutex_unlock(&rt->sampler_mutex); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
454 |
} |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
455 |
} |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
456 |
|
46 | 457 |
void Raytracer::render() |
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
|
458 |
{ |
46 | 459 |
if (!sampler || !camera || !top) |
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
460 |
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
|
461 |
|
55 | 462 |
static const int my_count_max = 256; |
463 |
sample_queue_size = my_count_max*2*num_threads; |
|
464 |
sample_queue = new Sample [sample_queue_size]; |
|
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
465 |
sample_queue_pos = 0; |
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
466 |
sample_queue_count = 0; |
54
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
467 |
end_of_samples = false; |
55 | 468 |
int my_count, my_pos = 0; |
54
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
469 |
int sampnum = 0, sampdone; |
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
470 |
bool more_samples; |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
471 |
|
55 | 472 |
sampler->init(); |
473 |
||
46 | 474 |
// create workers |
51
89fec8668768
remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
475 |
dbgmsg(1, "* running %d threads\n", num_threads); |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
476 |
pthread_t threads[num_threads]; |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
477 |
for (int t = 0; t < num_threads; t++) |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
478 |
{ |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
479 |
int rc = pthread_create(&threads[t], NULL, raytrace_worker, (void*)this); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
480 |
if (rc) { |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
481 |
dbgmsg(0, "\nE pthread_create unsuccessful, return code was %d\n", rc); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
482 |
exit(1); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
483 |
} |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
484 |
} |
46 | 485 |
|
51
89fec8668768
remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
486 |
dbgmsg(1, "* raytracing...\n"); |
89fec8668768
remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
487 |
dbgmsg(2, "- 0%% done"); |
89fec8668768
remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
488 |
|
54
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
489 |
pthread_mutex_lock(&sampler_mutex); |
46 | 490 |
while ( (sampnum = sampler->initSampleSet()) > 0 ) |
491 |
{ |
|
51
89fec8668768
remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
492 |
sampdone = 0; |
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
493 |
for (;;) |
46 | 494 |
{ |
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
495 |
my_count = 0; |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
496 |
while ( more_samples = sampler->nextSample(&sample_queue[my_pos++]) ) |
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
497 |
{ |
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
498 |
my_count++; |
55 | 499 |
if (my_pos >= sample_queue_size) |
500 |
my_pos = 0; |
|
54
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
501 |
if (my_count >= my_count_max) |
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
502 |
break; |
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
503 |
} |
55 | 504 |
if (!more_samples && !my_count) |
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
505 |
break; |
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
506 |
|
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
507 |
pthread_mutex_unlock(&sampler_mutex); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
508 |
pthread_mutex_lock(&sample_queue_mutex); |
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
509 |
sample_queue_count += my_count; |
46 | 510 |
|
51
89fec8668768
remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
511 |
// wait for workers if there is enough samples ready on queue |
55 | 512 |
while (sample_queue_count > (2*num_threads-1)*my_count_max) |
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
513 |
pthread_cond_wait(&worker_ready_cond, &sample_queue_mutex); |
55 | 514 |
|
515 |
pthread_cond_signal(&sample_queue_cond); |
|
516 |
pthread_mutex_unlock(&sample_queue_mutex); |
|
46 | 517 |
|
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
518 |
sampdone += my_count; |
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
519 |
dbgmsg(2, "\b\b\b\b\b\b\b\b%2d%% done", (sampdone - sample_queue_count)*100/sampnum); |
51
89fec8668768
remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
520 |
|
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
521 |
pthread_mutex_lock(&sampler_mutex); |
46 | 522 |
} |
51
89fec8668768
remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
523 |
dbgmsg(2, "\b\b\b\b\b\b\b\b100%% done\n"); |
46 | 524 |
} |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
525 |
pthread_mutex_unlock(&sampler_mutex); |
46 | 526 |
|
527 |
// wait for workers |
|
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
528 |
dbgmsg(2, "- waiting for threads to finish\n"); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
529 |
|
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
530 |
pthread_mutex_lock(&sample_queue_mutex); |
54
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
531 |
end_of_samples = true; |
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
532 |
while (sample_queue_count) |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
533 |
{ |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
534 |
pthread_cond_broadcast(&sample_queue_cond); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
535 |
pthread_mutex_unlock(&sample_queue_mutex); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
536 |
pthread_mutex_lock(&sample_queue_mutex); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
537 |
} |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
538 |
pthread_cond_broadcast(&sample_queue_cond); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
539 |
pthread_mutex_unlock(&sample_queue_mutex); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
540 |
|
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
541 |
for (int t = 0; t < num_threads; t++) |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
542 |
pthread_join(threads[t], NULL); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
543 |
|
52
a6413a3d741d
new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
544 |
delete[] sample_queue; |
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
|
545 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
546 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
547 |
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
|
548 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
549 |
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
|
550 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
551 |
|
22 | 552 |
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
|
553 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
554 |
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
|
555 |
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
|
556 |
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
|
557 |
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
|
558 |
/* 0 ==> Inf */ |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
559 |
ao_distance = Inf; |
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
|
560 |
} |