forked from pedrofe/vr_camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVRCamera.cpp
executable file
·474 lines (429 loc) · 11.9 KB
/
VRCamera.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <ai.h>
#include <ai_cameras.h>
#include <ai_nodes.h>
#include <cstring>
#include <ai_metadata.h>
#include <ai_shaders.h>
AI_CAMERA_NODE_EXPORT_METHODS(VRCameraMethods);
namespace
{
#define _mode (params[0].INT )
#define _projection (params[1].INT )
#define _eyeSeparation (params[2].FLT )
#define _eyeToNeckDistance (params[3].FLT )
#define _topMergeMode (params[4].INT )
#define _topMergeAngle (params[5].FLT )
#define _topMergeExp (params[6].FLT )
#define _bottomMergeMode (params[7].INT )
#define _bottomMergeAngle (params[8].FLT )
#define _bottomMergeExp (params[9].FLT )
#define _mergeShader (params[10].FLT )
enum mode
{
M_SBS = 0,
M_OU,
M_UO,
M_LE,
M_RE
};
const char* mode_list[] =
{
"Side by Side",
"Over Under",
"Under Over",
"Left Eye",
"Right Eye",
NULL
};
enum projection
{
P_LATLONG = 0,
P_CUBEMAP_NVD,
P_CUBEMAP_3x2
};
const char* projection_list[] =
{
"Latlong",
"Cube Map (NVIDIA)",
"Cube Map (3x2)",
NULL
};
enum merge_mode
{
M_OFF = 0,
M_LINEAR,
M_COS,
M_SHADER
};
const char* merge_mode_list[] =
{
"Off",
"Linear",
"Cos",
"Shader",
NULL
};
enum eye
{
E_RIGHT_EYE = 0,
E_LEFT_EYE
};
enum merge_zone
{
Z_NONE = 0,
Z_UP,
Z_BOTTOM
};
struct CameraData
{
AtShaderGlobals* sg;
float frame_aspect_ratio;
int mode;
int projection;
float eyeSeparation;
float eyeToNeckDistance;
int topMergeMode;
float topMergeAngle;
float topMergeExp;
int bottomMergeMode;
float bottomMergeAngle;
float bottomMergeExp;
float mergeValue;
AtNode* mergeShader;
};
};
node_parameters
{
AiParameterEnum("mode", 0, mode_list);
AiParameterEnum("projection", 0, projection_list);
AiParameterFlt("eyeSeparation", 0.65f);
AiParameterFlt("eyeToNeckDistance", 0.65f);
AiParameterEnum("topMergeMode", 2, merge_mode_list);
AiParameterFlt("topMergeAngle", 0.0f);
AiParameterFlt("topMergeExp", 1.0f);
AiParameterEnum("bottomMergeMode", 2, merge_mode_list);
AiParameterFlt("bottomMergeAngle", 0.0f);
AiParameterFlt("bottomMergeExp", 1.0f);
AiParameterRGB("mergeShader", 0.0f, 0.0f, 0.0f);
}
node_initialize
{
CameraData *data = (CameraData*) AiMalloc(sizeof(CameraData));
data->sg = AiShaderGlobals();
data->sg->Rt = AI_RAY_CAMERA;
AtNode *options = AiUniverseGetOptions();
float xres = AiNodeGetInt(options, "xres");
float yres = AiNodeGetInt(options, "yres");
float pixel_aspect_ratio = AiNodeGetFlt(options, "aspect_ratio");
data->frame_aspect_ratio = xres / pixel_aspect_ratio / yres;
AiCameraInitialize(node, data);
}
node_update
{
CameraData* data = (CameraData*) AiCameraGetLocalData(node);
data->mode = _mode;
data->projection = _projection;
data->eyeSeparation = _eyeSeparation;
data->eyeToNeckDistance = _eyeToNeckDistance;
data->topMergeMode = _topMergeMode;
data->topMergeAngle = _topMergeAngle * AI_PI / 180.0f;
data->topMergeExp = _topMergeExp;
data->bottomMergeMode = _bottomMergeMode;
data->bottomMergeAngle = _bottomMergeAngle * AI_PI / 180.0f;
data->bottomMergeExp = _bottomMergeExp;
// Check that merge angles and exponents are in the correct ranges
if(data->topMergeExp < 0.0f)
data->topMergeExp = 0.0f;
if(data->bottomMergeExp < 0.0f)
data->bottomMergeExp = 0.0f;
if(data->topMergeAngle > AI_PIOVER2 - AI_EPSILON)
data->topMergeAngle = AI_PIOVER2 - AI_EPSILON;
else if(data->topMergeAngle < -AI_PIOVER2 + AI_EPSILON)
data->topMergeAngle = -AI_PIOVER2 + AI_EPSILON;
if(data->bottomMergeAngle < -AI_PIOVER2 + AI_EPSILON)
data->bottomMergeAngle = -AI_PIOVER2 + AI_EPSILON;
else if(data->bottomMergeAngle > data->topMergeAngle)
data->bottomMergeAngle = data->topMergeAngle;
data->mergeValue = _mergeShader;
data->mergeShader = AiNodeGetLink(node, "mergeShader");
AiCameraUpdate(node, false);
}
node_finish
{
CameraData* data = (CameraData *)AiCameraGetLocalData(node);
if (data)
{
AiShaderGlobalsDestroy(data->sg);
AiFree(data);
}
AiCameraDestroy(node);
}
camera_create_ray
{
CameraData* data = (CameraData*) AiCameraGetLocalData(node);
int currentEye = E_RIGHT_EYE;
int mergeZone = Z_NONE;
float sx = input->sx;
float sy = input->sy * data->frame_aspect_ratio;
// These values are needed for every projection to be able to calculate
// the camera position due eye separation and pole merge
float theta = 0.0f;
float phi = 0.0f;
float sin_theta = 0.0f;
float cos_theta = 0.0f;
////////////////////////////////////////////////////
// Stereoscopic mode.
// Decide what eye we are rendering. If needed update sx and sy.
////////////////////////////////////////////////////
if(data->mode == M_SBS)
{
if(sx < 0)
{
currentEye = E_LEFT_EYE;
sx = 2 * (sx + 0.5f);
}
else
{
currentEye = E_RIGHT_EYE;
sx = 2 * (sx - 0.5f);
}
}
else if (data->mode == M_OU)
{
if(sy < 0)
{
currentEye = E_RIGHT_EYE;
sy = 2 * (sy + 0.5f);
}
else
{
currentEye = E_LEFT_EYE;
sy = 2 * (sy - 0.5f);
}
}
else if (data->mode == M_UO)
{
if(sy < 0)
{
currentEye = E_LEFT_EYE;
sy = 2 * (sy + 0.5f);
}
else
{
currentEye = E_RIGHT_EYE;
sy = 2 * (sy - 0.5f);
}
}
else if (data->mode == M_LE)
{
currentEye = E_LEFT_EYE;
}
else if (data->mode == M_RE)
{
currentEye = E_RIGHT_EYE;
}
////////////////////////////////////////////////////
// Proyection type
////////////////////////////////////////////////////
if (data->projection == P_LATLONG)
{
// Calculate spherical angles
theta = AI_PI * sx;
phi = AI_PIOVER2 * sy;
sin_theta = sinf(theta);
cos_theta = cosf(theta);
const float sin_phi = sinf(phi);
const float cos_phi = cosf(phi);
// normalized direction
output->dir.x = sin_theta * cos_phi;
output->dir.y = sin_phi;
output->dir.z = -cos_theta * cos_phi;
}
else if(data->projection == P_CUBEMAP_NVD)
{
if (sx < -2*(1.0f / 3.0f))
{
// P_PX
output->dir.x = 1.0f;
output->dir.y = -6*(sx+(5.0f/6.0f));
output->dir.z = sy;
}
else if (sx < -(1.0f / 3.0f))
{
// P_NX
output->dir.x = -1.0f;
output->dir.y = 6*(sx+(3.0f/6.0f));
output->dir.z = sy;
}
else if (sx < 0.0f)
{
// P_PZ
output->dir.x = 6*(sx+(1.0f/6.0f));
output->dir.y = -sy;
output->dir.z = 1.0f;
}
else if (sx < (1.0f / 3.0f))
{
// P_NZ
output->dir.x = 6*(sx-(1.0f/6.0f));
output->dir.y = sy;
output->dir.z = -1.0f;
}
else if (sx < (2.0f / 3.0f))
{
// P_PY
output->dir.x = 6*(sx-(3.0f/6.0f));
output->dir.y = 1.0f;
output->dir.z = sy;
}
else
{
// P_NY
output->dir.x = -6*(sx-(5.0f/6.0f));
output->dir.y = -1.0f;
output->dir.z = sy;
}
theta = atan2(output->dir.x,-output->dir.z);
phi = AI_PIOVER2 - acos(output->dir.y / sqrt(output->dir.x * output->dir.x + output->dir.y * output->dir.y + output->dir.z * output->dir.z ));
sin_theta = sinf(theta);
cos_theta = cosf(theta);
}
else if(data->projection == P_CUBEMAP_3x2)
{
if (sy < 0.0f)
{
if (sx < -(1.0f / 3.0f))
{
// P_PX
output->dir.x = 1.0f;
output->dir.y = 2*(sy+0.5f);
output->dir.z = 3*(sx+(2.0f/3.0f));
}
else if (sx < (1.0f / 3.0f))
{
// P_PZ
output->dir.x = -3*(sx);
output->dir.y = 2*(sy+0.5f);
output->dir.z = 1.0f;
}
else
{
// P_NX
output->dir.x = -1.0f;
output->dir.y = 2*(sy+0.5f);
output->dir.z = -3*(sx-(2.0f/3.0f));
}
}
else
{
if (sx < -(1.0f / 3.0f))
{
// P_NZ
output->dir.x = 3*(sx+(2.0f/3.0f));
output->dir.y = 2*(sy-0.5f);
output->dir.z = -1.0f;
}
else if (sx < (1.0f / 3.0f))
{
// P_PY
output->dir.x = -3*(sx);
output->dir.y = 1.0f;
output->dir.z = -2*(sy-0.5f);
}
else
{
// P_NY
output->dir.x = -3*(sx-(2.0f/3.0f));
output->dir.y = -1.0f;
output->dir.z = 2*(sy-0.5f);
}
}
theta = atan2(output->dir.x,-output->dir.z);
phi = AI_PIOVER2 - acos(output->dir.y / sqrt(output->dir.x * output->dir.x + output->dir.y * output->dir.y + output->dir.z * output->dir.z ));
sin_theta = sinf(theta);
cos_theta = cosf(theta);
}
////////////////////////////////////////////////////
// Update origin position usinf eye separation
////////////////////////////////////////////////////
if(currentEye == E_LEFT_EYE)
{
output->origin.x = -0.5*data->eyeSeparation*cos_theta + data->eyeToNeckDistance*sin_theta;
output->origin.z = -0.5*data->eyeSeparation*sin_theta - data->eyeToNeckDistance*cos_theta;
}
else
{
output->origin.x = 0.5*data->eyeSeparation*cos_theta + data->eyeToNeckDistance*sin_theta;
output->origin.z = 0.5*data->eyeSeparation*sin_theta - data->eyeToNeckDistance*cos_theta;
}
////////////////////////////////////////////////////
// Merge poles to avoid artifacts
////////////////////////////////////////////////////
// merge method:
// phi > 0:
// linear:
// (-2*phi + pi) / (pi - 2*offset)
// cos:
// ( cos( (phi - offset) * ( pi / (pi - 2*offset) ) ) ) ^ exp
// phi < 0:
// linear:
// (2*phi + pi) / (pi + 2*offset)
// cos:
// ( cos( (phi - offset) * ( -pi / (pi + 2*offset) ) ) ) ^ exp
if(phi > data->topMergeAngle)
{
mergeZone = Z_UP;
}
else if(phi < data->bottomMergeAngle)
{
mergeZone = Z_BOTTOM;
}
float factor = 1.0f;
if(((data->topMergeMode == M_LINEAR) && (mergeZone == Z_UP)) ||
((data->bottomMergeMode == M_LINEAR) && (mergeZone == Z_BOTTOM)))
{
if(mergeZone == Z_UP)
factor = (-2.0f * phi + AI_PI) / (AI_PI - 2*data->topMergeAngle);
else
factor = (2.0f * phi + AI_PI) / (AI_PI + 2*data->bottomMergeAngle);
}
else if(((data->topMergeMode == M_COS) && (mergeZone == Z_UP)) ||
((data->bottomMergeMode == M_COS) && (mergeZone == Z_BOTTOM)))
{
if(mergeZone == Z_UP)
factor = powf( MAX(0.0f, cosf( (phi - data->topMergeAngle) * (AI_PI / (AI_PI - 2.0f*data->topMergeAngle)) )), data->topMergeExp );
else
factor = powf( MAX(0.0f, cosf( (phi - data->bottomMergeAngle) * (-AI_PI / (AI_PI + 2.0f*data->bottomMergeAngle)) )), data->bottomMergeExp );
}
else if(((data->topMergeMode == M_SHADER) && (mergeZone == Z_UP)) ||
((data->bottomMergeMode == M_SHADER) && (mergeZone == Z_BOTTOM)))
{
if(data->mergeShader == NULL)
{
factor = data->mergeValue;
}
else
{
// copy and prepare the Shader Globals
AtShaderGlobals sg = *(data->sg);
sg.tid = tid;
sg.u = 0.5f * (theta / AI_PI + 1.0f);
sg.v = 0.5f * (phi / AI_PIOVER2 + 1.0f);
AiShaderEvaluate(data->mergeShader, &sg);
factor = sg.out.RGB.r;
}
}
output->origin.x *= factor;
output->origin.z *= factor;
}
node_loader
{
if (i > 0)
return false;
node->methods = VRCameraMethods;
node->output_type = AI_TYPE_NONE;
node->name = "vr_camera";
node->node_type = AI_NODE_CAMERA;
strcpy(node->version, AI_VERSION);
return true;
}