154 |
155 |
155 // all intersections (only for CSG) |
156 // all intersections (only for CSG) |
156 virtual bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const = 0; |
157 virtual bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const = 0; |
157 |
158 |
158 // normal at point P |
159 // normal at point P |
159 virtual Vector3 normal(Vector3 &P) const = 0; |
160 virtual const Vector3 normal(const Vector3 &P) const = 0; |
160 |
161 |
161 virtual BBox get_bbox() const = 0; |
162 virtual BBox get_bbox() const = 0; |
162 }; |
163 }; |
163 |
164 |
164 class ShapeList: public vector<Shape*> |
165 class ShapeList: public vector<Shape*> |
176 Sphere(const Vector3 &acenter, const Float aradius, Material *amaterial): |
177 Sphere(const Vector3 &acenter, const Float aradius, Material *amaterial): |
177 sqr_radius(aradius*aradius), inv_radius(1.0f/aradius), |
178 sqr_radius(aradius*aradius), inv_radius(1.0f/aradius), |
178 center(acenter), radius(aradius) { material = amaterial; } |
179 center(acenter), radius(aradius) { material = amaterial; } |
179 bool intersect(const Ray &ray, Float &dist) const; |
180 bool intersect(const Ray &ray, Float &dist) const; |
180 bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const; |
181 bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const; |
181 Vector3 normal(Vector3 &P) const { return (P - center) * inv_radius; }; |
182 const Vector3 normal(const Vector3 &P) const { return (P - center) * inv_radius; }; |
182 BBox get_bbox() const; |
183 BBox get_bbox() const; |
183 }; |
184 }; |
184 |
185 |
185 class Box: public Shape |
186 class Box: public Shape |
186 { |
187 { |
194 swap(L.cell[i], H.cell[i]); |
195 swap(L.cell[i], H.cell[i]); |
195 material = amaterial; |
196 material = amaterial; |
196 }; |
197 }; |
197 bool intersect(const Ray &ray, Float &dist) const; |
198 bool intersect(const Ray &ray, Float &dist) const; |
198 bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const { return false; }; |
199 bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const { return false; }; |
199 Vector3 normal(Vector3 &P) const; |
200 const Vector3 normal(const Vector3 &P) const; |
200 BBox get_bbox() const { return BBox(L, H); }; |
201 BBox get_bbox() const { return BBox(L, H); }; |
201 }; |
202 }; |
|
203 |
|
204 class Vertex |
|
205 { |
|
206 public: |
|
207 Vector3 P; |
|
208 Vertex(const Vector3 &aP): P(aP) {}; |
|
209 }; |
|
210 |
|
211 class NormalVertex: public Vertex |
|
212 { |
|
213 public: |
|
214 Vector3 N; |
|
215 NormalVertex(const Vector3 &aP): Vertex(aP) {}; |
|
216 NormalVertex(const Vector3 &aP, const Vector3 &aN): Vertex(aP), N(aN) {}; |
|
217 const Vector3 &getNormal() { return N; }; |
|
218 void setNormal(const Vector3 &aN) { N = aN; }; |
|
219 }; |
|
220 |
202 |
221 |
203 class Triangle: public Shape |
222 class Triangle: public Shape |
204 { |
223 { |
205 #ifdef TRI_BARI_PRE |
224 #ifdef TRI_BARI_PRE |
206 Float nu, nv, nd; |
225 Float nu, nv, nd; |
212 int k; // dominant axis |
231 int k; // dominant axis |
213 #endif |
232 #endif |
214 #ifdef TRI_PLUCKER |
233 #ifdef TRI_PLUCKER |
215 Float pla[6], plb[6], plc[6]; |
234 Float pla[6], plb[6], plc[6]; |
216 #endif |
235 #endif |
217 public: |
236 Vector3 N; |
218 Vector3 A, B, C, N; |
237 bool smooth; |
219 |
238 const Vector3 smooth_normal(const Vector3 &P) const |
220 Triangle(const Vector3 &aA, const Vector3 &aB, const Vector3 &aC, Material *amaterial); |
239 { |
|
240 #ifdef TRI_BARI_PRE |
|
241 const Vector3 &NA = static_cast<NormalVertex*>(A)->N; |
|
242 const Vector3 &NB = static_cast<NormalVertex*>(B)->N; |
|
243 const Vector3 &NC = static_cast<NormalVertex*>(C)->N; |
|
244 static const int modulo3[5] = {0,1,2,0,1}; |
|
245 register const int ku = modulo3[k+1]; |
|
246 register const int kv = modulo3[k+2]; |
|
247 const Float pu = P[ku] - A->P[ku]; |
|
248 const Float pv = P[kv] - A->P[kv]; |
|
249 const Float u = pv * bnu + pu * bnv; |
|
250 const Float v = pu * cnv + pv * cnu; |
|
251 Vector3 n = NA + u * (NB - NA) + v * (NC - NA); |
|
252 n.normalize(); |
|
253 return n; |
|
254 #else |
|
255 return N; // not implemented for other algorithms |
|
256 #endif |
|
257 }; |
|
258 public: |
|
259 Vertex *A, *B, *C; |
|
260 |
|
261 Triangle(Vertex *aA, Vertex *aB, Vertex *aC, Material *amaterial); |
221 bool intersect(const Ray &ray, Float &dist) const; |
262 bool intersect(const Ray &ray, Float &dist) const; |
222 bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const {return false;}; |
263 bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const {return false;}; |
223 Vector3 normal(Vector3 &) const { return N; }; |
264 const Vector3 normal(const Vector3 &P) const { return (smooth ? smooth_normal(P) : N); }; |
|
265 const Vector3 getNormal() const { return N; }; |
|
266 void setSmooth() { smooth = true; };//(typeid(*A) == typeid(*B) == typeid(*C) == typeid(NormalVertex)); }; |
|
267 void setFlat() { smooth = false; }; |
|
268 bool getSmooth() const { return smooth; }; |
224 BBox get_bbox() const; |
269 BBox get_bbox() const; |
225 }; |
270 }; |
226 |
271 |
227 #endif |
272 #endif |