0031279: Visualization, TKOpenGl - environment background is misplaced within Ray...
[occt.git] / src / Shaders / RaytraceBase.fs
CommitLineData
3a9b5dc8 1#ifdef ADAPTIVE_SAMPLING
2 #extension GL_ARB_shader_image_load_store : require
e084dbbc 3#endif
4#ifdef ADAPTIVE_SAMPLING_ATOMIC
3a9b5dc8 5 #extension GL_NV_shader_atomic_float : require
6#endif
7
25ef750e 8#ifdef USE_TEXTURES
9 #extension GL_ARB_bindless_texture : require
10#endif
11
fc73a202 12//! Normalized pixel coordinates.
13in vec2 vPixel;
14
50d0e1ce 15//! Sub-pixel offset in X direction for FSAA.
16uniform float uOffsetX = 0.f;
17//! Sub-pixel offset in Y direction for FSAA.
18uniform float uOffsetY = 0.f;
19
fc73a202 20//! Origin of viewing ray in left-top corner.
21uniform vec3 uOriginLT;
22//! Origin of viewing ray in left-bottom corner.
23uniform vec3 uOriginLB;
24//! Origin of viewing ray in right-top corner.
25uniform vec3 uOriginRT;
26//! Origin of viewing ray in right-bottom corner.
27uniform vec3 uOriginRB;
28
312a4043 29//! Width of the rendering window.
30uniform int uWinSizeX;
31//! Height of the rendering window.
32uniform int uWinSizeY;
33
fc73a202 34//! Direction of viewing ray in left-top corner.
35uniform vec3 uDirectLT;
36//! Direction of viewing ray in left-bottom corner.
37uniform vec3 uDirectLB;
38//! Direction of viewing ray in right-top corner.
39uniform vec3 uDirectRT;
40//! Direction of viewing ray in right-bottom corner.
41uniform vec3 uDirectRB;
42
a89742cf 43//! Inverse model-view-projection matrix.
25ef750e 44uniform mat4 uUnviewMat;
45
1d865689 46//! Model-view-projection matrix.
47uniform mat4 uViewMat;
48
e2da917a 49//! Texture buffer of data records of bottom-level BVH nodes.
fc73a202 50uniform isamplerBuffer uSceneNodeInfoTexture;
e2da917a 51//! Texture buffer of minimum points of bottom-level BVH nodes.
fc73a202 52uniform samplerBuffer uSceneMinPointTexture;
e2da917a 53//! Texture buffer of maximum points of bottom-level BVH nodes.
fc73a202 54uniform samplerBuffer uSceneMaxPointTexture;
84c71f29 55//! Texture buffer of transformations of high-level BVH nodes.
56uniform samplerBuffer uSceneTransformTexture;
fc73a202 57
fc73a202 58//! Texture buffer of vertex coords.
59uniform samplerBuffer uGeometryVertexTexture;
60//! Texture buffer of vertex normals.
61uniform samplerBuffer uGeometryNormalTexture;
25ef750e 62#ifdef USE_TEXTURES
63 //! Texture buffer of per-vertex UV-coordinates.
64 uniform samplerBuffer uGeometryTexCrdTexture;
65#endif
fc73a202 66//! Texture buffer of triangle indices.
67uniform isamplerBuffer uGeometryTriangTexture;
68
69//! Texture buffer of material properties.
70uniform samplerBuffer uRaytraceMaterialTexture;
71//! Texture buffer of light source properties.
72uniform samplerBuffer uRaytraceLightSrcTexture;
89d855ba 73
74#ifdef BACKGROUND_CUBEMAP
75 //! Environment cubemap texture.
76 uniform samplerCube uEnvMapTexture;
77 //! Coefficient of Y controlling horizontal flip of cubemap
78 uniform int uYCoeff;
79 //! Coefficient of Z controlling vertical flip of cubemap
80 uniform int uZCoeff;
81#else
82 //! Environment map texture.
83 uniform sampler2D uEnvMapTexture;
84#endif
fc73a202 85
86//! Total number of light sources.
87uniform int uLightCount;
88//! Intensity of global ambient light.
89uniform vec4 uGlobalAmbient;
90
189f85a3 91//! Enables/disables hard shadows.
92uniform int uShadowsEnabled;
93//! Enables/disables specular reflections.
94uniform int uReflectEnabled;
89d855ba 95//! Enables/disables environment map lighting.
96uniform int uEnvMapEnabled;
189f85a3 97//! Enables/disables environment map background.
89d855ba 98uniform int uEnvMapForBack;
fc73a202 99
100//! Radius of bounding sphere of the scene.
101uniform float uSceneRadius;
102//! Scene epsilon to prevent self-intersections.
103uniform float uSceneEpsilon;
104
25ef750e 105#ifdef USE_TEXTURES
106 //! Unique 64-bit handles of OpenGL textures.
47e9c178 107 uniform uvec2 uTextureSamplers[MAX_TEX_NUMBER];
25ef750e 108#endif
109
3a9b5dc8 110#ifdef ADAPTIVE_SAMPLING
111 //! OpenGL image used for accumulating rendering result.
e084dbbc 112 volatile restrict layout(r32f) uniform image2D uRenderImage;
3a9b5dc8 113
e084dbbc 114#ifdef ADAPTIVE_SAMPLING_ATOMIC
3a9b5dc8 115 //! OpenGL image storing offsets of sampled pixels blocks.
e084dbbc 116 coherent restrict layout(rg32i) uniform iimage2D uOffsetImage;
117#else
118 //! OpenGL image defining per-tile amount of samples.
119 volatile restrict layout(r32i) uniform iimage2D uTilesImage;
120#endif
66d1cdc6 121
122 //! Screen space tile size.
123 uniform ivec2 uTileSize;
3a9b5dc8 124#endif
125
189f85a3 126//! Top color of gradient background.
127uniform vec4 uBackColorTop = vec4 (0.0);
128//! Bottom color of gradient background.
129uniform vec4 uBackColorBot = vec4 (0.0);
130
b27ab03d 131//! Aperture radius of camera used for depth-of-field
132uniform float uApertureRadius = 0.f;
133
134//! Focal distance of camera used for depth-of field
135uniform float uFocalPlaneDist = 10.f;
136
137//! Camera position used for projective mode
138uniform vec3 uEyeOrig;
139
140//! Camera view direction used for projective mode
141uniform vec3 uEyeView;
142
143//! Camera's screen vertical direction used for projective mode
144uniform vec3 uEyeVert;
145
146//! Camera's screen horizontal direction used for projective mode
147uniform vec3 uEyeSide;
148
149//! Camera's screen size used for projective mode
150uniform vec2 uEyeSize;
151
fc73a202 152/////////////////////////////////////////////////////////////////////////////////////////
153// Specific data types
189f85a3 154
fc73a202 155//! Stores ray parameters.
156struct SRay
157{
158 vec3 Origin;
189f85a3 159
fc73a202 160 vec3 Direct;
161};
162
163//! Stores intersection parameters.
164struct SIntersect
165{
166 float Time;
189f85a3 167
fc73a202 168 vec2 UV;
189f85a3 169
fc73a202 170 vec3 Normal;
171};
172
78607702 173//! Stores triangle's vertex indexes and vertexes itself
174struct STriangle
175{
176 ivec4 TriIndex;
177
178 vec3 Points[3];
179};
180
fc73a202 181/////////////////////////////////////////////////////////////////////////////////////////
182// Some useful constants
183
184#define MAXFLOAT 1e15f
185
dc9ef964 186#define SMALL vec3 (exp2 (-80.0f))
fc73a202 187
dc9ef964 188#define ZERO vec3 (0.0f, 0.0f, 0.0f)
189#define UNIT vec3 (1.0f, 1.0f, 1.0f)
fc73a202 190
dc9ef964 191#define AXIS_X vec3 (1.0f, 0.0f, 0.0f)
192#define AXIS_Y vec3 (0.0f, 1.0f, 0.0f)
193#define AXIS_Z vec3 (0.0f, 0.0f, 1.0f)
fc73a202 194
6e728f3b 195#define M_PI 3.141592653f
196#define M_2_PI 6.283185307f
197#define M_PI_2 1.570796327f
189f85a3 198
199#define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)
200
201// =======================================================================
202// function : MatrixRowMultiplyDir
203// purpose : Multiplies a vector by matrix
204// =======================================================================
205vec3 MatrixRowMultiplyDir (in vec3 v,
206 in vec4 m0,
207 in vec4 m1,
208 in vec4 m2)
209{
210 return vec3 (dot (m0.xyz, v),
211 dot (m1.xyz, v),
212 dot (m2.xyz, v));
213}
214
312a4043 215//! 32-bit state of random number generator.
216uint RandState;
217
218// =======================================================================
219// function : SeedRand
220// purpose : Applies hash function by Thomas Wang to randomize seeds
221// (see http://www.burtleburtle.net/bob/hash/integer.html)
222// =======================================================================
8c820969 223void SeedRand (in int theSeed, in int theSizeX, in int theRadius)
312a4043 224{
8c820969 225 RandState = uint (int (gl_FragCoord.y) / theRadius * theSizeX + int (gl_FragCoord.x) / theRadius + theSeed);
312a4043 226
227 RandState = (RandState + 0x479ab41du) + (RandState << 8);
228 RandState = (RandState ^ 0xe4aa10ceu) ^ (RandState >> 5);
229 RandState = (RandState + 0x9942f0a6u) - (RandState << 14);
230 RandState = (RandState ^ 0x5aedd67du) ^ (RandState >> 3);
231 RandState = (RandState + 0x17bea992u) + (RandState << 7);
232}
233
234// =======================================================================
235// function : RandInt
236// purpose : Generates integer using Xorshift algorithm by G. Marsaglia
237// =======================================================================
238uint RandInt()
239{
240 RandState ^= (RandState << 13);
241 RandState ^= (RandState >> 17);
242 RandState ^= (RandState << 5);
243
244 return RandState;
245}
246
247// =======================================================================
248// function : RandFloat
ee5befae 249// purpose : Generates a random float in 0 <= x < 1 range
312a4043 250// =======================================================================
251float RandFloat()
252{
253 return float (RandInt()) * (1.f / 4294967296.f);
254}
255
bc8c79bb 256// =======================================================================
257// function : MatrixColMultiplyPnt
258// purpose : Multiplies a vector by matrix
259// =======================================================================
260vec3 MatrixColMultiplyPnt (in vec3 v,
261 in vec4 m0,
262 in vec4 m1,
263 in vec4 m2,
264 in vec4 m3)
265{
47e9c178 266 return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z + m3.x,
267 m0.y * v.x + m1.y * v.y + m2.y * v.z + m3.y,
268 m0.z * v.x + m1.z * v.y + m2.z * v.z + m3.z);
bc8c79bb 269}
84c71f29 270
271// =======================================================================
bc8c79bb 272// function : MatrixColMultiplyDir
84c71f29 273// purpose : Multiplies a vector by matrix
274// =======================================================================
bc8c79bb 275vec3 MatrixColMultiplyDir (in vec3 v,
276 in vec4 m0,
277 in vec4 m1,
47e9c178 278 in vec4 m2)
84c71f29 279{
47e9c178 280 return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z,
281 m0.y * v.x + m1.y * v.y + m2.y * v.z,
282 m0.z * v.x + m1.z * v.y + m2.z * v.z);
84c71f29 283}
284
189f85a3 285//=======================================================================
286// function : InverseDirection
287// purpose : Returns safely inverted direction of the given one
288//=======================================================================
289vec3 InverseDirection (in vec3 theInput)
290{
291 vec3 anInverse = 1.f / max (abs (theInput), SMALL);
292
293 return mix (-anInverse, anInverse, step (ZERO, theInput));
294}
295
296//=======================================================================
297// function : BackgroundColor
298// purpose : Returns color of gradient background
299//=======================================================================
300vec4 BackgroundColor()
301{
e084dbbc 302#ifdef ADAPTIVE_SAMPLING_ATOMIC
3a9b5dc8 303
304 ivec2 aFragCoord = ivec2 (gl_FragCoord.xy);
305
66d1cdc6 306 ivec2 aTileXY = imageLoad (uOffsetImage, aFragCoord / uTileSize).xy * uTileSize;
3a9b5dc8 307
66d1cdc6 308 aTileXY.y += aFragCoord.y % min (uWinSizeY - aTileXY.y, uTileSize.y);
3a9b5dc8 309
310 return mix (uBackColorBot, uBackColorTop, float (aTileXY.y) / uWinSizeY);
311
312#else
313
189f85a3 314 return mix (uBackColorBot, uBackColorTop, vPixel.y);
3a9b5dc8 315
316#endif
189f85a3 317}
318
fc73a202 319/////////////////////////////////////////////////////////////////////////////////////////
320// Functions for compute ray-object intersection
321
b27ab03d 322//=======================================================================
323// function : sampleUniformDisk
324// purpose :
325//=======================================================================
326vec2 sampleUniformDisk ()
327{
328 vec2 aPoint;
329
330 float aKsi1 = 2.f * RandFloat () - 1.f;
331 float aKsi2 = 2.f * RandFloat () - 1.f;
332
333 if (aKsi1 > -aKsi2)
334 {
335 if (aKsi1 > aKsi2)
336 aPoint = vec2 (aKsi1, (M_PI / 4.f) * (0.f + aKsi2 / aKsi1));
337 else
338 aPoint = vec2 (aKsi2, (M_PI / 4.f) * (2.f - aKsi1 / aKsi2));
339 }
340 else
341 {
342 if (aKsi1 < aKsi2)
343 aPoint = vec2 (-aKsi1, (M_PI / 4.f) * (4.f + aKsi2 / aKsi1));
344 else
345 aPoint = vec2 (-aKsi2, (M_PI / 4.f) * (6.f - aKsi1 / aKsi2));
346 }
347
348 return vec2 (sin (aPoint.y), cos (aPoint.y)) * aPoint.x;
349}
350
fc73a202 351// =======================================================================
352// function : GenerateRay
353// purpose :
354// =======================================================================
355SRay GenerateRay (in vec2 thePixel)
356{
b27ab03d 357#ifndef DEPTH_OF_FIELD
358
fc73a202 359 vec3 aP0 = mix (uOriginLB, uOriginRB, thePixel.x);
360 vec3 aP1 = mix (uOriginLT, uOriginRT, thePixel.x);
361
362 vec3 aD0 = mix (uDirectLB, uDirectRB, thePixel.x);
363 vec3 aD1 = mix (uDirectLT, uDirectRT, thePixel.x);
a89742cf 364
365 vec3 aDirection = normalize (mix (aD0, aD1, thePixel.y));
366
367 return SRay (mix (aP0, aP1, thePixel.y), aDirection);
b27ab03d 368
369#else
370
371 vec2 aPixel = uEyeSize * (thePixel - vec2 (0.5f)) * 2.f;
372
373 vec2 aAperturePnt = sampleUniformDisk () * uApertureRadius;
374
375 vec3 aLocalDir = normalize (vec3 (
376 aPixel * uFocalPlaneDist - aAperturePnt, uFocalPlaneDist));
377
378 vec3 aOrigin = uEyeOrig +
379 uEyeSide * aAperturePnt.x +
380 uEyeVert * aAperturePnt.y;
381
382 vec3 aDirect = uEyeView * aLocalDir.z +
383 uEyeSide * aLocalDir.x +
384 uEyeVert * aLocalDir.y;
385
386 return SRay (aOrigin, aDirect);
387
388#endif
a89742cf 389}
390
391// =======================================================================
fc73a202 392// function : IntersectSphere
393// purpose : Computes ray-sphere intersection
394// =======================================================================
395float IntersectSphere (in SRay theRay, in float theRadius)
396{
397 float aDdotD = dot (theRay.Direct, theRay.Direct);
398 float aDdotO = dot (theRay.Direct, theRay.Origin);
399 float aOdotO = dot (theRay.Origin, theRay.Origin);
e2da917a 400
fc73a202 401 float aD = aDdotO * aDdotO - aDdotD * (aOdotO - theRadius * theRadius);
e2da917a 402
dc9ef964 403 if (aD > 0.0f)
fc73a202 404 {
dc9ef964 405 float aTime = (sqrt (aD) - aDdotO) * (1.0f / aDdotD);
fc73a202 406
dc9ef964 407 return aTime > 0.0f ? aTime : MAXFLOAT;
fc73a202 408 }
e2da917a 409
fc73a202 410 return MAXFLOAT;
411}
412
413// =======================================================================
414// function : IntersectTriangle
415// purpose : Computes ray-triangle intersection (branchless version)
416// =======================================================================
f2474958 417void IntersectTriangle (in SRay theRay,
418 in vec3 thePnt0,
419 in vec3 thePnt1,
420 in vec3 thePnt2,
421 out vec3 theUVT,
422 out vec3 theNorm)
fc73a202 423{
f2474958 424 vec3 aToTrg = thePnt0 - theRay.Origin;
425
fc73a202 426 vec3 aEdge0 = thePnt1 - thePnt0;
427 vec3 aEdge1 = thePnt0 - thePnt2;
e2da917a 428
fc73a202 429 theNorm = cross (aEdge1, aEdge0);
430
f2474958 431 vec3 theVect = cross (theRay.Direct, aToTrg);
e2da917a 432
f2474958 433 theUVT = vec3 (dot (theNorm, aToTrg),
434 dot (theVect, aEdge1),
435 dot (theVect, aEdge0)) * (1.f / dot (theNorm, theRay.Direct));
fc73a202 436
f2474958 437 theUVT.x = any (lessThan (theUVT, ZERO)) || (theUVT.y + theUVT.z) > 1.f ? MAXFLOAT : theUVT.x;
438}
e2da917a 439
f2474958 440#define EMPTY_ROOT ivec4(0)
e2da917a 441
f2474958 442//! Utility structure containing information about
443//! currently traversing sub-tree of scene's BVH.
444struct SSubTree
445{
446 //! Transformed ray.
447 SRay TrsfRay;
fc73a202 448
f2474958 449 //! Inversed ray direction.
450 vec3 Inverse;
fc73a202 451
f2474958 452 //! Parameters of sub-root node.
453 ivec4 SubData;
454};
84c71f29 455
05aa616d 456#define MATERIAL_AMBN(index) (19 * index + 0)
457#define MATERIAL_DIFF(index) (19 * index + 1)
458#define MATERIAL_SPEC(index) (19 * index + 2)
459#define MATERIAL_EMIS(index) (19 * index + 3)
460#define MATERIAL_REFL(index) (19 * index + 4)
461#define MATERIAL_REFR(index) (19 * index + 5)
462#define MATERIAL_TRAN(index) (19 * index + 6)
463#define MATERIAL_TRS1(index) (19 * index + 7)
464#define MATERIAL_TRS2(index) (19 * index + 8)
465#define MATERIAL_TRS3(index) (19 * index + 9)
47e9c178 466
47e9c178 467#define TRS_OFFSET(treelet) treelet.SubData.x
468#define BVH_OFFSET(treelet) treelet.SubData.y
469#define VRT_OFFSET(treelet) treelet.SubData.z
470#define TRG_OFFSET(treelet) treelet.SubData.w
471
f2474958 472//! Identifies the absence of intersection.
78607702 473#define INVALID_HIT ivec4 (-1)
f2474958 474
475//! Global stack shared between traversal functions.
476int Stack[STACK_SIZE];
477
478// =======================================================================
479// function : pop
480// purpose :
481// =======================================================================
482int pop (inout int theHead)
483{
484 int aData = Stack[theHead];
485
486 int aMask = aData >> 26;
487 int aNode = aMask & 0x3;
488
489 aMask >>= 2;
490
491 if ((aMask & 0x3) == aNode)
492 {
493 --theHead;
494 }
495 else
496 {
497 aMask |= (aMask << 2) & 0x30;
498
499 Stack[theHead] = (aData & 0x03FFFFFF) | (aMask << 26);
500 }
501
502 return (aData & 0x03FFFFFF) + aNode;
503}
47e9c178 504
fc73a202 505// =======================================================================
47e9c178 506// function : SceneNearestHit
507// purpose : Finds intersection with nearest scene triangle
fc73a202 508// =======================================================================
78607702 509STriangle SceneNearestHit (in SRay theRay, in vec3 theInverse, inout SIntersect theHit, out int theTrsfId)
fc73a202 510{
78607702 511 STriangle aTriangle = STriangle (INVALID_HIT, vec3[](vec3(0.0), vec3(0.0), vec3(0.0)));
fc73a202 512
47e9c178 513 int aNode = 0; // node to traverse
514 int aHead = -1; // pointer of stack
515 int aStop = -1; // BVH level switch
516
517 SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
518
f2474958 519 for (bool toContinue = true; toContinue; /* none */)
fc73a202 520 {
47e9c178 521 ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
fc73a202 522
523 if (aData.x == 0) // if inner node
524 {
47e9c178 525 aData.y += BVH_OFFSET (aSubTree);
e2da917a 526
f2474958 527 vec4 aHitTimes = vec4 (MAXFLOAT,
528 MAXFLOAT,
529 MAXFLOAT,
530 MAXFLOAT);
531
532 vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;
fc73a202 533
f2474958 534 vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;
535 vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;
536 vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
537 vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
538 vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;
539 vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;
540 vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
541 vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
e2da917a 542
f2474958 543 vec3 aTimeMax = max (aNodeMin0, aNodeMax0);
544 vec3 aTimeMin = min (aNodeMin0, aNodeMax0);
fc73a202 545
f2474958 546 float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
547 float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
e2da917a 548
06e06389 549 aHitTimes.x = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;
fc73a202 550
f2474958 551 aTimeMax = max (aNodeMin1, aNodeMax1);
552 aTimeMin = min (aNodeMin1, aNodeMax1);
fc73a202 553
f2474958 554 aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
555 aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
fc73a202 556
06e06389 557 aHitTimes.y = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;
fc73a202 558
f2474958 559 aTimeMax = max (aNodeMin2, aNodeMax2);
560 aTimeMin = min (aNodeMin2, aNodeMax2);
fc73a202 561
f2474958 562 aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
563 aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
47e9c178 564
06e06389 565 aHitTimes.z = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 1) ? aTimeEnter : MAXFLOAT;
f2474958 566
567 aTimeMax = max (aNodeMin3, aNodeMax3);
568 aTimeMin = min (aNodeMin3, aNodeMax3);
569
570 aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
571 aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
572
06e06389 573 aHitTimes.w = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 2) ? aTimeEnter : MAXFLOAT;
f2474958 574
575 ivec4 aChildren = ivec4 (0, 1, 2, 3);
576
577 aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;
578 aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;
579 aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;
580 aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;
581 aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;
582 aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;
583 aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;
584 aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;
585 aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;
586 aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;
587
588 if (aHitTimes.x != MAXFLOAT)
fc73a202 589 {
f2474958 590 int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2
591 | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);
592
593 if (aHitTimes.y != MAXFLOAT)
594 Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;
e2da917a 595
f2474958 596 aNode = aData.y + aChildren.x;
fc73a202 597 }
f2474958 598 else
fc73a202 599 {
47e9c178 600 toContinue = (aHead >= 0);
601
602 if (aHead == aStop) // go to top-level BVH
fc73a202 603 {
47e9c178 604 aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
fc73a202 605 }
25ef750e 606
f2474958 607 if (aHead >= 0)
608 aNode = pop (aHead);
fc73a202 609 }
610 }
f2474958 611 else if (aData.x < 0) // leaf node (contains triangles)
fc73a202 612 {
613 vec3 aNormal;
f2474958 614 vec3 aTimeUV;
84c71f29 615
fc73a202 616 for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)
617 {
78607702 618 ivec4 aTriIndex = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));
619 vec3 aPoints[3];
fc73a202 620
78607702 621 aPoints[0] = texelFetch (uGeometryVertexTexture, aTriIndex.x += VRT_OFFSET (aSubTree)).xyz;
622 aPoints[1] = texelFetch (uGeometryVertexTexture, aTriIndex.y += VRT_OFFSET (aSubTree)).xyz;
623 aPoints[2] = texelFetch (uGeometryVertexTexture, aTriIndex.z += VRT_OFFSET (aSubTree)).xyz;
fc73a202 624
78607702 625 IntersectTriangle (aSubTree.TrsfRay, aPoints[0], aPoints[1], aPoints[2], aTimeUV, aNormal);
e2da917a 626
f2474958 627 if (aTimeUV.x < theHit.Time)
fc73a202 628 {
78607702 629 aTriangle.TriIndex = aTriIndex;
630 for (int i = 0; i < 3; ++i)
631 {
632 aTriangle.Points[i] = aPoints[i];
633 }
a89742cf 634
47e9c178 635 theTrsfId = TRS_OFFSET (aSubTree);
636
f2474958 637 theHit = SIntersect (aTimeUV.x, aTimeUV.yz, aNormal);
fc73a202 638 }
639 }
fc73a202 640
47e9c178 641 toContinue = (aHead >= 0);
642
643 if (aHead == aStop) // go to top-level BVH
644 {
645 aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
646 }
647
f2474958 648 if (aHead >= 0)
649 aNode = pop (aHead);
47e9c178 650 }
651 else if (aData.x > 0) // switch node
652 {
653 aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root
654
655 vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);
656 vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);
657 vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);
658 vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);
659
660 aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,
661 aInvTransf0,
662 aInvTransf1,
663 aInvTransf2);
664
665 aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) /
666 max (abs (aSubTree.TrsfRay.Direct), SMALL);
e2da917a 667
47e9c178 668 aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,
669 aInvTransf0,
670 aInvTransf1,
671 aInvTransf2,
672 aInvTransf3);
673
674 aNode = BVH_OFFSET (aSubTree); // go to sub-root node
675
676 aStop = aHead; // store current stack pointer
fc73a202 677 }
678 }
679
78607702 680 return aTriangle;
fc73a202 681}
682
683// =======================================================================
47e9c178 684// function : SceneAnyHit
685// purpose : Finds intersection with any scene triangle
fc73a202 686// =======================================================================
47e9c178 687float SceneAnyHit (in SRay theRay, in vec3 theInverse, in float theDistance)
fc73a202 688{
8c820969 689 float aFactor = 1.f;
fc73a202 690
47e9c178 691 int aNode = 0; // node to traverse
692 int aHead = -1; // pointer of stack
693 int aStop = -1; // BVH level switch
694
695 SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
696
f2474958 697 for (bool toContinue = true; toContinue; /* none */)
fc73a202 698 {
e2da917a 699 ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
fc73a202 700
701 if (aData.x == 0) // if inner node
702 {
47e9c178 703 aData.y += BVH_OFFSET (aSubTree);
e2da917a 704
f2474958 705 vec4 aHitTimes = vec4 (MAXFLOAT,
706 MAXFLOAT,
707 MAXFLOAT,
708 MAXFLOAT);
709
710 vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;
fc73a202 711
f2474958 712 vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;
713 vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;
714 vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
715 vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
716 vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;
717 vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;
718 vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
719 vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;
fc73a202 720
f2474958 721 vec3 aTimeMax = max (aNodeMin0, aNodeMax0);
722 vec3 aTimeMin = min (aNodeMin0, aNodeMax0);
fc73a202 723
f2474958 724 float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
725 float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
e2da917a 726
06e06389 727 aHitTimes.x = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;
fc73a202 728
f2474958 729 aTimeMax = max (aNodeMin1, aNodeMax1);
730 aTimeMin = min (aNodeMin1, aNodeMax1);
fc73a202 731
f2474958 732 aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
733 aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
fc73a202 734
06e06389 735 aHitTimes.y = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;
fc73a202 736
f2474958 737 aTimeMax = max (aNodeMin2, aNodeMax2);
738 aTimeMin = min (aNodeMin2, aNodeMax2);
fc73a202 739
f2474958 740 aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
741 aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
47e9c178 742
06e06389 743 aHitTimes.z = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 1) ? aTimeEnter : MAXFLOAT;
f2474958 744
745 aTimeMax = max (aNodeMin3, aNodeMax3);
746 aTimeMin = min (aNodeMin3, aNodeMax3);
747
748 aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
749 aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
750
06e06389 751 aHitTimes.w = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 2) ? aTimeEnter : MAXFLOAT;
f2474958 752
753 ivec4 aChildren = ivec4 (0, 1, 2, 3);
754
755 aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;
756 aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;
757 aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;
758 aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;
759 aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;
760 aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;
761 aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;
762 aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;
763 aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;
764 aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;
765
766 if (aHitTimes.x != MAXFLOAT)
fc73a202 767 {
f2474958 768 int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2
769 | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);
fc73a202 770
f2474958 771 if (aHitTimes.y != MAXFLOAT)
772 Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;
773
774 aNode = aData.y + aChildren.x;
fc73a202 775 }
f2474958 776 else
fc73a202 777 {
47e9c178 778 toContinue = (aHead >= 0);
779
780 if (aHead == aStop) // go to top-level BVH
fc73a202 781 {
47e9c178 782 aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
fc73a202 783 }
fc73a202 784
f2474958 785 if (aHead >= 0)
786 aNode = pop (aHead);
fc73a202 787 }
788 }
47e9c178 789 else if (aData.x < 0) // leaf node
fc73a202 790 {
791 vec3 aNormal;
f2474958 792 vec3 aTimeUV;
25ef750e 793
fc73a202 794 for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)
795 {
47e9c178 796 ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));
fc73a202 797
47e9c178 798 vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += VRT_OFFSET (aSubTree)).xyz;
799 vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += VRT_OFFSET (aSubTree)).xyz;
800 vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += VRT_OFFSET (aSubTree)).xyz;
fc73a202 801
f2474958 802 IntersectTriangle (aSubTree.TrsfRay, aPoint0, aPoint1, aPoint2, aTimeUV, aNormal);
bc8c79bb 803
804#ifdef TRANSPARENT_SHADOWS
f2474958 805 if (aTimeUV.x < theDistance)
bc8c79bb 806 {
8c820969 807 aFactor *= 1.f - texelFetch (uRaytraceMaterialTexture, MATERIAL_TRAN (aTriangle.w)).x;
bc8c79bb 808 }
809#else
f2474958 810 if (aTimeUV.x < theDistance)
8c820969 811 {
812 aFactor = 0.f;
813 }
bc8c79bb 814#endif
fc73a202 815 }
25ef750e 816
3a9b5dc8 817 toContinue = (aHead >= 0) && (aFactor > 0.1f);
25ef750e 818
47e9c178 819 if (aHead == aStop) // go to top-level BVH
fc73a202 820 {
47e9c178 821 aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
fc73a202 822 }
e2da917a 823
f2474958 824 if (aHead >= 0)
825 aNode = pop (aHead);
fc73a202 826 }
47e9c178 827 else if (aData.x > 0) // switch node
fc73a202 828 {
47e9c178 829 aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root
bc8c79bb 830
47e9c178 831 vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);
832 vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);
833 vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);
834 vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);
fc73a202 835
47e9c178 836 aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,
837 aInvTransf0,
838 aInvTransf1,
839 aInvTransf2);
bc8c79bb 840
47e9c178 841 aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,
842 aInvTransf0,
843 aInvTransf1,
844 aInvTransf2,
845 aInvTransf3);
fc73a202 846
47e9c178 847 aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) / max (abs (aSubTree.TrsfRay.Direct), SMALL);
bc8c79bb 848
47e9c178 849 aNode = BVH_OFFSET (aSubTree); // go to sub-root node
84c71f29 850
47e9c178 851 aStop = aHead; // store current stack pointer
fc73a202 852 }
853 }
25ef750e 854
bc8c79bb 855 return aFactor;
fc73a202 856}
857
858#define PI 3.1415926f
859
860// =======================================================================
861// function : Latlong
862// purpose : Converts world direction to environment texture coordinates
863// =======================================================================
864vec2 Latlong (in vec3 thePoint, in float theRadius)
865{
866 float aPsi = acos (-thePoint.z / theRadius);
25ef750e 867
fc73a202 868 float aPhi = atan (thePoint.y, thePoint.x) + PI;
25ef750e 869
fc73a202 870 return vec2 (aPhi * 0.1591549f,
871 aPsi * 0.3183098f);
872}
873
89d855ba 874#ifdef BACKGROUND_CUBEMAP
875//! Transform texture coordinates for cubemap lookup.
876vec3 cubemapVectorTransform (in vec3 theVec, in float theRadius)
877{
878 vec3 aVec = theVec.yzx;
879 aVec.y *= float(uYCoeff);
880 aVec.z *= float(uZCoeff);
881 return aVec;
882}
883#endif
884
fc73a202 885// =======================================================================
886// function : SmoothNormal
887// purpose : Interpolates normal across the triangle
888// =======================================================================
889vec3 SmoothNormal (in vec2 theUV, in ivec4 theTriangle)
890{
891 vec3 aNormal0 = texelFetch (uGeometryNormalTexture, theTriangle.x).xyz;
892 vec3 aNormal1 = texelFetch (uGeometryNormalTexture, theTriangle.y).xyz;
893 vec3 aNormal2 = texelFetch (uGeometryNormalTexture, theTriangle.z).xyz;
25ef750e 894
fc73a202 895 return normalize (aNormal1 * theUV.x +
896 aNormal2 * theUV.y +
dc9ef964 897 aNormal0 * (1.0f - theUV.x - theUV.y));
898}
899
be86ba90 900#define POLYGON_OFFSET_UNIT 0.f
901#define POLYGON_OFFSET_FACTOR 1.f
902#define POLYGON_OFFSET_SCALE 0.006f
903
904// =======================================================================
905// function : PolygonOffset
906// purpose : Computes OpenGL polygon offset
907// =======================================================================
908float PolygonOffset (in vec3 theNormal, in vec3 thePoint)
909{
910 vec4 aProjectedNorm = vec4 (theNormal, -dot (theNormal, thePoint)) * uUnviewMat;
911
912 float aPolygonOffset = POLYGON_OFFSET_UNIT;
913
914 if (aProjectedNorm.z * aProjectedNorm.z > 1e-20f)
915 {
916 aProjectedNorm.xy *= 1.f / aProjectedNorm.z;
917
918 aPolygonOffset += POLYGON_OFFSET_FACTOR * max (abs (aProjectedNorm.x),
919 abs (aProjectedNorm.y));
920 }
921
922 return aPolygonOffset;
923}
924
dc9ef964 925// =======================================================================
25ef750e 926// function : SmoothUV
927// purpose : Interpolates UV coordinates across the triangle
928// =======================================================================
929#ifdef USE_TEXTURES
78607702 930vec2 SmoothUV (in vec2 theUV, in ivec4 theTriangle, out vec2[3] theUVs)
25ef750e 931{
78607702 932 theUVs[0] = texelFetch (uGeometryTexCrdTexture, theTriangle.x).st;
933 theUVs[1] = texelFetch (uGeometryTexCrdTexture, theTriangle.y).st;
934 theUVs[2] = texelFetch (uGeometryTexCrdTexture, theTriangle.z).st;
25ef750e 935
78607702 936 return theUVs[1] * theUV.x +
937 theUVs[2] * theUV.y +
938 theUVs[0] * (1.0f - theUV.x - theUV.y);
939}
940
941vec2 SmoothUV (in vec2 theUV, in ivec4 theTriangle)
942{
943 vec2 aUVs[3];
944 return SmoothUV (theUV, theTriangle, aUVs);
25ef750e 945}
946#endif
947
948// =======================================================================
189f85a3 949// function : FetchEnvironment
950// purpose :
951// =======================================================================
89d855ba 952vec4 FetchEnvironment (in vec3 theTexCoord, in float theRadius, in bool theIsBackground)
189f85a3 953{
89d855ba 954 if (uEnvMapEnabled == 0)
955 {
956#ifdef PATH_TRACING
957 return theIsBackground ? vec4 (0.0, 0.0, 0.0, 1.0) : uGlobalAmbient;
958#else
959 return vec4 (0.0, 0.0, 0.0, 1.0);
960#endif
961 }
962
963 vec4 anAmbScale = theIsBackground ? vec4(1.0) : uGlobalAmbient;
964 vec4 anEnvColor =
965#ifdef BACKGROUND_CUBEMAP
966 textureLod (uEnvMapTexture, cubemapVectorTransform (theTexCoord, theRadius), 0.0);
967#else
968 textureLod (uEnvMapTexture, Latlong (theTexCoord, theRadius), 0.0);
969#endif
970 return anEnvColor * anAmbScale;
189f85a3 971}
972
973// =======================================================================
dc9ef964 974// function : Refract
bc8c79bb 975// purpose : Computes refraction ray (also handles TIR)
dc9ef964 976// =======================================================================
189f85a3 977#ifndef PATH_TRACING
dc9ef964 978vec3 Refract (in vec3 theInput,
979 in vec3 theNormal,
980 in float theRefractIndex,
981 in float theInvRefractIndex)
982{
25ef750e 983 float aNdotI = dot (theInput, theNormal);
984
dc9ef964 985 float anIndex = aNdotI < 0.0f
986 ? theInvRefractIndex
987 : theRefractIndex;
25ef750e 988
dc9ef964 989 float aSquare = anIndex * anIndex * (1.0f - aNdotI * aNdotI);
25ef750e 990
dc9ef964 991 if (aSquare > 1.0f)
992 {
993 return reflect (theInput, theNormal);
994 }
25ef750e 995
dc9ef964 996 float aNdotT = sqrt (1.0f - aSquare);
25ef750e 997
dc9ef964 998 return normalize (anIndex * theInput -
bc8c79bb 999 (anIndex * aNdotI + (aNdotI < 0.0f ? aNdotT : -aNdotT)) * theNormal);
fc73a202 1000}
189f85a3 1001#endif
fc73a202 1002
a89742cf 1003#define MIN_SLOPE 0.0001f
1004#define EPS_SCALE 8.0000f
1005
bc8c79bb 1006#define THRESHOLD vec3 (0.1f)
fc73a202 1007
1804bb99 1008#define INVALID_BOUNCES 1000
1009
fc73a202 1010#define LIGHT_POS(index) (2 * index + 1)
1011#define LIGHT_PWR(index) (2 * index + 0)
1012
1013// =======================================================================
1014// function : Radiance
25ef750e 1015// purpose : Computes color along the given ray
fc73a202 1016// =======================================================================
189f85a3 1017#ifndef PATH_TRACING
fc73a202 1018vec4 Radiance (in SRay theRay, in vec3 theInverse)
1019{
dc9ef964 1020 vec3 aResult = vec3 (0.0f);
1021 vec4 aWeight = vec4 (1.0f);
84c71f29 1022
8c820969 1023 int aTrsfId;
a89742cf 1024
1d865689 1025 float aRaytraceDepth = MAXFLOAT;
832a6f44 1026 float aRefractionIdx = 0.0;
a89742cf 1027
25ef750e 1028 for (int aDepth = 0; aDepth < NB_BOUNCES; ++aDepth)
fc73a202 1029 {
1030 SIntersect aHit = SIntersect (MAXFLOAT, vec2 (ZERO), ZERO);
a89742cf 1031
78607702 1032 ivec4 aTriIndex = SceneNearestHit (theRay, theInverse, aHit, aTrsfId).TriIndex;
fc73a202 1033
1034 if (aTriIndex.x == -1)
1035 {
189f85a3 1036 vec4 aColor = vec4 (0.0);
25ef750e 1037
832a6f44 1038 if (bool(uEnvMapForBack) || aWeight.w == 0.0 /* reflection */)
fc73a202 1039 {
832a6f44 1040 float aRadius = uSceneRadius;
1041 vec3 aTexCoord = vec3 (0.0);
25ef750e 1042
832a6f44 1043 if (aDepth == 0 || (aRefractionIdx == 1.0 && aWeight.w != 0.0))
1044 {
1045 vec2 aPixel = uEyeSize * (vPixel - vec2 (0.5)) * 2.0;
1046 vec2 anAperturePnt = sampleUniformDisk() * uApertureRadius;
1047 vec3 aLocalDir = normalize (vec3 (aPixel * uFocalPlaneDist - anAperturePnt, uFocalPlaneDist));
1048 vec3 aDirect = uEyeView * aLocalDir.z +
1049 uEyeSide * aLocalDir.x +
1050 uEyeVert * aLocalDir.y;
1051
1052 aTexCoord = aDirect * uSceneRadius;
1053 aRadius = length (aTexCoord);
1054 }
1055 else
1056 {
1057 float aTime = IntersectSphere (theRay, uSceneRadius);
1058 aTexCoord = theRay.Direct * aTime + theRay.Origin;
1059 }
1060
1061 aColor = FetchEnvironment (aTexCoord, aRadius, aWeight.w != 0.0);
189f85a3 1062 }
1063 else
1064 {
3a9b5dc8 1065 aColor = BackgroundColor();
fc73a202 1066 }
25ef750e 1067
8c820969 1068 aResult += aWeight.xyz * aColor.xyz; aWeight.w *= aColor.w;
1069
1070 break; // terminate path
fc73a202 1071 }
a89742cf 1072
8c820969 1073 vec3 aInvTransf0 = texelFetch (uSceneTransformTexture, aTrsfId + 0).xyz;
1074 vec3 aInvTransf1 = texelFetch (uSceneTransformTexture, aTrsfId + 1).xyz;
1075 vec3 aInvTransf2 = texelFetch (uSceneTransformTexture, aTrsfId + 2).xyz;
cf537aac 1076
1077 aHit.Normal = normalize (vec3 (dot (aInvTransf0, aHit.Normal),
1078 dot (aInvTransf1, aHit.Normal),
1079 dot (aInvTransf2, aHit.Normal)));
1804bb99 1080
1804bb99 1081 theRay.Origin += theRay.Direct * aHit.Time; // intersection point
1082
3a9b5dc8 1083 // Evaluate depth on first hit
1d865689 1084 if (aDepth == 0)
1085 {
be86ba90 1086 vec4 aNDCPoint = uViewMat * vec4 (theRay.Origin, 1.f);
1d865689 1087
be86ba90 1088 float aPolygonOffset = PolygonOffset (aHit.Normal, theRay.Origin);
1089 aRaytraceDepth = (aNDCPoint.z / aNDCPoint.w + aPolygonOffset * POLYGON_OFFSET_SCALE) * 0.5f + 0.5f;
1d865689 1090 }
1091
1804bb99 1092 vec3 aNormal = SmoothNormal (aHit.UV, aTriIndex);
1093
1094 aNormal = normalize (vec3 (dot (aInvTransf0, aNormal),
1095 dot (aInvTransf1, aNormal),
1096 dot (aInvTransf2, aNormal)));
25ef750e 1097
dc9ef964 1098 vec3 aAmbient = texelFetch (
1099 uRaytraceMaterialTexture, MATERIAL_AMBN (aTriIndex.w)).rgb;
25ef750e 1100 vec4 aDiffuse = texelFetch (
1101 uRaytraceMaterialTexture, MATERIAL_DIFF (aTriIndex.w));
dc9ef964 1102 vec4 aSpecular = texelFetch (
1103 uRaytraceMaterialTexture, MATERIAL_SPEC (aTriIndex.w));
1104 vec4 aOpacity = texelFetch (
1105 uRaytraceMaterialTexture, MATERIAL_TRAN (aTriIndex.w));
25ef750e 1106
1107#ifdef USE_TEXTURES
1108 if (aDiffuse.w >= 0.f)
1109 {
1110 vec4 aTexCoord = vec4 (SmoothUV (aHit.UV, aTriIndex), 0.f, 1.f);
1111
1112 vec4 aTrsfRow1 = texelFetch (
1113 uRaytraceMaterialTexture, MATERIAL_TRS1 (aTriIndex.w));
1114 vec4 aTrsfRow2 = texelFetch (
1115 uRaytraceMaterialTexture, MATERIAL_TRS2 (aTriIndex.w));
1116
1117 aTexCoord.st = vec2 (dot (aTrsfRow1, aTexCoord),
1118 dot (aTrsfRow2, aTexCoord));
1119
f411f94f 1120 vec4 aTexColor = textureLod (
1121 sampler2D (uTextureSamplers[int(aDiffuse.w)]), aTexCoord.st, 0.f);
25ef750e 1122
f411f94f 1123 aDiffuse.rgb *= aTexColor.rgb;
1124 aAmbient.rgb *= aTexColor.rgb;
1125
1126 // keep refractive index untouched (Z component)
1127 aOpacity.xy = vec2 (aTexColor.w * aOpacity.x, 1.0f - aTexColor.w * aOpacity.x);
25ef750e 1128 }
1129#endif
84c71f29 1130
1804bb99 1131 vec3 aEmission = texelFetch (
1132 uRaytraceMaterialTexture, MATERIAL_EMIS (aTriIndex.w)).rgb;
84c71f29 1133
1804bb99 1134 float aGeomFactor = dot (aNormal, theRay.Direct);
1135
1136 aResult.xyz += aWeight.xyz * aOpacity.x * (
1137 uGlobalAmbient.xyz * aAmbient * max (abs (aGeomFactor), 0.5f) + aEmission);
25ef750e 1138
1804bb99 1139 vec3 aSidedNormal = mix (aNormal, -aNormal, step (0.0f, aGeomFactor));
a89742cf 1140
fc73a202 1141 for (int aLightIdx = 0; aLightIdx < uLightCount; ++aLightIdx)
1142 {
1143 vec4 aLight = texelFetch (
1144 uRaytraceLightSrcTexture, LIGHT_POS (aLightIdx));
e2da917a 1145
fc73a202 1146 float aDistance = MAXFLOAT;
e2da917a 1147
dc9ef964 1148 if (aLight.w != 0.0f) // point light source
fc73a202 1149 {
1804bb99 1150 aDistance = length (aLight.xyz -= theRay.Origin);
e2da917a 1151
dc9ef964 1152 aLight.xyz *= 1.0f / aDistance;
fc73a202 1153 }
1154
1804bb99 1155 float aLdotN = dot (aLight.xyz, aSidedNormal);
e2da917a 1156
1804bb99 1157 if (aLdotN > 0.0f) // first check if light source is important
fc73a202 1158 {
1804bb99 1159 float aVisibility = 1.0f;
25ef750e 1160
189f85a3 1161 if (bool(uShadowsEnabled))
1804bb99 1162 {
1163 SRay aShadow = SRay (theRay.Origin, aLight.xyz);
e2da917a 1164
1804bb99 1165 aShadow.Origin += uSceneEpsilon * (aLight.xyz +
1166 mix (-aHit.Normal, aHit.Normal, step (0.0f, dot (aHit.Normal, aLight.xyz))));
25ef750e 1167
1804bb99 1168 vec3 aInverse = 1.0f / max (abs (aLight.xyz), SMALL);
25ef750e 1169
1804bb99 1170 aVisibility = SceneAnyHit (
1171 aShadow, mix (-aInverse, aInverse, step (ZERO, aLight.xyz)), aDistance);
1172 }
25ef750e 1173
1804bb99 1174 if (aVisibility > 0.0f)
fc73a202 1175 {
6e728f3b 1176 vec3 aIntensity = min (UNIT, vec3 (texelFetch (
1177 uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx))));
1804bb99 1178
1179 float aRdotV = dot (reflect (aLight.xyz, aSidedNormal), theRay.Direct);
25ef750e 1180
bc8c79bb 1181 aResult.xyz += aWeight.xyz * (aOpacity.x * aVisibility) * aIntensity *
1804bb99 1182 (aDiffuse.xyz * aLdotN + aSpecular.xyz * pow (max (0.f, aRdotV), aSpecular.w));
fc73a202 1183 }
1184 }
1185 }
0e330c8c 1186
dc9ef964 1187 if (aOpacity.x != 1.0f)
fc73a202 1188 {
1189 aWeight *= aOpacity.y;
832a6f44 1190 aRefractionIdx = aOpacity.z;
a89742cf 1191
bc8c79bb 1192 if (aOpacity.z != 1.0f)
1193 {
1194 theRay.Direct = Refract (theRay.Direct, aNormal, aOpacity.z, aOpacity.w);
a89742cf 1195 }
fc73a202 1196 }
1197 else
1198 {
189f85a3 1199 aWeight *= bool(uReflectEnabled) ?
dc9ef964 1200 texelFetch (uRaytraceMaterialTexture, MATERIAL_REFL (aTriIndex.w)) : vec4 (0.0f);
25ef750e 1201
1804bb99 1202 vec3 aReflect = reflect (theRay.Direct, aNormal);
25ef750e 1203
1804bb99 1204 if (dot (aReflect, aHit.Normal) * dot (theRay.Direct, aHit.Normal) > 0.0f)
fc73a202 1205 {
1804bb99 1206 aReflect = reflect (theRay.Direct, aHit.Normal);
fc73a202 1207 }
1208
1804bb99 1209 theRay.Direct = aReflect;
fc73a202 1210 }
25ef750e 1211
fc73a202 1212 if (all (lessThanEqual (aWeight.xyz, THRESHOLD)))
1213 {
1804bb99 1214 aDepth = INVALID_BOUNCES;
1215 }
1216 else if (aOpacity.x == 1.0f || aOpacity.z != 1.0f) // if no simple transparency
1217 {
1218 theRay.Origin += aHit.Normal * mix (
1219 -uSceneEpsilon, uSceneEpsilon, step (0.0f, dot (aHit.Normal, theRay.Direct)));
1220
1221 theInverse = 1.0f / max (abs (theRay.Direct), SMALL);
1222
1223 theInverse = mix (-theInverse, theInverse, step (ZERO, theRay.Direct));
fc73a202 1224 }
25ef750e 1225
1804bb99 1226 theRay.Origin += theRay.Direct * uSceneEpsilon;
fc73a202 1227 }
1228
1d865689 1229 gl_FragDepth = aRaytraceDepth;
1230
fc73a202 1231 return vec4 (aResult.x,
1232 aResult.y,
1233 aResult.z,
1234 aWeight.w);
1235}
189f85a3 1236#endif