366 const bool is_testShadow =
m_boardAdapter.m_Cfg->m_Render.raytrace_shadows;
372 const RAY& rayAA = aRayPck[i];
375 hitAA.
m_tHit = std::numeric_limits<float>::infinity();
380 const unsigned int idx0y1 = ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 );
381 const unsigned int idx1y1 = ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 );
387 unsigned int nodex1y0 = 0;
392 unsigned int nodex0y1 = 0;
397 unsigned int nodex1y1 = 0;
404 if( ( ( nodex0y0 == nodex1y0 ) || ( nodex1y0 == 0 ) )
405 && ( ( nodex0y0 == nodex0y1 ) || ( nodex0y1 == 0 ) )
406 && ( ( nodex0y0 == nodex1y1 ) || ( nodex1y1 == 0 ) )
407 && ( nodex0y0 == node_AA_x0y0 ) )
443 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex0y0 );
445 if( ( nodex1y0 != 0 ) && ( nodex0y0 != nodex1y0 ) )
446 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex1y0 );
448 if( ( nodex0y1 != 0 ) && ( nodex0y0 != nodex0y1 ) && ( nodex1y0 != nodex0y1 ) )
449 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex0y1 );
451 if( ( nodex1y1 != 0 ) && ( nodex0y0 != nodex1y1 ) && ( nodex0y1 != nodex1y1 ) &&
452 ( nodex1y0 != nodex1y1 ) )
453 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex1y1 );
455 if( (node_AA_x0y0 != 0 ) && ( nodex0y0 != node_AA_x0y0 ) &&
456 ( nodex0y1 != node_AA_x0y0 ) && ( nodex1y0 != node_AA_x0y0 ) &&
457 ( nodex1y1 != node_AA_x0y0 ) )
458 hitted |=
m_accelerator->Intersect( rayAA, hitAA, node_AA_x0y0 );
463 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0,
473 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0,
507 const float posYfactor = (float) ( blockPosI.y + y ) / (float)
m_windowSize.y;
514 if( !
m_accelerator->Intersect( blockPacket, hitPacket_X0Y0 ) )
521 const SFVEC4F& outColor = bgColor[y];
523 const unsigned int yBlockPos = blockPos.y + y;
537 const bool isFinalColor = !
m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing;
541 const SFVEC4F& outColor = bgColor[y];
543 const unsigned int yConst = blockPos.x + ( ( y + blockPos.y ) *
m_realBufferSize.x );
547 uint8_t* ptr = &ptrPBO[( yConst + x ) * 4];
575 if( !
m_accelerator->Intersect( blockPacket_AA_X1Y1, hitPacket_AA_X1Y1 ) )
580 const SFVEC4F& outColor = bgColor[y];
583 hitColor_AA_X1Y1[i] = outColor;
589 m_boardAdapter.m_Cfg->m_Render.raytrace_shadows, hitColor_AA_X1Y1 );
598 SFVEC4F color_average = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] ) *
SFVEC4F( 0.5f );
600 hitColor_AA_X1Y0[i] = color_average;
601 hitColor_AA_X0Y1[i] = color_average;
602 hitColor_AA_X0Y1_half[i] = color_average;
611 randDisp, blockRayPck_AA_X1Y0 );
615 randDisp, blockRayPck_AA_X0Y1 );
619 randDisp, blockRayPck_AA_X1Y1_half );
628 blockRayPck_AA_X1Y1_half, hitColor_AA_X0Y1_half );
633 hitColor_X0Y0[i] = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] + hitColor_AA_X1Y0[i] +
634 hitColor_AA_X0Y1[i] + hitColor_AA_X0Y1_half[i] ) *
640 uint8_t* ptr = &ptrPBO[( blockPos.x + ( blockPos.y *
m_realBufferSize.x ) ) * 4];
655 const SFVEC4F& hColor = hitColor_X0Y0[i];
657 if( hitPacket_X0Y0[i].m_hitresult ==
true )
817 std::atomic<size_t> nextBlock( 0 );
818 std::atomic<size_t> threadsFinished( 0 );
820 size_t parallelThreadCount = std::min<size_t>(
821 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
824 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
826 std::thread t = std::thread( [&]()
829 iBlock = nextBlock.fetch_add( 1 ) )
842 packet.m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
843 packet.m_HitInfo.m_acc_node_info = 0;
844 packet.m_hitresult =
false;
858 const float posYfactor =
859 (float) ( windowsPos.y + y * 4.0f ) / (float)
m_windowSize.y;
861 bgColor[y] = bgTopColor *
SFVEC4F( posYfactor )
871 if( hitPacket[i].m_hitresult ==
true )
880 hitColorShading[i] = bhColorY;
887 const SFVEC4F bgColorY = bgColor[y];
908 const unsigned int iLT = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 0 ) );
909 const unsigned int iRT = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 0 ) );
910 const unsigned int iLB = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 ) );
911 const unsigned int iRB = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 ) );
914 const COLOR_RGBA& cLT = hitColorShading[ iLT ];
915 const COLOR_RGBA& cRT = hitColorShading[ iRT ];
916 const COLOR_RGBA& cLB = hitColorShading[ iLB ];
917 const COLOR_RGBA& cRB = hitColorShading[ iRB ];
932 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
934 bool hittedC =
false;
936 if( ( hitPacket[iLT].m_hitresult ==
true )
937 || ( hitPacket[iRT].m_hitresult ==
true )
938 || ( hitPacket[iLB].m_hitresult ==
true )
939 || ( hitPacket[iRB].m_hitresult ==
true ) )
941 oriC = ( oriLT + oriRB ) * 0.5f;
942 dirC = glm::normalize( ( dirLT + dirRB ) * 0.5f );
946 centerRay.
Init( oriC, dirC );
954 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
957 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
958 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
961 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) && ( nodeLB != nodeRT ) )
962 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
965 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) && ( nodeRB != nodeLT )
966 && ( nodeRB != nodeRT ) )
967 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
977 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
978 hittedC =
m_accelerator->Intersect( centerRay, centerHitInfo );
996 rayLRT.
Init( ( oriLT + oriRT ) * 0.5f,
997 glm::normalize( ( dirLT + dirRT ) * 0.5f ) );
1000 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
1002 if( hitPacket[iLT].m_hitresult && hitPacket[iRT].m_hitresult
1003 && ( hitPacket[iLT].m_HitInfo.pHitObject
1010 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1021 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iRT ].m_hitresult )
1023 const unsigned int nodeLT =
1025 const unsigned int nodeRT =
1028 bool hittedLRT =
false;
1034 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
1040 false, 0,
false ) );
1043 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
1068 rayLTB.
Init( ( oriLT + oriLB ) * 0.5f,
1069 glm::normalize( ( dirLT + dirLB ) * 0.5f ) );
1072 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1074 if( hitPacket[ iLT ].m_hitresult && hitPacket[ iLB ].m_hitresult
1075 && ( hitPacket[ iLT ].m_HitInfo.pHitObject ==
1082 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1086 shadeHit( bgColorY, rayLTB, hitInfoLTB,
false, 0,
false ) );
1092 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iLB ].m_hitresult )
1094 const unsigned int nodeLT =
1096 const unsigned int nodeLB =
1099 bool hittedLTB =
false;
1105 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) )
1111 false, 0,
false ) );
1114 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1134 rayRTB.
Init( ( oriRT + oriRB ) * 0.5f,
1135 glm::normalize( ( dirRT + dirRB ) * 0.5f ) );
1138 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1140 if( hitPacket[ iRT ].m_hitresult && hitPacket[ iRB ].m_hitresult
1141 && ( hitPacket[ iRT ].m_HitInfo.pHitObject ==
1150 glm::normalize( ( hitPacket[iRT].m_HitInfo.m_HitNormal
1161 if( hitPacket[ iRT ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1163 const unsigned int nodeRT =
1165 const unsigned int nodeRB =
1168 bool hittedRTB =
false;
1174 if( ( nodeRB != 0 ) && ( nodeRB != nodeRT ) )
1185 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1189 false, 0,
false ) );
1204 rayLRB.
Init( ( oriLB + oriRB ) * 0.5f,
1205 glm::normalize( ( dirLB + dirRB ) * 0.5f ) );
1208 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1210 if( hitPacket[iLB].m_hitresult && hitPacket[iRB].m_hitresult
1211 && ( hitPacket[iLB].m_HitInfo.pHitObject ==
1220 glm::normalize( ( hitPacket[iLB].m_HitInfo.m_HitNormal
1231 if( hitPacket[ iLB ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1233 const unsigned int nodeLB =
1235 const unsigned int nodeRB =
1238 bool hittedLRB =
false;
1244 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) )
1251 false, 0,
false ) );
1255 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1259 false, 0,
false ) );
1269 if( hitPacket[ iLT ].m_hitresult || hittedC )
1273 rayLTC.
Init( ( oriLT + oriC ) * 0.5f,
1274 glm::normalize( ( dirLT + dirC ) * 0.5f ) );
1277 hitInfoLTC.
m_tHit = std::numeric_limits<float>::infinity();
1279 bool hitted =
false;
1283 else if( hitPacket[ iLT ].m_hitresult )
1296 if( hitPacket[ iRT ].m_hitresult || hittedC )
1300 rayRTC.
Init( ( oriRT + oriC ) * 0.5f,
1301 glm::normalize( ( dirRT + dirC ) * 0.5f ) );
1304 hitInfoRTC.
m_tHit = std::numeric_limits<float>::infinity();
1306 bool hitted =
false;
1310 else if( hitPacket[ iRT ].m_hitresult )
1312 rayRTC, hitInfoRTC );
1322 if( hitPacket[ iLB ].m_hitresult || hittedC )
1326 rayLBC.
Init( ( oriLB + oriC ) * 0.5f,
1327 glm::normalize( ( dirLB + dirC ) * 0.5f ) );
1330 hitInfoLBC.
m_tHit = std::numeric_limits<float>::infinity();
1332 bool hitted =
false;
1336 else if( hitPacket[ iLB ].m_hitresult )
1338 rayLBC, hitInfoLBC );
1348 if( hitPacket[ iRB ].m_hitresult || hittedC )
1352 rayRBC.
Init( ( oriRB + oriC ) * 0.5f,
1353 glm::normalize( ( dirRB + dirC ) * 0.5f ) );
1356 hitInfoRBC.
m_tHit = std::numeric_limits<float>::infinity();
1358 bool hitted =
false;
1362 else if( hitPacket[ iRB ].m_hitresult )
1364 rayRBC, hitInfoRBC );
1409 while( threadsFinished < parallelThreadCount )
1410 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
1417 HITINFO& aHitInfo,
bool aIsInsideObject,
1418 unsigned int aRecursiveLevel,
bool is_testShadow )
const
1421 wxASSERT( objMaterial !=
nullptr );
1426 if( aRecursiveLevel > 7 )
1433 const SFVEC4F diffuseColorObj =
1436#if USE_EXPERIMENTAL_SOFT_SHADOWS
1440 float shadow_att_factor_sum = 0.0f;
1442 unsigned int nr_lights_that_can_cast_shadows = 0;
1450 light->GetLightParameters( hitPoint, vectorToLight, colorOfLight, distToLight );
1452 const float NdotL = glm::dot( aHitInfo.
m_HitNormal, vectorToLight );
1456 if( NdotL >= FLT_EPSILON )
1458 float shadow_att_factor_light = 1.0f;
1460 if( is_testShadow && light->GetCastShadows() )
1462 nr_lights_that_can_cast_shadows++;
1463#if USE_EXPERIMENTAL_SOFT_SHADOWS
1465 if( aRecursiveLevel > 0 )
1469 rayToLight.
Init( hitPoint, vectorToLight );
1474 shadow_att_factor_light = 0.0f;
1476#if USE_EXPERIMENTAL_SOFT_SHADOWS
1480 const unsigned int shadow_number_of_samples =
1482 const float shadow_inc_factor = 1.0f / (float) ( shadow_number_of_samples );
1484 for(
unsigned int i = 0; i < shadow_number_of_samples; ++i )
1490 rayToLight.
Init( hitPoint, vectorToLight );
1495 const SFVEC3F disturbed_vector_to_light =
1496 glm::normalize( vectorToLight + unifVector *
1499 rayToLight.
Init( hitPoint, disturbed_vector_to_light );
1507 shadow_att_factor_light -= shadow_inc_factor;
1512 shadow_att_factor_sum += shadow_att_factor_light;
1515 outColor +=
SFVEC4F( objMaterial->
Shade( aRay, aHitInfo, NdotL, diffuseColorObj,
1516 vectorToLight, colorOfLight,
1517 shadow_att_factor_light ),
1523 if( nr_lights_that_can_cast_shadows > 0 )
1526 shadow_att_factor_sum / (
float) ( nr_lights_that_can_cast_shadows * 1.0f ), 0.0f );
1534 outColor = glm::min( outColor,
SFVEC4F( 1.0f ) );
1541 && ( aRecursiveLevel < objMaterial->GetReflectionRecursionCount() ) )
1543 const unsigned int reflection_number_of_samples =
1551 for(
unsigned int i = 0; i < reflection_number_of_samples; ++i )
1557 reflectedRay.
Init( hitPoint, reflectVector );
1562 const SFVEC3F random_reflectVector =
1563 glm::normalize( reflectVector
1566 .raytrace_spread_reflections );
1568 reflectedRay.
Init( hitPoint, random_reflectVector );
1572 reflectedHit.
m_tHit = std::numeric_limits<float>::infinity();
1574 if(
m_accelerator->Intersect( reflectedRay, reflectedHit ) )
1578 shadeHit( aBgColor, reflectedRay, reflectedHit,
false,
1579 aRecursiveLevel + 1, is_testShadow ) *
1582 (1.0f / ( 1.0f + 0.75f * reflectedHit.
m_tHit *
1583 reflectedHit.
m_tHit) ) );
1589 outColor += (sum_color /
SFVEC4F( (
float)reflection_number_of_samples) );
1595 if( ( objTransparency > 0.0f ) &&
m_boardAdapter.m_Cfg->m_Render.raytrace_refractions
1596 && ( aRecursiveLevel < objMaterial->GetRefractionRecursionCount() ) )
1598 const float airIndex = 1.000293f;
1599 const float glassIndex = 1.49f;
1600 const float air_over_glass = airIndex / glassIndex;
1601 const float glass_over_air = glassIndex / airIndex;
1603 const float refractionRatio = aIsInsideObject?glass_over_air:air_over_glass;
1615 const unsigned int refractions_number_of_samples =
1620 for(
unsigned int i = 0; i < refractions_number_of_samples; ++i )
1626 refractedRay.
Init( startPoint, refractedVector );
1631 const SFVEC3F randomizeRefractedVector =
1632 glm::normalize( refractedVector +
1636 refractedRay.
Init( startPoint, randomizeRefractedVector );
1640 refractedHit.
m_tHit = std::numeric_limits<float>::infinity();
1642 SFVEC4F refractedColor = aBgColor;
1644 if(
m_accelerator->Intersect( refractedRay, refractedHit ) )
1646 refractedColor =
shadeHit( aBgColor, refractedRay, refractedHit,
1647 !aIsInsideObject, aRecursiveLevel + 1,
false );
1650 (1.0f - objTransparency ) *
1654 const SFVEC4F transparency = 1.0f / ( absorbance + 1.0f );
1656 sum_color += refractedColor * transparency;
1660 sum_color += refractedColor;
1664 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * sum_color
1665 /
SFVEC4F( (
float) refractions_number_of_samples );
1669 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * aBgColor;