367 const bool is_testShadow =
m_boardAdapter.m_Cfg->m_Render.raytrace_shadows;
373 const RAY& rayAA = aRayPck[i];
376 hitAA.
m_tHit = std::numeric_limits<float>::infinity();
381 const unsigned int idx0y1 = ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 );
382 const unsigned int idx1y1 = ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 );
388 unsigned int nodex1y0 = 0;
393 unsigned int nodex0y1 = 0;
398 unsigned int nodex1y1 = 0;
408 if( ( nodex0y0 != nodex1y0 && nodex1y0 != 0 )
409 || ( nodex0y0 != nodex0y1 && nodex0y1 != 0 )
410 || ( nodex0y0 != nodex1y1 && nodex1y1 != 0 )
411 || ( nodex0y0 != node_AA_x0y0 ) )
418 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex0y0 );
421 && nodex0y0 != nodex1y0 )
423 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex1y0 );
427 && nodex0y0 != nodex0y1
428 && nodex1y0 != nodex0y1 )
430 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex0y1 );
434 && nodex0y0 != nodex1y1
435 && nodex0y1 != nodex1y1
436 && nodex1y0 != nodex1y1 )
438 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex1y1 );
441 if( node_AA_x0y0 != 0
442 && nodex0y0 != node_AA_x0y0
443 && nodex0y1 != node_AA_x0y0
444 && nodex1y0 != node_AA_x0y0
445 && nodex1y1 != node_AA_x0y0 )
447 hitted |=
m_accelerator->Intersect( rayAA, hitAA, node_AA_x0y0 );
453 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0, is_testShadow );
462 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0, is_testShadow );
495 const float posYfactor = (float) ( blockPosI.y + y ) / (float)
m_windowSize.y;
502 if( !
m_accelerator->Intersect( blockPacket, hitPacket_X0Y0 ) )
509 const SFVEC4F& outColor = bgColor[y];
511 const unsigned int yBlockPos = blockPos.y + y;
525 const bool isFinalColor = !
m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing;
529 const SFVEC4F& outColor = bgColor[y];
531 const unsigned int yConst = blockPos.x + ( ( y + blockPos.y ) *
m_realBufferSize.x );
535 uint8_t* ptr = &ptrPBO[( yConst + x ) * 4];
563 if( !
m_accelerator->Intersect( blockPacket_AA_X1Y1, hitPacket_AA_X1Y1 ) )
568 const SFVEC4F& outColor = bgColor[y];
571 hitColor_AA_X1Y1[i] = outColor;
577 m_boardAdapter.m_Cfg->m_Render.raytrace_shadows, hitColor_AA_X1Y1 );
586 SFVEC4F color_average = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] ) *
SFVEC4F( 0.5f );
588 hitColor_AA_X1Y0[i] = color_average;
589 hitColor_AA_X0Y1[i] = color_average;
590 hitColor_AA_X0Y1_half[i] = color_average;
599 randDisp, blockRayPck_AA_X1Y0 );
603 randDisp, blockRayPck_AA_X0Y1 );
607 randDisp, blockRayPck_AA_X1Y1_half );
616 blockRayPck_AA_X1Y1_half, hitColor_AA_X0Y1_half );
621 hitColor_X0Y0[i] = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] + hitColor_AA_X1Y0[i] +
622 hitColor_AA_X0Y1[i] + hitColor_AA_X0Y1_half[i] ) *
628 uint8_t* ptr = &ptrPBO[( blockPos.x + ( blockPos.y *
m_realBufferSize.x ) ) * 4];
643 const SFVEC4F& hColor = hitColor_X0Y0[i];
645 if( hitPacket_X0Y0[i].m_hitresult ==
true )
805 std::atomic<size_t> nextBlock( 0 );
806 std::atomic<size_t> threadsFinished( 0 );
808 size_t parallelThreadCount = std::min<size_t>(
809 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
812 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
814 std::thread t = std::thread( [&]()
817 iBlock = nextBlock.fetch_add( 1 ) )
830 packet.m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
831 packet.m_HitInfo.m_acc_node_info = 0;
832 packet.m_hitresult =
false;
846 const float posYfactor =
847 (float) ( windowsPos.y + y * 4.0f ) / (float)
m_windowSize.y;
849 bgColor[y] = bgTopColor *
SFVEC4F( posYfactor )
859 if( hitPacket[i].m_hitresult ==
true )
868 hitColorShading[i] = bhColorY;
875 const SFVEC4F bgColorY = bgColor[y];
896 const unsigned int iLT = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 0 ) );
897 const unsigned int iRT = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 0 ) );
898 const unsigned int iLB = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 ) );
899 const unsigned int iRB = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 ) );
902 const COLOR_RGBA& cLT = hitColorShading[ iLT ];
903 const COLOR_RGBA& cRT = hitColorShading[ iRT ];
904 const COLOR_RGBA& cLB = hitColorShading[ iLB ];
905 const COLOR_RGBA& cRB = hitColorShading[ iRB ];
920 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
922 bool hittedC =
false;
924 if( ( hitPacket[iLT].m_hitresult ==
true )
925 || ( hitPacket[iRT].m_hitresult ==
true )
926 || ( hitPacket[iLB].m_hitresult ==
true )
927 || ( hitPacket[iRB].m_hitresult ==
true ) )
929 oriC = ( oriLT + oriRB ) * 0.5f;
930 dirC = glm::normalize( ( dirLT + dirRB ) * 0.5f );
934 centerRay.
Init( oriC, dirC );
942 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
945 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
946 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
949 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) && ( nodeLB != nodeRT ) )
950 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
953 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) && ( nodeRB != nodeLT )
954 && ( nodeRB != nodeRT ) )
955 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
965 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
966 hittedC =
m_accelerator->Intersect( centerRay, centerHitInfo );
984 rayLRT.
Init( ( oriLT + oriRT ) * 0.5f,
985 glm::normalize( ( dirLT + dirRT ) * 0.5f ) );
988 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
990 if( hitPacket[iLT].m_hitresult && hitPacket[iRT].m_hitresult
991 && ( hitPacket[iLT].m_HitInfo.pHitObject
998 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1009 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iRT ].m_hitresult )
1011 const unsigned int nodeLT =
1013 const unsigned int nodeRT =
1016 bool hittedLRT =
false;
1022 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
1028 false, 0,
false ) );
1031 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
1056 rayLTB.
Init( ( oriLT + oriLB ) * 0.5f,
1057 glm::normalize( ( dirLT + dirLB ) * 0.5f ) );
1060 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1062 if( hitPacket[ iLT ].m_hitresult && hitPacket[ iLB ].m_hitresult
1063 && ( hitPacket[ iLT ].m_HitInfo.pHitObject ==
1070 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1074 shadeHit( bgColorY, rayLTB, hitInfoLTB,
false, 0,
false ) );
1080 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iLB ].m_hitresult )
1082 const unsigned int nodeLT =
1084 const unsigned int nodeLB =
1087 bool hittedLTB =
false;
1093 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) )
1099 false, 0,
false ) );
1102 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1122 rayRTB.
Init( ( oriRT + oriRB ) * 0.5f,
1123 glm::normalize( ( dirRT + dirRB ) * 0.5f ) );
1126 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1128 if( hitPacket[ iRT ].m_hitresult && hitPacket[ iRB ].m_hitresult
1129 && ( hitPacket[ iRT ].m_HitInfo.pHitObject ==
1138 glm::normalize( ( hitPacket[iRT].m_HitInfo.m_HitNormal
1149 if( hitPacket[ iRT ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1151 const unsigned int nodeRT =
1153 const unsigned int nodeRB =
1156 bool hittedRTB =
false;
1162 if( ( nodeRB != 0 ) && ( nodeRB != nodeRT ) )
1173 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1177 false, 0,
false ) );
1192 rayLRB.
Init( ( oriLB + oriRB ) * 0.5f,
1193 glm::normalize( ( dirLB + dirRB ) * 0.5f ) );
1196 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1198 if( hitPacket[iLB].m_hitresult && hitPacket[iRB].m_hitresult
1199 && ( hitPacket[iLB].m_HitInfo.pHitObject ==
1208 glm::normalize( ( hitPacket[iLB].m_HitInfo.m_HitNormal
1219 if( hitPacket[ iLB ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1221 const unsigned int nodeLB =
1223 const unsigned int nodeRB =
1226 bool hittedLRB =
false;
1232 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) )
1239 false, 0,
false ) );
1243 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1247 false, 0,
false ) );
1257 if( hitPacket[ iLT ].m_hitresult || hittedC )
1261 rayLTC.
Init( ( oriLT + oriC ) * 0.5f,
1262 glm::normalize( ( dirLT + dirC ) * 0.5f ) );
1265 hitInfoLTC.
m_tHit = std::numeric_limits<float>::infinity();
1267 bool hitted =
false;
1271 else if( hitPacket[ iLT ].m_hitresult )
1284 if( hitPacket[ iRT ].m_hitresult || hittedC )
1288 rayRTC.
Init( ( oriRT + oriC ) * 0.5f,
1289 glm::normalize( ( dirRT + dirC ) * 0.5f ) );
1292 hitInfoRTC.
m_tHit = std::numeric_limits<float>::infinity();
1294 bool hitted =
false;
1298 else if( hitPacket[ iRT ].m_hitresult )
1300 rayRTC, hitInfoRTC );
1310 if( hitPacket[ iLB ].m_hitresult || hittedC )
1314 rayLBC.
Init( ( oriLB + oriC ) * 0.5f,
1315 glm::normalize( ( dirLB + dirC ) * 0.5f ) );
1318 hitInfoLBC.
m_tHit = std::numeric_limits<float>::infinity();
1320 bool hitted =
false;
1324 else if( hitPacket[ iLB ].m_hitresult )
1326 rayLBC, hitInfoLBC );
1336 if( hitPacket[ iRB ].m_hitresult || hittedC )
1340 rayRBC.
Init( ( oriRB + oriC ) * 0.5f,
1341 glm::normalize( ( dirRB + dirC ) * 0.5f ) );
1344 hitInfoRBC.
m_tHit = std::numeric_limits<float>::infinity();
1346 bool hitted =
false;
1350 else if( hitPacket[ iRB ].m_hitresult )
1352 rayRBC, hitInfoRBC );
1397 while( threadsFinished < parallelThreadCount )
1398 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
1405 HITINFO& aHitInfo,
bool aIsInsideObject,
1406 unsigned int aRecursiveLevel,
bool is_testShadow )
const
1409 wxASSERT( objMaterial !=
nullptr );
1414 if( aRecursiveLevel > 7 )
1421 const SFVEC4F diffuseColorObj =
1425 float shadow_att_factor_sum = 0.0f;
1427 unsigned int nr_lights_that_can_cast_shadows = 0;
1435 light->GetLightParameters( hitPoint, vectorToLight, colorOfLight, distToLight );
1437 const float NdotL = glm::dot( aHitInfo.
m_HitNormal, vectorToLight );
1441 if( NdotL >= FLT_EPSILON )
1443 float shadow_att_factor_light = 1.0f;
1445 if( is_testShadow && light->GetCastShadows() )
1447 nr_lights_that_can_cast_shadows++;
1450 if( aRecursiveLevel > 0 )
1453 rayToLight.
Init( hitPoint, vectorToLight );
1458 shadow_att_factor_light = 0.0f;
1463 const unsigned int shadow_number_of_samples =
1465 const float shadow_inc_factor = 1.0f / (float) ( shadow_number_of_samples );
1467 for(
unsigned int i = 0; i < shadow_number_of_samples; ++i )
1473 rayToLight.
Init( hitPoint, vectorToLight );
1478 const SFVEC3F disturbed_vector_to_light =
1479 glm::normalize( vectorToLight + unifVector *
1482 rayToLight.
Init( hitPoint, disturbed_vector_to_light );
1486 shadow_att_factor_light -= shadow_inc_factor;
1490 shadow_att_factor_sum += shadow_att_factor_light;
1493 outColor +=
SFVEC4F( objMaterial->
Shade( aRay, aHitInfo, NdotL, diffuseColorObj,
1494 vectorToLight, colorOfLight,
1495 shadow_att_factor_light ),
1501 if( nr_lights_that_can_cast_shadows > 0 )
1504 shadow_att_factor_sum / (
float) ( nr_lights_that_can_cast_shadows * 1.0f ), 0.0f );
1512 outColor = glm::min( outColor,
SFVEC4F( 1.0f ) );
1519 && ( aRecursiveLevel < objMaterial->GetReflectionRecursionCount() ) )
1521 const unsigned int reflection_number_of_samples =
1529 for(
unsigned int i = 0; i < reflection_number_of_samples; ++i )
1535 reflectedRay.
Init( hitPoint, reflectVector );
1540 const SFVEC3F random_reflectVector =
1541 glm::normalize( reflectVector
1544 .raytrace_spread_reflections );
1546 reflectedRay.
Init( hitPoint, random_reflectVector );
1550 reflectedHit.
m_tHit = std::numeric_limits<float>::infinity();
1552 if(
m_accelerator->Intersect( reflectedRay, reflectedHit ) )
1556 shadeHit( aBgColor, reflectedRay, reflectedHit,
false,
1557 aRecursiveLevel + 1, is_testShadow ) *
1560 (1.0f / ( 1.0f + 0.75f * reflectedHit.
m_tHit *
1561 reflectedHit.
m_tHit) ) );
1567 outColor += (sum_color /
SFVEC4F( (
float)reflection_number_of_samples) );
1573 if( ( objTransparency > 0.0f ) &&
m_boardAdapter.m_Cfg->m_Render.raytrace_refractions
1574 && ( aRecursiveLevel < objMaterial->GetRefractionRecursionCount() ) )
1576 const float airIndex = 1.000293f;
1577 const float glassIndex = 1.49f;
1578 const float air_over_glass = airIndex / glassIndex;
1579 const float glass_over_air = glassIndex / airIndex;
1581 const float refractionRatio = aIsInsideObject?glass_over_air:air_over_glass;
1593 const unsigned int refractions_number_of_samples =
1598 for(
unsigned int i = 0; i < refractions_number_of_samples; ++i )
1604 refractedRay.
Init( startPoint, refractedVector );
1609 const SFVEC3F randomizeRefractedVector =
1610 glm::normalize( refractedVector +
1614 refractedRay.
Init( startPoint, randomizeRefractedVector );
1618 refractedHit.
m_tHit = std::numeric_limits<float>::infinity();
1620 SFVEC4F refractedColor = aBgColor;
1622 if(
m_accelerator->Intersect( refractedRay, refractedHit ) )
1624 refractedColor =
shadeHit( aBgColor, refractedRay, refractedHit,
1625 !aIsInsideObject, aRecursiveLevel + 1,
false );
1628 (1.0f - objTransparency ) *
1632 const SFVEC4F transparency = 1.0f / ( absorbance + 1.0f );
1634 sum_color += refractedColor * transparency;
1638 sum_color += refractedColor;
1642 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * sum_color
1643 /
SFVEC4F( (
float) refractions_number_of_samples );
1647 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * aBgColor;