53 Vector3 o, dir; |
53 Vector3 o, dir; |
54 Ray(const Vector3 &ao, const Vector3 &adir): |
54 Ray(const Vector3 &ao, const Vector3 &adir): |
55 o(ao), dir(adir) {}; |
55 o(ao), dir(adir) {}; |
56 }; |
56 }; |
57 |
57 |
|
58 /** |
|
59 * sample |
|
60 */ |
|
61 class Sample |
|
62 { |
|
63 public: |
|
64 Float x,y; |
|
65 }; |
|
66 |
|
67 /** |
|
68 * A abstract sampler. |
|
69 * It generates screen samples in coordinates between [-1..1] for height |
|
70 * and [-w/h..w/h] for width. It works in phases: initSampleSet returns |
|
71 * number of samples for each phase, then samples can be generated using |
|
72 * nextSample method. The resulting colour of each sample should be returned |
|
73 * via saveSample method. The sampler should save the results to given buffer |
|
74 * and decide if other phase is needed. When the picture is complete, |
|
75 * initSampleSet returns zero and picture can be read from buffer. |
|
76 */ |
|
77 class Sampler |
|
78 { |
|
79 public: |
|
80 Float *buffer; |
|
81 int w,h; |
|
82 |
|
83 Sampler(Float *abuffer, int &aw, int &ah): buffer(abuffer), w(aw), h(ah) {}; |
|
84 void resetBuffer(Float *abuffer, int &aw, int &ah) { buffer = abuffer; w = aw; h = ah; }; |
|
85 virtual void init() = 0; |
|
86 virtual int initSampleSet() = 0; |
|
87 virtual Sample *nextSample(Sample *prev) = 0; |
|
88 virtual void saveSample(Sample *samp, Colour &col) = 0; |
|
89 }; |
|
90 |
|
91 /** |
|
92 * default sample |
|
93 */ |
|
94 class DefaultSample: public Sample |
|
95 { |
|
96 friend class DefaultSampler; |
|
97 int sx,sy; |
|
98 }; |
|
99 |
|
100 /** |
|
101 * Default sampler. |
|
102 */ |
|
103 class DefaultSampler: public Sampler |
|
104 { |
|
105 int phase; |
|
106 public: |
|
107 DefaultSampler(Float *abuffer, int &aw, int &ah): Sampler(abuffer, aw, ah), phase(-1) {}; |
|
108 void init() { phase = 0; }; |
|
109 int initSampleSet(); |
|
110 Sample *nextSample(Sample *prev); |
|
111 void saveSample(Sample *samp, Colour &col); |
|
112 }; |
|
113 |
|
114 /** |
|
115 * a camera |
|
116 */ |
58 class Camera |
117 class Camera |
59 { |
118 { |
60 public: |
119 public: |
61 Vector3 eye, p, u, v; |
120 Vector3 eye, p, u, v; |
62 Float f; |
121 Float f; |
66 eye(C), p(ap), u(au), v(av), f(3.14/4.0) {}; |
125 eye(C), p(ap), u(au), v(av), f(3.14/4.0) {}; |
67 void setEye(const Vector3 &aeye) { eye = aeye; }; |
126 void setEye(const Vector3 &aeye) { eye = aeye; }; |
68 void setFocalLength(const Float af) { f = af; }; |
127 void setFocalLength(const Float af) { f = af; }; |
69 void rotate(const Quaternion &q); |
128 void rotate(const Quaternion &q); |
70 void move(const Float fw, const Float left, const Float up); |
129 void move(const Float fw, const Float left, const Float up); |
71 }; |
130 |
72 |
131 Ray makeRay(Sample *samp); |
73 /* axis-aligned bounding box */ |
132 }; |
|
133 |
|
134 /** |
|
135 * axis-aligned bounding box |
|
136 */ |
74 class BBox |
137 class BBox |
75 { |
138 { |
76 public: |
139 public: |
77 Vector3 L; |
140 Vector3 L; |
78 Vector3 H; |
141 Vector3 H; |
96 Light(const Vector3 &position, const Colour &acolour): |
162 Light(const Vector3 &position, const Colour &acolour): |
97 pos(position), colour(acolour), cast_shadows(true) {}; |
163 pos(position), colour(acolour), cast_shadows(true) {}; |
98 void castShadows(bool cast) { cast_shadows = cast; }; |
164 void castShadows(bool cast) { cast_shadows = cast; }; |
99 }; |
165 }; |
100 |
166 |
|
167 /** |
|
168 * texture |
|
169 */ |
101 class Texture |
170 class Texture |
102 { |
171 { |
103 public: |
172 public: |
104 virtual Colour evaluate(Vector3 point) = 0; |
173 virtual Colour evaluate(Vector3 point) = 0; |
105 }; |
174 }; |
106 |
175 |
|
176 /** |
|
177 * material |
|
178 */ |
107 class Material |
179 class Material |
108 { |
180 { |
109 public: |
181 public: |
110 Colour colour; |
182 Colour colour; |
111 Float ambient, diffuse, specular, shininess; // Phong constants |
183 Float ambient, diffuse, specular, shininess; // Phong constants |
192 bool intersect_bbox(const BBox &bbox) const; |
276 bool intersect_bbox(const BBox &bbox) const; |
193 const Vector3 normal(const Vector3 &P) const; |
277 const Vector3 normal(const Vector3 &P) const; |
194 BBox get_bbox() const { return BBox(L, H); }; |
278 BBox get_bbox() const { return BBox(L, H); }; |
195 }; |
279 }; |
196 |
280 |
|
281 /** |
|
282 * triangle vertex |
|
283 */ |
197 class Vertex |
284 class Vertex |
198 { |
285 { |
199 public: |
286 public: |
200 Vector3 P; |
287 Vector3 P; |
201 Vertex(const Vector3 &aP): P(aP) {}; |
288 Vertex(const Vector3 &aP): P(aP) {}; |
202 }; |
289 }; |
203 |
290 |
|
291 /** |
|
292 * triangle vertex with normal |
|
293 */ |
204 class NormalVertex: public Vertex |
294 class NormalVertex: public Vertex |
205 { |
295 { |
206 public: |
296 public: |
207 Vector3 N; |
297 Vector3 N; |
208 NormalVertex(const Vector3 &aP): Vertex(aP) {}; |
298 NormalVertex(const Vector3 &aP): Vertex(aP) {}; |
209 NormalVertex(const Vector3 &aP, const Vector3 &aN): Vertex(aP), N(aN) {}; |
299 NormalVertex(const Vector3 &aP, const Vector3 &aN): Vertex(aP), N(aN) {}; |
210 const Vector3 &getNormal() { return N; }; |
300 const Vector3 &getNormal() { return N; }; |
211 void setNormal(const Vector3 &aN) { N = aN; }; |
301 void setNormal(const Vector3 &aN) { N = aN; }; |
212 }; |
302 }; |
213 |
303 |
214 |
304 /** |
|
305 * triangle shape |
|
306 */ |
215 class Triangle: public Shape |
307 class Triangle: public Shape |
216 { |
308 { |
217 #ifdef TRI_BARI_PRE |
309 #ifdef TRI_BARI_PRE |
218 Float nu, nv, nd; |
310 Float nu, nv, nd; |
219 int k; // dominant axis |
311 int k; // dominant axis |