32#include "../color_rgba.h"
41 m_postShaderSsao( aCamera )
43 wxLogTrace(
m_logTrace, wxT(
"RENDER_3D_RAYTRACE_BASE::RENDER_3D_RAYTRACE_BASE" ) );
71 wxLogTrace(
m_logTrace, wxT(
"RENDER_3D_RAYTRACE_BASE::~RENDER_3D_RAYTRACE_BASE" ) );
120 return SFVEC4F( aInput.r * aInput.a, aInput.g * aInput.a, aInput.b * aInput.a, aInput.a );
139 uint8_t* tmp_ptrPBO = ptrPBO + 3;
141 for(
unsigned int i = 0; i < nPixels; ++i )
169 wxASSERT_MSG(
false, wxT(
"Invalid state on m_renderState" ) );
179 aStatusReporter->
Report( wxString::Format(
_(
"Rendering time %.3f s" ), elapsed_time ) );
188 auto startTime = std::chrono::steady_clock::now();
189 bool breakLoop =
false;
191 std::atomic<size_t> numBlocksRendered( 0 );
192 std::atomic<size_t> currentBlock( 0 );
193 std::atomic<size_t> threadsFinished( 0 );
195 size_t parallelThreadCount = std::min<size_t>(
196 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
201 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
203 std::thread t = std::thread( [&]()
205 for(
size_t iBlock = currentBlock.fetch_add( 1 );
207 iBlock = currentBlock.fetch_add( 1 ) )
209 if( !m_blockPositionsWasProcessed[iBlock] )
211 renderBlockTracing( ptrPBO, iBlock );
213 m_blockPositionsWasProcessed[iBlock] = 1;
217 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(
218 std::chrono::steady_clock::now() - startTime );
220 if( diff.count() > timeLimit )
231 while( threadsFinished < parallelThreadCount )
232 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
234 m_blockRenderProgressCount += numBlocksRendered;
236 if( aStatusReporter )
237 aStatusReporter->Report( wxString::Format(
_(
"Rendering: %.0f %%" ),
238 (
float) ( m_blockRenderProgressCount * 100 )
239 / (
float) m_blockPositions.size() ) );
243 if( m_blockRenderProgressCount >= m_blockPositions.size() )
245 if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
256#define SRGB_GAMA 2.4f
263 const float gammaCorrection = 1.0f /
SRGB_GAMA;
266 return glm::mix( glm::pow( clampedColor,
SFVEC3F(gammaCorrection) ) * 1.055f - 0.055f,
267 clampedColor * 12.92f,
268 glm::lessThan( clampedColor,
SFVEC3F(0.0031308f) ) );
283 return glm::mix( glm::pow( ( aSRGBcolor +
SFVEC3F( 0.055f ) )
284 *
SFVEC3F( 0.94786729857819905213270142180095f ),
286 aSRGBcolor *
SFVEC3F( 0.07739938080495356037151702786378f ),
287 glm::lessThanEqual( aSRGBcolor,
SFVEC3F( 0.04045f ) ) );
299 bool applyColorSpaceConversion )
308 if( applyColorSpaceConversion )
312 ptrPBO[0] = (
unsigned int) glm::clamp( (
int) (
color.r * 255 ), 0, 255 );
313 ptrPBO[1] = (
unsigned int) glm::clamp( (
int) (
color.g * 255 ), 0, 255 );
314 ptrPBO[2] = (
unsigned int) glm::clamp( (
int) (
color.b * 255 ), 0, 255 );
315 ptrPBO[3] = (
unsigned int) glm::clamp( (
int) (
color.a * 255 ), 0, 255 );
324 aHitPacket[i].
m_HitInfo.
m_tHit = std::numeric_limits<float>::infinity();
341 if( aHitPacket[i].m_hitresult ==
true )
343 aOutHitColor[i] =
shadeHit( bgColorY[y], aRayPkt[i], aHitPacket[i].m_HitInfo,
344 false, 0, is_testShadow );
348 aOutHitColor[i] = bgColorY[y];
366 const RAY& rayAA = aRayPck[i];
369 hitAA.
m_tHit = std::numeric_limits<float>::infinity();
374 const unsigned int idx0y1 = ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 );
375 const unsigned int idx1y1 = ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 );
381 unsigned int nodex1y0 = 0;
384 nodex1y0 = aHitPck_X0Y0[ i + 1 ].m_HitInfo.m_acc_node_info;
386 unsigned int nodex0y1 = 0;
389 nodex0y1 = aHitPck_X0Y0[idx0y1].m_HitInfo.m_acc_node_info;
391 unsigned int nodex1y1 = 0;
398 if( ( ( nodex0y0 == nodex1y0 ) || ( nodex1y0 == 0 ) )
399 && ( ( nodex0y0 == nodex0y1 ) || ( nodex0y1 == 0 ) )
400 && ( ( nodex0y0 == nodex1y1 ) || ( nodex1y1 == 0 ) )
401 && ( nodex0y0 == node_AA_x0y0 ) )
439 if( ( nodex1y0 != 0 ) && ( nodex0y0 != nodex1y0 ) )
442 if( ( nodex0y1 != 0 ) && ( nodex0y0 != nodex0y1 ) && ( nodex1y0 != nodex0y1 ) )
445 if( ( nodex1y1 != 0 ) && ( nodex0y0 != nodex1y1 ) && ( nodex0y1 != nodex1y1 ) &&
446 ( nodex1y0 != nodex1y1 ) )
449 if( (node_AA_x0y0 != 0 ) && ( nodex0y0 != node_AA_x0y0 ) &&
450 ( nodex0y1 != node_AA_x0y0 ) && ( nodex1y0 != node_AA_x0y0 ) &&
451 ( nodex1y1 != node_AA_x0y0 ) )
457 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0,
467 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0,
476#define DISP_FACTOR 0.075f
498 const float posYfactor = (float) ( blockPosI.y + y ) / (float)
m_windowSize.y;
512 const SFVEC4F& outColor = bgColor[y];
514 const unsigned int yBlockPos = blockPos.y + y;
532 const SFVEC4F& outColor = bgColor[y];
534 const unsigned int yConst = blockPos.x + ( ( y + blockPos.y ) *
m_realBufferSize.x );
538 uint8_t* ptr = &ptrPBO[( yConst + x ) * 4];
571 const SFVEC4F& outColor = bgColor[y];
574 hitColor_AA_X1Y1[i] = outColor;
589 SFVEC4F color_average = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] ) *
SFVEC4F( 0.5f );
591 hitColor_AA_X1Y0[i] = color_average;
592 hitColor_AA_X0Y1[i] = color_average;
593 hitColor_AA_X0Y1_half[i] = color_average;
619 blockRayPck_AA_X1Y1_half, hitColor_AA_X0Y1_half );
624 hitColor_X0Y0[i] = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] + hitColor_AA_X1Y0[i] +
625 hitColor_AA_X0Y1[i] + hitColor_AA_X0Y1_half[i] ) *
631 uint8_t* ptr = &ptrPBO[( blockPos.x + ( blockPos.y *
m_realBufferSize.x ) ) * 4];
646 const SFVEC4F& hColor = hitColor_X0Y0[i];
648 if( hitPacket_X0Y0[i].m_hitresult ==
true )
694 if( aStatusReporter )
695 aStatusReporter->
Report(
_(
"Rendering: Post processing shader" ) );
699 std::atomic<size_t> nextBlock( 0 );
700 std::atomic<size_t> threadsFinished( 0 );
702 size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
704 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
706 std::thread t = std::thread( [&]()
709 y = nextBlock.fetch_add( 1 ) )
711 SFVEC3F* ptr = &m_shaderBuffer[ y * m_realBufferSize.x ];
713 for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
715 *ptr = m_postShaderSsao.Shade( SFVEC2I( x, y ) );
726 while( threadsFinished < parallelThreadCount )
727 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
747 std::atomic<size_t> nextBlock( 0 );
748 std::atomic<size_t> threadsFinished( 0 );
750 size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
752 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
754 std::thread t = std::thread( [&]()
757 y = nextBlock.fetch_add( 1 ) )
759 uint8_t* ptr = &ptrPBO[ y * m_realBufferSize.x * 4 ];
761 for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
763 const SFVEC3F bluredShadeColor = m_postShaderSsao.Blur( SFVEC2I( x, y ) );
766 const SFVEC4F originColor = convertLinearToSRGBA(
767 m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) ) );
769 const SFVEC4F originColor =
770 m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) );
772 const SFVEC4F shadedColor = m_postShaderSsao.ApplyShadeColor(
773 SFVEC2I( x, y ), originColor, bluredShadeColor );
775 renderFinalColor( ptr, shadedColor, false );
787 while( threadsFinished < parallelThreadCount )
788 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
803 std::atomic<size_t> nextBlock( 0 );
804 std::atomic<size_t> threadsFinished( 0 );
806 size_t parallelThreadCount = std::min<size_t>(
807 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
810 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
812 std::thread t = std::thread( [&]()
815 iBlock = nextBlock.fetch_add( 1 ) )
817 const SFVEC2UI& windowPosUI = m_blockPositionsFast[ iBlock ];
818 const SFVEC2I windowsPos = SFVEC2I( windowPosUI.x + m_xoffset,
819 windowPosUI.y + m_yoffset );
821 RAYPACKET blockPacket( m_camera, windowsPos, 4 );
823 HITINFO_PACKET hitPacket[RAYPACKET_RAYS_PER_PACKET];
826 for( HITINFO_PACKET& packet : hitPacket )
828 packet.m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
829 packet.m_HitInfo.m_acc_node_info = 0;
830 packet.m_hitresult = false;
844 const float posYfactor =
845 (float) ( windowsPos.y + y * 4.0f ) / (float) m_windowSize.y;
847 bgColor[y] = bgTopColor * SFVEC4F( posYfactor )
848 + bgBotColor * ( SFVEC4F( 1.0f ) - SFVEC4F( posYfactor ) );
855 const SFVEC4F bhColorY = bgColor[i / RAYPACKET_DIM];
857 if( hitPacket[i].m_hitresult == true )
859 const SFVEC4F hitColor = shadeHit( bhColorY, blockPacket.m_ray[i],
860 hitPacket[i].m_HitInfo, false,
863 hitColorShading[i] = COLOR_RGBA( hitColor );
866 hitColorShading[i] = bhColorY;
873 const SFVEC4F bgColorY = bgColor[y];
894 const unsigned int iLT = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 0 ) );
895 const unsigned int iRT = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 0 ) );
896 const unsigned int iLB = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 ) );
897 const unsigned int iRB = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 ) );
900 const COLOR_RGBA& cLT = hitColorShading[ iLT ];
901 const COLOR_RGBA& cRT = hitColorShading[ iRT ];
902 const COLOR_RGBA& cLB = hitColorShading[ iLB ];
903 const COLOR_RGBA& cRB = hitColorShading[ iRB ];
908 const SFVEC3F& oriLT = blockPacket.m_ray[ iLT ].m_Origin;
909 const SFVEC3F& oriRB = blockPacket.m_ray[ iRB ].m_Origin;
911 const SFVEC3F& dirLT = blockPacket.m_ray[ iLT ].m_Dir;
912 const SFVEC3F& dirRB = blockPacket.m_ray[ iRB ].m_Dir;
918 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
920 bool hittedC =
false;
922 if( ( hitPacket[iLT].m_hitresult ==
true )
923 || ( hitPacket[iRT].m_hitresult ==
true )
924 || ( hitPacket[iLB].m_hitresult ==
true )
925 || ( hitPacket[iRB].m_hitresult ==
true ) )
927 oriC = ( oriLT + oriRB ) * 0.5f;
928 dirC = glm::normalize( ( dirLT + dirRB ) * 0.5f );
932 centerRay.
Init( oriC, dirC );
934 const unsigned int nodeLT = hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
935 const unsigned int nodeRT = hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
936 const unsigned int nodeLB = hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
937 const unsigned int nodeRB = hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
943 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
947 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) && ( nodeLB != nodeRT ) )
951 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) && ( nodeRB != nodeLT )
952 && ( nodeRB != nodeRT ) )
963 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
975 const SFVEC3F& oriRT = blockPacket.m_ray[ iRT ].m_Origin;
976 const SFVEC3F& dirRT = blockPacket.m_ray[ iRT ].m_Dir;
982 rayLRT.
Init( ( oriLT + oriRT ) * 0.5f,
983 glm::normalize( ( dirLT + dirRT ) * 0.5f ) );
986 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
988 if( hitPacket[iLT].m_hitresult && hitPacket[iRT].m_hitresult
989 && ( hitPacket[iLT].m_HitInfo.pHitObject
990 == hitPacket[iRT].m_HitInfo.pHitObject ) )
992 hitInfoLRT.
pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
993 hitInfoLRT.
m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
994 hitPacket[ iRT ].m_HitInfo.m_tHit ) * 0.5f;
996 glm::normalize( ( hitPacket[ iLT ].m_HitInfo.m_HitNormal +
997 hitPacket[ iRT ].m_HitInfo.m_HitNormal ) * 0.5f );
1006 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iRT ].m_hitresult )
1008 const unsigned int nodeLT =
1009 hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
1010 const unsigned int nodeRT =
1011 hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1013 bool hittedLRT =
false;
1019 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
1025 false, 0,
false ) );
1028 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
1048 const SFVEC3F &oriLB = blockPacket.m_ray[ iLB ].m_Origin;
1049 const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
1053 rayLTB.
Init( ( oriLT + oriLB ) * 0.5f,
1054 glm::normalize( ( dirLT + dirLB ) * 0.5f ) );
1057 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1059 if( hitPacket[ iLT ].m_hitresult && hitPacket[ iLB ].m_hitresult
1060 && ( hitPacket[ iLT ].m_HitInfo.pHitObject ==
1061 hitPacket[ iLB ].m_HitInfo.pHitObject ) )
1063 hitInfoLTB.
pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
1064 hitInfoLTB.
m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
1065 hitPacket[ iLB ].m_HitInfo.m_tHit ) * 0.5f;
1067 glm::normalize( ( hitPacket[ iLT ].m_HitInfo.m_HitNormal +
1068 hitPacket[ iLB ].m_HitInfo.m_HitNormal ) * 0.5f );
1076 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iLB ].m_hitresult )
1078 const unsigned int nodeLT =
1079 hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
1080 const unsigned int nodeLB =
1081 hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1083 bool hittedLTB =
false;
1089 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) )
1095 false, 0,
false ) );
1098 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1118 rayRTB.
Init( ( oriRT + oriRB ) * 0.5f,
1119 glm::normalize( ( dirRT + dirRB ) * 0.5f ) );
1122 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1124 if( hitPacket[ iRT ].m_hitresult && hitPacket[ iRB ].m_hitresult
1125 && ( hitPacket[ iRT ].m_HitInfo.pHitObject ==
1126 hitPacket[ iRB ].m_HitInfo.pHitObject ) )
1128 hitInfoRTB.
pHitObject = hitPacket[ iRT ].m_HitInfo.pHitObject;
1130 hitInfoRTB.
m_tHit = ( hitPacket[ iRT ].m_HitInfo.m_tHit +
1131 hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
1134 glm::normalize( ( hitPacket[ iRT ].m_HitInfo.m_HitNormal +
1135 hitPacket[ iRB ].m_HitInfo.m_HitNormal ) * 0.5f );
1144 if( hitPacket[ iRT ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1146 const unsigned int nodeRT =
1147 hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1148 const unsigned int nodeRB =
1149 hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1151 bool hittedRTB =
false;
1157 if( ( nodeRB != 0 ) && ( nodeRB != nodeRT ) )
1168 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1172 false, 0,
false ) );
1182 const SFVEC3F& oriLB = blockPacket.m_ray[ iLB ].m_Origin;
1183 const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
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 ==
1195 hitPacket[iRB].m_HitInfo.pHitObject ) )
1197 hitInfoLRB.
pHitObject = hitPacket[ iLB ].m_HitInfo.pHitObject;
1199 hitInfoLRB.
m_tHit = ( hitPacket[ iLB ].m_HitInfo.m_tHit +
1200 hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
1203 glm::normalize( ( hitPacket[ iLB ].m_HitInfo.m_HitNormal +
1204 hitPacket[ iRB ].m_HitInfo.m_HitNormal ) * 0.5f );
1213 if( hitPacket[ iLB ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1215 const unsigned int nodeLB =
1216 hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1217 const unsigned int nodeRB =
1218 hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1220 bool hittedLRB =
false;
1226 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) )
1233 false, 0,
false ) );
1237 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1241 false, 0,
false ) );
1251 if( hitPacket[ iLT ].m_hitresult || hittedC )
1255 rayLTC.
Init( ( oriLT + oriC ) * 0.5f,
1256 glm::normalize( ( dirLT + dirC ) * 0.5f ) );
1259 hitInfoLTC.
m_tHit = std::numeric_limits<float>::infinity();
1261 bool hitted =
false;
1265 else if( hitPacket[ iLT ].m_hitresult )
1266 hitted = hitPacket[ iLT ].m_HitInfo.pHitObject->Intersect(
1278 if( hitPacket[ iRT ].m_hitresult || hittedC )
1282 rayRTC.
Init( ( oriRT + oriC ) * 0.5f,
1283 glm::normalize( ( dirRT + dirC ) * 0.5f ) );
1286 hitInfoRTC.
m_tHit = std::numeric_limits<float>::infinity();
1288 bool hitted =
false;
1292 else if( hitPacket[ iRT ].m_hitresult )
1293 hitted = hitPacket[ iRT ].m_HitInfo.pHitObject->Intersect( rayRTC,
1304 if( hitPacket[ iLB ].m_hitresult || hittedC )
1308 rayLBC.
Init( ( oriLB + oriC ) * 0.5f,
1309 glm::normalize( ( dirLB + dirC ) * 0.5f ) );
1312 hitInfoLBC.
m_tHit = std::numeric_limits<float>::infinity();
1314 bool hitted =
false;
1318 else if( hitPacket[ iLB ].m_hitresult )
1319 hitted = hitPacket[ iLB ].m_HitInfo.pHitObject->Intersect( rayLBC,
1330 if( hitPacket[ iRB ].m_hitresult || hittedC )
1334 rayRBC.
Init( ( oriRB + oriC ) * 0.5f,
1335 glm::normalize( ( dirRB + dirC ) * 0.5f ) );
1338 hitInfoRBC.
m_tHit = std::numeric_limits<float>::infinity();
1340 bool hitted =
false;
1344 else if( hitPacket[ iRB ].m_hitresult )
1345 hitted = hitPacket[ iRB ].m_HitInfo.pHitObject->Intersect( rayRBC,
1390 while( threadsFinished < parallelThreadCount )
1391 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
1395#define USE_EXPERIMENTAL_SOFT_SHADOWS 1
1398 bool aIsInsideObject,
unsigned int aRecursiveLevel,
1399 bool is_testShadow )
const
1402 wxASSERT( objMaterial !=
nullptr );
1407 if( aRecursiveLevel > 7 )
1414 const SFVEC4F diffuseColorObj =
1417#if USE_EXPERIMENTAL_SOFT_SHADOWS
1421 float shadow_att_factor_sum = 0.0f;
1423 unsigned int nr_lights_that_can_cast_shadows = 0;
1431 light->GetLightParameters( hitPoint, vectorToLight, colorOfLight, distToLight );
1434 colorOfLight =
SFVEC3F( 1.0f );
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++;
1447#if USE_EXPERIMENTAL_SOFT_SHADOWS
1449 if( aRecursiveLevel > 0 )
1453 rayToLight.
Init( hitPoint, vectorToLight );
1458 shadow_att_factor_light = 0.0f;
1460#if USE_EXPERIMENTAL_SOFT_SHADOWS
1464 const unsigned int shadow_number_of_samples =
1466 const float shadow_inc_factor = 1.0f / (float) ( shadow_number_of_samples );
1468 for(
unsigned int i = 0; i < shadow_number_of_samples; ++i )
1474 rayToLight.
Init( hitPoint, vectorToLight );
1479 const SFVEC3F disturbed_vector_to_light =
1480 glm::normalize( vectorToLight + unifVector *
1483 rayToLight.
Init( hitPoint, disturbed_vector_to_light );
1491 shadow_att_factor_light -= shadow_inc_factor;
1496 shadow_att_factor_sum += shadow_att_factor_light;
1499 outColor +=
SFVEC4F( objMaterial->
Shade( aRay, aHitInfo, NdotL, diffuseColorObj,
1500 vectorToLight, colorOfLight,
1501 shadow_att_factor_light ),
1511 if( nr_lights_that_can_cast_shadows > 0 )
1514 shadow_att_factor_sum / (
float) ( nr_lights_that_can_cast_shadows * 1.0f ), 0.0f );
1522 outColor = glm::min( outColor,
SFVEC4F( 1.0f ) );
1529 && ( aRecursiveLevel < objMaterial->GetReflectionRecursionCount() ) )
1531 const unsigned int reflection_number_of_samples =
1539 for(
unsigned int i = 0; i < reflection_number_of_samples; ++i )
1545 reflectedRay.
Init( hitPoint, reflectVector );
1550 const SFVEC3F random_reflectVector =
1551 glm::normalize( reflectVector +
1555 reflectedRay.
Init( hitPoint, random_reflectVector );
1559 reflectedHit.
m_tHit = std::numeric_limits<float>::infinity();
1564 shadeHit( aBgColor, reflectedRay, reflectedHit,
false,
1565 aRecursiveLevel + 1, is_testShadow ) *
1568 (1.0f / ( 1.0f + 0.75f * reflectedHit.
m_tHit *
1569 reflectedHit.
m_tHit) ) );
1575 outColor += (sum_color /
SFVEC4F( (
float)reflection_number_of_samples) );
1584 const float airIndex = 1.000293f;
1585 const float glassIndex = 1.49f;
1586 const float air_over_glass = airIndex / glassIndex;
1587 const float glass_over_air = glassIndex / airIndex;
1589 const float refractionRatio = aIsInsideObject?glass_over_air:air_over_glass;
1601 const unsigned int refractions_number_of_samples =
1606 for(
unsigned int i = 0; i < refractions_number_of_samples; ++i )
1612 refractedRay.
Init( startPoint, refractedVector );
1617 const SFVEC3F randomizeRefractedVector =
1618 glm::normalize( refractedVector +
1622 refractedRay.
Init( startPoint, randomizeRefractedVector );
1626 refractedHit.
m_tHit = std::numeric_limits<float>::infinity();
1628 SFVEC4F refractedColor = aBgColor;
1632 refractedColor =
shadeHit( aBgColor, refractedRay, refractedHit,
1633 !aIsInsideObject, aRecursiveLevel + 1,
false );
1636 (1.0f - objTransparency ) *
1640 const SFVEC4F transparency = 1.0f / ( absorbance + 1.0f );
1642 sum_color += refractedColor * transparency;
1646 sum_color += refractedColor;
1650 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * sum_color
1651 /
SFVEC4F( (
float) refractions_number_of_samples );
1655 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * aBgColor;
1666 const float dx = (float) a.x - (
float) b.x;
1667 const float dy = (float) a.y - (
float) b.y;
1668 return hypotf( dx, dy );
1724 for(
int x = 0; x < blocks_x; ++x )
1726 for(
int y = 0; y < blocks_y; ++y )
1735 float distanceA = distance( a, center );
1736 float distanceB = distance( b, center );
1738 if( distanceA != distanceB )
1739 return distanceA < distanceB;
1758 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.
CAMERA & m_camera
Flag if the canvas specific for this render was already initialized.
BOARD_ADAPTER & m_boardAdapter
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
Encode the Morton code positions (on fast preview mode).
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
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
Used to see if the windows size changed.
SFVEC4F m_backgroundColorTop
BVH_CONTAINER_2D * m_antioutlineBoard2dObjects
void restartRenderState()
wxSize m_oldWindowsSize
Encode Morton code positions.
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
Flag if a position was already processed (cleared each new render).
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)
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