32#include "../color_rgba.h"
50 const float gammaCorrection = 1.0f /
SRGB_GAMA;
53 return glm::mix( glm::pow( clampedColor,
SFVEC3F(gammaCorrection) ) * 1.055f - 0.055f,
54 clampedColor * 12.92f,
55 glm::lessThan( clampedColor,
SFVEC3F(0.0031308f) ) );
71 return glm::mix( glm::pow( ( aSRGBcolor +
SFVEC3F( 0.055f ) )
72 *
SFVEC3F( 0.94786729857819905213270142180095f ),
74 aSRGBcolor *
SFVEC3F( 0.07739938080495356037151702786378f ),
75 glm::lessThanEqual( aSRGBcolor,
SFVEC3F( 0.04045f ) ) );
89 m_postShaderSsao( aCamera )
91 wxLogTrace(
m_logTrace, wxT(
"RENDER_3D_RAYTRACE_BASE::RENDER_3D_RAYTRACE_BASE" ) );
119 wxLogTrace(
m_logTrace, wxT(
"RENDER_3D_RAYTRACE_BASE::~RENDER_3D_RAYTRACE_BASE" ) );
181 return SFVEC4F( aInput.r * aInput.a, aInput.g * aInput.a, aInput.b * aInput.a, aInput.a );
200 uint8_t* tmp_ptrPBO = ptrPBO + 3;
202 for(
unsigned int i = 0; i < nPixels; ++i )
230 wxASSERT_MSG(
false, wxT(
"Invalid state on m_renderState" ) );
240 aStatusReporter->
Report( wxString::Format(
_(
"Rendering time %.3f s" ), elapsed_time ) );
249 auto startTime = std::chrono::steady_clock::now();
250 std::atomic<size_t> numBlocksRendered( 0 );
251 std::atomic<size_t> currentBlock( 0 );
256 auto processBlocks = [&]()
258 for(
size_t iBlock = currentBlock.fetch_add( 1 );
260 iBlock = currentBlock.fetch_add( 1 ) )
269 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(
270 std::chrono::steady_clock::now() - startTime );
272 if( diff.count() > timeLimit )
277 for(
size_t i = 0; i <
tp.get_thread_count() + 1; ++i )
278 tp.push_task( processBlocks );
284 if( aStatusReporter )
285 aStatusReporter->
Report( wxString::Format(
_(
"Rendering: %.0f %%" ),
302 bool applyColorSpaceConversion )
311 if( applyColorSpaceConversion )
315 ptrPBO[0] = (
unsigned int) glm::clamp( (
int) (
color.r * 255 ), 0, 255 );
316 ptrPBO[1] = (
unsigned int) glm::clamp( (
int) (
color.g * 255 ), 0, 255 );
317 ptrPBO[2] = (
unsigned int) glm::clamp( (
int) (
color.b * 255 ), 0, 255 );
318 ptrPBO[3] = (
unsigned int) glm::clamp( (
int) (
color.a * 255 ), 0, 255 );
327 aHitPacket[i].
m_HitInfo.
m_tHit = std::numeric_limits<float>::infinity();
344 if( aHitPacket[i].m_hitresult ==
true )
346 aOutHitColor[i] =
shadeHit( bgColorY[y], aRayPkt[i], aHitPacket[i].m_HitInfo,
347 false, 0, is_testShadow );
351 aOutHitColor[i] = bgColorY[y];
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;
387 nodex1y0 = aHitPck_X0Y0[i + 1].m_HitInfo.m_acc_node_info;
389 unsigned int nodex0y1 = 0;
392 nodex0y1 = aHitPck_X0Y0[idx0y1].m_HitInfo.m_acc_node_info;
394 unsigned int nodex1y1 = 0;
401 if( ( ( nodex0y0 == nodex1y0 ) || ( nodex1y0 == 0 ) )
402 && ( ( nodex0y0 == nodex0y1 ) || ( nodex0y1 == 0 ) )
403 && ( ( nodex0y0 == nodex1y1 ) || ( nodex1y1 == 0 ) )
404 && ( nodex0y0 == node_AA_x0y0 ) )
442 if( ( nodex1y0 != 0 ) && ( nodex0y0 != nodex1y0 ) )
445 if( ( nodex0y1 != 0 ) && ( nodex0y0 != nodex0y1 ) && ( nodex1y0 != nodex0y1 ) )
448 if( ( nodex1y1 != 0 ) && ( nodex0y0 != nodex1y1 ) && ( nodex0y1 != nodex1y1 ) &&
449 ( nodex1y0 != nodex1y1 ) )
452 if( (node_AA_x0y0 != 0 ) && ( nodex0y0 != node_AA_x0y0 ) &&
453 ( nodex0y1 != node_AA_x0y0 ) && ( nodex1y0 != node_AA_x0y0 ) &&
454 ( nodex1y1 != node_AA_x0y0 ) )
460 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0,
470 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0,
479#define DISP_FACTOR 0.075f
501 const float posYfactor = (float) ( blockPosI.y + y ) / (float)
m_windowSize.y;
515 const SFVEC4F& outColor = bgColor[y];
517 const unsigned int yBlockPos = blockPos.y + y;
535 const SFVEC4F& outColor = bgColor[y];
537 const unsigned int yConst = blockPos.x + ( ( y + blockPos.y ) *
m_realBufferSize.x );
541 uint8_t* ptr = &ptrPBO[( yConst + x ) * 4];
574 const SFVEC4F& outColor = bgColor[y];
577 hitColor_AA_X1Y1[i] = outColor;
592 SFVEC4F color_average = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] ) *
SFVEC4F( 0.5f );
594 hitColor_AA_X1Y0[i] = color_average;
595 hitColor_AA_X0Y1[i] = color_average;
596 hitColor_AA_X0Y1_half[i] = color_average;
622 blockRayPck_AA_X1Y1_half, hitColor_AA_X0Y1_half );
627 hitColor_X0Y0[i] = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] + hitColor_AA_X1Y0[i] +
628 hitColor_AA_X0Y1[i] + hitColor_AA_X0Y1_half[i] ) *
634 uint8_t* ptr = &ptrPBO[( blockPos.x + ( blockPos.y *
m_realBufferSize.x ) ) * 4];
649 const SFVEC4F& hColor = hitColor_X0Y0[i];
651 if( hitPacket_X0Y0[i].m_hitresult ==
true )
697 if( aStatusReporter )
698 aStatusReporter->
Report(
_(
"Rendering: Post processing shader" ) );
702 std::atomic<size_t> nextBlock( 0 );
703 std::atomic<size_t> threadsFinished( 0 );
705 size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
707 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
709 std::thread t = std::thread( [&]()
712 y = nextBlock.fetch_add( 1 ) )
714 SFVEC3F* ptr = &m_shaderBuffer[ y * m_realBufferSize.x ];
716 for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
718 *ptr = m_postShaderSsao.Shade( SFVEC2I( x, y ) );
729 while( threadsFinished < parallelThreadCount )
730 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
751 std::atomic<size_t> nextBlock( 0 );
752 std::atomic<size_t> threadsFinished( 0 );
754 size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
756 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
758 std::thread t = std::thread( [&]()
761 y = nextBlock.fetch_add( 1 ) )
763 uint8_t* ptr = &ptrPBO[ y * m_realBufferSize.x * 4 ];
765 for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
767 const SFVEC3F bluredShadeColor = m_postShaderSsao.Blur( SFVEC2I( x, y ) );
770 const SFVEC4F originColor = convertLinearToSRGBA(
771 m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) ) );
773 const SFVEC4F originColor =
774 m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) );
776 const SFVEC4F shadedColor = m_postShaderSsao.ApplyShadeColor(
777 SFVEC2I( x, y ), originColor, bluredShadeColor );
779 renderFinalColor( ptr, shadedColor, false );
791 while( threadsFinished < parallelThreadCount )
792 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
811 std::atomic<size_t> nextBlock( 0 );
812 std::atomic<size_t> threadsFinished( 0 );
814 size_t parallelThreadCount = std::min<size_t>(
815 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
818 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
820 std::thread t = std::thread( [&]()
823 iBlock = nextBlock.fetch_add( 1 ) )
825 const SFVEC2UI& windowPosUI = m_blockPositionsFast[ iBlock ];
826 const SFVEC2I windowsPos = SFVEC2I( windowPosUI.x + m_xoffset,
827 windowPosUI.y + m_yoffset );
829 RAYPACKET blockPacket( m_camera, windowsPos, 4 );
831 HITINFO_PACKET hitPacket[RAYPACKET_RAYS_PER_PACKET];
834 for( HITINFO_PACKET& packet : hitPacket )
836 packet.m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
837 packet.m_HitInfo.m_acc_node_info = 0;
838 packet.m_hitresult = false;
852 const float posYfactor =
853 (float) ( windowsPos.y + y * 4.0f ) / (float) m_windowSize.y;
855 bgColor[y] = bgTopColor * SFVEC4F( posYfactor )
856 + bgBotColor * ( SFVEC4F( 1.0f ) - SFVEC4F( posYfactor ) );
863 const SFVEC4F bhColorY = bgColor[i / RAYPACKET_DIM];
865 if( hitPacket[i].m_hitresult == true )
867 const SFVEC4F hitColor = shadeHit( bhColorY, blockPacket.m_ray[i],
868 hitPacket[i].m_HitInfo, false,
871 hitColorShading[i] = COLOR_RGBA( hitColor );
874 hitColorShading[i] = bhColorY;
881 const SFVEC4F bgColorY = bgColor[y];
902 const unsigned int iLT = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 0 ) );
903 const unsigned int iRT = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 0 ) );
904 const unsigned int iLB = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 ) );
905 const unsigned int iRB = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 ) );
908 const COLOR_RGBA& cLT = hitColorShading[ iLT ];
909 const COLOR_RGBA& cRT = hitColorShading[ iRT ];
910 const COLOR_RGBA& cLB = hitColorShading[ iLB ];
911 const COLOR_RGBA& cRB = hitColorShading[ iRB ];
916 const SFVEC3F& oriLT = blockPacket.m_ray[ iLT ].m_Origin;
917 const SFVEC3F& oriRB = blockPacket.m_ray[ iRB ].m_Origin;
919 const SFVEC3F& dirLT = blockPacket.m_ray[ iLT ].m_Dir;
920 const SFVEC3F& dirRB = blockPacket.m_ray[ iRB ].m_Dir;
926 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
928 bool hittedC =
false;
930 if( ( hitPacket[iLT].m_hitresult ==
true )
931 || ( hitPacket[iRT].m_hitresult ==
true )
932 || ( hitPacket[iLB].m_hitresult ==
true )
933 || ( hitPacket[iRB].m_hitresult ==
true ) )
935 oriC = ( oriLT + oriRB ) * 0.5f;
936 dirC = glm::normalize( ( dirLT + dirRB ) * 0.5f );
940 centerRay.
Init( oriC, dirC );
942 const unsigned int nodeLT = hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
943 const unsigned int nodeRT = hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
944 const unsigned int nodeLB = hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
945 const unsigned int nodeRB = hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
951 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
955 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) && ( nodeLB != nodeRT ) )
959 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) && ( nodeRB != nodeLT )
960 && ( nodeRB != nodeRT ) )
971 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
983 const SFVEC3F& oriRT = blockPacket.m_ray[ iRT ].m_Origin;
984 const SFVEC3F& dirRT = blockPacket.m_ray[ iRT ].m_Dir;
990 rayLRT.
Init( ( oriLT + oriRT ) * 0.5f,
991 glm::normalize( ( dirLT + dirRT ) * 0.5f ) );
994 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
996 if( hitPacket[iLT].m_hitresult && hitPacket[iRT].m_hitresult
997 && ( hitPacket[iLT].m_HitInfo.pHitObject
998 == hitPacket[iRT].m_HitInfo.pHitObject ) )
1000 hitInfoLRT.
pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
1001 hitInfoLRT.
m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
1002 hitPacket[ iRT ].m_HitInfo.m_tHit ) * 0.5f;
1004 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1005 + hitPacket[iRT].m_HitInfo.m_HitNormal )
1015 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iRT ].m_hitresult )
1017 const unsigned int nodeLT =
1018 hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
1019 const unsigned int nodeRT =
1020 hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1022 bool hittedLRT =
false;
1028 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
1034 false, 0,
false ) );
1037 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
1057 const SFVEC3F &oriLB = blockPacket.m_ray[ iLB ].m_Origin;
1058 const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
1062 rayLTB.
Init( ( oriLT + oriLB ) * 0.5f,
1063 glm::normalize( ( dirLT + dirLB ) * 0.5f ) );
1066 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1068 if( hitPacket[ iLT ].m_hitresult && hitPacket[ iLB ].m_hitresult
1069 && ( hitPacket[ iLT ].m_HitInfo.pHitObject ==
1070 hitPacket[ iLB ].m_HitInfo.pHitObject ) )
1072 hitInfoLTB.
pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
1073 hitInfoLTB.
m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
1074 hitPacket[ iLB ].m_HitInfo.m_tHit ) * 0.5f;
1076 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1077 + hitPacket[iLB].m_HitInfo.m_HitNormal )
1080 shadeHit( bgColorY, rayLTB, hitInfoLTB,
false, 0,
false ) );
1086 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iLB ].m_hitresult )
1088 const unsigned int nodeLT =
1089 hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
1090 const unsigned int nodeLB =
1091 hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1093 bool hittedLTB =
false;
1099 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) )
1105 false, 0,
false ) );
1108 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1128 rayRTB.
Init( ( oriRT + oriRB ) * 0.5f,
1129 glm::normalize( ( dirRT + dirRB ) * 0.5f ) );
1132 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1134 if( hitPacket[ iRT ].m_hitresult && hitPacket[ iRB ].m_hitresult
1135 && ( hitPacket[ iRT ].m_HitInfo.pHitObject ==
1136 hitPacket[ iRB ].m_HitInfo.pHitObject ) )
1138 hitInfoRTB.
pHitObject = hitPacket[ iRT ].m_HitInfo.pHitObject;
1140 hitInfoRTB.
m_tHit = ( hitPacket[ iRT ].m_HitInfo.m_tHit +
1141 hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
1144 glm::normalize( ( hitPacket[iRT].m_HitInfo.m_HitNormal
1145 + hitPacket[iRB].m_HitInfo.m_HitNormal )
1155 if( hitPacket[ iRT ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1157 const unsigned int nodeRT =
1158 hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1159 const unsigned int nodeRB =
1160 hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1162 bool hittedRTB =
false;
1168 if( ( nodeRB != 0 ) && ( nodeRB != nodeRT ) )
1179 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1183 false, 0,
false ) );
1193 const SFVEC3F& oriLB = blockPacket.m_ray[ iLB ].m_Origin;
1194 const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
1198 rayLRB.
Init( ( oriLB + oriRB ) * 0.5f,
1199 glm::normalize( ( dirLB + dirRB ) * 0.5f ) );
1202 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1204 if( hitPacket[iLB].m_hitresult && hitPacket[iRB].m_hitresult
1205 && ( hitPacket[iLB].m_HitInfo.pHitObject ==
1206 hitPacket[iRB].m_HitInfo.pHitObject ) )
1208 hitInfoLRB.
pHitObject = hitPacket[ iLB ].m_HitInfo.pHitObject;
1210 hitInfoLRB.
m_tHit = ( hitPacket[ iLB ].m_HitInfo.m_tHit +
1211 hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
1214 glm::normalize( ( hitPacket[iLB].m_HitInfo.m_HitNormal
1215 + hitPacket[iRB].m_HitInfo.m_HitNormal )
1225 if( hitPacket[ iLB ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1227 const unsigned int nodeLB =
1228 hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1229 const unsigned int nodeRB =
1230 hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1232 bool hittedLRB =
false;
1238 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) )
1245 false, 0,
false ) );
1249 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1253 false, 0,
false ) );
1263 if( hitPacket[ iLT ].m_hitresult || hittedC )
1267 rayLTC.
Init( ( oriLT + oriC ) * 0.5f,
1268 glm::normalize( ( dirLT + dirC ) * 0.5f ) );
1271 hitInfoLTC.
m_tHit = std::numeric_limits<float>::infinity();
1273 bool hitted =
false;
1277 else if( hitPacket[ iLT ].m_hitresult )
1278 hitted = hitPacket[ iLT ].m_HitInfo.pHitObject->Intersect(
1290 if( hitPacket[ iRT ].m_hitresult || hittedC )
1294 rayRTC.
Init( ( oriRT + oriC ) * 0.5f,
1295 glm::normalize( ( dirRT + dirC ) * 0.5f ) );
1298 hitInfoRTC.
m_tHit = std::numeric_limits<float>::infinity();
1300 bool hitted =
false;
1304 else if( hitPacket[ iRT ].m_hitresult )
1305 hitted = hitPacket[iRT].m_HitInfo.pHitObject->Intersect(
1306 rayRTC, hitInfoRTC );
1316 if( hitPacket[ iLB ].m_hitresult || hittedC )
1320 rayLBC.
Init( ( oriLB + oriC ) * 0.5f,
1321 glm::normalize( ( dirLB + dirC ) * 0.5f ) );
1324 hitInfoLBC.
m_tHit = std::numeric_limits<float>::infinity();
1326 bool hitted =
false;
1330 else if( hitPacket[ iLB ].m_hitresult )
1331 hitted = hitPacket[iLB].m_HitInfo.pHitObject->Intersect(
1332 rayLBC, hitInfoLBC );
1342 if( hitPacket[ iRB ].m_hitresult || hittedC )
1346 rayRBC.
Init( ( oriRB + oriC ) * 0.5f,
1347 glm::normalize( ( dirRB + dirC ) * 0.5f ) );
1350 hitInfoRBC.
m_tHit = std::numeric_limits<float>::infinity();
1352 bool hitted =
false;
1356 else if( hitPacket[ iRB ].m_hitresult )
1357 hitted = hitPacket[iRB].m_HitInfo.pHitObject->Intersect(
1358 rayRBC, hitInfoRBC );
1403 while( threadsFinished < parallelThreadCount )
1404 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
1408#define USE_EXPERIMENTAL_SOFT_SHADOWS 1
1411 HITINFO& aHitInfo,
bool aIsInsideObject,
1412 unsigned int aRecursiveLevel,
bool is_testShadow )
const
1415 wxASSERT( objMaterial !=
nullptr );
1420 if( aRecursiveLevel > 7 )
1427 const SFVEC4F diffuseColorObj =
1430#if USE_EXPERIMENTAL_SOFT_SHADOWS
1434 float shadow_att_factor_sum = 0.0f;
1436 unsigned int nr_lights_that_can_cast_shadows = 0;
1444 light->GetLightParameters( hitPoint, vectorToLight, colorOfLight, distToLight );
1446 const float NdotL = glm::dot( aHitInfo.
m_HitNormal, vectorToLight );
1450 if( NdotL >= FLT_EPSILON )
1452 float shadow_att_factor_light = 1.0f;
1454 if( is_testShadow && light->GetCastShadows() )
1456 nr_lights_that_can_cast_shadows++;
1457#if USE_EXPERIMENTAL_SOFT_SHADOWS
1459 if( aRecursiveLevel > 0 )
1463 rayToLight.
Init( hitPoint, vectorToLight );
1468 shadow_att_factor_light = 0.0f;
1470#if USE_EXPERIMENTAL_SOFT_SHADOWS
1474 const unsigned int shadow_number_of_samples =
1476 const float shadow_inc_factor = 1.0f / (float) ( shadow_number_of_samples );
1478 for(
unsigned int i = 0; i < shadow_number_of_samples; ++i )
1484 rayToLight.
Init( hitPoint, vectorToLight );
1489 const SFVEC3F disturbed_vector_to_light =
1490 glm::normalize( vectorToLight + unifVector *
1493 rayToLight.
Init( hitPoint, disturbed_vector_to_light );
1501 shadow_att_factor_light -= shadow_inc_factor;
1506 shadow_att_factor_sum += shadow_att_factor_light;
1509 outColor +=
SFVEC4F( objMaterial->
Shade( aRay, aHitInfo, NdotL, diffuseColorObj,
1510 vectorToLight, colorOfLight,
1511 shadow_att_factor_light ),
1517 if( nr_lights_that_can_cast_shadows > 0 )
1520 shadow_att_factor_sum / (
float) ( nr_lights_that_can_cast_shadows * 1.0f ), 0.0f );
1528 outColor = glm::min( outColor,
SFVEC4F( 1.0f ) );
1535 && ( aRecursiveLevel < objMaterial->GetReflectionRecursionCount() ) )
1537 const unsigned int reflection_number_of_samples =
1545 for(
unsigned int i = 0; i < reflection_number_of_samples; ++i )
1551 reflectedRay.
Init( hitPoint, reflectVector );
1556 const SFVEC3F random_reflectVector =
1557 glm::normalize( reflectVector
1562 reflectedRay.
Init( hitPoint, random_reflectVector );
1566 reflectedHit.
m_tHit = std::numeric_limits<float>::infinity();
1572 shadeHit( aBgColor, reflectedRay, reflectedHit,
false,
1573 aRecursiveLevel + 1, is_testShadow ) *
1576 (1.0f / ( 1.0f + 0.75f * reflectedHit.
m_tHit *
1577 reflectedHit.
m_tHit) ) );
1583 outColor += (sum_color /
SFVEC4F( (
float)reflection_number_of_samples) );
1592 const float airIndex = 1.000293f;
1593 const float glassIndex = 1.49f;
1594 const float air_over_glass = airIndex / glassIndex;
1595 const float glass_over_air = glassIndex / airIndex;
1597 const float refractionRatio = aIsInsideObject?glass_over_air:air_over_glass;
1609 const unsigned int refractions_number_of_samples =
1614 for(
unsigned int i = 0; i < refractions_number_of_samples; ++i )
1620 refractedRay.
Init( startPoint, refractedVector );
1625 const SFVEC3F randomizeRefractedVector =
1626 glm::normalize( refractedVector +
1630 refractedRay.
Init( startPoint, randomizeRefractedVector );
1634 refractedHit.
m_tHit = std::numeric_limits<float>::infinity();
1636 SFVEC4F refractedColor = aBgColor;
1640 refractedColor =
shadeHit( aBgColor, refractedRay, refractedHit,
1641 !aIsInsideObject, aRecursiveLevel + 1,
false );
1644 (1.0f - objTransparency ) *
1648 const SFVEC4F transparency = 1.0f / ( absorbance + 1.0f );
1650 sum_color += refractedColor * transparency;
1654 sum_color += refractedColor;
1658 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * sum_color
1659 /
SFVEC4F( (
float) refractions_number_of_samples );
1663 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * aBgColor;
1674 const float dx = (float) a.x - (
float) b.x;
1675 const float dy = (float) a.y - (
float) b.y;
1676 return hypotf( dx, dy );
1735 constexpr auto hilbert_get_pos = [](
size_t aT,
size_t& aX,
size_t& aY )
1737 static const size_t htab[] = { 0b0010, 0b0100, 0b1100, 0b1001, 0b1111, 0b0101,
1738 0b0001, 0b1000, 0b0000, 0b1010, 0b1110, 0b0111,
1739 0b1101, 0b1011, 0b0011, 0b0110 };
1740 static const size_t size =
sizeof( size_t ) * 8;
1745 for(
size_t i = 0; i < ( size / 2 ); ++i )
1747 size_t abi = aT >> ( size - 2 );
1750 size_t st = htab[( c01 << 2 ) | abi];
1753 yv = ( yv << 1 ) | ( ( st >> 2 ) & 1 );
1754 xv = ( xv << 1 ) | ( st >> 3 );
1761 size_t total_blocks = blocks_x * blocks_y;
1768 hilbert_get_pos( pos++, x, y );
1770 if( x < blocks_x && y < blocks_y )
1785 hitInfo.
m_tHit = std::numeric_limits<float>::infinity();
Defines math related functions.
Defines math related functions.
bool Refract(const SFVEC3F &aInVector, const SFVEC3F &aNormal, float aRin_over_Rout, SFVEC3F &aOutVector)
Based on: https://github.com/mmp/pbrt-v3/blob/master/src/core/reflection.h See also: http://www....
SFVEC3F UniformRandomHemisphereDirection()
virtual bool Intersect(const RAY &aRay, HITINFO &aHitInfo) const =0
virtual bool IntersectP(const RAY &aRay, float aMaxDistance) const =0
Helper class to handle information needed to display 3D board.
SFVEC4F m_BgColorTop
background top color
float GetNonCopperLayerThickness() const noexcept
Get the non copper layers thickness (in 3D units).
EDA_3D_VIEWER_SETTINGS * m_Cfg
SFVEC4F m_BgColorBot
background bottom color
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
A class used to derive camera objects from.
const SFVEC3F & GetDir() const
void SetDirection(const SFVEC3F &aDir)
Set directional light orientation.
A base light class to derive to implement other light classes.
Base material class that can be used to derive other material implementations.
float GetAbsorvance() const
virtual SFVEC3F Shade(const RAY &aRay, const HITINFO &aHitInfo, float NdotL, const SFVEC3F &aDiffuseObjColor, const SFVEC3F &aDirToLight, const SFVEC3F &aLightColor, float aShadowAttenuationFactor) const =0
Shade an intersection point.
const SFVEC3F & GetAmbientColor() const
unsigned int GetRefractionRayCount() const
unsigned int GetRefractionRecursionCount() const
const SFVEC3F & GetSpecularColor() const
const SFVEC3F & GetEmissiveColor() const
float GetReflection() const
unsigned int GetReflectionRayCount() const
virtual SFVEC3F GetDiffuseColor(const HITINFO &aHitInfo) const =0
BOARD_ITEM * GetBoardItem() const
virtual bool Intersect(const RAY &aRay, HITINFO &aHitInfo) const =0
float GetModelTransparency() const
const MATERIAL * GetMaterial() const
void SetShadedBuffer(SFVEC3F *aShadedBuffer)
void SetShadowsEnabled(bool aIsShadowsEnabled)
void SetPixelData(unsigned int x, unsigned int y, const SFVEC3F &aNormal, const SFVEC4F &aColor, const SFVEC3F &aHitPosition, float aDepth, float aShadowAttFactor)
void UpdateSize(const SFVEC2UI &aSize)
This is a base class to hold data and functions for render targets.
wxSize m_windowSize
The window size that this camera is working.
BOARD_ADAPTER & m_boardAdapter
Settings reference in use for this render.
void renderBlockTracing(uint8_t *ptrPBO, signed int iBlock)
static SFVEC4F premultiplyAlpha(const SFVEC4F &aInput)
RENDER_3D_RAYTRACE_BASE(BOARD_ADAPTER &aAdapter, CAMERA &aCamera)
void renderFinalColor(uint8_t *ptrPBO, const SFVEC4F &rgbColor, bool applyColorSpaceConversion)
~RENDER_3D_RAYTRACE_BASE()
HITINFO_PACKET * m_firstHitinfo
void renderTracing(uint8_t *ptrPBO, REPORTER *aStatusReporter)
void render(uint8_t *ptrPBO, REPORTER *aStatusReporter)
void postProcessBlurFinish(uint8_t *ptrPBO, REPORTER *aStatusReporter)
void renderAntiAliasPackets(const SFVEC4F *aBgColorY, const HITINFO_PACKET *aHitPck_X0Y0, const HITINFO_PACKET *aHitPck_AA_X1Y1, const RAY *aRayPck, SFVEC4F *aOutHitColor)
POST_SHADER_SSAO m_postShaderSsao
bool m_is_canvas_initialized
void postProcessShading(uint8_t *ptrPBO, REPORTER *aStatusReporter)
std::vector< int > m_blockPositionsWasProcessed
Flag if a position was already processed (cleared each new render).
ACCELERATOR_3D * m_accelerator
DIRECTIONAL_LIGHT * m_cameraLight
BOARD_ITEM * IntersectBoardItem(const RAY &aRay)
unsigned int m_convertedDummyBlockCount
RT_RENDER_STATE m_renderState
State used on quality render.
SFVEC2UI m_fastPreviewModeSize
void renderRayPackets(const SFVEC4F *bgColorY, const RAY *aRayPkt, HITINFO_PACKET *aHitPacket, bool is_testShadow, SFVEC4F *aOutHitColor)
int GetWaitForEditingTimeOut() override
Give the interface the time (in ms) that it should wait for editing or movements before (this works f...
std::vector< SFVEC2UI > m_blockPositionsFast
Encode the Morton code positions (on fast preview mode).
SFVEC4F shadeHit(const SFVEC4F &aBgColor, const RAY &aRay, HITINFO &aHitInfo, bool aIsInsideObject, unsigned int aRecursiveLevel, bool is_testShadow) const
CONTAINER_2D * m_outlineBoard2dObjects
SFVEC4F m_backgroundColorBottom
SFVEC4F m_backgroundColorTop
BVH_CONTAINER_2D * m_antioutlineBoard2dObjects
void restartRenderState()
wxSize m_oldWindowsSize
Used to see if the windows size changed.
void renderPreview(uint8_t *ptrPBO)
std::list< LIGHT * > m_lights
size_t m_blockRenderProgressCount
Save the number of blocks progress of the render.
unsigned int m_converted2dRoundSegmentCount
int64_t m_renderStartTime
Time that the render starts.
void initializeBlockPositions()
std::vector< SFVEC2UI > m_blockPositions
Encode Morton code positions.
SFVEC2UI m_realBufferSize
A pure virtual class used to derive REPORTER objects from.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
COLOR_RGBA BlendColor(const COLOR_RGBA &aC1, const COLOR_RGBA &aC2)
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
uint32_t DecodeMorton2Y(uint32_t code)
uint32_t DecodeMorton2X(uint32_t code)
Implements Morton Codes https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/ http://www....
int64_t GetRunningMicroSecs()
An alternate way to calculate an elapsed time (in microsecondes) to class PROF_COUNTER.
void RAYPACKET_InitRays_with2DDisplacement(const CAMERA &aCamera, const SFVEC2F &aWindowsPosition, const SFVEC2F &a2DWindowsPosDisplacementFactor, RAY *aRayPck)
#define RAYPACKET_INVMASK
#define RAYPACKET_RAYS_PER_PACKET
static SFVEC4F convertLinearToSRGBA(const SFVEC4F &aRGBAcolor)
SFVEC3F ConvertSRGBToLinear(const SFVEC3F &aSRGBcolor)
static void SetPixel(uint8_t *p, const COLOR_RGBA &v)
static void HITINFO_PACKET_init(HITINFO_PACKET *aHitPacket)
static SFVEC3F convertLinearToSRGB(const SFVEC3F &aRGBcolor)
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
static void SetPixelSRGBA(uint8_t *p, const COLOR_RGBA &v)
SFVEC4F ConvertSRGBAToLinear(const SFVEC4F &aSRGBAcolor)
@ RT_RENDER_STATE_POST_PROCESS_SHADE
@ RT_RENDER_STATE_POST_PROCESS_BLUR_AND_FINISH
@ RT_RENDER_STATE_TRACING
SFVEC4F ConvertSRGBAToLinear(const SFVEC4F &aSRGBAcolor)
bool raytrace_post_processing
float raytrace_spread_shadows
bool raytrace_reflections
float raytrace_spread_reflections
int raytrace_nrsamples_shadows
bool raytrace_refractions
float raytrace_spread_refractions
bool raytrace_anti_aliasing
Stores the hit information of a ray with a point on the surface of a object.
unsigned int m_acc_node_info
( 4) The acc stores here the node that it hits
float m_tHit
( 4) distance
const OBJECT_3D * pHitObject
( 4) Object that was hitted
float m_ShadowFactor
( 4) Shadow attenuation (1.0 no shadow, 0.0f darkness)
SFVEC3F m_HitNormal
(12) normal at the hit point
SFVEC3F m_HitPoint
(12) hit position
RAY m_ray[RAYPACKET_RAYS_PER_PACKET]
void Init(const SFVEC3F &o, const SFVEC3F &d)
SFVEC3F at(float t) const
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
BS::thread_pool thread_pool