-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.cpp
More file actions
287 lines (254 loc) · 7.8 KB
/
scene.cpp
File metadata and controls
287 lines (254 loc) · 7.8 KB
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
#include "scene.h"
Scene::Scene(){}
void Scene::setRes(int w, int h, int d, int div) {
image.setRes(w / div, h / div, d);
windowScale = 1.0 / image.width / scale;
windowVect();
}
Light* Scene::newLight() {
Light *l = new Light;
Lights.push_back(l);
return l;
}
fimage* Scene::AddTexture(const char *c) {
for (unsigned int i = 0; i < Textures.size(); i++) {
if (strcmp(c,Textures[i]->fileName) == 0) {
return Textures[i];
}
}
fimage *fi = new fimage(c);
Textures.push_back(fi);
return fi;
}
void Scene::setCamera(Vec origin, Vec focus, double s) {
scale = s;
for (int i = 0; i < 3; i++) {
pov[i] = origin[i];
win[i] = focus[i];
}
win.normalize();
windowScale = 1.0 / image.width / scale;
windowVect();
}
Scene::Scene(int w, int h, int d) {
image.setRes(w, h, d);
windowScale = 1.0 / image.width / scale;
windowVect();
output[0] = '\0';
}
void Scene::windowVect() {
for (int i = 0; i < 3; i++)
p[i] = pov[i] + win[i];
u[0] = -1.0 * win[1];
u[1] = win[0];
u[2] = 0.0;
u.normalize();
v = win.cross(u);
v.normalize();
}
Vec Scene::getPixelCoordinate(double x, double y) {
Vec A;
double cx = x - ((double)image.width / 2);
double cy = y - ((double)image.height / 2);
for (int i = 0; i < 3; i++)
A[i] = p[i] - cx * windowScale * u[i] - cy * windowScale * v[i];
return A;
}
void Scene::getPixelVector(double x, double y, Vec *Rp, Vec *RA) {
Vec w;
w = getPixelCoordinate(x,y);
*Rp = pov;
*RA = w - pov;
}
void Scene::drawPixel(double x, double y) {
Vec o;
Vec d;
pass = 0;
Vec co;
if (sampleMethod == 0 || superSample == 1.0){
for (double m1 = 0; m1 < superSample; m1++) {
for (double m2 = 0; m2 < superSample; m2++) {
double mx = (double)x + m1 / superSample;
double my = (double)y + m2 / superSample;
getPixelVector(mx,my,&o,&d);
co = co + Cast(o, d, recursion);
}
}
}else if (sampleMethod == 1) {
int ss = (int)(superSample*superSample);
for (int i = 0; i < ss; i++) {
double mx = (double)x + RAND;
double my = (double)y + RAND;
getPixelVector(mx,my,&o,&d);
co = co + Cast(o, d, recursion);
}
}
co = co * (1.0/superSample/superSample);
image.setPixel(x,y,co);
}
void Scene::drawRange(int threadCount, int threadNum) {
int lastRep = 0;
printf("Thread: %d, core: %d\n", threadNum, sched_getcpu());
double imLength = image.width * image.height;
for (double xy = threadNum; xy < imLength; xy+=threadCount) {
double x = fmod(xy,image.width);
double y = floor(xy / image.width);
drawPixel(x, y);
if (threadNum == 0) {
int rep = (int)(xy / imLength * 20.0);
if (rep > lastRep) {
lastRep = rep;
printf("%d%%\n", rep * 5);
}
}
}
printf("Thread: %d, core: %d, done\n", threadNum, sched_getcpu());
}
void Scene::drawScene() {
if (superSample < 1.0)
superSample = 1.0;
ofstream outfile;
if (output[0] != '\0') {
outfile.open(output, ios_base::app);
outfile << "Number of primatives: " << Objects.size() << endl;
}
int threadCount = thread::hardware_concurrency();
printf("\n\nthreads: %d\n\n", threadCount);
thread* threads = NULL;
threads = new thread[threadCount];
state = new unsigned int[threadCount];
for (int i = 0; i < threadCount; i++) {
state[i] = i;
threads[i] = thread(&Scene::drawRange, this, threadCount, i);
}
for (int i = 0; i < threadCount; i++) {
threads[i].join();
}
}
bool report() {
static int r = 0;
if (++r == 10000) {
r = 0;
return 1;
}
return 0;
}
double Scene::SampleLight(hit *h, Vec *d, Light *l, int rnd) {
Vec lightDir;
if (rnd)
lightDir = l->RandomSpot() - h->location;
else
lightDir = l->origin - h->location;
double b = h->normal.GetNormalized().dot(lightDir.GetNormalized());
double c = h->normal.dot(*d);
if ((b < 0.0) == (c < 0.0)) //If the light is on the opposite side we are testing
return 0.0;
double lightDist = lightDir.magnitude();
hit obstacle = Closest(h->location, lightDir);
if (obstacle.contact && obstacle.distance < lightDist)
return 0.0;
return fabs(b);
}
Vec Scene::TraceLight(hit h, Vec d) {
Vec brightness = ambient;
int init;
if (!softShadows)
init = 1;
else
init = 15;
for (uint i = 0; i < Lights.size(); i++) {
double brightness2 = 0.0;
double min = 9e9;
double max = 0;
double cur;
for (int j = 0; j < init; j++){
cur = SampleLight(&h, &d, Lights[i],softShadows);
brightness2 = brightness2 + cur;
if (cur < min)
min = cur;
if (cur > max)
max = cur;
}
int addSamples = 0;
if (softShadows) {
double diff = max - min;
if (diff != 0.0)
addSamples = (int)(1000.0 * pow(diff,4) / diff);
}
for (int j = 0; j < addSamples; j++){
brightness2 = brightness2 + SampleLight(&h, &d, Lights[i],1);
}
brightness = brightness + ((Lights[i]->texture.color * Lights[i]->intensity) * (brightness2 / (double)(init + addSamples)));
}
return brightness;
}
double Scene::Occlusion(hit h, Vec d) {
double dist = 0.0;
double th = occlusionRadius;
if ( ! occlusionMethod)
srand(pass++);
for (int i = 0; i < occlusionSamples; i++) {
Vec dir(RAND - 0.5, RAND - 0.5, RAND - 0.5); //Uneven distribution, fix this
dir.normalize();
if ((h.normal.dot(dir) < 0.0) == (h.normal.dot(d) < 0.0))
dir = dir * -1.0;
hit h2 = Closest(h.location, dir);
if (h2.contact) {
double dis = h2.distance;
dis = dis * (1.0-h.normal.dot(h2.normal));
if (dis > th)
dis = th;
dist += dis;
} else {
dist += th;
}
}
dist /= occlusionSamples;
if (dist > th)
dist = th;
return dist / th;
}
hit Scene::Closest(Vec o, Vec d){
double closest = INF;
hit closestHit(0);
Vec newO = o + (d.GetNormalized() * 0.0001);//step forward a little
for (uint i = 0; i < Objects.size(); i++) {
hit h = Objects[i]->Trace(newO, d);
if (h.contact && h.distance < closest) {
closest = h.distance;
closestHit = h;
}
}
return closestHit;
}
Vec Scene::Cast(Vec o, Vec d, int depth) {
Vec PixelColor;
Object *ob;
hit closestHit = Closest(o,d);
if (closestHit.contact == 0) {
PixelColor[0] = PixelColor[1] = 1.0 - d[2] * 1.0;
PixelColor[2] = 1.0;
return PixelColor;
} else {
ob = closestHit.object;
Vec c = ob->Color(closestHit);
Vec l;
if (ob->texture.intensity == 1.0)
l = Vec(1.0,1.0,1.0);
else
l = TraceLight(closestHit,d);
if (ob->texture.reflection < 1.0) {//Dont bother with color if it is 100% reflective
PixelColor[0] = c[0] * l[0];
PixelColor[1] = c[1] * l[1];
PixelColor[2] = c[2] * l[2];
}
}
if (ob->texture.reflection > 0.0 && depth > 0) {
Vec reflect = d - (closestHit.normal * 2.0 * d.dot(closestHit.normal));
reflect = Cast(closestHit.location,reflect,depth-1);
PixelColor = PixelColor + (reflect * ob->texture.reflection * 0.75);
}
if (occlusion && ob->texture.intensity != 1.0)
PixelColor = PixelColor * (0.5+(Occlusion(closestHit,d)/2.0));
return PixelColor;
}