162 void setTransmissivity(const Float trans, const Float rindex) |
153 void setTransmissivity(const Float trans, const Float rindex) |
163 { transmissivity = trans; refract_index = rindex; }; |
154 { transmissivity = trans; refract_index = rindex; }; |
164 void setSmooth(bool sm) { smooth = sm; }; |
155 void setSmooth(bool sm) { smooth = sm; }; |
165 }; |
156 }; |
166 |
157 |
167 /** |
|
168 * shape |
|
169 */ |
|
170 class Shape |
|
171 { |
|
172 public: |
|
173 Material *material; |
|
174 Shape() {}; |
|
175 virtual ~Shape() {}; |
|
176 |
|
177 // first intersection point |
|
178 virtual bool intersect(const Ray &ray, Float &dist) const = 0; |
|
179 |
|
180 // all intersections (only for CSG) |
|
181 virtual bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const = 0; |
|
182 |
|
183 // intersection with AABB |
|
184 virtual bool intersect_bbox(const BBox &bbox) const = 0; |
|
185 |
|
186 // normal at point P |
|
187 virtual const Vector3 normal(const Vector3 &P) const = 0; |
|
188 |
|
189 virtual BBox get_bbox() const = 0; |
|
190 }; |
|
191 |
|
192 /** |
|
193 * list of shapes |
|
194 */ |
|
195 class ShapeList: public vector<Shape*> |
|
196 { |
|
197 }; |
|
198 |
|
199 /** |
|
200 * sphere shape |
|
201 */ |
|
202 class Sphere: public Shape |
|
203 { |
|
204 Float sqr_radius; |
|
205 Float inv_radius; |
|
206 public: |
|
207 Vector3 center; |
|
208 Float radius; |
|
209 |
|
210 Sphere(const Vector3 &acenter, const Float aradius, Material *amaterial): |
|
211 sqr_radius(aradius*aradius), inv_radius(1.0f/aradius), |
|
212 center(acenter), radius(aradius) { material = amaterial; } |
|
213 bool intersect(const Ray &ray, Float &dist) const; |
|
214 bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const; |
|
215 bool intersect_bbox(const BBox &bbox) const; |
|
216 const Vector3 normal(const Vector3 &P) const { return (P - center) * inv_radius; }; |
|
217 BBox get_bbox() const; |
|
218 }; |
|
219 |
|
220 /** |
|
221 * box shape |
|
222 */ |
|
223 class Box: public Shape |
|
224 { |
|
225 Vector3 L; |
|
226 Vector3 H; |
|
227 public: |
|
228 Box(const Vector3 &aL, const Vector3 &aH, Material *amaterial): L(aL), H(aH) |
|
229 { |
|
230 for (int i = 0; i < 3; i++) |
|
231 if (L.cell[i] > H.cell[i]) |
|
232 swap(L.cell[i], H.cell[i]); |
|
233 material = amaterial; |
|
234 }; |
|
235 bool intersect(const Ray &ray, Float &dist) const; |
|
236 bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const { return false; }; |
|
237 bool intersect_bbox(const BBox &bbox) const; |
|
238 const Vector3 normal(const Vector3 &P) const; |
|
239 BBox get_bbox() const { return BBox(L, H); }; |
|
240 }; |
|
241 |
|
242 /** |
|
243 * triangle vertex |
|
244 */ |
|
245 class Vertex |
|
246 { |
|
247 public: |
|
248 Vector3 P; |
|
249 Vertex(const Vector3 &aP): P(aP) {}; |
|
250 }; |
|
251 |
|
252 /** |
|
253 * triangle vertex with normal |
|
254 */ |
|
255 class NormalVertex: public Vertex |
|
256 { |
|
257 public: |
|
258 Vector3 N; |
|
259 NormalVertex(const NormalVertex *v): Vertex(v->P), N(v->N) {}; |
|
260 NormalVertex(const Vector3 &aP): Vertex(aP) {}; |
|
261 NormalVertex(const Vector3 &aP, const Vector3 &aN): Vertex(aP), N(aN) {}; |
|
262 const Vector3 &getNormal() { return N; }; |
|
263 void setNormal(const Vector3 &aN) { N = aN; }; |
|
264 }; |
|
265 |
|
266 /** |
|
267 * triangle shape |
|
268 */ |
|
269 class Triangle: public Shape |
|
270 { |
|
271 #ifdef TRI_BARI_PRE |
|
272 Float nu, nv, nd; |
|
273 int k; // dominant axis |
|
274 Float bnu, bnv; |
|
275 Float cnu, cnv; |
|
276 #endif |
158 #endif |
277 #ifdef TRI_BARI |
|
278 int k; // dominant axis |
|
279 #endif |
|
280 #ifdef TRI_PLUCKER |
|
281 Float pla[6], plb[6], plc[6]; |
|
282 #endif |
|
283 Vector3 N; |
|
284 const Vector3 smooth_normal(const Vector3 &P) const |
|
285 { |
|
286 #ifdef TRI_BARI_PRE |
|
287 const Vector3 &NA = static_cast<NormalVertex*>(A)->N; |
|
288 const Vector3 &NB = static_cast<NormalVertex*>(B)->N; |
|
289 const Vector3 &NC = static_cast<NormalVertex*>(C)->N; |
|
290 static const int modulo3[5] = {0,1,2,0,1}; |
|
291 register const int ku = modulo3[k+1]; |
|
292 register const int kv = modulo3[k+2]; |
|
293 const Float pu = P[ku] - A->P[ku]; |
|
294 const Float pv = P[kv] - A->P[kv]; |
|
295 const Float u = pv * bnu + pu * bnv; |
|
296 const Float v = pu * cnv + pv * cnu; |
|
297 Vector3 n = NA + u * (NB - NA) + v * (NC - NA); |
|
298 n.normalize(); |
|
299 return n; |
|
300 #else |
|
301 return N; // not implemented for other algorithms |
|
302 #endif |
|
303 }; |
|
304 public: |
|
305 Vertex *A, *B, *C; |
|
306 |
|
307 Triangle() {}; |
|
308 Triangle(Vertex *aA, Vertex *aB, Vertex *aC, Material *amaterial); |
|
309 bool intersect(const Ray &ray, Float &dist) const; |
|
310 bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const {return false;}; |
|
311 bool intersect_bbox(const BBox &bbox) const; |
|
312 const Vector3 normal(const Vector3 &P) const { return (material->smooth ? smooth_normal(P) : N); }; |
|
313 const Vector3 getNormal() const { return N; }; |
|
314 BBox get_bbox() const; |
|
315 }; |
|
316 |
|
317 template <class T> class Array |
|
318 { |
|
319 T *array; |
|
320 public: |
|
321 Array(int n) { array = new T[n]; }; |
|
322 ~Array() { delete[] array; }; |
|
323 const T &operator[](int i) const { return array[i]; }; |
|
324 }; |
|
325 |
|
326 typedef Array<Vertex> VertexArray; |
|
327 typedef Array<NormalVertex> NormalVertexArray; |
|
328 typedef Array<Triangle> TriangleArray; |
|
329 |
|
330 #endif |
|