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;
407 if( ( nodex0y0 != nodex1y0 && nodex1y0 != 0 )
408 || ( nodex0y0 != nodex0y1 && nodex0y1 != 0 )
409 || ( nodex0y0 != nodex1y1 && nodex1y1 != 0 )
410 || ( nodex0y0 != node_AA_x0y0 ) )
417 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex0y0 );
420 && nodex0y0 != nodex1y0 )
422 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex1y0 );
426 && nodex0y0 != nodex0y1
427 && nodex1y0 != nodex0y1 )
429 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex0y1 );
433 && nodex0y0 != nodex1y1
434 && nodex0y1 != nodex1y1
435 && nodex1y0 != nodex1y1 )
437 hitted |=
m_accelerator->Intersect( rayAA, hitAA, nodex1y1 );
440 if( node_AA_x0y0 != 0
441 && nodex0y0 != node_AA_x0y0
442 && nodex0y1 != node_AA_x0y0
443 && nodex1y0 != node_AA_x0y0
444 && nodex1y1 != node_AA_x0y0 )
446 hitted |=
m_accelerator->Intersect( rayAA, hitAA, node_AA_x0y0 );
452 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0, is_testShadow );
461 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0, is_testShadow );
494 const float posYfactor = (float) ( blockPosI.y + y ) / (float)
m_windowSize.y;
501 if( !
m_accelerator->Intersect( blockPacket, hitPacket_X0Y0 ) )
508 const SFVEC4F& outColor = bgColor[y];
510 const unsigned int yBlockPos = blockPos.y + y;
524 const bool isFinalColor = !
m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing;
528 const SFVEC4F& outColor = bgColor[y];
530 const unsigned int yConst = blockPos.x + ( ( y + blockPos.y ) *
m_realBufferSize.x );
534 uint8_t* ptr = &ptrPBO[( yConst + x ) * 4];
562 if( !
m_accelerator->Intersect( blockPacket_AA_X1Y1, hitPacket_AA_X1Y1 ) )
567 const SFVEC4F& outColor = bgColor[y];
570 hitColor_AA_X1Y1[i] = outColor;
576 m_boardAdapter.m_Cfg->m_Render.raytrace_shadows, hitColor_AA_X1Y1 );
585 SFVEC4F color_average = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] ) *
SFVEC4F( 0.5f );
587 hitColor_AA_X1Y0[i] = color_average;
588 hitColor_AA_X0Y1[i] = color_average;
589 hitColor_AA_X0Y1_half[i] = color_average;
598 randDisp, blockRayPck_AA_X1Y0 );
602 randDisp, blockRayPck_AA_X0Y1 );
606 randDisp, blockRayPck_AA_X1Y1_half );
615 blockRayPck_AA_X1Y1_half, hitColor_AA_X0Y1_half );
620 hitColor_X0Y0[i] = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] + hitColor_AA_X1Y0[i] +
621 hitColor_AA_X0Y1[i] + hitColor_AA_X0Y1_half[i] ) *
627 uint8_t* ptr = &ptrPBO[( blockPos.x + ( blockPos.y *
m_realBufferSize.x ) ) * 4];
642 const SFVEC4F& hColor = hitColor_X0Y0[i];
644 if( hitPacket_X0Y0[i].m_hitresult ==
true )
804 std::atomic<size_t> nextBlock( 0 );
805 std::atomic<size_t> threadsFinished( 0 );
807 size_t parallelThreadCount = std::min<size_t>(
808 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
811 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
813 std::thread t = std::thread( [&]()
816 iBlock = nextBlock.fetch_add( 1 ) )
829 packet.m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
830 packet.m_HitInfo.m_acc_node_info = 0;
831 packet.m_hitresult =
false;
845 const float posYfactor =
846 (float) ( windowsPos.y + y * 4.0f ) / (float)
m_windowSize.y;
848 bgColor[y] = bgTopColor *
SFVEC4F( posYfactor )
858 if( hitPacket[i].m_hitresult ==
true )
867 hitColorShading[i] = bhColorY;
874 const SFVEC4F bgColorY = bgColor[y];
895 const unsigned int iLT = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 0 ) );
896 const unsigned int iRT = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 0 ) );
897 const unsigned int iLB = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 ) );
898 const unsigned int iRB = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 ) );
901 const COLOR_RGBA& cLT = hitColorShading[ iLT ];
902 const COLOR_RGBA& cRT = hitColorShading[ iRT ];
903 const COLOR_RGBA& cLB = hitColorShading[ iLB ];
904 const COLOR_RGBA& cRB = hitColorShading[ iRB ];
919 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
921 bool hittedC =
false;
923 if( ( hitPacket[iLT].m_hitresult ==
true )
924 || ( hitPacket[iRT].m_hitresult ==
true )
925 || ( hitPacket[iLB].m_hitresult ==
true )
926 || ( hitPacket[iRB].m_hitresult ==
true ) )
928 oriC = ( oriLT + oriRB ) * 0.5f;
929 dirC = glm::normalize( ( dirLT + dirRB ) * 0.5f );
933 centerRay.
Init( oriC, dirC );
941 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
944 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
945 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
948 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) && ( nodeLB != nodeRT ) )
949 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
952 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) && ( nodeRB != nodeLT )
953 && ( nodeRB != nodeRT ) )
954 hittedC |=
m_accelerator->Intersect( centerRay, centerHitInfo,
964 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
965 hittedC =
m_accelerator->Intersect( centerRay, centerHitInfo );
983 rayLRT.
Init( ( oriLT + oriRT ) * 0.5f,
984 glm::normalize( ( dirLT + dirRT ) * 0.5f ) );
987 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
989 if( hitPacket[iLT].m_hitresult && hitPacket[iRT].m_hitresult
990 && ( hitPacket[iLT].m_HitInfo.pHitObject
997 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1008 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iRT ].m_hitresult )
1010 const unsigned int nodeLT =
1012 const unsigned int nodeRT =
1015 bool hittedLRT =
false;
1021 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
1027 false, 0,
false ) );
1030 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
1055 rayLTB.
Init( ( oriLT + oriLB ) * 0.5f,
1056 glm::normalize( ( dirLT + dirLB ) * 0.5f ) );
1059 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1061 if( hitPacket[ iLT ].m_hitresult && hitPacket[ iLB ].m_hitresult
1062 && ( hitPacket[ iLT ].m_HitInfo.pHitObject ==
1069 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1073 shadeHit( bgColorY, rayLTB, hitInfoLTB,
false, 0,
false ) );
1079 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iLB ].m_hitresult )
1081 const unsigned int nodeLT =
1083 const unsigned int nodeLB =
1086 bool hittedLTB =
false;
1092 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) )
1098 false, 0,
false ) );
1101 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1121 rayRTB.
Init( ( oriRT + oriRB ) * 0.5f,
1122 glm::normalize( ( dirRT + dirRB ) * 0.5f ) );
1125 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1127 if( hitPacket[ iRT ].m_hitresult && hitPacket[ iRB ].m_hitresult
1128 && ( hitPacket[ iRT ].m_HitInfo.pHitObject ==
1137 glm::normalize( ( hitPacket[iRT].m_HitInfo.m_HitNormal
1148 if( hitPacket[ iRT ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1150 const unsigned int nodeRT =
1152 const unsigned int nodeRB =
1155 bool hittedRTB =
false;
1161 if( ( nodeRB != 0 ) && ( nodeRB != nodeRT ) )
1172 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1176 false, 0,
false ) );
1191 rayLRB.
Init( ( oriLB + oriRB ) * 0.5f,
1192 glm::normalize( ( dirLB + dirRB ) * 0.5f ) );
1195 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1197 if( hitPacket[iLB].m_hitresult && hitPacket[iRB].m_hitresult
1198 && ( hitPacket[iLB].m_HitInfo.pHitObject ==
1207 glm::normalize( ( hitPacket[iLB].m_HitInfo.m_HitNormal
1218 if( hitPacket[ iLB ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1220 const unsigned int nodeLB =
1222 const unsigned int nodeRB =
1225 bool hittedLRB =
false;
1231 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) )
1238 false, 0,
false ) );
1242 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1246 false, 0,
false ) );
1256 if( hitPacket[ iLT ].m_hitresult || hittedC )
1260 rayLTC.
Init( ( oriLT + oriC ) * 0.5f,
1261 glm::normalize( ( dirLT + dirC ) * 0.5f ) );
1264 hitInfoLTC.
m_tHit = std::numeric_limits<float>::infinity();
1266 bool hitted =
false;
1270 else if( hitPacket[ iLT ].m_hitresult )
1283 if( hitPacket[ iRT ].m_hitresult || hittedC )
1287 rayRTC.
Init( ( oriRT + oriC ) * 0.5f,
1288 glm::normalize( ( dirRT + dirC ) * 0.5f ) );
1291 hitInfoRTC.
m_tHit = std::numeric_limits<float>::infinity();
1293 bool hitted =
false;
1297 else if( hitPacket[ iRT ].m_hitresult )
1299 rayRTC, hitInfoRTC );
1309 if( hitPacket[ iLB ].m_hitresult || hittedC )
1313 rayLBC.
Init( ( oriLB + oriC ) * 0.5f,
1314 glm::normalize( ( dirLB + dirC ) * 0.5f ) );
1317 hitInfoLBC.
m_tHit = std::numeric_limits<float>::infinity();
1319 bool hitted =
false;
1323 else if( hitPacket[ iLB ].m_hitresult )
1325 rayLBC, hitInfoLBC );
1335 if( hitPacket[ iRB ].m_hitresult || hittedC )
1339 rayRBC.
Init( ( oriRB + oriC ) * 0.5f,
1340 glm::normalize( ( dirRB + dirC ) * 0.5f ) );
1343 hitInfoRBC.
m_tHit = std::numeric_limits<float>::infinity();
1345 bool hitted =
false;
1349 else if( hitPacket[ iRB ].m_hitresult )
1351 rayRBC, hitInfoRBC );
1396 while( threadsFinished < parallelThreadCount )
1397 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
1404 HITINFO& aHitInfo,
bool aIsInsideObject,
1405 unsigned int aRecursiveLevel,
bool is_testShadow )
const
1408 wxASSERT( objMaterial !=
nullptr );
1413 if( aRecursiveLevel > 7 )
1420 const SFVEC4F diffuseColorObj =
1424 float shadow_att_factor_sum = 0.0f;
1426 unsigned int nr_lights_that_can_cast_shadows = 0;
1434 light->GetLightParameters( hitPoint, vectorToLight, colorOfLight, distToLight );
1436 const float NdotL = glm::dot( aHitInfo.
m_HitNormal, vectorToLight );
1440 if( NdotL >= FLT_EPSILON )
1442 float shadow_att_factor_light = 1.0f;
1444 if( is_testShadow && light->GetCastShadows() )
1446 nr_lights_that_can_cast_shadows++;
1449 if( aRecursiveLevel > 0 )
1452 rayToLight.
Init( hitPoint, vectorToLight );
1457 shadow_att_factor_light = 0.0f;
1462 const unsigned int shadow_number_of_samples =
1464 const float shadow_inc_factor = 1.0f / (float) ( shadow_number_of_samples );
1466 for(
unsigned int i = 0; i < shadow_number_of_samples; ++i )
1472 rayToLight.
Init( hitPoint, vectorToLight );
1477 const SFVEC3F disturbed_vector_to_light =
1478 glm::normalize( vectorToLight + unifVector *
1481 rayToLight.
Init( hitPoint, disturbed_vector_to_light );
1485 shadow_att_factor_light -= shadow_inc_factor;
1489 shadow_att_factor_sum += shadow_att_factor_light;
1492 outColor +=
SFVEC4F( objMaterial->
Shade( aRay, aHitInfo, NdotL, diffuseColorObj,
1493 vectorToLight, colorOfLight,
1494 shadow_att_factor_light ),
1500 if( nr_lights_that_can_cast_shadows > 0 )
1503 shadow_att_factor_sum / (
float) ( nr_lights_that_can_cast_shadows * 1.0f ), 0.0f );
1511 outColor = glm::min( outColor,
SFVEC4F( 1.0f ) );
1518 && ( aRecursiveLevel < objMaterial->GetReflectionRecursionCount() ) )
1520 const unsigned int reflection_number_of_samples =
1528 for(
unsigned int i = 0; i < reflection_number_of_samples; ++i )
1534 reflectedRay.
Init( hitPoint, reflectVector );
1539 const SFVEC3F random_reflectVector =
1540 glm::normalize( reflectVector
1543 .raytrace_spread_reflections );
1545 reflectedRay.
Init( hitPoint, random_reflectVector );
1549 reflectedHit.
m_tHit = std::numeric_limits<float>::infinity();
1551 if(
m_accelerator->Intersect( reflectedRay, reflectedHit ) )
1555 shadeHit( aBgColor, reflectedRay, reflectedHit,
false,
1556 aRecursiveLevel + 1, is_testShadow ) *
1559 (1.0f / ( 1.0f + 0.75f * reflectedHit.
m_tHit *
1560 reflectedHit.
m_tHit) ) );
1566 outColor += (sum_color /
SFVEC4F( (
float)reflection_number_of_samples) );
1572 if( ( objTransparency > 0.0f ) &&
m_boardAdapter.m_Cfg->m_Render.raytrace_refractions
1573 && ( aRecursiveLevel < objMaterial->GetRefractionRecursionCount() ) )
1575 const float airIndex = 1.000293f;
1576 const float glassIndex = 1.49f;
1577 const float air_over_glass = airIndex / glassIndex;
1578 const float glass_over_air = glassIndex / airIndex;
1580 const float refractionRatio = aIsInsideObject?glass_over_air:air_over_glass;
1592 const unsigned int refractions_number_of_samples =
1597 for(
unsigned int i = 0; i < refractions_number_of_samples; ++i )
1603 refractedRay.
Init( startPoint, refractedVector );
1608 const SFVEC3F randomizeRefractedVector =
1609 glm::normalize( refractedVector +
1613 refractedRay.
Init( startPoint, randomizeRefractedVector );
1617 refractedHit.
m_tHit = std::numeric_limits<float>::infinity();
1619 SFVEC4F refractedColor = aBgColor;
1621 if(
m_accelerator->Intersect( refractedRay, refractedHit ) )
1623 refractedColor =
shadeHit( aBgColor, refractedRay, refractedHit,
1624 !aIsInsideObject, aRecursiveLevel + 1,
false );
1627 (1.0f - objTransparency ) *
1631 const SFVEC4F transparency = 1.0f / ( absorbance + 1.0f );
1633 sum_color += refractedColor * transparency;
1637 sum_color += refractedColor;
1641 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * sum_color
1642 /
SFVEC4F( (
float) refractions_number_of_samples );
1646 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * aBgColor;