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 shapes.h |
|
3 |
* @brief Shape classes: Box, Sphere, Triangle and helpers |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
* This file is part of Pyrit Ray Tracer. |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
* Copyright 2006, 2007, 2008 Radek Brich |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
* of this software and associated documentation files (the "Software"), to deal |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
* in the Software without restriction, including without limitation the rights |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
* copies of the Software, and to permit persons to whom the Software is |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
* furnished to do so, subject to the following conditions: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
* The above copyright notice and this permission notice shall be included in |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
* all copies or substantial portions of the Software. |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
* THE SOFTWARE. |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
#ifndef SHAPES_H |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
#define SHAPES_H |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
#include "common.h" |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
#include "scene.h" |
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
33 |
#include "material.h" |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
/* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
triangle intersection alghoritm |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
options are: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
TRI_PLUCKER |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
TRI_BARI |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
TRI_BARI_PRE |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
#if !defined(TRI_PLUCKER) && !defined(TRI_BARI) && !defined(TRI_BARI_PRE) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
# define TRI_BARI_PRE |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
#endif |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
/** |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
47 |
* abstract shape class |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
class Shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
public: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
Material *material; |
93 | 53 |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
Shape() {}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
virtual ~Shape() {}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
57 |
/** |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
58 |
* intersect ray with sphere |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
59 |
* @param[in] ray the ray |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
60 |
* @param[in] dist maximum allowed distance of intersection |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
61 |
* @param[out] dist distance of the intersection if found, unchanged otherwise |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
62 |
* @return true if ray intersects the sphere |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
63 |
*/ |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
virtual bool intersect(const Ray &ray, Float &dist) const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
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 |
* same as intersect, but for ray packets |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
68 |
*/ |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
69 |
#ifndef NO_SIMD |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
70 |
virtual mfloat4 intersect_packet(const RayPacket &rays, mfloat4 &dists) const |
84
6f7fe14782c2
prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents:
82
diff
changeset
|
71 |
{ |
100
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
72 |
union { |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
73 |
mfloat4 mresults; |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
74 |
int32_t results[4]; |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
75 |
}; |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
76 |
union { |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
77 |
mfloat4 m; |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
78 |
float arr[4]; |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
79 |
} d = { dists }; |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
80 |
for (int i = 0; i < 4; i++) |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
81 |
results[i] = intersect(rays[i], d.arr[i]) ? -1 : 0; |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
98
diff
changeset
|
82 |
return mresults; |
84
6f7fe14782c2
prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents:
82
diff
changeset
|
83 |
}; |
91 | 84 |
#endif |
84
6f7fe14782c2
prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents:
82
diff
changeset
|
85 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
86 |
/** get all intersections -- not needed nor used currently */ |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
87 |
virtual bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
88 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
89 |
/** test intersection with bounding box */ |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
virtual bool intersect_bbox(const BBox &bbox) const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
91 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
92 |
/** get surface normal at point P */ |
91 | 93 |
virtual const Vector normal(const Vector &P) const = 0; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
94 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
95 |
/** get bounding box of this shape */ |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
96 |
virtual BBox get_bbox() const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
97 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
98 |
/** write textual representation of the shape to stream */ |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
99 |
virtual ostream & dump(ostream &st) const = 0; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
100 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
101 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
102 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
103 |
* list of shapes |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
104 |
*/ |
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
105 |
typedef vector<const Shape*> ShapeList; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
106 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
107 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
108 |
* sphere shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
109 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
110 |
class Sphere: public Shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
111 |
{ |
91 | 112 |
Vector center; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
113 |
Float radius; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
114 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
115 |
Float sqr_radius; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
116 |
Float inv_radius; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
117 |
public: |
91 | 118 |
Sphere(const Vector &acenter, const Float aradius, Material *amaterial): |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
119 |
center(acenter), radius(aradius), |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
120 |
sqr_radius(aradius*aradius), inv_radius(1.0f/aradius) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
121 |
{ material = amaterial; } |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
122 |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
123 |
bool intersect(const Ray &ray, Float &dist) const; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
124 |
#ifndef NO_SIMD |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
125 |
mfloat4 intersect_packet(const RayPacket &rays, mfloat4 &dists) const; |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
126 |
#endif |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
127 |
bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
128 |
bool intersect_bbox(const BBox &bbox) const; |
91 | 129 |
const Vector normal(const Vector &P) const { return (P - center) * inv_radius; }; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
130 |
BBox get_bbox() const; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
131 |
|
91 | 132 |
const Vector getCenter() const { return center; }; |
84
6f7fe14782c2
prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents:
82
diff
changeset
|
133 |
Float getRadius() const { return radius; }; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
134 |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
135 |
ostream & dump(ostream &st) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
136 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
137 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
138 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
139 |
* box shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
140 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
141 |
class Box: public Shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
142 |
{ |
91 | 143 |
Vector L; |
144 |
Vector H; |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
145 |
public: |
91 | 146 |
Box(const Vector &aL, const Vector &aH, Material *amaterial): L(aL), H(aH) |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
147 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
148 |
for (int i = 0; i < 3; i++) |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
149 |
if (L[i] > H[i]) |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
150 |
swap(L[i], H[i]); |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
151 |
material = amaterial; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
152 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
153 |
bool intersect(const Ray &ray, Float &dist) const; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
154 |
#ifndef NO_SIMD |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
155 |
mfloat4 intersect_packet(const RayPacket &rays, mfloat4 &dists) const; |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
156 |
#endif |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
157 |
bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const { return false; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
158 |
bool intersect_bbox(const BBox &bbox) const; |
91 | 159 |
const Vector normal(const Vector &P) const; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
160 |
BBox get_bbox() const { return BBox(L, H); }; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
161 |
|
91 | 162 |
const Vector getL() const { return L; }; |
163 |
const Vector getH() const { return H; }; |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
164 |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
165 |
ostream & dump(ostream &st) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
166 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
167 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
168 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
169 |
* triangle vertex |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
170 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
171 |
class Vertex |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
172 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
173 |
public: |
91 | 174 |
Vector P; |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
175 |
|
91 | 176 |
Vertex(const Vector &aP): P(aP) {}; |
82
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
79
diff
changeset
|
177 |
virtual ~Vertex() {}; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
178 |
virtual ostream & dump(ostream &st) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
179 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
180 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
181 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
182 |
* triangle vertex with normal |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
183 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
184 |
class NormalVertex: public Vertex |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
185 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
186 |
public: |
91 | 187 |
Vector N; |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
188 |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
189 |
NormalVertex(const NormalVertex *v): Vertex(v->P), N(v->N) {}; |
91 | 190 |
NormalVertex(const Vector &aP): Vertex(aP) {}; |
191 |
NormalVertex(const Vector &aP, const Vector &aN): Vertex(aP), N(aN) {}; |
|
192 |
const Vector &getNormal() { return N; }; |
|
193 |
void setNormal(const Vector &aN) { N = aN; }; |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
194 |
ostream & dump(ostream &st) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
195 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
196 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
197 |
/** |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
198 |
* triangle shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
199 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
200 |
class Triangle: public Shape |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
201 |
{ |
93 | 202 |
Vector N; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
203 |
#ifdef TRI_BARI_PRE |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
204 |
Float nu, nv, nd; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
205 |
Float bnu, bnv; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
206 |
Float cnu, cnv; |
93 | 207 |
int k; // dominant axis |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
208 |
#endif |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
209 |
#ifdef TRI_BARI |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
210 |
int k; // dominant axis |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
211 |
#endif |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
212 |
#ifdef TRI_PLUCKER |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
213 |
Float pla[6], plb[6], plc[6]; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
214 |
#endif |
93 | 215 |
|
91 | 216 |
const Vector smooth_normal(const Vector &P) const |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
217 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
218 |
#ifdef TRI_BARI_PRE |
91 | 219 |
const Vector &NA = static_cast<NormalVertex*>(A)->N; |
220 |
const Vector &NB = static_cast<NormalVertex*>(B)->N; |
|
221 |
const Vector &NC = static_cast<NormalVertex*>(C)->N; |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
222 |
static const int modulo3[5] = {0,1,2,0,1}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
223 |
register const int ku = modulo3[k+1]; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
224 |
register const int kv = modulo3[k+2]; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
225 |
const Float pu = P[ku] - A->P[ku]; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
226 |
const Float pv = P[kv] - A->P[kv]; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
227 |
const Float u = pv * bnu + pu * bnv; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
228 |
const Float v = pu * cnv + pv * cnu; |
91 | 229 |
Vector n = NA + u * (NB - NA) + v * (NC - NA); |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
230 |
n.normalize(); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
231 |
return n; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
232 |
#else |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
233 |
return N; // not implemented for other algorithms |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
234 |
#endif |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
235 |
}; |
93 | 236 |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
237 |
public: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
238 |
Vertex *A, *B, *C; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
239 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
240 |
Triangle() {}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
241 |
Triangle(Vertex *aA, Vertex *aB, Vertex *aC, Material *amaterial); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
242 |
bool intersect(const Ray &ray, Float &dist) const; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
243 |
#if !defined(NO_SIMD) && defined(TRI_BARI_PRE) |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
244 |
mfloat4 intersect_packet(const RayPacket &rays, mfloat4 &dists) const; |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
245 |
#endif |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
246 |
bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const {return false;}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
247 |
bool intersect_bbox(const BBox &bbox) const; |
91 | 248 |
const Vector normal(const Vector &P) const { return (material->smooth ? smooth_normal(P) : N); }; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
249 |
BBox get_bbox() const; |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
250 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
251 |
/** get real normal of the triangle */ |
91 | 252 |
const Vector getNormal() const { return N; }; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
253 |
|
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
254 |
ostream & dump(ostream &st) const; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
255 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
256 |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
257 |
/** template for triangle arrays, currently not used */ |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
258 |
template <class T> class Array |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
259 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
260 |
T *array; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
261 |
public: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
262 |
Array(int n) { array = new T[n]; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
263 |
~Array() { delete[] array; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
264 |
const T &operator[](int i) const { return array[i]; }; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
265 |
}; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
266 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
267 |
typedef Array<Vertex> VertexArray; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
268 |
typedef Array<NormalVertex> NormalVertexArray; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
269 |
typedef Array<Triangle> TriangleArray; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
270 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
271 |
#endif |