/usr/share/oce-0.18/src/Shaders/RaytraceBase.fs is in liboce-visualization11 0.18.2-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 | #ifdef USE_TEXTURES
#extension GL_ARB_bindless_texture : require
#endif
//! Normalized pixel coordinates.
in vec2 vPixel;
//! Sub-pixel offset in X direction for FSAA.
uniform float uOffsetX = 0.f;
//! Sub-pixel offset in Y direction for FSAA.
uniform float uOffsetY = 0.f;
//! Origin of viewing ray in left-top corner.
uniform vec3 uOriginLT;
//! Origin of viewing ray in left-bottom corner.
uniform vec3 uOriginLB;
//! Origin of viewing ray in right-top corner.
uniform vec3 uOriginRT;
//! Origin of viewing ray in right-bottom corner.
uniform vec3 uOriginRB;
//! Width of the rendering window.
uniform int uWinSizeX;
//! Height of the rendering window.
uniform int uWinSizeY;
//! Direction of viewing ray in left-top corner.
uniform vec3 uDirectLT;
//! Direction of viewing ray in left-bottom corner.
uniform vec3 uDirectLB;
//! Direction of viewing ray in right-top corner.
uniform vec3 uDirectRT;
//! Direction of viewing ray in right-bottom corner.
uniform vec3 uDirectRB;
//! Inverse model-view-projection matrix.
uniform mat4 uUnviewMat;
//! Texture buffer of data records of bottom-level BVH nodes.
uniform isamplerBuffer uSceneNodeInfoTexture;
//! Texture buffer of minimum points of bottom-level BVH nodes.
uniform samplerBuffer uSceneMinPointTexture;
//! Texture buffer of maximum points of bottom-level BVH nodes.
uniform samplerBuffer uSceneMaxPointTexture;
//! Texture buffer of transformations of high-level BVH nodes.
uniform samplerBuffer uSceneTransformTexture;
//! Texture buffer of vertex coords.
uniform samplerBuffer uGeometryVertexTexture;
//! Texture buffer of vertex normals.
uniform samplerBuffer uGeometryNormalTexture;
#ifdef USE_TEXTURES
//! Texture buffer of per-vertex UV-coordinates.
uniform samplerBuffer uGeometryTexCrdTexture;
#endif
//! Texture buffer of triangle indices.
uniform isamplerBuffer uGeometryTriangTexture;
//! Texture buffer of material properties.
uniform samplerBuffer uRaytraceMaterialTexture;
//! Texture buffer of light source properties.
uniform samplerBuffer uRaytraceLightSrcTexture;
//! Environment map texture.
uniform sampler2D uEnvironmentMapTexture;
//! Input pre-raytracing image rendered by OpenGL.
uniform sampler2D uOpenGlColorTexture;
//! Input pre-raytracing depth image rendered by OpenGL.
uniform sampler2D uOpenGlDepthTexture;
//! Total number of light sources.
uniform int uLightCount;
//! Intensity of global ambient light.
uniform vec4 uGlobalAmbient;
//! Enables/disables environment map.
uniform int uEnvironmentEnable;
//! Enables/disables computation of shadows.
uniform int uShadowsEnable;
//! Enables/disables computation of reflections.
uniform int uReflectionsEnable;
//! Radius of bounding sphere of the scene.
uniform float uSceneRadius;
//! Scene epsilon to prevent self-intersections.
uniform float uSceneEpsilon;
#ifdef USE_TEXTURES
//! Unique 64-bit handles of OpenGL textures.
uniform sampler2D uTextureSamplers[MAX_TEX_NUMBER];
#endif
/////////////////////////////////////////////////////////////////////////////////////////
// Specific data types
//! Stores ray parameters.
struct SRay
{
vec3 Origin;
vec3 Direct;
};
//! Stores intersection parameters.
struct SIntersect
{
float Time;
vec2 UV;
vec3 Normal;
};
/////////////////////////////////////////////////////////////////////////////////////////
// Some useful constants
#define MAXFLOAT 1e15f
#define SMALL vec3 (exp2 (-80.0f))
#define ZERO vec3 (0.0f, 0.0f, 0.0f)
#define UNIT vec3 (1.0f, 1.0f, 1.0f)
#define AXIS_X vec3 (1.0f, 0.0f, 0.0f)
#define AXIS_Y vec3 (0.0f, 1.0f, 0.0f)
#define AXIS_Z vec3 (0.0f, 0.0f, 1.0f)
//! 32-bit state of random number generator.
uint RandState;
// =======================================================================
// function : SeedRand
// purpose : Applies hash function by Thomas Wang to randomize seeds
// (see http://www.burtleburtle.net/bob/hash/integer.html)
// =======================================================================
void SeedRand (in int theSeed)
{
RandState = uint (int (gl_FragCoord.y) * uWinSizeX + int (gl_FragCoord.x) + theSeed);
RandState = (RandState + 0x479ab41du) + (RandState << 8);
RandState = (RandState ^ 0xe4aa10ceu) ^ (RandState >> 5);
RandState = (RandState + 0x9942f0a6u) - (RandState << 14);
RandState = (RandState ^ 0x5aedd67du) ^ (RandState >> 3);
RandState = (RandState + 0x17bea992u) + (RandState << 7);
}
// =======================================================================
// function : RandInt
// purpose : Generates integer using Xorshift algorithm by G. Marsaglia
// =======================================================================
uint RandInt()
{
RandState ^= (RandState << 13);
RandState ^= (RandState >> 17);
RandState ^= (RandState << 5);
return RandState;
}
// =======================================================================
// function : RandFloat
// purpose : Generates a random float in [0, 1) range
// =======================================================================
float RandFloat()
{
return float (RandInt()) * (1.f / 4294967296.f);
}
// =======================================================================
// function : MatrixColMultiplyPnt
// purpose : Multiplies a vector by matrix
// =======================================================================
vec3 MatrixColMultiplyPnt (in vec3 v,
in vec4 m0,
in vec4 m1,
in vec4 m2,
in vec4 m3)
{
return vec3 (m0[0] * v.x + m1[0] * v.y + m2[0] * v.z + m3[0],
m0[1] * v.x + m1[1] * v.y + m2[1] * v.z + m3[1],
m0[2] * v.x + m1[2] * v.y + m2[2] * v.z + m3[2]);
}
// =======================================================================
// function : MatrixColMultiplyDir
// purpose : Multiplies a vector by matrix
// =======================================================================
vec3 MatrixColMultiplyDir (in vec3 v,
in vec4 m0,
in vec4 m1,
in vec4 m2,
in vec4 m3)
{
return vec3 (m0[0] * v.x + m1[0] * v.y + m2[0] * v.z,
m0[1] * v.x + m1[1] * v.y + m2[1] * v.z,
m0[2] * v.x + m1[2] * v.y + m2[2] * v.z);
}
/////////////////////////////////////////////////////////////////////////////////////////
// Functions for compute ray-object intersection
// =======================================================================
// function : GenerateRay
// purpose :
// =======================================================================
SRay GenerateRay (in vec2 thePixel)
{
vec3 aP0 = mix (uOriginLB, uOriginRB, thePixel.x);
vec3 aP1 = mix (uOriginLT, uOriginRT, thePixel.x);
vec3 aD0 = mix (uDirectLB, uDirectRB, thePixel.x);
vec3 aD1 = mix (uDirectLT, uDirectRT, thePixel.x);
vec3 aDirection = normalize (mix (aD0, aD1, thePixel.y));
return SRay (mix (aP0, aP1, thePixel.y), aDirection);
}
// =======================================================================
// function : ComputeOpenGlDepth
// purpose :
// =======================================================================
float ComputeOpenGlDepth (in SRay theRay)
{
// a depth in range [0,1]
float anOpenGlDepth = texelFetch (uOpenGlDepthTexture, ivec2 (gl_FragCoord.xy), 0).r;
// pixel point in NDC-space [-1,1]
vec4 aPoint = vec4 (2.0f * vPixel.x - 1.0f,
2.0f * vPixel.y - 1.0f,
2.0f * anOpenGlDepth - 1.0f,
1.0f);
vec4 aFinal = uUnviewMat * aPoint;
aFinal.xyz *= 1.f / aFinal.w;
return (anOpenGlDepth < 1.f) ? length (aFinal.xyz - theRay.Origin) : MAXFLOAT;
}
// =======================================================================
// function : ComputeOpenGlColor
// purpose :
// =======================================================================
vec4 ComputeOpenGlColor (in SRay theRay)
{
vec4 anOpenGlColor = texelFetch (uOpenGlColorTexture, ivec2 (gl_FragCoord.xy), 0);
// During blending with factors GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA (for text and markers)
// the alpha channel (written in the color buffer) was squared.
anOpenGlColor.a = 1.f - sqrt (anOpenGlColor.a);
return anOpenGlColor;
}
// =======================================================================
// function : IntersectSphere
// purpose : Computes ray-sphere intersection
// =======================================================================
float IntersectSphere (in SRay theRay, in float theRadius)
{
float aDdotD = dot (theRay.Direct, theRay.Direct);
float aDdotO = dot (theRay.Direct, theRay.Origin);
float aOdotO = dot (theRay.Origin, theRay.Origin);
float aD = aDdotO * aDdotO - aDdotD * (aOdotO - theRadius * theRadius);
if (aD > 0.0f)
{
float aTime = (sqrt (aD) - aDdotO) * (1.0f / aDdotD);
return aTime > 0.0f ? aTime : MAXFLOAT;
}
return MAXFLOAT;
}
// =======================================================================
// function : IntersectTriangle
// purpose : Computes ray-triangle intersection (branchless version)
// =======================================================================
float IntersectTriangle (in SRay theRay,
in vec3 thePnt0,
in vec3 thePnt1,
in vec3 thePnt2,
out vec2 theUV,
out vec3 theNorm)
{
vec3 aEdge0 = thePnt1 - thePnt0;
vec3 aEdge1 = thePnt0 - thePnt2;
theNorm = cross (aEdge1, aEdge0);
vec3 aEdge2 = (1.0f / dot (theNorm, theRay.Direct)) * (thePnt0 - theRay.Origin);
float aTime = dot (theNorm, aEdge2);
vec3 theVec = cross (theRay.Direct, aEdge2);
theUV.x = dot (theVec, aEdge1);
theUV.y = dot (theVec, aEdge0);
return bool (int(aTime >= 0.0f) &
int(theUV.x >= 0.0f) &
int(theUV.y >= 0.0f) &
int(theUV.x + theUV.y <= 1.0f)) ? aTime : MAXFLOAT;
}
//! Identifies the absence of intersection.
#define INALID_HIT ivec4 (-1)
//! Global stack shared between traversal functions.
int Stack[STACK_SIZE];
// =======================================================================
// function : ObjectNearestHit
// purpose : Finds intersection with nearest object triangle
// =======================================================================
ivec4 ObjectNearestHit (in int theBVHOffset, in int theVrtOffset, in int theTrgOffset,
in SRay theRay, in vec3 theInverse, inout SIntersect theHit, in int theSentinel)
{
int aHead = theSentinel; // stack pointer
int aNode = theBVHOffset; // node to visit
ivec4 aTriIndex = INALID_HIT;
bool toContinue = true;
while (toContinue)
{
ivec3 aData = texelFetch (uSceneNodeInfoTexture, aNode).xyz;
if (aData.x == 0) // if inner node
{
float aTimeOut;
float aTimeLft;
float aTimeRgh;
aData.y += theBVHOffset;
aData.z += theBVHOffset;
vec3 aNodeMinLft = texelFetch (uSceneMinPointTexture, aData.y).xyz;
vec3 aNodeMinRgh = texelFetch (uSceneMinPointTexture, aData.z).xyz;
vec3 aNodeMaxLft = texelFetch (uSceneMaxPointTexture, aData.y).xyz;
vec3 aNodeMaxRgh = texelFetch (uSceneMaxPointTexture, aData.z).xyz;
vec3 aTime0 = (aNodeMinLft - theRay.Origin) * theInverse;
vec3 aTime1 = (aNodeMaxLft - theRay.Origin) * theInverse;
vec3 aTimeMax = max (aTime0, aTime1);
vec3 aTimeMin = min (aTime0, aTime1);
aTime0 = (aNodeMinRgh - theRay.Origin) * theInverse;
aTime1 = (aNodeMaxRgh - theRay.Origin) * theInverse;
aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
aTimeLft = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
int aHitLft = int(aTimeLft <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeLft <= theHit.Time);
aTimeMax = max (aTime0, aTime1);
aTimeMin = min (aTime0, aTime1);
aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
aTimeRgh = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
int aHitRgh = int(aTimeRgh <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeRgh <= theHit.Time);
if (bool(aHitLft & aHitRgh))
{
aNode = (aTimeLft < aTimeRgh) ? aData.y : aData.z;
Stack[++aHead] = (aTimeLft < aTimeRgh) ? aData.z : aData.y;
}
else
{
if (bool(aHitLft | aHitRgh))
{
aNode = bool(aHitLft) ? aData.y : aData.z;
}
else
{
toContinue = (aHead != theSentinel);
if (toContinue)
aNode = Stack[aHead--];
}
}
}
else // if leaf node
{
vec3 aNormal;
vec2 aParams;
for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)
{
ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + theTrgOffset);
vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += theVrtOffset).xyz;
vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += theVrtOffset).xyz;
vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += theVrtOffset).xyz;
float aTime = IntersectTriangle (theRay,
aPoint0,
aPoint1,
aPoint2,
aParams,
aNormal);
if (aTime < theHit.Time)
{
aTriIndex = aTriangle;
theHit = SIntersect (aTime, aParams, aNormal);
}
}
toContinue = (aHead != theSentinel);
if (toContinue)
aNode = Stack[aHead--];
}
}
return aTriIndex;
}
#define MATERIAL_AMBN(index) (11 * index + 0)
#define MATERIAL_DIFF(index) (11 * index + 1)
#define MATERIAL_SPEC(index) (11 * index + 2)
#define MATERIAL_EMIS(index) (11 * index + 3)
#define MATERIAL_REFL(index) (11 * index + 4)
#define MATERIAL_REFR(index) (11 * index + 5)
#define MATERIAL_TRAN(index) (11 * index + 6)
#define MATERIAL_TRS1(index) (11 * index + 7)
#define MATERIAL_TRS2(index) (11 * index + 8)
#define MATERIAL_TRS3(index) (11 * index + 9)
// =======================================================================
// function : ObjectAnyHit
// purpose : Finds intersection with any object triangle
// =======================================================================
float ObjectAnyHit (in int theBVHOffset, in int theVrtOffset, in int theTrgOffset,
in SRay theRay, in vec3 theInverse, in float theDistance, in int theSentinel)
{
int aHead = theSentinel; // stack pointer
int aNode = theBVHOffset; // node to visit
#ifdef TRANSPARENT_SHADOWS
float aFactor = 1.0f;
#endif
while (true)
{
ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
if (aData.x == 0) // if inner node
{
float aTimeOut;
float aTimeLft;
float aTimeRgh;
aData.y += theBVHOffset;
aData.z += theBVHOffset;
vec3 aNodeMinLft = texelFetch (uSceneMinPointTexture, aData.y).xyz;
vec3 aNodeMaxLft = texelFetch (uSceneMaxPointTexture, aData.y).xyz;
vec3 aNodeMinRgh = texelFetch (uSceneMinPointTexture, aData.z).xyz;
vec3 aNodeMaxRgh = texelFetch (uSceneMaxPointTexture, aData.z).xyz;
vec3 aTime0 = (aNodeMinLft - theRay.Origin) * theInverse;
vec3 aTime1 = (aNodeMaxLft - theRay.Origin) * theInverse;
vec3 aTimeMax = max (aTime0, aTime1);
vec3 aTimeMin = min (aTime0, aTime1);
aTime0 = (aNodeMinRgh - theRay.Origin) * theInverse;
aTime1 = (aNodeMaxRgh - theRay.Origin) * theInverse;
aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
aTimeLft = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
int aHitLft = int(aTimeLft <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeLft <= theDistance);
aTimeMax = max (aTime0, aTime1);
aTimeMin = min (aTime0, aTime1);
aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
aTimeRgh = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
int aHitRgh = int(aTimeRgh <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeRgh <= theDistance);
if (bool(aHitLft & aHitRgh))
{
aNode = (aTimeLft < aTimeRgh) ? aData.y : aData.z;
Stack[++aHead] = (aTimeLft < aTimeRgh) ? aData.z : aData.y;
}
else
{
if (bool(aHitLft | aHitRgh))
{
aNode = bool(aHitLft) ? aData.y : aData.z;
}
else
{
#ifdef TRANSPARENT_SHADOWS
if (aHead == theSentinel)
return aFactor;
#else
if (aHead == theSentinel)
return 1.0f;
#endif
aNode = Stack[aHead--];
}
}
}
else // if leaf node
{
vec3 aNormal;
vec2 aParams;
for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)
{
ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + theTrgOffset);
vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x + theVrtOffset).xyz;
vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y + theVrtOffset).xyz;
vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z + theVrtOffset).xyz;
float aTime = IntersectTriangle (theRay,
aPoint0,
aPoint1,
aPoint2,
aParams,
aNormal);
#ifdef TRANSPARENT_SHADOWS
if (aTime < theDistance)
{
aFactor *= 1.0f - texelFetch (uRaytraceMaterialTexture, MATERIAL_TRAN (aTriangle.w)).x;
}
#else
if (aTime < theDistance)
return 0.0f;
#endif
}
#ifdef TRANSPARENT_SHADOWS
if (aHead == theSentinel || aFactor < 0.1f)
return aFactor;
#else
if (aHead == theSentinel)
return 1.0f;
#endif
aNode = Stack[aHead--];
}
}
#ifdef TRANSPARENT_SHADOWS
return aFactor;
#else
return 1.0f;
#endif
}
// =======================================================================
// function : SceneNearestHit
// purpose : Finds intersection with nearest scene triangle
// =======================================================================
ivec4 SceneNearestHit (in SRay theRay, in vec3 theInverse, inout SIntersect theHit, out int theObjectId)
{
int aHead = -1; // stack pointer
int aNode = 0; // node to visit
ivec4 aHitObject = INALID_HIT;
while (true)
{
ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
if (aData.x != 0) // if leaf node
{
vec3 aNodeMin = texelFetch (uSceneMinPointTexture, aNode).xyz;
vec3 aNodeMax = texelFetch (uSceneMaxPointTexture, aNode).xyz;
vec3 aTime0 = (aNodeMin - theRay.Origin) * theInverse;
vec3 aTime1 = (aNodeMax - theRay.Origin) * theInverse;
vec3 aTimes = min (aTime0, aTime1);
if (max (aTimes.x, max (aTimes.y, aTimes.z)) < theHit.Time)
{
// fetch object transformation
int anObjectId = aData.x - 1;
vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 0);
vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 1);
vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 2);
vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 3);
SRay aTrsfRay = SRay (
MatrixColMultiplyPnt (theRay.Origin, aInvTransf0, aInvTransf1, aInvTransf2, aInvTransf3),
MatrixColMultiplyDir (theRay.Direct, aInvTransf0, aInvTransf1, aInvTransf2, aInvTransf3));
vec3 aTrsfInverse = 1.0f / max (abs (aTrsfRay.Direct), SMALL);
aTrsfInverse = mix (-aTrsfInverse, aTrsfInverse, step (ZERO, aTrsfRay.Direct));
ivec4 aTriIndex = ObjectNearestHit (
aData.y, aData.z, aData.w, aTrsfRay, aTrsfInverse, theHit, aHead);
if (aTriIndex.x != -1)
{
aHitObject = ivec4 (aTriIndex.x, // vertex 0
aTriIndex.y, // vertex 1
aTriIndex.z, // vertex 2
aTriIndex.w); // material
theObjectId = anObjectId;
}
}
if (aHead < 0)
return aHitObject;
aNode = Stack[aHead--];
}
else // if inner node
{
float aTimeOut;
float aTimeLft;
float aTimeRgh;
vec3 aNodeMinLft = texelFetch (uSceneMinPointTexture, aData.y).xyz;
vec3 aNodeMaxLft = texelFetch (uSceneMaxPointTexture, aData.y).xyz;
vec3 aNodeMinRgh = texelFetch (uSceneMinPointTexture, aData.z).xyz;
vec3 aNodeMaxRgh = texelFetch (uSceneMaxPointTexture, aData.z).xyz;
vec3 aTime0 = (aNodeMinLft - theRay.Origin) * theInverse;
vec3 aTime1 = (aNodeMaxLft - theRay.Origin) * theInverse;
vec3 aTimeMax = max (aTime0, aTime1);
vec3 aTimeMin = min (aTime0, aTime1);
aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
aTimeLft = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
int aHitLft = int(aTimeLft <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeLft <= theHit.Time);
aTime0 = (aNodeMinRgh - theRay.Origin) * theInverse;
aTime1 = (aNodeMaxRgh - theRay.Origin) * theInverse;
aTimeMax = max (aTime0, aTime1);
aTimeMin = min (aTime0, aTime1);
aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
aTimeRgh = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
int aHitRgh = int(aTimeRgh <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeRgh <= theHit.Time);
if (bool(aHitLft & aHitRgh))
{
aNode = (aTimeLft < aTimeRgh) ? aData.y : aData.z;
Stack[++aHead] = (aTimeLft < aTimeRgh) ? aData.z : aData.y;
}
else
{
if (bool(aHitLft | aHitRgh))
{
aNode = bool(aHitLft) ? aData.y : aData.z;
}
else
{
if (aHead < 0)
return aHitObject;
aNode = Stack[aHead--];
}
}
}
}
return aHitObject;
}
// =======================================================================
// function : SceneAnyHit
// purpose : Finds intersection with any scene triangle
// =======================================================================
float SceneAnyHit (in SRay theRay, in vec3 theInverse, in float theDistance)
{
int aHead = -1; // stack pointer
int aNode = 0; // node to visit
#ifdef TRANSPARENT_SHADOWS
float aFactor = 1.0f;
#endif
while (true)
{
ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
if (aData.x != 0) // if leaf node
{
// fetch object transformation
int anObjectId = aData.x - 1;
vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 0);
vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 1);
vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 2);
vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 3);
SRay aTrsfRay = SRay (
MatrixColMultiplyPnt (theRay.Origin, aInvTransf0, aInvTransf1, aInvTransf2, aInvTransf3),
MatrixColMultiplyDir (theRay.Direct, aInvTransf0, aInvTransf1, aInvTransf2, aInvTransf3));
vec3 aTrsfInverse = 1.0f / max (abs (aTrsfRay.Direct), SMALL);
aTrsfInverse = mix (-aTrsfInverse, aTrsfInverse, step (ZERO, aTrsfRay.Direct));
#ifdef TRANSPARENT_SHADOWS
aFactor *= ObjectAnyHit (
aData.y, aData.z, aData.w, aTrsfRay, aTrsfInverse, theDistance, aHead);
if (aHead < 0 || aFactor < 0.1f)
return aFactor;
#else
bool isShadow = 0.0f == ObjectAnyHit (
aData.y, aData.z, aData.w, aTrsfRay, aTrsfInverse, theDistance, aHead);
if (aHead < 0 || isShadow)
return isShadow ? 0.0f : 1.0f;
#endif
aNode = Stack[aHead--];
}
else // if inner node
{
float aTimeOut;
float aTimeLft;
float aTimeRgh;
vec3 aNodeMinLft = texelFetch (uSceneMinPointTexture, aData.y).xyz;
vec3 aNodeMaxLft = texelFetch (uSceneMaxPointTexture, aData.y).xyz;
vec3 aNodeMinRgh = texelFetch (uSceneMinPointTexture, aData.z).xyz;
vec3 aNodeMaxRgh = texelFetch (uSceneMaxPointTexture, aData.z).xyz;
vec3 aTime0 = (aNodeMinLft - theRay.Origin) * theInverse;
vec3 aTime1 = (aNodeMaxLft - theRay.Origin) * theInverse;
vec3 aTimeMax = max (aTime0, aTime1);
vec3 aTimeMin = min (aTime0, aTime1);
aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
aTimeLft = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
int aHitLft = int(aTimeLft <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeLft <= theDistance);
aTime0 = (aNodeMinRgh - theRay.Origin) * theInverse;
aTime1 = (aNodeMaxRgh - theRay.Origin) * theInverse;
aTimeMax = max (aTime0, aTime1);
aTimeMin = min (aTime0, aTime1);
aTimeOut = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));
aTimeRgh = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));
int aHitRgh = int(aTimeRgh <= aTimeOut) & int(aTimeOut >= 0.0f) & int(aTimeRgh <= theDistance);
if (bool(aHitLft & aHitRgh))
{
aNode = (aTimeLft < aTimeRgh) ? aData.y : aData.z;
Stack[++aHead] = (aTimeLft < aTimeRgh) ? aData.z : aData.y;
}
else
{
if (bool(aHitLft | aHitRgh))
{
aNode = bool(aHitLft) ? aData.y : aData.z;
}
else
{
#ifdef TRANSPARENT_SHADOWS
if (aHead < 0)
return aFactor;
#else
if (aHead < 0)
return 1.0f;
#endif
aNode = Stack[aHead--];
}
}
}
}
#ifdef TRANSPARENT_SHADOWS
return aFactor;
#else
return 1.0f;
#endif
}
#define PI 3.1415926f
// =======================================================================
// function : Latlong
// purpose : Converts world direction to environment texture coordinates
// =======================================================================
vec2 Latlong (in vec3 thePoint, in float theRadius)
{
float aPsi = acos (-thePoint.z / theRadius);
float aPhi = atan (thePoint.y, thePoint.x) + PI;
return vec2 (aPhi * 0.1591549f,
aPsi * 0.3183098f);
}
// =======================================================================
// function : SmoothNormal
// purpose : Interpolates normal across the triangle
// =======================================================================
vec3 SmoothNormal (in vec2 theUV, in ivec4 theTriangle)
{
vec3 aNormal0 = texelFetch (uGeometryNormalTexture, theTriangle.x).xyz;
vec3 aNormal1 = texelFetch (uGeometryNormalTexture, theTriangle.y).xyz;
vec3 aNormal2 = texelFetch (uGeometryNormalTexture, theTriangle.z).xyz;
return normalize (aNormal1 * theUV.x +
aNormal2 * theUV.y +
aNormal0 * (1.0f - theUV.x - theUV.y));
}
// =======================================================================
// function : SmoothUV
// purpose : Interpolates UV coordinates across the triangle
// =======================================================================
#ifdef USE_TEXTURES
vec2 SmoothUV (in vec2 theUV, in ivec4 theTriangle)
{
vec2 aTexCrd0 = texelFetch (uGeometryTexCrdTexture, theTriangle.x).st;
vec2 aTexCrd1 = texelFetch (uGeometryTexCrdTexture, theTriangle.y).st;
vec2 aTexCrd2 = texelFetch (uGeometryTexCrdTexture, theTriangle.z).st;
return aTexCrd1 * theUV.x +
aTexCrd2 * theUV.y +
aTexCrd0 * (1.0f - theUV.x - theUV.y);
}
#endif
// =======================================================================
// function : Refract
// purpose : Computes refraction ray (also handles TIR)
// =======================================================================
vec3 Refract (in vec3 theInput,
in vec3 theNormal,
in float theRefractIndex,
in float theInvRefractIndex)
{
float aNdotI = dot (theInput, theNormal);
float anIndex = aNdotI < 0.0f
? theInvRefractIndex
: theRefractIndex;
float aSquare = anIndex * anIndex * (1.0f - aNdotI * aNdotI);
if (aSquare > 1.0f)
{
return reflect (theInput, theNormal);
}
float aNdotT = sqrt (1.0f - aSquare);
return normalize (anIndex * theInput -
(anIndex * aNdotI + (aNdotI < 0.0f ? aNdotT : -aNdotT)) * theNormal);
}
#define MIN_SLOPE 0.0001f
#define EPS_SCALE 8.0000f
#define THRESHOLD vec3 (0.1f)
#define INVALID_BOUNCES 1000
#define LIGHT_POS(index) (2 * index + 1)
#define LIGHT_PWR(index) (2 * index + 0)
// =======================================================================
// function : Radiance
// purpose : Computes color along the given ray
// =======================================================================
vec4 Radiance (in SRay theRay, in vec3 theInverse)
{
vec3 aResult = vec3 (0.0f);
vec4 aWeight = vec4 (1.0f);
int anObjectId;
float anOpenGlDepth = ComputeOpenGlDepth (theRay);
for (int aDepth = 0; aDepth < NB_BOUNCES; ++aDepth)
{
SIntersect aHit = SIntersect (MAXFLOAT, vec2 (ZERO), ZERO);
ivec4 aTriIndex = SceneNearestHit (theRay, theInverse, aHit, anObjectId);
if (aTriIndex.x == -1)
{
vec4 aColor = vec4 (0.0f);
if (aWeight.w != 0.0f)
{
aColor = anOpenGlDepth != MAXFLOAT ?
ComputeOpenGlColor (theRay) : vec4 (0.0f, 0.0f, 0.0f, 1.0f);
}
else if (bool(uEnvironmentEnable))
{
float aTime = IntersectSphere (theRay, uSceneRadius);
aColor = textureLod (uEnvironmentMapTexture, Latlong (
theRay.Direct * aTime + theRay.Origin, uSceneRadius), 0.0f);
}
return vec4 (aResult.xyz + aWeight.xyz * aColor.xyz, aWeight.w * aColor.w);
}
vec3 aInvTransf0 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 0).xyz;
vec3 aInvTransf1 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 1).xyz;
vec3 aInvTransf2 = texelFetch (uSceneTransformTexture, anObjectId * 4 + 2).xyz;
aHit.Normal = normalize (vec3 (dot (aInvTransf0, aHit.Normal),
dot (aInvTransf1, aHit.Normal),
dot (aInvTransf2, aHit.Normal)));
// For polygons that are parallel to the screen plane, the depth slope
// is equal to 1, resulting in small polygon offset. For polygons that
// that are at a large angle to the screen, the depth slope tends to 1,
// resulting in a larger polygon offset
float aPolygonOffset = uSceneEpsilon * EPS_SCALE /
max (abs (dot (theRay.Direct, aHit.Normal)), MIN_SLOPE);
if (anOpenGlDepth < aHit.Time + aPolygonOffset)
{
vec4 aGlColor = ComputeOpenGlColor (theRay);
aResult += aWeight.xyz * aGlColor.xyz;
aWeight *= aGlColor.w;
}
theRay.Origin += theRay.Direct * aHit.Time; // intersection point
vec3 aNormal = SmoothNormal (aHit.UV, aTriIndex);
aNormal = normalize (vec3 (dot (aInvTransf0, aNormal),
dot (aInvTransf1, aNormal),
dot (aInvTransf2, aNormal)));
vec3 aAmbient = texelFetch (
uRaytraceMaterialTexture, MATERIAL_AMBN (aTriIndex.w)).rgb;
vec4 aDiffuse = texelFetch (
uRaytraceMaterialTexture, MATERIAL_DIFF (aTriIndex.w));
vec4 aSpecular = texelFetch (
uRaytraceMaterialTexture, MATERIAL_SPEC (aTriIndex.w));
vec4 aOpacity = texelFetch (
uRaytraceMaterialTexture, MATERIAL_TRAN (aTriIndex.w));
#ifdef USE_TEXTURES
if (aDiffuse.w >= 0.f)
{
vec4 aTexCoord = vec4 (SmoothUV (aHit.UV, aTriIndex), 0.f, 1.f);
vec4 aTrsfRow1 = texelFetch (
uRaytraceMaterialTexture, MATERIAL_TRS1 (aTriIndex.w));
vec4 aTrsfRow2 = texelFetch (
uRaytraceMaterialTexture, MATERIAL_TRS2 (aTriIndex.w));
aTexCoord.st = vec2 (dot (aTrsfRow1, aTexCoord),
dot (aTrsfRow2, aTexCoord));
vec3 aTexColor = textureLod (
uTextureSamplers[int(aDiffuse.w)], aTexCoord.st, 0.f).rgb;
aDiffuse.rgb *= aTexColor;
aAmbient.rgb *= aTexColor;
}
#endif
vec3 aEmission = texelFetch (
uRaytraceMaterialTexture, MATERIAL_EMIS (aTriIndex.w)).rgb;
float aGeomFactor = dot (aNormal, theRay.Direct);
aResult.xyz += aWeight.xyz * aOpacity.x * (
uGlobalAmbient.xyz * aAmbient * max (abs (aGeomFactor), 0.5f) + aEmission);
vec3 aSidedNormal = mix (aNormal, -aNormal, step (0.0f, aGeomFactor));
for (int aLightIdx = 0; aLightIdx < uLightCount; ++aLightIdx)
{
vec4 aLight = texelFetch (
uRaytraceLightSrcTexture, LIGHT_POS (aLightIdx));
float aDistance = MAXFLOAT;
if (aLight.w != 0.0f) // point light source
{
aDistance = length (aLight.xyz -= theRay.Origin);
aLight.xyz *= 1.0f / aDistance;
}
float aLdotN = dot (aLight.xyz, aSidedNormal);
if (aLdotN > 0.0f) // first check if light source is important
{
float aVisibility = 1.0f;
if (bool(uShadowsEnable))
{
SRay aShadow = SRay (theRay.Origin, aLight.xyz);
aShadow.Origin += uSceneEpsilon * (aLight.xyz +
mix (-aHit.Normal, aHit.Normal, step (0.0f, dot (aHit.Normal, aLight.xyz))));
vec3 aInverse = 1.0f / max (abs (aLight.xyz), SMALL);
aVisibility = SceneAnyHit (
aShadow, mix (-aInverse, aInverse, step (ZERO, aLight.xyz)), aDistance);
}
if (aVisibility > 0.0f)
{
vec3 aIntensity = vec3 (texelFetch (
uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx)));
float aRdotV = dot (reflect (aLight.xyz, aSidedNormal), theRay.Direct);
aResult.xyz += aWeight.xyz * (aOpacity.x * aVisibility) * aIntensity *
(aDiffuse.xyz * aLdotN + aSpecular.xyz * pow (max (0.f, aRdotV), aSpecular.w));
}
}
}
if (aOpacity.x != 1.0f)
{
aWeight *= aOpacity.y;
if (aOpacity.z != 1.0f)
{
theRay.Direct = Refract (theRay.Direct, aNormal, aOpacity.z, aOpacity.w);
}
else
{
anOpenGlDepth -= aHit.Time + uSceneEpsilon;
}
}
else
{
aWeight *= bool(uReflectionsEnable) ?
texelFetch (uRaytraceMaterialTexture, MATERIAL_REFL (aTriIndex.w)) : vec4 (0.0f);
vec3 aReflect = reflect (theRay.Direct, aNormal);
if (dot (aReflect, aHit.Normal) * dot (theRay.Direct, aHit.Normal) > 0.0f)
{
aReflect = reflect (theRay.Direct, aHit.Normal);
}
theRay.Direct = aReflect;
}
if (all (lessThanEqual (aWeight.xyz, THRESHOLD)))
{
aDepth = INVALID_BOUNCES;
}
else if (aOpacity.x == 1.0f || aOpacity.z != 1.0f) // if no simple transparency
{
theRay.Origin += aHit.Normal * mix (
-uSceneEpsilon, uSceneEpsilon, step (0.0f, dot (aHit.Normal, theRay.Direct)));
theInverse = 1.0f / max (abs (theRay.Direct), SMALL);
theInverse = mix (-theInverse, theInverse, step (ZERO, theRay.Direct));
anOpenGlDepth = MAXFLOAT; // disable combining image with OpenGL output
}
theRay.Origin += theRay.Direct * uSceneEpsilon;
}
return vec4 (aResult.x,
aResult.y,
aResult.z,
aWeight.w);
}
|