-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.cpp
215 lines (169 loc) · 6.37 KB
/
scene.cpp
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
/*
* Copyright (c) 2007 Alexander Strange <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "scene.h"
//extern bool verbose_log;
#define verbose_log 0
intersectResult sphere::intersects(const ray &r, world_distance *dist, world_distance max, bool consider_close_miss) const
{
intersectResult res = MISS;
vector3 sc_v = origin - r.origin; // sphere center
world_distance dist_sq = sc_v.dot_self(); // distance(r.origin, sphere.origin)
world_distance dist_from_rad_sq = dist_sq - radSq;
world_distance r_dist;
world_distance closest_approach = sc_v.dot(r.dir);
if (dist_from_rad_sq > EPSILON) {
if (closest_approach >= 0) { // ray is pointing towards sphere
world_distance half_cord = closest_approach*closest_approach - dist_from_rad_sq;
if (half_cord > 0) {
r_dist = closest_approach - sqrt(half_cord);
if (above(r_dist, 0) && r_dist < max) {
res = HIT;
*dist = r_dist;
}
}
}
} else { //inside sphere
if (dist_from_rad_sq > -EPSILON && consider_close_miss) return MISS;
world_distance half_cord = closest_approach*closest_approach - dist_from_rad_sq;
r_dist = closest_approach + sqrt(half_cord);
if (above(r_dist, 0) && r_dist < max) {
res = HITINSIDE;
*dist = r_dist; // far end of sphere
}
}
return res;
}
intersectResult plane_prim::intersects(const ray &r, world_distance *cdist, world_distance max, bool consider_close_miss) const
{
world_distance d = dot(normal, r.dir);
if (!close(d, 0)) {
world_distance col_dist = -(dot(normal, r.origin) + dist) / d;
if (above(col_dist, 0) && col_dist < max) {
*cdist = col_dist;
return (d > 0) ? HIT : HITINSIDE;
}
}
return MISS;
}
color4 checkerboard_texture::colorAt(world_distance u, world_distance v)
{
int iu = lrint(u), iv = lrint(v);
if ((iu+iv)&1) {
return odd;
} else {
return even;
}
}
color4 image_texture::pixelAt(size_t x, size_t y)
{
return *pixelAddressAt(image, x, y, w);
}
static real gaussian(world_distance x, world_distance s)
{
return exp(s ? -(x*x) / (2*s*s) : 0); // scale factor isn't correct but it has to be rescaled anyway
}
static void make_filter(real *filter, unsigned char support, world_distance pos)
{
int i;
world_distance p = ((1. - pos) - .5) - support;
real sum = 0, rescale;
if (verbose_log) printf("filter: p %f\n", p);
for (i = -support; i <= support; i++) {
real v = dmax(gaussian(p, (1. / sqrt(2.))), 0.);
sum += v;
filter[i + support] = v;
p += 1.;
}
rescale = sum ? (1. / sum) : 1.;
for (i = -support; i <= support; i++)
{filter[i + support] *= rescale;if (verbose_log) printf("filter: %d %f -> %f\n", i, p, filter[i+support]);}
}
static inline color4 apply_filter_refine(image_texture *tex, size_t x, size_t y, real *filter_x, real *filter_y, uint8_t support, bool first = true, color4 previous = color4())
{
const color Yf = color(.2126,.7152,.0722);
color4 acc;
world_distance luma = 0;
if (!first) luma = dot(Yf, from_premultiplied(previous, NULL));
for (int i = -support; i <= support; i++) {
for (int j = -support; j <= support; j++) {
real factor = filter_x[j + support] * filter_y[i + support];
color4 p = tex->pixelAt(x+j, y+i);
if (first) {
acc += p * factor;
} else {
world_distance cY = dot(Yf, from_premultiplied(p, NULL));
acc += ((fabs(luma - cY) < .4) ? p : previous) * factor;
}
}
}
return acc;
}
static color4 apply_filter(image_texture *tex, size_t x, size_t y, real *filter_x, real *filter_y, uint8_t support, unsigned refinements = 1)
{
color4 c = apply_filter_refine(tex, x, y, filter_x, filter_y, support);
for (unsigned i = 0; i < refinements; i++) c = apply_filter_refine(tex, x, y, filter_x, filter_y, support, false, c);
return c;
}
color4 image_texture::colorAt(world_distance u, world_distance v)
{
if (verbose_log) printf("img: u %f v %f\n", u, v);
if (v < 0 || v > fh || u < 0 || u > fw) {
if (repeat) {
bool rev_u = u < 0, rev_v = v < 0;
u = fmod(fabs(u), fw+1.);
v = fmod(fabs(v), fh+1.);
if (rev_u) u = fw-u;
if (rev_v) v = fh-v;
} else {
if (verbose_log) printf("img: ...is outside w %f h %f\n",fw,fh);
return color4();
}
}
u = fw-u;
world_distance uf = floor(u), vf = floor(v);
world_distance ud = u - uf, vd = v - vf;
const unsigned char support = IMG_SUPPORT;
if (verbose_log) printf("img: post-adj u %f v %f\n", u, v);
real filter_x[support*2 + 1] = {0}, filter_y[support*2 + 1] = {0};
make_filter(filter_x, support, ud);
make_filter(filter_y, support, vd);
color4 c = apply_filter(this, uf, vf, filter_x, filter_y, support);
if (verbose_log) {printf("img: color res "); c.print();}
return c;
}
static color4 color_of_placement(texture_placement *p, world_distance u, world_distance v)
{
return p->tex->colorAt(u * p->uScale + p->uShift, v * p->vScale + p->vShift);
}
static color4 rec_colorOfTextureStackAt(texture_placement *textures, world_distance u, world_distance v, size_t i, color4 above)
{
color4 thisC = color_of_placement(&textures[i], u, v);
if (verbose_log) {printf("tex recurse: i %lu, this color ",i); thisC.print();}
color4 resC = over(above, thisC);
if (i == 0 || close(thisC.a, 1)) return resC;
return rec_colorOfTextureStackAt(textures, u, v, i-1, resC);
}
static color4 colorOfTextureStackAt(texture_placement *textures, world_distance u, world_distance v, size_t texcount)
{
color4 thisC = color_of_placement(&textures[texcount-1], u, v);
if (verbose_log) {printf("tex stack top: tcount %lu, color ",texcount); thisC.print();}
if (texcount == 1 || close(thisC.a, 1)) return thisC;
return rec_colorOfTextureStackAt(textures, u, v, texcount-2, thisC);
}
color4 surface::colorAt(world_distance u, world_distance v)
{
return colorOfTextureStackAt(textures, u, v, texcount);
}