1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527 */
/* without consent of either the College of William & Mary or */
/* Pieter Peers. */
/******************************************************************/
#include <cassert>
#include "shader_base.h"
#include "intersectionPoint.h"
/////////////////
// Constructor //
/////////////////
intersectionPoint::intersectionPoint(void)
{
_hit = false;
}
intersectionPoint::intersectionPoint(const ray& r, float rayParameter, const std::shared_ptr<const shader_base>& shader, const vec3d& normal, const vec3d& axis, const vec2d& textureCoordinate)
{
// copy
_ray = r;
_rayParameter = rayParameter;
_hit = true;
_shader = shader;
_normal = normalize(normal);
_axis = normalize(axis);
_textureCoordinate = textureCoordinate;
// compute position
_position = _ray(_rayParameter);
}
intersectionPoint::intersectionPoint(const intersectionPoint& ip)
{
_hit = ip._hit;
// copy remainder if hit
if(_hit)
{
_ray = ip._ray;
_rayParameter = ip._rayParameter;
_shader = ip._shader;
_normal = ip._normal;
_position = ip._position;
_axis = ip._axis;
_textureCoordinate = ip._textureCoordinate;
}
else _shader = nullptr;
}
///////////////
// Operators //
///////////////
intersectionPoint& intersectionPoint::operator=(const intersectionPoint& ip)
{
_assign(ip);
return *this;
}
bool intersectionPoint::operator<(const intersectionPoint& ip) const
{
// handle case where either one of the intersectionPoints is not a hit
// => assume no-hit equals a hit at infinity
if(!_hit) return false;
if(!ip._hit) return true;
// decide based on rayParameter
return _rayParameter < ip._rayParameter;
}
bool intersectionPoint::operator>(const intersectionPoint& ip) const
{
// handle case where either one of the intersectionPoints is not a hit
// => assume no-hit equals a hit at infinity
if(!_hit) return true;
if(!ip._hit) return false;
// decide based on rayParameter
return _rayParameter > ip._rayParameter;
}
bool intersectionPoint::operator<(const lightSample& ls) const
{
// handle case where this is not a hit
if(!_hit) return false;
// decide based on rayParameter
return _rayParameter < ls.distance();
}
bool intersectionPoint::operator>(const lightSample& ls) const
{
// handle case where this is not a hit
if(!_hit) return true;
// decide based on rayParameter
return _rayParameter > ls.distance();
}
////////////////
// Inspectors //
////////////////
bool intersectionPoint::isHit(void) const
{
return _hit;
}
shaderProperties intersectionPoint::getShaderProperties(void) const
{
assert(hasShader());
return _shader->properties(*this);
}
bool intersectionPoint::hasShader(void) const
{
return (_shader != nullptr);
}
float intersectionPoint::distance(const intersectionPoint& ip) const
{
// set distance to infinity of one of the two points is not a hit
if(!_hit || !ip._hit) return +LARGE;
// compute distance
return _position.distance(ip._position);
}
const vec3d& intersectionPoint::position(void) const
{
return _position;
}
const vec3d& intersectionPoint::direction(void) const
{
return _ray.direction();
}
const vec2d& intersectionPoint::textureCoordinate(void) const
{
return _textureCoordinate;
}
const vec3d& intersectionPoint::normal(void) const
{
return _normal;
}
coordinateTransformation intersectionPoint::shadingFrame(void) const
{
// transform => to global shading frame
// inverseTransform => to local shading frame
return coordinateTransformation(_normal, _axis);
}
//////////////
// Mutators //
//////////////
void intersectionPoint::transform(const transformation3d& t)
{
// sanity check
if(!_hit) return;
/// transform
_ray.transform(t);
_position = t.transformPoint(_position);
_normal = t.transformNormal(_normal);
_axis = t.transformNormal(_axis);
// recompute the ray parameter (to account for potential scaling)
_rayParameter = _ray(_position);
}
void intersectionPoint::inverseTransform(const transformation3d& t)
{
// sanity check
if(!_hit) return;
// inverse transform
_ray.inverseTransform(t);
_position = t.inverseTransformPoint(_position);
_normal = t.inverseTransformNormal(_normal);
_axis = t.inverseTransformNormal(_axis);
// recompute the ray parameter (to account for potential scaling)
_rayParameter = _ray(_position);
}
void intersectionPoint::transformShadingFrame(const transformation3d& sft)
{
// sanity check
if(!_hit) return;
// Note: shading frame transformation are defined
// with respect to the local shading frame (i.e., N=Z, axis=X)
transformation3d t = shadingFrame();
// transform
_normal = t.transformNormal( sft.transformNormal(vec3d(0.0f, 0.0f, 1.0f)) );
_axis = t.transformNormal( sft.transformNormal(vec3d(1.0f, 0.0f, 0.0f)) );
// Done.
}
void intersectionPoint::inverseTransformShadingFrame(const transformation3d& sft)
{
// sanity check
if(!_hit) return;
// Note: shading frame transformation are defined
// with respect to the local shading frame (i.e., N=Z, axis=X)
// Note: this transformation will not undo the effects of
// transformShadingFrame since the local shading frame
// is altered by the transformation (and thus the frame
// with respect to which the inverseTransformation is
// applied)
transformation3d t = shadingFrame();
// transform
_normal = t.transformNormal( sft.inverseTransformNormal(vec3d(0.0f, 0.0f, 1.0f)) );
_axis = t.transformNormal( sft.inverseTransformNormal(vec3d(1.0f, 0.0f, 0.0f)) );
// Done.
}
void intersectionPoint::setShader(const std::shared_ptr<const class shader_base>& shader)
{
_shader = shader;
}
/////////////
// Methods //
/////////////
color intersectionPoint::shade(const vec3d& out) const
{
assert(hasShader());
if(!_hit) return color(0.0f);
return _shader->shade(*this, out);
}
color intersectionPoint::shade(const lightSample& ls) const
{
assert(hasShader());
if(!_hit) return color(0.0f);
return _shader->shade(*this, ls.directionToLight()) * ls.emittance();
}
float intersectionPoint::reflectivity(void) const
{
assert(hasShader());
// get reflectivity
float albedo = _shader->reflectivity(*this);
// cannot reflect more than 100%
assert(albedo <= 1.0f);
// Done.
return albedo;
}
color intersectionPoint::reflectance(const vec3d& out) const
{
assert(hasShader());
return _shader->reflectance(*this, out);
}
brdfSample intersectionPoint::sample(float r1, float r2) const
{
assert(hasShader());
return _shader->sample(*this, r1, r2);
}
color intersectionPoint::emittance(void) const
{
assert(hasShader());
return _shader->emittance(*this);
}
/////////////////////
// Private Methods //
/////////////////////
void intersectionPoint::_assign(const intersectionPoint& ip)
{
// sanity check
if(&ip == this) return;
// copy if hit
_hit = ip._hit;
if(_hit)
{
_ray = ip._ray;
_rayParameter = ip._rayParameter;
_position = ip._position;
_normal = ip._normal;
_axis = ip._axis;
_textureCoordinate = ip._textureCoordinate;
_shader = ip._shader;
}
else _shader = nullptr;
}
void intersectionPoint::_swap(intersectionPoint& ip)
{
swap(_ray, ip._ray);
std::swap(_hit, ip._hit);
std::swap(_rayParameter, ip._rayParameter);
swap(_position, ip._position);
swap(_normal, ip._normal);
swap(_axis, ip._axis);
swap(_textureCoordinate, ip._textureCoordinate);
std::swap(_shader, ip._shader);
}
|