363 const bool is_testShadow =
m_boardAdapter.m_Cfg->m_Render.raytrace_shadows;
369 const RAY& rayAA = aRayPck[i];
372 hitAA.
m_tHit = std::numeric_limits<float>::infinity();
377 const unsigned int idx0y1 = ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 );
378 const unsigned int idx1y1 = ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 );
384 unsigned int nodex1y0 = 0;
389 unsigned int nodex0y1 = 0;
394 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 ) )
414 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex0y0 );
417 && nodex0y0 != nodex1y0 )
419 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex1y0 );
423 && nodex0y0 != nodex0y1
424 && nodex1y0 != nodex0y1 )
426 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex0y1 );
430 && nodex0y0 != nodex1y1
431 && nodex0y1 != nodex1y1
432 && nodex1y0 != nodex1y1 )
434 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex1y1 );
437 if( node_AA_x0y0 != 0
438 && nodex0y0 != node_AA_x0y0
439 && nodex0y1 != node_AA_x0y0
440 && nodex1y0 != node_AA_x0y0
441 && nodex1y1 != node_AA_x0y0 )
443 hitted |=
m_accelerator->Intersect( rayAA, hitAA, node_AA_x0y0 );
449 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0, is_testShadow );
458 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0, is_testShadow );
491 const float posYfactor = (float) ( blockPosI.y + y ) / (float)
m_windowSize.y;
498 if( !
m_accelerator->Intersect( blockPacket, hitPacket_X0Y0 ) )
505 const SFVEC4F& outColor = bgColor[y];
507 const unsigned int yBlockPos = blockPos.y + y;
521 const bool isFinalColor = !
m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing;
525 const SFVEC4F& outColor = bgColor[y];
527 const unsigned int yConst = blockPos.x + ( ( y + blockPos.y ) *
m_realBufferSize.x );
531 uint8_t* ptr = &ptrPBO[( yConst + x ) * 4];
559 if( !
m_accelerator->Intersect( blockPacket_AA_X1Y1, hitPacket_AA_X1Y1 ) )
564 const SFVEC4F& outColor = bgColor[y];
567 hitColor_AA_X1Y1[i] = outColor;
573 m_boardAdapter.m_Cfg->m_Render.raytrace_shadows, hitColor_AA_X1Y1 );
582 SFVEC4F color_average = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] ) *
SFVEC4F( 0.5f );
584 hitColor_AA_X1Y0[i] = color_average;
585 hitColor_AA_X0Y1[i] = color_average;
586 hitColor_AA_X0Y1_half[i] = color_average;
595 randDisp, blockRayPck_AA_X1Y0 );
599 randDisp, blockRayPck_AA_X0Y1 );
603 randDisp, blockRayPck_AA_X1Y1_half );
612 blockRayPck_AA_X1Y1_half, hitColor_AA_X0Y1_half );
617 hitColor_X0Y0[i] = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] + hitColor_AA_X1Y0[i] +
618 hitColor_AA_X0Y1[i] + hitColor_AA_X0Y1_half[i] ) *
624 uint8_t* ptr = &ptrPBO[( blockPos.x + ( blockPos.y *
m_realBufferSize.x ) ) * 4];
639 const SFVEC4F& hColor = hitColor_X0Y0[i];
641 if( hitPacket_X0Y0[i].m_hitresult ==
true )
800 std::atomic<size_t> nextBlock( 0 );
801 std::atomic<size_t> threadsFinished( 0 );
803 size_t parallelThreadCount = std::min<size_t>(
804 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
807 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
809 std::thread t = std::thread( [&]()
812 iBlock = nextBlock.fetch_add( 1 ) )
825 packet.m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
826 packet.m_HitInfo.m_acc_node_info = 0;
827 packet.m_hitresult =
false;
841 const float posYfactor =
842 (float) ( windowsPos.y + y * 4.0f ) / (float)
m_windowSize.y;
844 bgColor[y] = bgTopColor *
SFVEC4F( posYfactor )
854 if( hitPacket[i].m_hitresult ==
true )
863 hitColorShading[i] = bhColorY;
870 const SFVEC4F bgColorY = bgColor[y];
891 const unsigned int iLT = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 0 ) );
892 const unsigned int iRT = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 0 ) );
893 const unsigned int iLB = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 ) );
894 const unsigned int iRB = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 ) );
897 const COLOR_RGBA& cLT = hitColorShading[ iLT ];
898 const COLOR_RGBA& cRT = hitColorShading[ iRT ];
899 const COLOR_RGBA& cLB = hitColorShading[ iLB ];
900 const COLOR_RGBA& cRB = hitColorShading[ iRB ];
915 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
917 bool hittedC =
false;
919 if( ( hitPacket[iLT].m_hitresult ==
true )
920 || ( hitPacket[iRT].m_hitresult ==
true )
921 || ( hitPacket[iLB].m_hitresult ==
true )
922 || ( hitPacket[iRB].m_hitresult ==
true ) )
924 oriC = ( oriLT + oriRB ) * 0.5f;
925 dirC = glm::normalize( ( dirLT + dirRB ) * 0.5f );
929 centerRay.
Init( oriC, dirC );
937 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
940 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
941 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
944 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) && ( nodeLB != nodeRT ) )
945 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
948 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) && ( nodeRB != nodeLT )
949 && ( nodeRB != nodeRT ) )
950 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
960 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
961 hittedC =
m_accelerator->Intersect( centerRay, centerHitInfo );
979 rayLRT.
Init( ( oriLT + oriRT ) * 0.5f,
980 glm::normalize( ( dirLT + dirRT ) * 0.5f ) );
983 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
985 if( hitPacket[iLT].m_hitresult && hitPacket[iRT].m_hitresult
986 && ( hitPacket[iLT].m_HitInfo.pHitObject
993 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1004 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iRT ].m_hitresult )
1006 const unsigned int nodeLT =
1008 const unsigned int nodeRT =
1011 bool hittedLRT =
false;
1017 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
1023 false, 0,
false ) );
1026 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
1051 rayLTB.
Init( ( oriLT + oriLB ) * 0.5f,
1052 glm::normalize( ( dirLT + dirLB ) * 0.5f ) );
1055 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1057 if( hitPacket[ iLT ].m_hitresult && hitPacket[ iLB ].m_hitresult
1058 && ( hitPacket[ iLT ].m_HitInfo.pHitObject ==
1065 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1069 shadeHit( bgColorY, rayLTB, hitInfoLTB,
false, 0,
false ) );
1075 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iLB ].m_hitresult )
1077 const unsigned int nodeLT =
1079 const unsigned int nodeLB =
1082 bool hittedLTB =
false;
1088 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) )
1094 false, 0,
false ) );
1097 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1117 rayRTB.
Init( ( oriRT + oriRB ) * 0.5f,
1118 glm::normalize( ( dirRT + dirRB ) * 0.5f ) );
1121 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1123 if( hitPacket[ iRT ].m_hitresult && hitPacket[ iRB ].m_hitresult
1124 && ( hitPacket[ iRT ].m_HitInfo.pHitObject ==
1133 glm::normalize( ( hitPacket[iRT].m_HitInfo.m_HitNormal
1144 if( hitPacket[ iRT ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1146 const unsigned int nodeRT =
1148 const unsigned int nodeRB =
1151 bool hittedRTB =
false;
1157 if( ( nodeRB != 0 ) && ( nodeRB != nodeRT ) )
1168 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1172 false, 0,
false ) );
1187 rayLRB.
Init( ( oriLB + oriRB ) * 0.5f,
1188 glm::normalize( ( dirLB + dirRB ) * 0.5f ) );
1191 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1193 if( hitPacket[iLB].m_hitresult && hitPacket[iRB].m_hitresult
1194 && ( hitPacket[iLB].m_HitInfo.pHitObject ==
1203 glm::normalize( ( hitPacket[iLB].m_HitInfo.m_HitNormal
1214 if( hitPacket[ iLB ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1216 const unsigned int nodeLB =
1218 const unsigned int nodeRB =
1221 bool hittedLRB =
false;
1227 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) )
1234 false, 0,
false ) );
1238 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1242 false, 0,
false ) );
1252 if( hitPacket[ iLT ].m_hitresult || hittedC )
1256 rayLTC.
Init( ( oriLT + oriC ) * 0.5f,
1257 glm::normalize( ( dirLT + dirC ) * 0.5f ) );
1260 hitInfoLTC.
m_tHit = std::numeric_limits<float>::infinity();
1262 bool hitted =
false;
1266 else if( hitPacket[ iLT ].m_hitresult )
1279 if( hitPacket[ iRT ].m_hitresult || hittedC )
1283 rayRTC.
Init( ( oriRT + oriC ) * 0.5f,
1284 glm::normalize( ( dirRT + dirC ) * 0.5f ) );
1287 hitInfoRTC.
m_tHit = std::numeric_limits<float>::infinity();
1289 bool hitted =
false;
1293 else if( hitPacket[ iRT ].m_hitresult )
1295 rayRTC, hitInfoRTC );
1305 if( hitPacket[ iLB ].m_hitresult || hittedC )
1309 rayLBC.
Init( ( oriLB + oriC ) * 0.5f,
1310 glm::normalize( ( dirLB + dirC ) * 0.5f ) );
1313 hitInfoLBC.
m_tHit = std::numeric_limits<float>::infinity();
1315 bool hitted =
false;
1319 else if( hitPacket[ iLB ].m_hitresult )
1321 rayLBC, hitInfoLBC );
1331 if( hitPacket[ iRB ].m_hitresult || hittedC )
1335 rayRBC.
Init( ( oriRB + oriC ) * 0.5f,
1336 glm::normalize( ( dirRB + dirC ) * 0.5f ) );
1339 hitInfoRBC.
m_tHit = std::numeric_limits<float>::infinity();
1341 bool hitted =
false;
1345 else if( hitPacket[ iRB ].m_hitresult )
1347 rayRBC, hitInfoRBC );
1392 while( threadsFinished < parallelThreadCount )
1393 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
1400 HITINFO& aHitInfo,
bool aIsInsideObject,
1401 unsigned int aRecursiveLevel,
bool is_testShadow )
const
1404 wxASSERT( objMaterial !=
nullptr );
1409 if( aRecursiveLevel > 7 )
1416 const SFVEC4F diffuseColorObj =
1420 float shadow_att_factor_sum = 0.0f;
1422 unsigned int nr_lights_that_can_cast_shadows = 0;
1430 light->GetLightParameters( hitPoint, vectorToLight, colorOfLight, distToLight );
1432 const float NdotL = glm::dot( aHitInfo.
m_HitNormal, vectorToLight );
1436 if( NdotL >= FLT_EPSILON )
1438 float shadow_att_factor_light = 1.0f;
1440 if( is_testShadow && light->GetCastShadows() )
1442 nr_lights_that_can_cast_shadows++;
1445 if( aRecursiveLevel > 0 )
1448 rayToLight.
Init( hitPoint, vectorToLight );
1453 shadow_att_factor_light = 0.0f;
1458 const unsigned int shadow_number_of_samples =
1460 const float shadow_inc_factor = 1.0f / (float) ( shadow_number_of_samples );
1462 for(
unsigned int i = 0; i < shadow_number_of_samples; ++i )
1468 rayToLight.
Init( hitPoint, vectorToLight );
1473 const SFVEC3F disturbed_vector_to_light =
1474 glm::normalize( vectorToLight + unifVector *
1477 rayToLight.
Init( hitPoint, disturbed_vector_to_light );
1481 shadow_att_factor_light -= shadow_inc_factor;
1485 shadow_att_factor_sum += shadow_att_factor_light;
1488 outColor +=
SFVEC4F( objMaterial->
Shade( aRay, aHitInfo, NdotL, diffuseColorObj,
1489 vectorToLight, colorOfLight,
1490 shadow_att_factor_light ),
1496 if( nr_lights_that_can_cast_shadows > 0 )
1499 shadow_att_factor_sum / (
float) ( nr_lights_that_can_cast_shadows * 1.0f ), 0.0f );
1507 outColor = glm::min( outColor,
SFVEC4F( 1.0f ) );
1514 && ( aRecursiveLevel < objMaterial->GetReflectionRecursionCount() ) )
1516 const unsigned int reflection_number_of_samples =
1524 for(
unsigned int i = 0; i < reflection_number_of_samples; ++i )
1530 reflectedRay.
Init( hitPoint, reflectVector );
1535 const SFVEC3F random_reflectVector =
1536 glm::normalize( reflectVector
1539 .raytrace_spread_reflections );
1541 reflectedRay.
Init( hitPoint, random_reflectVector );
1545 reflectedHit.
m_tHit = std::numeric_limits<float>::infinity();
1547 if(
m_accelerator->Intersect( reflectedRay, reflectedHit ) )
1551 shadeHit( aBgColor, reflectedRay, reflectedHit,
false,
1552 aRecursiveLevel + 1, is_testShadow ) *
1555 (1.0f / ( 1.0f + 0.75f * reflectedHit.
m_tHit *
1556 reflectedHit.
m_tHit) ) );
1562 outColor += (sum_color /
SFVEC4F( (
float)reflection_number_of_samples) );
1568 if( ( objTransparency > 0.0f ) &&
m_boardAdapter.m_Cfg->m_Render.raytrace_refractions
1569 && ( aRecursiveLevel < objMaterial->GetRefractionRecursionCount() ) )
1571 const float airIndex = 1.000293f;
1572 const float glassIndex = 1.49f;
1573 const float air_over_glass = airIndex / glassIndex;
1574 const float glass_over_air = glassIndex / airIndex;
1576 const float refractionRatio = aIsInsideObject?glass_over_air:air_over_glass;
1588 const unsigned int refractions_number_of_samples =
1593 for(
unsigned int i = 0; i < refractions_number_of_samples; ++i )
1599 refractedRay.
Init( startPoint, refractedVector );
1604 const SFVEC3F randomizeRefractedVector =
1605 glm::normalize( refractedVector +
1609 refractedRay.
Init( startPoint, randomizeRefractedVector );
1613 refractedHit.
m_tHit = std::numeric_limits<float>::infinity();
1615 SFVEC4F refractedColor = aBgColor;
1617 if(
m_accelerator->Intersect( refractedRay, refractedHit ) )
1619 refractedColor =
shadeHit( aBgColor, refractedRay, refractedHit,
1620 !aIsInsideObject, aRecursiveLevel + 1,
false );
1623 (1.0f - objTransparency ) *
1627 const SFVEC4F transparency = 1.0f / ( absorbance + 1.0f );
1629 sum_color += refractedColor * transparency;
1633 sum_color += refractedColor;
1637 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * sum_color
1638 /
SFVEC4F( (
float) refractions_number_of_samples );
1642 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * aBgColor;