-
Notifications
You must be signed in to change notification settings - Fork 0
/
HaloRenderingThread.cpp
370 lines (343 loc) · 14.1 KB
/
HaloRenderingThread.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
Copyright (c) 2012, Dávid Csirmaz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
DEFINE_EVENT_TYPE(wxEVT_HALO_RENDERING_THREAD_NOTIFY)
void HaloRenderingThread::sendNotifyString(const wxString &str, int terminate)
{
wxCommandEvent evt(wxEVT_HALO_RENDERING_THREAD_NOTIFY, wxID_ANY);
evt.SetString(str);
evt.SetInt(terminate);
setupStruct.notifee->AddPendingEvent(evt);
}
Matrix HaloRenderingThread::getOrientationMatrix(CrystalDescriptor::OrientationType orientation)
{
Matrix m;
switch (orientation)
{
case CrystalDescriptor::OT_RANDOM:
{
/* First rotate the crystal around the Z axis:*/
Matrix zRot = createRotationMatrix(Vector3(0, 0, 1), randFloat(-3.14159265, 3.14159265));
/* Then rotate the Z axis into an uniformly distributed position*/
const Vector3 newAxis = getRandomVector();
Vector3 rotAxis = Vector3(0, 0, 1) % newAxis;
Matrix axisRot = getIdentityMatrix();
if (rotAxis * rotAxis != 0)
{
rotAxis /= ~rotAxis;
double rotAngle = acos(Vector3(0, 0, 1) * newAxis);
axisRot = createRotationMatrix(rotAxis, rotAngle);
}
return zRot % axisRot;
}
break;
case CrystalDescriptor::OT_PARRY:
{
/* For hexagonal prisms, one side face is horizontal. */
m = createRotationMatrix(Vector3(0, 1, 0), randFloat(0, 3.1415926536));
}
break;
case CrystalDescriptor::OT_COLUMN:
{
/* Main axis horizontal. */
Matrix n = createRotationMatrix(Vector3(0, 0, 1), randFloat(0, 3.1415926536));
Matrix o = createRotationMatrix(Vector3(0, 1, 0), randFloat(0, 3.1415926536));
m = n % o;
}
break;
case CrystalDescriptor::OT_PLATE:
{
/* Main axis vertical. */
m = createRotationMatrix(Vector3(1, 0, 0), 3.14159265636 * 0.5) % createRotationMatrix(Vector3(0, 1, 0), randFloat(0, 3.1415926536));
}
break;
case CrystalDescriptor::OT_LOWITZ:
{
/* Rotation around a diagonal axis. The axis is horizontal.*/
m =
createRotationMatrix(Vector3(1, 0, 0), 3.14159265636 * randFloat(-3.1415 * 0.5, 3.1415 * 0.5)) %
createRotationMatrix(Vector3(0, 1, 0), randFloat(0, 3.1415926536));
}
break;
default:
{
sendNotifyString(wxT("Invalid orientation type."), 1);
}
}
return m;
}
void HaloRenderingThread::freeBuffer(uint32_t *ptr)
{
delete[] ptr;
}
void HaloRenderingThread::freeMap(std::map<RayPathId, RayPathDescriptor> *ptr)
{
delete ptr;
}
wxThread::ExitCode HaloRenderingThread::Entry()
{
wxCommandEvent evt(wxEVT_HALO_RENDERING_THREAD_NOTIFY, wxID_ANY);
setupStruct.resultValid = false;
setupStruct.imageBuffer = 0;
setupStruct.rayPaths = new map<RayPathId, RayPathDescriptor>;
if (setupStruct.crystals.size() == 0)
{
evt.SetString(wxT("No crystal population set up."));
evt.SetInt(1);
setupStruct.notifee->AddPendingEvent(evt);
return 0;
}
// Cast rays on crystals and refracted rays will form a pixel.
int crystalTypeIndex = 0;
CrystalDescriptor *currentDescriptor = &setupStruct.crystals[0];
const int N_CRYSTAL_TYPES = setupStruct.crystals.size();
int crystalsRemaining = currentDescriptor->populationWeight;
Mesh currentMesh;
size_t percentStep = setupStruct.crystalCount / 100;
if (percentStep < 1) percentStep = 1;
Vector3 sunPos(0, 0, -100000);
rotate2d(sunPos.y, sunPos.z, setupStruct.solarAltitude * 3.14159265636 / 180.0);
// Two prependicular vector
Vector3 prepX = sunPos % Vector3(0, 1, 0); // FIXME: Sun on the zenith
Vector3 prepY = sunPos % prepX;
prepX /= ~prepX;
prepY /= ~prepY;
setupStruct.deleteBuffer = freeBuffer;
setupStruct.deleteMap = freeMap;
size_t size = setupStruct.imageSize;
setupStruct.imageBuffer = new uint32_t[size * size * 6];
memset(setupStruct.imageBuffer, 0, size * size * 6 * sizeof(uint32_t));
uint32_t *leftPlane = setupStruct.imageBuffer; // 0th plane
uint32_t *rightPlane = setupStruct.imageBuffer + size * size; // 1st plane
uint32_t *topPlane = setupStruct.imageBuffer + 2 * size * size; // 2nd plane
uint32_t *bottomPlane = setupStruct.imageBuffer + 3 * size * size; // 3rd plane
uint32_t *backPlane = setupStruct.imageBuffer + 4 * size * size; // 4th plane
uint32_t *frontPlane = setupStruct.imageBuffer + 5 * size * size; // 5th plane
double halfImageSize = size * 0.5;
int maxRays = (setupStruct.maxRayCastInfoSize * 1000000) / (sizeof(RayPathDescriptor) + sizeof(RayPathId));
if (maxRays == 0) maxRays = 1;
int recordRayModulus = setupStruct.crystalCount / maxRays;
if (!recordRayModulus) recordRayModulus = 1;
struct RefractionColor
{
int rgb;
double refractionIndex;
};
RefractionColor colors[] =
{
{ 0x0000FF, 1.3072 },
{ 0x0080FF, 1.3094 },
{ 0x00FFFF, 1.31 },
{ 0x00FF80, 1.3107 },
{ 0x00FF00, 1.3114 },
{ 0x80FF00, 1.3125 },
{ 0xFFFF00, 1.3136 },
{ 0xFF8000, 1.3147 },
{ 0xFF0000, 1.3158 },
{ 0xFF0040, 1.3172 },
{ 0xFF0080, 1.32 },
};
const int N_COLORS = sizeof(colors) / sizeof(colors[0]);
// Cast many rays.
for (size_t i = 0; i < setupStruct.crystalCount; i++)
{
if (setupStruct.cancelled)
{
// If the user shut the rendering down...
delete[] setupStruct.imageBuffer;
setupStruct.imageBuffer = 0;
return 0;
}
if (!crystalsRemaining)
{
// We iterate through the crystals based on their population weights.
crystalTypeIndex++;
if (crystalTypeIndex >= N_CRYSTAL_TYPES) crystalTypeIndex = 0;
currentDescriptor = &setupStruct.crystals[crystalTypeIndex];
crystalsRemaining = currentDescriptor->populationWeight;
}
// Get the raw mesh
currentMesh = currentDescriptor->mesh;
// Rotate it according to the orientation.
Matrix orientationMatrix = getOrientationMatrix(currentDescriptor->orientation);
// Apply wobbliness
Vector3 wobbleRotationAxis(1, 0, 0);
rotate2d(wobbleRotationAxis.x, wobbleRotationAxis.z, randFloat(0, 3.1415));
double wobblinessLimit = currentDescriptor->wobbliness * 3.1415 / 180.0;
Matrix wobbleMatrix = createRotationMatrix(
wobbleRotationAxis,
randFloatNormal(
0,
wobblinessLimit
)
);
Matrix transformation = orientationMatrix % wobbleMatrix;
transformMesh(currentMesh, transformation);
// Crystal created, now cast a ray on it.
Vector3 offset = prepX * randFloat(-1, 1) + prepY * randFloat(-1, 1);
vector<RayPath> rayPaths;
// Compute color here
double colorCode = randFloat(0, N_COLORS - 1);
RefractionColor prev = colors[(int)(floor(colorCode))];
RefractionColor next = colors[(int)(ceil(colorCode))];
double kColor = colorCode - floor(colorCode);
RefractionColor currentColor;
currentColor.rgb = prev.rgb;
currentColor.refractionIndex = (1 - kColor) * prev.refractionIndex + kColor * next.refractionIndex;
// Compute real poisition of the ray (Sun is a disk)
Vector3 realSunPos = sunPos;
Vector3 rayRotVector;
double rayRotVectorLength;
do
{
rayRotVector = realSunPos % getRandomVector();
rayRotVectorLength = ~rayRotVector;
}
while (rayRotVectorLength == 0);
rayRotVector /= rayRotVectorLength;
realSunPos = transformVector(
createRotationMatrix(
rayRotVector,
randFloat(
0,
setupStruct.solarDiskRadius * 3.1415926536 / 180.0
)
),
realSunPos
);
computeRayPathInGlassMesh(currentMesh, currentColor.refractionIndex, realSunPos + offset, -realSunPos, 0.01, rayPaths);
/* Project rays on the six planes. */
for (size_t j = 0; j < rayPaths.size(); j++)
{
RayPath ¤t = rayPaths[j];
size_t pathLength = current.size();
Vector3 exitDir = current[pathLength - 1].first - current[pathLength - 2].first;
Vector3 projectionDir = -exitDir;
double xPos, yPos;
// select the plane to project on.
uint32_t *plane;
double xm = 1, ym = 1;
int planeId;
if ((fabs(projectionDir.x) > fabs(projectionDir.y)) && (fabs(projectionDir.x) > fabs(projectionDir.z)))
{
if (projectionDir.x < 0)
{
plane = leftPlane;
planeId = 0;
xm = -1;
}
else
{
plane = rightPlane;
planeId = 1;
}
xPos = xm * projectionDir.z / fabs(projectionDir.x) * halfImageSize + halfImageSize;
yPos = -ym * projectionDir.y / fabs(projectionDir.x) * halfImageSize + halfImageSize;
}
if ((fabs(projectionDir.y) > fabs(projectionDir.x)) && (fabs(projectionDir.y) > fabs(projectionDir.z)))
{
if (projectionDir.y < 0)
{
plane = bottomPlane;
planeId = 3;
ym = -1;
}
else
{
plane = topPlane;
planeId = 2;
}
xPos = xm * projectionDir.x / fabs(projectionDir.y) * halfImageSize + halfImageSize;
yPos = -ym * projectionDir.z / fabs(projectionDir.y) * halfImageSize + halfImageSize;
}
if ((fabs(projectionDir.z) > fabs(projectionDir.x)) && (fabs(projectionDir.z) > fabs(projectionDir.y)))
{
if (projectionDir.z < 0)
{
plane = frontPlane;
planeId = 5;
}
else
{
plane = backPlane;
planeId = 4;
xm = -1;
}
xPos = xm * projectionDir.x / fabs(projectionDir.z) * halfImageSize + halfImageSize;
yPos = -ym * projectionDir.y / fabs(projectionDir.z) * halfImageSize + halfImageSize;
}
// Calculate the new intensity of the pixel.
xPos = clampInInt(xPos, 0, size - 1);
yPos = clampInInt(yPos, 0, size - 1);
int prevPixel = plane[(int)(yPos) * size + (int)(xPos)];
int nextPixel = 0;
double intensity = current[pathLength - 2].second * 20;
for (int j = 0; j < 3; j++)
{
int currentSaturation = (prevPixel >> (8 * j)) & 0xFF;
int toAdd = (int)(((currentColor.rgb >> (8 * j)) & 0xFF) * intensity) >> 8;
int nextSaturation;
if (currentSaturation + toAdd > 255) nextSaturation = 255;
else nextSaturation = currentSaturation + toAdd;
nextPixel += (1 << (8 * j)) * nextSaturation * setupStruct.pixelIntensity;
}
plane[(int)(yPos) * size + (int)(xPos)] = nextPixel;
// Record the ray if needed
if (!(i % recordRayModulus))
{
RayPathId pixelId(planeId, (int)xPos, (int)yPos);
RayPathDescriptor theDescriptor(
¤tDescriptor->mesh,
transformation,
realSunPos + offset,
-realSunPos,
intensity
);
map<RayPathId, RayPathDescriptor>::iterator it = setupStruct.rayPaths->find(pixelId);
if (it == setupStruct.rayPaths->end())
{
// If not found, insert it as new
setupStruct.rayPaths->insert(
make_pair(
pixelId,
theDescriptor
)
);
}
else if (it->second.intensity < intensity)
{
// If found, update it if the current ray is more intense
it->second = theDescriptor;
}
}
}
if (i % percentStep == 0)
{
sendNotifyString(wxString::Format(wxT("Casting rays: %d%%"), i / percentStep));
}
crystalsRemaining--;
}
evt.SetString(wxT("Completed."));
evt.SetInt(1); //< 1 means the operation is finished.
setupStruct.resultValid = true;
setupStruct.notifee->AddPendingEvent(evt);
return 0;
}