1 | #pragma once |
---|
2 | #include "Intersectable.hpp" |
---|
3 | #include "SquareLight.hpp" |
---|
4 | |
---|
5 | class PlaneXAligned : public Intersectable { |
---|
6 | // const Material* material; |
---|
7 | float position; |
---|
8 | bool positiveFacing; |
---|
9 | public: |
---|
10 | float yMin, yMax, zMin, zMax; |
---|
11 | public: |
---|
12 | PlaneXAligned(std::istream& isc, Material** materialTable, int nMaterials) |
---|
13 | { |
---|
14 | material = &Material::DIFFUSEWHITE; |
---|
15 | position = 0.0f; |
---|
16 | yMin = zMin = -100.0f; |
---|
17 | yMax = zMax = 100.0f; |
---|
18 | positiveFacing = true; |
---|
19 | char key[200]; |
---|
20 | do |
---|
21 | { |
---|
22 | isc >> key; |
---|
23 | if(strcmp(key,"x") == 0) isc >> position; |
---|
24 | else if(strcmp(key,"facing") == 0) |
---|
25 | { |
---|
26 | isc >> key; |
---|
27 | if(strcmp(key,"positive") == 0) |
---|
28 | positiveFacing = true; |
---|
29 | else |
---|
30 | positiveFacing = false; |
---|
31 | } |
---|
32 | else if(strcmp(key,"y") == 0) { isc >> yMin; isc >> yMax; } |
---|
33 | else if(strcmp(key,"z") == 0) { isc >> zMin; isc >> zMax; } |
---|
34 | else if(strcmp(key,"material") == 0) |
---|
35 | { |
---|
36 | isc >> key; |
---|
37 | for(int i=0; i < nMaterials; i++) |
---|
38 | { |
---|
39 | material = materialTable[i]; |
---|
40 | if(strcmp(key, material->getName()) == 0) break; |
---|
41 | } |
---|
42 | } |
---|
43 | }while(strcmp(key,"end") != 0); |
---|
44 | bbox.minPoint.x = bbox.maxPoint.x = position; |
---|
45 | bbox.maxPoint.y = yMax; |
---|
46 | bbox.maxPoint.z = zMax; |
---|
47 | bbox.minPoint.y = yMin; |
---|
48 | bbox.minPoint.z = zMin; |
---|
49 | } |
---|
50 | PlaneXAligned(const float position, const bool positiveFacing, const Material* material) |
---|
51 | { |
---|
52 | this->material = material; |
---|
53 | this->position = position; |
---|
54 | this->positiveFacing = positiveFacing; |
---|
55 | bbox.minPoint.x = bbox.maxPoint.x = position; |
---|
56 | bbox.maxPoint.y = bbox.maxPoint.z = 100.0f; |
---|
57 | bbox.minPoint.y = bbox.minPoint.z = -100.0f; |
---|
58 | } |
---|
59 | bool intersectBackSide(const Ray& ray, float& depth, float rayMin, float rayMax ) |
---|
60 | { |
---|
61 | lastTestedRayResult.isIntersect = false; |
---|
62 | return false; |
---|
63 | return intersect(ray, depth, rayMin, rayMax); |
---|
64 | } |
---|
65 | |
---|
66 | bool intersect(const Ray& ray, float& depth, float rayMin, float rayMax ) |
---|
67 | { |
---|
68 | lastTestedRayId = ray.id; |
---|
69 | float distx = position - ray.origin.x; |
---|
70 | if(positiveFacing && distx < 0 && ray.dir.x < 0 |
---|
71 | || |
---|
72 | !positiveFacing && distx > 0 && ray.dir.x > 0) |
---|
73 | { |
---|
74 | depth = distx / ray.dir.x; |
---|
75 | lastTestedRayResult.isIntersect = true; |
---|
76 | lastTestedRayResult.depth = depth; |
---|
77 | lastTestedRayResult.material = this->material; |
---|
78 | lastTestedRayResult.normal = positiveFacing?Vector(1.0f, 0.0f, 0.0f):Vector(-1.0f, 0.0f, 0.0f); |
---|
79 | lastTestedRayResult.point = ray.origin + ray.dir * depth; |
---|
80 | if(lastTestedRayResult.point.y < yMin || |
---|
81 | lastTestedRayResult.point.y > yMax || |
---|
82 | lastTestedRayResult.point.z < zMin || |
---|
83 | lastTestedRayResult.point.z > zMax) |
---|
84 | { |
---|
85 | lastTestedRayResult.isIntersect = false; |
---|
86 | return false; |
---|
87 | } |
---|
88 | lastTestedRayResult.object = this; |
---|
89 | return true; |
---|
90 | } |
---|
91 | lastTestedRayResult.isIntersect = false; |
---|
92 | return false; |
---|
93 | } |
---|
94 | Vector getShadingNormal(const Vector& point) |
---|
95 | { |
---|
96 | return positiveFacing?Vector(1.0f, 0.0f, 0.0f):Vector(-1.0f, 0.0f, 0.0f); |
---|
97 | } |
---|
98 | Light* makeLight() const |
---|
99 | { |
---|
100 | Vector corners[4]; |
---|
101 | corners[0] = Vector(position, yMin, zMin); |
---|
102 | corners[positiveFacing?3:1] = Vector(position, yMax, zMin); |
---|
103 | corners[2] = Vector(position, yMax, zMax); |
---|
104 | corners[positiveFacing?1:3] = Vector(position, yMin, zMax); |
---|
105 | return new SquareLight(corners, 4, material->getSurfaceRadiance()); |
---|
106 | } |
---|
107 | }; |
---|
108 | |
---|
109 | class PlaneYAligned : public Intersectable { |
---|
110 | // const Material* material; |
---|
111 | float position; |
---|
112 | bool positiveFacing; |
---|
113 | public: |
---|
114 | float xMin, xMax, zMin, zMax; |
---|
115 | public: |
---|
116 | PlaneYAligned(std::istream& isc, Material** materialTable, int nMaterials) |
---|
117 | { |
---|
118 | material = &Material::DIFFUSEWHITE; |
---|
119 | position = 0.0f; |
---|
120 | xMin = zMin = -100.0f; |
---|
121 | xMax = zMax = 100.0f; |
---|
122 | positiveFacing = true; |
---|
123 | char key[200]; |
---|
124 | do |
---|
125 | { |
---|
126 | isc >> key; |
---|
127 | if(strcmp(key,"y") == 0) isc >> position; |
---|
128 | else if(strcmp(key,"facing") == 0) |
---|
129 | { |
---|
130 | isc >> key; |
---|
131 | if(strcmp(key,"positive") == 0) |
---|
132 | positiveFacing = true; |
---|
133 | else |
---|
134 | positiveFacing = false; |
---|
135 | } |
---|
136 | else if(strcmp(key,"x") == 0) { isc >> xMin; isc >> xMax; } |
---|
137 | else if(strcmp(key,"z") == 0) { isc >> zMin; isc >> zMax; } |
---|
138 | else if(strcmp(key,"material") == 0) |
---|
139 | { |
---|
140 | isc >> key; |
---|
141 | for(int i=0; i < nMaterials; i++) |
---|
142 | { |
---|
143 | material = materialTable[i]; |
---|
144 | if(strcmp(key, material->getName()) == 0) break; |
---|
145 | } |
---|
146 | } |
---|
147 | }while(strcmp(key,"end") != 0); |
---|
148 | bbox.minPoint.y = bbox.maxPoint.y = position; |
---|
149 | bbox.maxPoint.x = xMax; |
---|
150 | bbox.maxPoint.z = zMax; |
---|
151 | bbox.minPoint.x = xMin; |
---|
152 | bbox.minPoint.z = zMin; |
---|
153 | } |
---|
154 | PlaneYAligned(const float position, const bool positiveFacing, const Material* material) |
---|
155 | { |
---|
156 | this->material = material; |
---|
157 | this->position = position; |
---|
158 | this->positiveFacing = positiveFacing; |
---|
159 | bbox.minPoint.y = bbox.maxPoint.y = position; |
---|
160 | bbox.maxPoint.x = bbox.maxPoint.z = 100.0f; |
---|
161 | bbox.minPoint.x = bbox.minPoint.z = -100.0f; |
---|
162 | } |
---|
163 | bool intersectBackSide(const Ray& ray, float& depth, float rayMin, float rayMax ) |
---|
164 | { |
---|
165 | lastTestedRayResult.isIntersect = false; |
---|
166 | return false; |
---|
167 | return intersect(ray, depth, rayMin, rayMax); |
---|
168 | } |
---|
169 | bool intersect(const Ray& ray, float& depth, float rayMin, float rayMax) |
---|
170 | { |
---|
171 | lastTestedRayId = ray.id; |
---|
172 | float disty = position - ray.origin.y; |
---|
173 | if(positiveFacing && disty < 0 && ray.dir.y < 0 |
---|
174 | || |
---|
175 | !positiveFacing && disty > 0 && ray.dir.y > 0) |
---|
176 | { |
---|
177 | depth = disty / ray.dir.y; |
---|
178 | lastTestedRayResult.isIntersect = true; |
---|
179 | lastTestedRayResult.depth = depth; |
---|
180 | lastTestedRayResult.material = this->material; |
---|
181 | lastTestedRayResult.normal = positiveFacing?Vector(0.0f, 1.0f, 0.0f):Vector(0.0f, -1.0f, 0.0f); |
---|
182 | lastTestedRayResult.point = ray.origin + ray.dir * depth; |
---|
183 | if(lastTestedRayResult.point.x < xMin || |
---|
184 | lastTestedRayResult.point.x > xMax || |
---|
185 | lastTestedRayResult.point.z < zMin || |
---|
186 | lastTestedRayResult.point.z > zMax) |
---|
187 | { |
---|
188 | lastTestedRayResult.isIntersect = false; |
---|
189 | return false; |
---|
190 | } |
---|
191 | lastTestedRayResult.object = this; |
---|
192 | return true; |
---|
193 | } |
---|
194 | lastTestedRayResult.isIntersect = false; |
---|
195 | return false; |
---|
196 | } |
---|
197 | Vector getShadingNormal(const Vector& point) |
---|
198 | { |
---|
199 | return positiveFacing?Vector(0.0f, 1.0f, 0.0f):Vector(0.0f, -1.0f, 0.0f); |
---|
200 | } |
---|
201 | Light* makeLight() const |
---|
202 | { |
---|
203 | Vector corners[4]; |
---|
204 | corners[0] = Vector(xMin, position, zMin); |
---|
205 | corners[positiveFacing?1:3] = Vector(xMax, position, zMin); |
---|
206 | corners[2] = Vector(xMax, position, zMax); |
---|
207 | corners[positiveFacing?3:1] = Vector(xMin, position, zMax); |
---|
208 | return new SquareLight(corners, 4, material->getSurfaceRadiance()); |
---|
209 | } |
---|
210 | }; |
---|
211 | |
---|
212 | class PlaneZAligned : public Intersectable { |
---|
213 | // const Material* material; |
---|
214 | float position; |
---|
215 | bool positiveFacing; |
---|
216 | public: |
---|
217 | float xMin, xMax, yMin, yMax; |
---|
218 | public: |
---|
219 | PlaneZAligned(std::istream& isc, Material** materialTable, int nMaterials) |
---|
220 | { |
---|
221 | material = &Material::DIFFUSEWHITE; |
---|
222 | position = 0.0f; |
---|
223 | yMin = xMin = -100.0f; |
---|
224 | yMax = xMax = 100.0f; |
---|
225 | positiveFacing = true; |
---|
226 | char key[200]; |
---|
227 | do |
---|
228 | { |
---|
229 | isc >> key; |
---|
230 | if(strcmp(key,"z") == 0) isc >> position; |
---|
231 | else if(strcmp(key,"facing") == 0) |
---|
232 | { |
---|
233 | isc >> key; |
---|
234 | if(strcmp(key,"positive") == 0) |
---|
235 | positiveFacing = true; |
---|
236 | else |
---|
237 | positiveFacing = false; |
---|
238 | } |
---|
239 | else if(strcmp(key,"y") == 0) { isc >> yMin; isc >> yMax; } |
---|
240 | else if(strcmp(key,"x") == 0) { isc >> xMin; isc >> xMax; } |
---|
241 | else if(strcmp(key,"material") == 0) |
---|
242 | { |
---|
243 | isc >> key; |
---|
244 | for(int i=0; i < nMaterials; i++) |
---|
245 | { |
---|
246 | material = materialTable[i]; |
---|
247 | if(strcmp(key, material->getName()) == 0) break; |
---|
248 | } |
---|
249 | } |
---|
250 | }while(strcmp(key,"end") != 0); |
---|
251 | bbox.minPoint.z = bbox.maxPoint.z = position; |
---|
252 | bbox.maxPoint.y = yMax; |
---|
253 | bbox.maxPoint.x = xMax; |
---|
254 | bbox.minPoint.y = yMin; |
---|
255 | bbox.minPoint.x = xMin; |
---|
256 | } |
---|
257 | PlaneZAligned(const float position, const bool positiveFacing, const Material* material) |
---|
258 | { |
---|
259 | this->material = material; |
---|
260 | this->position = position; |
---|
261 | this->positiveFacing = positiveFacing; |
---|
262 | bbox.minPoint.z = bbox.maxPoint.z = position; |
---|
263 | bbox.maxPoint.y = bbox.maxPoint.x = 100.0f; |
---|
264 | bbox.minPoint.y = bbox.minPoint.x = -100.0f; |
---|
265 | } |
---|
266 | bool intersectBackSide(const Ray& ray, float& depth, float rayMin, float rayMax ) |
---|
267 | { |
---|
268 | lastTestedRayResult.isIntersect = false; |
---|
269 | return false; |
---|
270 | return intersect(ray, depth, rayMin, rayMax); |
---|
271 | } |
---|
272 | bool intersect(const Ray& ray, float& depth, float rayMin, float rayMax) |
---|
273 | { |
---|
274 | lastTestedRayId = ray.id; |
---|
275 | float distz = position - ray.origin.z; |
---|
276 | if(positiveFacing && distz < 0 && ray.dir.z < 0 |
---|
277 | || |
---|
278 | !positiveFacing && distz > 0 && ray.dir.z > 0) |
---|
279 | { |
---|
280 | depth = distz / ray.dir.z; |
---|
281 | |
---|
282 | lastTestedRayResult.isIntersect = true; |
---|
283 | lastTestedRayResult.depth = depth; |
---|
284 | lastTestedRayResult.material = this->material; |
---|
285 | lastTestedRayResult.normal = positiveFacing?Vector(0.0f, 0.0f, 1.0f):Vector(0.0f, 0.0f, -1.0f); |
---|
286 | lastTestedRayResult.point = ray.origin + ray.dir * depth; |
---|
287 | if(lastTestedRayResult.point.x < xMin || |
---|
288 | lastTestedRayResult.point.x > xMax || |
---|
289 | lastTestedRayResult.point.y < yMin || |
---|
290 | lastTestedRayResult.point.y > yMax) |
---|
291 | { |
---|
292 | lastTestedRayResult.isIntersect = false; |
---|
293 | return false; |
---|
294 | } |
---|
295 | lastTestedRayResult.object = this; |
---|
296 | return true; |
---|
297 | } |
---|
298 | lastTestedRayResult.isIntersect = false; |
---|
299 | return false; |
---|
300 | } |
---|
301 | Vector getShadingNormal(const Vector& point) |
---|
302 | { |
---|
303 | return positiveFacing?Vector(0.0f, 0.0f, 1.0f):Vector(0.0f, 0.0f, -1.0f); |
---|
304 | } |
---|
305 | Light* makeLight() const |
---|
306 | { |
---|
307 | Vector corners[4]; |
---|
308 | corners[0] = Vector(xMin, yMin, position); |
---|
309 | corners[positiveFacing?3:1] = Vector(xMax, yMin, position); |
---|
310 | corners[2] = Vector(xMax, yMax, position); |
---|
311 | corners[positiveFacing?1:3] = Vector(xMin, yMax, position); |
---|
312 | return new SquareLight(corners, 4, material->getSurfaceRadiance()); |
---|
313 | } |
---|
314 | }; |
---|