author | Radek Brich <radek.brich@devl.cz> |
Mon, 28 Apr 2008 11:44:11 +0200 | |
branch | pyrit |
changeset 88 | f7edb3b90816 |
parent 87 | 1081e3dd3f3e |
child 90 | f6a72eb99631 |
permissions | -rw-r--r-- |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
/* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
2 |
* sampler.h: screen sample generation and image reconstruction |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
* This file is part of Pyrit Ray Tracer. |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
* Copyright 2008 Radek Brich |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
* of this software and associated documentation files (the "Software"), to deal |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
* in the Software without restriction, including without limitation the rights |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
* copies of the Software, and to permit persons to whom the Software is |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
* furnished to do so, subject to the following conditions: |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
* The above copyright notice and this permission notice shall be included in |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
* all copies or substantial portions of the Software. |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
* THE SOFTWARE. |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
*/ |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
#ifndef SAMPLER_H |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
#define SAMPLER_H |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
#include "common.h" |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
#include "vector.h" |
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
32 |
#include "pixmap.h" |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
using namespace std; |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
/** |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
* sample |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
*/ |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
class Sample |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
{ |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
41 |
friend class Sampler; |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
public: |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
Float x,y; |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
44 |
int sx,sy,osa_samp; |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
}; |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
47 |
/** |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
* A abstract sampler. |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
* It generates screen samples in coordinates between [-1..1] for height |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
* and [-w/h..w/h] for width. It works in phases: initSampleSet returns |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
* number of samples for each phase, then samples can be generated using |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
* nextSample method. The resulting colour of each sample should be returned |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
* via saveSample method. The sampler should save the results to given buffer |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
* and decide if other phase is needed. When the picture is complete, |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
* initSampleSet returns zero and picture can be read from buffer. |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
*/ |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
class Sampler |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
{ |
82
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
59 |
protected: |
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
60 |
|
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
61 |
Pixmap pixmap; |
82
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
62 |
bool packetable; |
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
63 |
public: |
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
64 |
Sampler(Float *buffer, const int &w, const int &h): |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
65 |
pixmap(buffer, w, h), packetable(false) {}; |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
66 |
Sampler(const int &w, const int &h): |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
67 |
pixmap(w, h), packetable(false) {}; |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
virtual ~Sampler() {}; |
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
69 |
void resetBuffer(Float *buffer, int &w, int &h) |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
70 |
{ pixmap.setData(buffer, w, h); }; |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
71 |
virtual void init() = 0; |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
72 |
virtual int initSampleSet() = 0; |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
73 |
virtual bool nextSample(Sample *s) = 0; |
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
74 |
virtual void saveSample(Sample &samp, Colour &col) = 0; |
82
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
75 |
bool packetableSamples() { return packetable; }; |
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
76 |
const Pixmap &getPixmap() { return pixmap; }; |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
}; |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
/** |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
80 |
* Default sampler. |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
81 |
* Implements basic adaptive subsampling and oversampling. |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
82 |
*/ |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
83 |
class DefaultSampler: public Sampler |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
84 |
{ |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
85 |
int phase; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
86 |
int subsample; // 0,1 = no, 1+ = size of sampling grid |
87
1081e3dd3f3e
Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents:
82
diff
changeset
|
87 |
int oversample; // 0 = no, 1 = 4x, 2 = 9x, 3 = 16x |
50
14a727b70d07
rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
88 |
int sx,sy,osa_samp; // current sample properties |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
89 |
public: |
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
90 |
DefaultSampler(Float *buffer, const int &w, const int &h): |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
91 |
Sampler(buffer, w, h), phase(-1), subsample(0), oversample(0) {}; |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
92 |
DefaultSampler(const int &w, const int &h): |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
93 |
Sampler(w, h), phase(-1), subsample(0), oversample(0) {}; |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
94 |
void init(); |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
95 |
int initSampleSet(); |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
96 |
bool nextSample(Sample *s); |
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
97 |
void saveSample(Sample &samp, Colour &col); |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
98 |
|
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
99 |
void setSubsample(int sub) { subsample = sub; }; |
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
100 |
int getSubsample() { return subsample; }; |
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
101 |
void setOversample(int osa) { oversample = osa; }; |
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
102 |
int getOversample() { return oversample; }; |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
103 |
}; |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
104 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
105 |
#endif |