| author | Radek Brich <radek.brich@devl.cz> | 
| Fri, 28 Mar 2008 17:13:21 +0100 | |
| branch | pyrit | 
| changeset 50 | 14a727b70d07 | 
| parent 49 | 558fde7da82a | 
| child 51 | 89fec8668768 | 
| 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  | 
#ifdef PTHREADS  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
28  | 
#include <pthread.h>  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
29  | 
#endif  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
30  | 
|
| 
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
 | 
31  | 
#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
 | 
32  | 
#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
 | 
33  | 
#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
 | 
34  | 
|
| 
30
 
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
 
Radek Brich <radek.brich@devl.cz> 
parents: 
25 
diff
changeset
 | 
35  | 
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
 | 
36  | 
|
| 
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  | 
// 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
 | 
38  | 
// http://www.cse.cuhk.edu.hk/~ttwong/papers/udpoint/udpoints.html  | 
| 22 | 39  | 
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
 | 
40  | 
{
 | 
| 22 | 41  | 
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
 | 
42  | 
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
 | 
43  | 
|
| 
 
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 = 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
 | 
45  | 
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
 | 
46  | 
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
 | 
47  | 
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
 | 
48  | 
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
 | 
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  | 
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
 | 
51  | 
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
 | 
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  | 
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
 | 
54  | 
|
| 22 | 55  | 
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
 | 
56  | 
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
 | 
57  | 
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
 | 
58  | 
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
 | 
59  | 
|
| 
 
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  | 
// 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
 | 
61  | 
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
 | 
62  | 
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
 | 
63  | 
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
 | 
64  | 
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
 | 
65  | 
|
| 
 
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  | 
// 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
 | 
67  | 
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
 | 
68  | 
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
 | 
69  | 
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
 | 
70  | 
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
 | 
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  | 
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
 | 
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  | 
|
| 
 
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  | 
// ---- 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
 | 
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  | 
// 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
 | 
78  | 
// 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
 | 
79  | 
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
 | 
80  | 
{
 | 
| 42 | 81  | 
Colour col;  | 
82  | 
if (mat.texture)  | 
|
83  | 
col = mat.texture->evaluate(P);  | 
|
84  | 
else  | 
|
85  | 
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
 | 
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  | 
// 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
 | 
88  | 
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
 | 
89  | 
}  | 
| 
 
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  | 
|
| 
 
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  | 
/*  | 
| 
 
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  | 
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
 | 
93  | 
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
 | 
94  | 
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
 | 
95  | 
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
 | 
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 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
 | 
98  | 
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
 | 
99  | 
{
 | 
| 
 
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  | 
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
 | 
101  | 
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
 | 
102  | 
L.normalize();  | 
| 22 | 103  | 
Float L_dot_N = dot(L, N);  | 
104  | 
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
 | 
105  | 
|
| 42 | 106  | 
Colour col;  | 
107  | 
if (mat.texture)  | 
|
108  | 
col = mat.texture->evaluate(P);  | 
|
109  | 
else  | 
|
110  | 
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
 | 
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  | 
// 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
 | 
113  | 
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
 | 
114  | 
|
| 
 
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  | 
// 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
 | 
116  | 
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
 | 
117  | 
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
 | 
118  | 
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
 | 
119  | 
}  | 
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
120  | 
|
| 
 
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  | 
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
 | 
122  | 
{
 | 
| 22 | 123  | 
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
 | 
124  | 
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
 | 
125  | 
|
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
126  | 
	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
 | 
127  | 
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
 | 
128  | 
	} else {
 | 
| 31 | 129  | 
Colour col = Colour();  | 
| 
15
 
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
 
Radek Brich <radek.brich@devl.cz> 
parents: 
14 
diff
changeset
 | 
130  | 
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
 | 
131  | 
Vector3 normal = nearest_shape->normal(P);  | 
| 31 | 132  | 
bool from_inside = false;  | 
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  | 
// 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
 | 
135  | 
if (dot(normal, ray.dir) > 0.0)  | 
| 31 | 136  | 
		{
 | 
| 
25
 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 
Radek Brich <radek.brich@devl.cz> 
parents: 
22 
diff
changeset
 | 
137  | 
normal = - normal;  | 
| 31 | 138  | 
from_inside = true;  | 
139  | 
}  | 
|
140  | 
||
141  | 
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
 | 
142  | 
|
| 
 
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  | 
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
 | 
144  | 
		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
 | 
145  | 
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
 | 
146  | 
L.normalize();  | 
| 22 | 147  | 
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
 | 
148  | 
			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
 | 
149  | 
// 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
 | 
150  | 
				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
 | 
151  | 
Ray shadow_ray = Ray(P, L);  | 
| 22 | 152  | 
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
 | 
153  | 
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
 | 
154  | 
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
 | 
155  | 
}  | 
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
156  | 
|
| 
 
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  | 
// 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
 | 
158  | 
Vector3 R = L - 2.0 * L_dot_N * normal;  | 
| 31 | 159  | 
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
 | 
160  | 
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
 | 
161  | 
}  | 
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
162  | 
}  | 
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
163  | 
|
| 31 | 164  | 
if (depth < max_depth)  | 
165  | 
		{
 | 
|
166  | 
Colour trans_col, refl_col;  | 
|
167  | 
Float trans = nearest_shape->material->transmissivity;  | 
|
168  | 
Float refl = nearest_shape->material->reflectivity;  | 
|
169  | 
const Float cos_i = - dot(normal, ray.dir);  | 
|
170  | 
||
171  | 
// reflection  | 
|
172  | 
if (refl > 0.01)  | 
|
173  | 
			{
 | 
|
174  | 
Vector3 newdir = ray.dir + 2.0 * cos_i * normal;  | 
|
175  | 
Ray newray = Ray(P, newdir);  | 
|
176  | 
refl_col = raytrace(newray, depth + 1, nearest_shape);  | 
|
177  | 
}  | 
|
178  | 
||
179  | 
// refraction  | 
|
180  | 
if (trans > 0.01)  | 
|
181  | 
			{
 | 
|
182  | 
Float n, n1, n2;  | 
|
183  | 
if (from_inside)  | 
|
184  | 
				{
 | 
|
185  | 
n1 = nearest_shape->material->refract_index;  | 
|
186  | 
n2 = 1.0;  | 
|
187  | 
n = n1;  | 
|
188  | 
}  | 
|
189  | 
else  | 
|
190  | 
				{
 | 
|
191  | 
n1 = 1.0;  | 
|
192  | 
n2 = nearest_shape->material->refract_index;  | 
|
193  | 
n = 1.0 / n2;  | 
|
194  | 
}  | 
|
195  | 
const Float sin2_t = n*n * (1 - cos_i*cos_i);  | 
|
196  | 
if (sin2_t >= 1.0)  | 
|
197  | 
				{
 | 
|
198  | 
// totally reflected  | 
|
199  | 
refl += trans;  | 
|
200  | 
trans = 0;  | 
|
201  | 
}  | 
|
202  | 
else  | 
|
203  | 
				{
 | 
|
204  | 
const Float cos_t = sqrtf(1 - sin2_t);  | 
|
205  | 
const Float Rdiv = 1.0/(n1*cos_i + n2*cos_t);  | 
|
206  | 
const Float Rper = (n1*cos_i - n2*cos_t)*Rdiv;  | 
|
207  | 
const Float Rpar = (n2*cos_i - n1*cos_t)*Rdiv;  | 
|
208  | 
const Float R = (Rper*Rper + Rpar*Rpar)/2;  | 
|
209  | 
refl += R*trans;  | 
|
210  | 
trans = (1-R)*trans;  | 
|
211  | 
Vector3 newdir = n * ray.dir + (n*cos_i - cos_t) * normal;  | 
|
212  | 
Ray newray = Ray(P, newdir);  | 
|
213  | 
trans_col = raytrace(newray, depth + 1, nearest_shape);  | 
|
214  | 
}  | 
|
215  | 
}  | 
|
216  | 
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
 | 
217  | 
}  | 
| 
 
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  | 
|
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
219  | 
// ambient occlusion  | 
| 
40
 
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
 
Radek Brich <radek.brich@devl.cz> 
parents: 
34 
diff
changeset
 | 
220  | 
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
 | 
221  | 
		{
 | 
| 22 | 222  | 
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
 | 
223  | 
			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
 | 
224  | 
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
 | 
225  | 
Ray ao_ray = Ray(P, dir);  | 
| 22 | 226  | 
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
 | 
227  | 
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
 | 
228  | 
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
 | 
229  | 
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
 | 
230  | 
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
 | 
231  | 
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
 | 
232  | 
}  | 
| 22 | 233  | 
Float ao_intensity = miss / ao_samples;  | 
| 31 | 234  | 
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
 | 
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  | 
|
| 31 | 237  | 
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
 | 
238  | 
}  | 
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
239  | 
}  | 
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
240  | 
|
| 
48
 
a4913301c626
begin moving subsampling and oversampling to Sampler
 
Radek Brich <radek.brich@devl.cz> 
parents: 
47 
diff
changeset
 | 
241  | 
#if 0  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
242  | 
static void *renderrow(void *data)  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
243  | 
{
 | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
244  | 
RenderrowData *d = (RenderrowData*) data;  | 
| 
32
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
245  | 
const int subsample = d->rt->getSubsample();  | 
| 
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
246  | 
const Float subsample2 = 1.0/(subsample*subsample);  | 
| 
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
247  | 
const int oversample = d->rt->getOversample();  | 
| 
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
248  | 
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
 | 
249  | 
Vector3 dir = d->dfix;  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
250  | 
	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
 | 
251  | 
// 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
 | 
252  | 
if (subsample > 1)  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
253  | 
		{
 | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
254  | 
Colour ic;  | 
| 
32
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
255  | 
// top-left  | 
| 
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
256  | 
dir.normalize();  | 
| 
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
257  | 
Ray ray(d->eye, dir);  | 
| 
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
258  | 
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
 | 
259  | 
// top-right  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
260  | 
Vector3 tmpdir = dir + (subsample-1)*d->dx;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
261  | 
tmpdir.normalize();  | 
| 
32
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
262  | 
ray.dir = tmpdir;  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
263  | 
Colour c2 = d->rt->raytrace(ray, 0, NULL);  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
264  | 
// bottom right  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
265  | 
tmpdir += (subsample-1)*d->dy;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
266  | 
tmpdir.normalize();  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
267  | 
ray.dir = tmpdir;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
268  | 
Colour c4 = d->rt->raytrace(ray, 0, NULL);  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
269  | 
// bottom left  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
270  | 
tmpdir = dir + (subsample-1)*d->dy;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
271  | 
tmpdir.normalize();  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
272  | 
ray.dir = tmpdir;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
273  | 
Colour c3 = d->rt->raytrace(ray, 0, NULL);  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
274  | 
// are the colors similar?  | 
| 
32
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
275  | 
Float m = (c1-c2).mag2();  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
276  | 
m = max(m, (c2-c3).mag2());  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
277  | 
m = max(m, (c3-c4).mag2());  | 
| 
32
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
278  | 
m = max(m, (c4-c1).mag2());  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
279  | 
if (m < 0.001)  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
280  | 
			{
 | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
281  | 
// interpolate  | 
| 22 | 282  | 
Float *i = d->iter;  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
283  | 
for (int x = 0; x < subsample; x++)  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
284  | 
				{
 | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
285  | 
for (int y = 0; y < subsample; y++)  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
286  | 
					{
 | 
| 
32
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
287  | 
ic = c1*(subsample-x)*(subsample-y)*subsample2  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
288  | 
+ c2*(x)*(subsample-y)*subsample2  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
289  | 
+ c3*(subsample-x)*(y)*subsample2  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
290  | 
+ c4*(x)*(y)*subsample2;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
291  | 
*(i + ww*y) = ic.r;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
292  | 
*(i + ww*y + 1) = ic.g;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
293  | 
*(i + ww*y + 2) = ic.b;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
294  | 
}  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
295  | 
i += 3;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
296  | 
}  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
297  | 
d->iter = i;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
298  | 
}  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
299  | 
else  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
300  | 
			{
 | 
| 
33
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
301  | 
// render all pixels  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
302  | 
Vector3 tmpdir = dir;  | 
| 
33
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
303  | 
|
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
304  | 
if (oversample)  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
305  | 
				{
 | 
| 
33
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
306  | 
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
 | 
307  | 
					{
 | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
308  | 
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
 | 
309  | 
						{
 | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
310  | 
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
 | 
311  | 
samplepixel(ic, tmp2dir, d, oversample);  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
312  | 
*(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
 | 
313  | 
*(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
 | 
314  | 
*(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
 | 
315  | 
}  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
316  | 
d->iter += 3;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
317  | 
tmpdir += d->dx;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
318  | 
}  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
319  | 
}  | 
| 
33
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
320  | 
else  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
321  | 
				{
 | 
| 
33
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
322  | 
/* 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
 | 
323  | 
already computed corner pixels  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
324  | 
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
 | 
325  | 
// first column  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
326  | 
*(d->iter) = c1.r;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
327  | 
*(d->iter + 1) = c1.g;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
328  | 
*(d->iter + 2) = c1.b;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
329  | 
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
 | 
330  | 
					{
 | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
331  | 
Vector3 tmp2dir = tmpdir + y*d->dy;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
332  | 
tmp2dir.normalize();  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
333  | 
ray.dir = tmp2dir;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
334  | 
ic = d->rt->raytrace(ray, 0, NULL);  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
335  | 
*(d->iter + ww*y) = ic.r;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
336  | 
*(d->iter + ww*y + 1) = ic.g;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
337  | 
*(d->iter + ww*y + 2) = ic.b;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
338  | 
}  | 
| 
33
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
339  | 
*(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
 | 
340  | 
*(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
 | 
341  | 
*(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
 | 
342  | 
d->iter += 3;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
343  | 
tmpdir += d->dx;  | 
| 
33
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
344  | 
// middle  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
345  | 
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
 | 
346  | 
					{
 | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
347  | 
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
 | 
348  | 
						{
 | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
349  | 
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
 | 
350  | 
tmp2dir.normalize();  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
351  | 
ray.dir = tmp2dir;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
352  | 
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
 | 
353  | 
*(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
 | 
354  | 
*(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
 | 
355  | 
*(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
 | 
356  | 
}  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
357  | 
d->iter += 3;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
358  | 
tmpdir += d->dx;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
359  | 
}  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
360  | 
// last column  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
361  | 
*(d->iter) = c2.r;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
362  | 
*(d->iter + 1) = c2.g;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
363  | 
*(d->iter + 2) = c2.b;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
364  | 
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
 | 
365  | 
					{
 | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
366  | 
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
 | 
367  | 
tmp2dir.normalize();  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
368  | 
ray.dir = tmp2dir;  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
369  | 
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
 | 
370  | 
*(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
 | 
371  | 
*(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
 | 
372  | 
*(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
 | 
373  | 
}  | 
| 
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
374  | 
*(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
 | 
375  | 
*(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
 | 
376  | 
*(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
 | 
377  | 
d->iter += 3;  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
378  | 
}  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
379  | 
}  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
380  | 
}  | 
| 
32
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
381  | 
else // subsample <= 1  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
382  | 
		{
 | 
| 
32
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
383  | 
Colour c;  | 
| 
33
 
83d0200d4c09
make over-sampling work together with sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
32 
diff
changeset
 | 
384  | 
samplepixel(c, dir, d, oversample);  | 
| 
32
 
8af5c17d368b
new Raytracer option: oversampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
31 
diff
changeset
 | 
385  | 
// write color to buffer  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
386  | 
*d->iter++ = c.r;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
387  | 
*d->iter++ = c.g;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
388  | 
*d->iter++ = c.b;  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
389  | 
}  | 
| 
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
390  | 
dir += d->dx*subsample;  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
391  | 
}  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
392  | 
#ifdef PTHREADS  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
393  | 
pthread_exit((void *)d);  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
394  | 
#endif  | 
| 
6
 
d8d596d26f25
pthreads and other fixes for Windows
 
Radek Brich <radek.brich@devl.cz> 
parents: 
5 
diff
changeset
 | 
395  | 
return (void *)d;  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
396  | 
}  | 
| 
48
 
a4913301c626
begin moving subsampling and oversampling to Sampler
 
Radek Brich <radek.brich@devl.cz> 
parents: 
47 
diff
changeset
 | 
397  | 
#endif  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
398  | 
|
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
399  | 
void *Raytracer::raytrace_worker(void *d)  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
400  | 
{
 | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
401  | 
Raytracer *rt = (Raytracer*)d;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
402  | 
Sample *sample;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
403  | 
Colour col;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
404  | 
Ray ray;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
405  | 
for (;;)  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
406  | 
	{
 | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
407  | 
cout<<"#worker "<<pthread_self()<<" locking queue"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
408  | 
pthread_mutex_lock(&rt->sample_queue_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
409  | 
pthread_cond_signal(&rt->worker_ready_cond);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
410  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
411  | 
// if queue is empty, wait for samples  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
412  | 
		while (rt->sample_queue.empty()) {
 | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
413  | 
if (rt->sample_queue_end)  | 
| 
 
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  | 
cout<<"#worker "<<pthread_self()<<" end of queue, unlocking queue and exiting..."<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
416  | 
pthread_mutex_unlock(&rt->sample_queue_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
417  | 
pthread_exit(NULL);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
418  | 
}  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
419  | 
cout<<"#worker "<<pthread_self()<<" waiting for cond"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
420  | 
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
 | 
421  | 
}  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
422  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
423  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
424  | 
sample = rt->sample_queue.front();  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
425  | 
rt->sample_queue.pop();  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
426  | 
cout<<"#worker "<<pthread_self()<<" unlocking queue"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
427  | 
pthread_mutex_unlock(&rt->sample_queue_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
428  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
429  | 
// do the work  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
430  | 
cout<<"#worker "<<pthread_self()<<" locking sampler (camera)"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
431  | 
pthread_mutex_lock(&rt->sampler_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
432  | 
ray = rt->camera->makeRay(sample);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
433  | 
cout<<"#worker "<<pthread_self()<<" unlocking sampler (camera)"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
434  | 
pthread_mutex_unlock(&rt->sampler_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
435  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
436  | 
cout<<"#worker "<<pthread_self()<<" ray tracing..."<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
437  | 
col = rt->raytrace(ray, 0, NULL);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
438  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
439  | 
// save the result  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
440  | 
cout<<"#worker "<<pthread_self()<<" locking sampler"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
441  | 
pthread_mutex_lock(&rt->sampler_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
442  | 
rt->sampler->saveSample(sample, col);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
443  | 
cout<<"#worker "<<pthread_self()<<" unlocking sampler"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
444  | 
pthread_mutex_unlock(&rt->sampler_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
445  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
446  | 
delete sample;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
447  | 
}  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
448  | 
}  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
449  | 
|
| 46 | 450  | 
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
 | 
451  | 
{
 | 
| 46 | 452  | 
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
 | 
453  | 
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
 | 
454  | 
|
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
455  | 
sample_queue_end = false;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
456  | 
|
| 46 | 457  | 
// create workers  | 
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
458  | 
dbgmsg(1, "* using %d threads\n", num_threads);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
459  | 
pthread_t threads[num_threads];  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
460  | 
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
 | 
461  | 
	{
 | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
462  | 
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
 | 
463  | 
		if (rc) {
 | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
464  | 
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
 | 
465  | 
exit(1);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
466  | 
}  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
467  | 
}  | 
| 46 | 468  | 
|
469  | 
sampler->init();  | 
|
470  | 
int sampnum = 0;  | 
|
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
471  | 
cout<<"locking sampler"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
472  | 
pthread_mutex_lock(&sampler_mutex);  | 
| 46 | 473  | 
while ( (sampnum = sampler->initSampleSet()) > 0 )  | 
474  | 
	{
 | 
|
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
475  | 
Sample *sample;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
476  | 
while ( (sample = sampler->nextSample()) != NULL )  | 
| 46 | 477  | 
		{
 | 
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
478  | 
cout<<"unlocking sampler and locking queue"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
479  | 
pthread_mutex_unlock(&sampler_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
480  | 
pthread_mutex_lock(&sample_queue_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
481  | 
sample_queue.push(sample);  | 
| 46 | 482  | 
|
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
483  | 
if (sample_queue.size() > 1000)  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
484  | 
			{
 | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
485  | 
while (sample_queue.size() > 100)  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
486  | 
			    {
 | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
487  | 
pthread_cond_signal(&sample_queue_cond);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
488  | 
pthread_cond_wait(&worker_ready_cond, &sample_queue_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
489  | 
}  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
490  | 
}  | 
| 46 | 491  | 
|
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
492  | 
cout<<"sending signal and unlocking queue"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
493  | 
pthread_cond_signal(&sample_queue_cond);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
494  | 
pthread_mutex_unlock(&sample_queue_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
495  | 
|
| 
49
 
558fde7da82a
workaround for divide by zero bug in octree
 
Radek Brich <radek.brich@devl.cz> 
parents: 
48 
diff
changeset
 | 
496  | 
sampnum--;  | 
| 
 
558fde7da82a
workaround for divide by zero bug in octree
 
Radek Brich <radek.brich@devl.cz> 
parents: 
48 
diff
changeset
 | 
497  | 
if ((sampnum % 1000) == 0)  | 
| 
 
558fde7da82a
workaround for divide by zero bug in octree
 
Radek Brich <radek.brich@devl.cz> 
parents: 
48 
diff
changeset
 | 
498  | 
dbgmsg(2, "\b\b\b\b\b\b\b\b%8d", sampnum);  | 
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
499  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
500  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
501  | 
cout<<"locking sampler"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
502  | 
pthread_mutex_lock(&sampler_mutex);  | 
| 46 | 503  | 
}  | 
| 
49
 
558fde7da82a
workaround for divide by zero bug in octree
 
Radek Brich <radek.brich@devl.cz> 
parents: 
48 
diff
changeset
 | 
504  | 
dbgmsg(2, "\n");  | 
| 46 | 505  | 
}  | 
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
506  | 
cout<<"unlocking sampler"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
507  | 
pthread_mutex_unlock(&sampler_mutex);  | 
| 46 | 508  | 
|
509  | 
// wait for workers  | 
|
| 
50
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
510  | 
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
 | 
511  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
512  | 
cout<<"locking queue"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
513  | 
pthread_mutex_lock(&sample_queue_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
514  | 
sample_queue_end = true;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
515  | 
while (!sample_queue.empty())  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
516  | 
	{
 | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
517  | 
cout<<"broadcasting signal and unlocking queue"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
518  | 
pthread_cond_broadcast(&sample_queue_cond);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
519  | 
pthread_mutex_unlock(&sample_queue_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
520  | 
cout<<"locking queue"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
521  | 
pthread_mutex_lock(&sample_queue_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
522  | 
}  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
523  | 
cout<<"broadcasting signal and unlocking queue"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
524  | 
pthread_cond_broadcast(&sample_queue_cond);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
525  | 
pthread_mutex_unlock(&sample_queue_mutex);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
526  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
527  | 
cout<<"joining threads"<<endl;  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
528  | 
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
 | 
529  | 
pthread_join(threads[t], NULL);  | 
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
530  | 
|
| 
 
14a727b70d07
rewritten threads with heavy debug information
 
Radek Brich <radek.brich@devl.cz> 
parents: 
49 
diff
changeset
 | 
531  | 
cout<<"done!"<<endl;  | 
| 46 | 532  | 
|
533  | 
#if 0  | 
|
534  | 
||
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
535  | 
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
 | 
536  | 
|
| 22 | 537  | 
Float S = 0.5/w;  | 
| 
19
 
4e0955fca797
added Camera, currently w/o Python binding
 
Radek Brich <radek.brich@devl.cz> 
parents: 
16 
diff
changeset
 | 
538  | 
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
 | 
539  | 
+ 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
 | 
540  | 
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
 | 
541  | 
Vector3 dy = camera->v * (-S/camera->f);  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
542  | 
|
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
543  | 
#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
 | 
544  | 
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
 | 
545  | 
pthread_t threads[num_threads];  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
546  | 
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
 | 
547  | 
threads[t] = pthread_self();  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
548  | 
int t = 0;  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
549  | 
#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
 | 
550  | 
|
| 
15
 
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
 
Radek Brich <radek.brich@devl.cz> 
parents: 
14 
diff
changeset
 | 
551  | 
/* 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
 | 
552  | 
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
 | 
553  | 
dbgmsg(2, "- row 0 ( 0%% done)");  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
554  | 
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
 | 
555  | 
	{
 | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
556  | 
d = (RenderrowData*) malloc(sizeof(RenderrowData));  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
557  | 
d->rt = this;  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
558  | 
d->w = w;  | 
| 
19
 
4e0955fca797
added Camera, currently w/o Python binding
 
Radek Brich <radek.brich@devl.cz> 
parents: 
16 
diff
changeset
 | 
559  | 
d->eye = camera->eye;  | 
| 
 
4e0955fca797
added Camera, currently w/o Python binding
 
Radek Brich <radek.brich@devl.cz> 
parents: 
16 
diff
changeset
 | 
560  | 
d->dfix = dfix;  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
561  | 
d->dx = dx;  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
562  | 
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
 | 
563  | 
d->iter = buffer + y*3*w;  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
564  | 
#ifdef PTHREADS  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
565  | 
/* create new thread and increase 't' */  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
566  | 
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
 | 
567  | 
		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
 | 
568  | 
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
 | 
569  | 
exit(1);  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
570  | 
}  | 
| 46 | 571  | 
/* when 't' overflows, reset it */  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
572  | 
if (t >= num_threads)  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
573  | 
t = 0;  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
574  | 
/* 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
 | 
575  | 
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
 | 
576  | 
if (!pthread_equal(threads[t], pthread_self()))  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
577  | 
if (pthread_join(threads[t], (void**)&d) == 0)  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
578  | 
free(d);  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
579  | 
#else  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
580  | 
renderrow((void *)d);  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
581  | 
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
 | 
582  | 
#endif  | 
| 
21
 
79b516a3803d
naive color driven sub-sampling
 
Radek Brich <radek.brich@devl.cz> 
parents: 
20 
diff
changeset
 | 
583  | 
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
 | 
584  | 
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
 | 
585  | 
}  | 
| 
30
 
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
 
Radek Brich <radek.brich@devl.cz> 
parents: 
25 
diff
changeset
 | 
586  | 
dbgmsg(2, "\n");  | 
| 
4
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
587  | 
|
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
588  | 
#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
 | 
589  | 
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
 | 
590  | 
for (t = 0; t < num_threads; t++)  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
591  | 
if (pthread_join(threads[t], (void**)&d) == 0)  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
592  | 
free(d);  | 
| 
 
c73bc405ee7a
multi-threaded rendering via pthreads
 
Radek Brich <radek.brich@devl.cz> 
parents: 
0 
diff
changeset
 | 
593  | 
#endif  | 
| 46 | 594  | 
|
595  | 
#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
 | 
596  | 
}  | 
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
597  | 
|
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
598  | 
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
 | 
599  | 
{
 | 
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
600  | 
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
 | 
601  | 
}  | 
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
602  | 
|
| 22 | 603  | 
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
 | 
604  | 
{
 | 
| 
 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 
Radek Brich <radek.brich@devl.cz> 
parents:  
diff
changeset
 | 
605  | 
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
 | 
606  | 
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
 | 
607  | 
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
 | 
608  | 
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
 | 
609  | 
/* 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
 | 
610  | 
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
 | 
611  | 
}  |