author | Radek Brich <radek.brich@devl.cz> |
Sun, 31 May 2009 23:06:03 +0200 | |
branch | pyrit |
changeset 100 | c005054bf4c1 |
parent 98 | 64638385798a |
permissions | -rw-r--r-- |
94 | 1 |
/** |
2 |
* @file raytracer.h |
|
3 |
* @brief The base class of the ray tracer |
|
44 | 4 |
* |
5 |
* This file is part of Pyrit Ray Tracer. |
|
6 |
* |
|
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
7 |
* 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
|
8 |
* |
44 | 9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10 |
* of this software and associated documentation files (the "Software"), to deal |
|
11 |
* in the Software without restriction, including without limitation the rights |
|
12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13 |
* copies of the Software, and to permit persons to whom the Software is |
|
14 |
* furnished to do so, subject to the following conditions: |
|
15 |
* |
|
16 |
* The above copyright notice and this permission notice shall be included in |
|
17 |
* all copies or substantial portions of the Software. |
|
18 |
* |
|
19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
25 |
* 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
|
26 |
*/ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
|
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 |
#ifndef 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
|
29 |
#define 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
|
30 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
#include <vector> |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
32 |
#include <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
|
33 |
|
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
34 |
#include "common.h" |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
35 |
#include "container.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
|
36 |
#include "scene.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
|
37 |
|
4
c73bc405ee7a
multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
38 |
class Raytracer; |
46 | 39 |
|
40 |
/** |
|
41 |
* main ray tracer class |
|
42 |
*/ |
|
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
|
43 |
class Raytracer |
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 |
{ |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
45 |
Container *top; /**< container with shapes */ |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
46 |
Sampler *sampler; /**< active sampler */ |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
47 |
Camera *camera; /**< active camera */ |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
48 |
vector<Light*> lights; /**< array of lights in the scene */ |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
49 |
Colour bg_colour; /**< background colour */ |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
50 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
51 |
/* ambient occlussion parameters */ |
93 | 52 |
Float ao_distance, ao_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
|
53 |
int ao_samples; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
55 |
int num_threads; /**< number of threads to use for rendering */ |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
56 |
int max_depth; /**< maximum depth of recursion */ |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
57 |
bool use_packets; /**< allow ray packet tracing */ |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
58 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
59 |
/* private helper variables */ |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
60 |
Sample *sample_queue; |
54
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
61 |
int sample_queue_pos, sample_queue_size, sample_queue_count; |
dbe3c7a4e0f0
more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
62 |
bool end_of_samples; |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
63 |
pthread_mutex_t sample_queue_mutex, sampler_mutex; |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
64 |
pthread_cond_t sample_queue_cond, worker_ready_cond; |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
65 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
66 |
/** |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
67 |
* Hammersley spherical point distribution function |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
68 |
* http://www.cse.cuhk.edu.hk/~ttwong/papers/udpoint/udpoints.html |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
69 |
* @param[in] i sample index |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
70 |
* @param[in] n number of samples |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
71 |
* @param[in] extent angle of dispersion |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
72 |
* @param[in] normal central direction vector |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
73 |
*/ |
91 | 74 |
Vector SphereDistribute(int i, int n, Float extent, const Vector &normal); |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
75 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
76 |
/** |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
77 |
* shader implementing Phong lighting model |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
78 |
* @param[in] P point of intersection |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
79 |
* @param[in] N normal in intersect. point |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
80 |
* @param[in] R direction of reflected ray |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
81 |
* @param[in] V direction to the viewer |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
82 |
* @return colour of the surface |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
83 |
*/ |
91 | 84 |
Colour PhongShader(const Shape *shape, |
85 |
const Vector &P, const Vector &N, const Vector &V); |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
86 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
87 |
/** light scattering function */ |
91 | 88 |
void lightScatter(const Ray &ray, const Shape *shape, int depth, |
89 |
const Vector &P, const Vector &normal, bool from_inside, Colour &col); |
|
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
90 |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
91 |
#ifndef NO_SIMD |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
92 |
VectorPacket PhongShader_packet(const Shape* const* shapes, |
91 | 93 |
const VectorPacket &P, const VectorPacket &N, const VectorPacket &V); |
84
6f7fe14782c2
prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents:
82
diff
changeset
|
94 |
void raytracePacket(RayPacket &rays, Colour *results); |
91 | 95 |
#endif |
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
96 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
97 |
/** main function of the ray tracing worker */ |
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
98 |
NORETURN static void *raytrace_worker(void *d); |
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
99 |
|
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
|
100 |
public: |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
54
diff
changeset
|
101 |
Raytracer(): top(NULL), camera(NULL), lights(), bg_colour(0., 0., 0.), |
94 | 102 |
ao_samples(0), num_threads(4), max_depth(3), use_packets(true) |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
103 |
{ |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
104 |
pthread_mutex_init(&sample_queue_mutex, NULL); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
105 |
pthread_mutex_init(&sampler_mutex, NULL); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
106 |
pthread_cond_init (&sample_queue_cond, NULL); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
107 |
pthread_cond_init (&worker_ready_cond, NULL); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
108 |
}; |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
109 |
~Raytracer() |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
110 |
{ |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
111 |
pthread_mutex_destroy(&sample_queue_mutex); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
112 |
pthread_mutex_destroy(&sampler_mutex); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
113 |
pthread_cond_destroy (&sample_queue_cond); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
114 |
pthread_cond_destroy (&worker_ready_cond); |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
115 |
} |
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
116 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
117 |
/** start the rendering process */ |
46 | 118 |
void render(); |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
119 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
120 |
/** ray trace one ray */ |
91 | 121 |
Colour raytrace(Ray &ray, int depth, const Shape *origin_shape); |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
122 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
123 |
/** add shape to container */ |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
54
diff
changeset
|
124 |
void addShape(Shape *shape) { top->addShape(shape); }; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
125 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
126 |
/** add light to scene */ |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
54
diff
changeset
|
127 |
void addLight(Light *light) { lights.push_back(light); }; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
128 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
129 |
/** set active sampler */ |
46 | 130 |
void setSampler(Sampler *sampl) { sampler = sampl; }; |
90
f6a72eb99631
rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents:
84
diff
changeset
|
131 |
Sampler *&getSampler() { return sampler; }; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
132 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
133 |
/** set active camera */ |
19
4e0955fca797
added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
134 |
void setCamera(Camera *cam) { camera = cam; }; |
90
f6a72eb99631
rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents:
84
diff
changeset
|
135 |
Camera *&getCamera() { return camera; }; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
136 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
137 |
/** set active container */ |
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
138 |
void setTop(Container *atop) { top = atop; }; |
90
f6a72eb99631
rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents:
84
diff
changeset
|
139 |
Container *&getTop() { return top; }; |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
140 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
141 |
/** set background colour */ |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
54
diff
changeset
|
142 |
void setBgColour(const Colour &bg) { bg_colour = bg; }; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
143 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
144 |
/** set maximum depth of recursion */ |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
145 |
void setMaxDepth(int newdepth) { max_depth = newdepth; }; |
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 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
147 |
/** set ambient occlusion parameters */ |
75
20dee9819b17
unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
148 |
void ambientOcclusion(int samples, Float distance, Float angle); |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
149 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
150 |
/** set number of threads to use for rendering */ |
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
151 |
void setThreads(int num) { num_threads = num; }; |
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
|
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 |
#endif |