32#include "../color_rgba.h"
42 m_postShaderSsao( aCamera )
44 wxLogTrace(
m_logTrace, wxT(
"RENDER_3D_RAYTRACE_BASE::RENDER_3D_RAYTRACE_BASE" ) );
72 wxLogTrace(
m_logTrace, wxT(
"RENDER_3D_RAYTRACE_BASE::~RENDER_3D_RAYTRACE_BASE" ) );
121 return SFVEC4F( aInput.r * aInput.a, aInput.g * aInput.a, aInput.b * aInput.a, aInput.a );
140 uint8_t* tmp_ptrPBO = ptrPBO + 3;
142 for(
unsigned int i = 0; i < nPixels; ++i )
170 wxASSERT_MSG(
false, wxT(
"Invalid state on m_renderState" ) );
180 aStatusReporter->
Report( wxString::Format(
_(
"Rendering time %.3f s" ), elapsed_time ) );
189 auto startTime = std::chrono::steady_clock::now();
190 std::atomic<size_t> numBlocksRendered( 0 );
191 std::atomic<size_t> currentBlock( 0 );
196 auto processBlocks = [&]()
198 for(
size_t iBlock = currentBlock.fetch_add( 1 );
200 iBlock = currentBlock.fetch_add( 1 ) )
209 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(
210 std::chrono::steady_clock::now() - startTime );
212 if( diff.count() > timeLimit )
217 for(
size_t i = 0; i <
tp.get_thread_count() + 1; ++i )
218 tp.push_task( processBlocks );
224 if( aStatusReporter )
225 aStatusReporter->
Report( wxString::Format(
_(
"Rendering: %.0f %%" ),
244#define SRGB_GAMA 2.4f
251 const float gammaCorrection = 1.0f /
SRGB_GAMA;
254 return glm::mix( glm::pow( clampedColor,
SFVEC3F(gammaCorrection) ) * 1.055f - 0.055f,
255 clampedColor * 12.92f,
256 glm::lessThan( clampedColor,
SFVEC3F(0.0031308f) ) );
271 return glm::mix( glm::pow( ( aSRGBcolor +
SFVEC3F( 0.055f ) )
272 *
SFVEC3F( 0.94786729857819905213270142180095f ),
274 aSRGBcolor *
SFVEC3F( 0.07739938080495356037151702786378f ),
275 glm::lessThanEqual( aSRGBcolor,
SFVEC3F( 0.04045f ) ) );
287 bool applyColorSpaceConversion )
296 if( applyColorSpaceConversion )
300 ptrPBO[0] = (
unsigned int) glm::clamp( (
int) (
color.r * 255 ), 0, 255 );
301 ptrPBO[1] = (
unsigned int) glm::clamp( (
int) (
color.g * 255 ), 0, 255 );
302 ptrPBO[2] = (
unsigned int) glm::clamp( (
int) (
color.b * 255 ), 0, 255 );
303 ptrPBO[3] = (
unsigned int) glm::clamp( (
int) (
color.a * 255 ), 0, 255 );
312 aHitPacket[i].
m_HitInfo.
m_tHit = std::numeric_limits<float>::infinity();
329 if( aHitPacket[i].m_hitresult ==
true )
331 aOutHitColor[i] =
shadeHit( bgColorY[y], aRayPkt[i], aHitPacket[i].m_HitInfo,
332 false, 0, is_testShadow );
336 aOutHitColor[i] = bgColorY[y];
354 const RAY& rayAA = aRayPck[i];
357 hitAA.
m_tHit = std::numeric_limits<float>::infinity();
362 const unsigned int idx0y1 = ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 );
363 const unsigned int idx1y1 = ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 );
369 unsigned int nodex1y0 = 0;
372 nodex1y0 = aHitPck_X0Y0[ i + 1 ].m_HitInfo.m_acc_node_info;
374 unsigned int nodex0y1 = 0;
377 nodex0y1 = aHitPck_X0Y0[idx0y1].m_HitInfo.m_acc_node_info;
379 unsigned int nodex1y1 = 0;
386 if( ( ( nodex0y0 == nodex1y0 ) || ( nodex1y0 == 0 ) )
387 && ( ( nodex0y0 == nodex0y1 ) || ( nodex0y1 == 0 ) )
388 && ( ( nodex0y0 == nodex1y1 ) || ( nodex1y1 == 0 ) )
389 && ( nodex0y0 == node_AA_x0y0 ) )
427 if( ( nodex1y0 != 0 ) && ( nodex0y0 != nodex1y0 ) )
430 if( ( nodex0y1 != 0 ) && ( nodex0y0 != nodex0y1 ) && ( nodex1y0 != nodex0y1 ) )
433 if( ( nodex1y1 != 0 ) && ( nodex0y0 != nodex1y1 ) && ( nodex0y1 != nodex1y1 ) &&
434 ( nodex1y0 != nodex1y1 ) )
437 if( (node_AA_x0y0 != 0 ) && ( nodex0y0 != node_AA_x0y0 ) &&
438 ( nodex0y1 != node_AA_x0y0 ) && ( nodex1y0 != node_AA_x0y0 ) &&
439 ( nodex1y1 != node_AA_x0y0 ) )
445 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0,
455 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0,
464#define DISP_FACTOR 0.075f
486 const float posYfactor = (float) ( blockPosI.y + y ) / (float)
m_windowSize.y;
500 const SFVEC4F& outColor = bgColor[y];
502 const unsigned int yBlockPos = blockPos.y + y;
520 const SFVEC4F& outColor = bgColor[y];
522 const unsigned int yConst = blockPos.x + ( ( y + blockPos.y ) *
m_realBufferSize.x );
526 uint8_t* ptr = &ptrPBO[( yConst + x ) * 4];
559 const SFVEC4F& outColor = bgColor[y];
562 hitColor_AA_X1Y1[i] = outColor;
577 SFVEC4F color_average = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] ) *
SFVEC4F( 0.5f );
579 hitColor_AA_X1Y0[i] = color_average;
580 hitColor_AA_X0Y1[i] = color_average;
581 hitColor_AA_X0Y1_half[i] = color_average;
607 blockRayPck_AA_X1Y1_half, hitColor_AA_X0Y1_half );
612 hitColor_X0Y0[i] = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] + hitColor_AA_X1Y0[i] +
613 hitColor_AA_X0Y1[i] + hitColor_AA_X0Y1_half[i] ) *
619 uint8_t* ptr = &ptrPBO[( blockPos.x + ( blockPos.y *
m_realBufferSize.x ) ) * 4];
634 const SFVEC4F& hColor = hitColor_X0Y0[i];
636 if( hitPacket_X0Y0[i].m_hitresult ==
true )
682 if( aStatusReporter )
683 aStatusReporter->
Report(
_(
"Rendering: Post processing shader" ) );
687 std::atomic<size_t> nextBlock( 0 );
688 std::atomic<size_t> threadsFinished( 0 );
690 size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
692 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
694 std::thread t = std::thread( [&]()
697 y = nextBlock.fetch_add( 1 ) )
699 SFVEC3F* ptr = &m_shaderBuffer[ y * m_realBufferSize.x ];
701 for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
703 *ptr = m_postShaderSsao.Shade( SFVEC2I( x, y ) );
714 while( threadsFinished < parallelThreadCount )
715 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
735 std::atomic<size_t> nextBlock( 0 );
736 std::atomic<size_t> threadsFinished( 0 );
738 size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
740 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
742 std::thread t = std::thread( [&]()
745 y = nextBlock.fetch_add( 1 ) )
747 uint8_t* ptr = &ptrPBO[ y * m_realBufferSize.x * 4 ];
749 for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
751 const SFVEC3F bluredShadeColor = m_postShaderSsao.Blur( SFVEC2I( x, y ) );
754 const SFVEC4F originColor = convertLinearToSRGBA(
755 m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) ) );
757 const SFVEC4F originColor =
758 m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) );
760 const SFVEC4F shadedColor = m_postShaderSsao.ApplyShadeColor(
761 SFVEC2I( x, y ), originColor, bluredShadeColor );
763 renderFinalColor( ptr, shadedColor, false );
775 while( threadsFinished < parallelThreadCount )
776 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
791 std::atomic<size_t> nextBlock( 0 );
792 std::atomic<size_t> threadsFinished( 0 );
794 size_t parallelThreadCount = std::min<size_t>(
795 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
798 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
800 std::thread t = std::thread( [&]()
803 iBlock = nextBlock.fetch_add( 1 ) )
805 const SFVEC2UI& windowPosUI = m_blockPositionsFast[ iBlock ];
806 const SFVEC2I windowsPos = SFVEC2I( windowPosUI.x + m_xoffset,
807 windowPosUI.y + m_yoffset );
809 RAYPACKET blockPacket( m_camera, windowsPos, 4 );
811 HITINFO_PACKET hitPacket[RAYPACKET_RAYS_PER_PACKET];
814 for( HITINFO_PACKET& packet : hitPacket )
816 packet.m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
817 packet.m_HitInfo.m_acc_node_info = 0;
818 packet.m_hitresult = false;
832 const float posYfactor =
833 (float) ( windowsPos.y + y * 4.0f ) / (float) m_windowSize.y;
835 bgColor[y] = bgTopColor * SFVEC4F( posYfactor )
836 + bgBotColor * ( SFVEC4F( 1.0f ) - SFVEC4F( posYfactor ) );
843 const SFVEC4F bhColorY = bgColor[i / RAYPACKET_DIM];
845 if( hitPacket[i].m_hitresult == true )
847 const SFVEC4F hitColor = shadeHit( bhColorY, blockPacket.m_ray[i],
848 hitPacket[i].m_HitInfo, false,
851 hitColorShading[i] = COLOR_RGBA( hitColor );
854 hitColorShading[i] = bhColorY;
861 const SFVEC4F bgColorY = bgColor[y];
882 const unsigned int iLT = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 0 ) );
883 const unsigned int iRT = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 0 ) );
884 const unsigned int iLB = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 ) );
885 const unsigned int iRB = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 ) );
888 const COLOR_RGBA& cLT = hitColorShading[ iLT ];
889 const COLOR_RGBA& cRT = hitColorShading[ iRT ];
890 const COLOR_RGBA& cLB = hitColorShading[ iLB ];
891 const COLOR_RGBA& cRB = hitColorShading[ iRB ];
896 const SFVEC3F& oriLT = blockPacket.m_ray[ iLT ].m_Origin;
897 const SFVEC3F& oriRB = blockPacket.m_ray[ iRB ].m_Origin;
899 const SFVEC3F& dirLT = blockPacket.m_ray[ iLT ].m_Dir;
900 const SFVEC3F& dirRB = blockPacket.m_ray[ iRB ].m_Dir;
906 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
908 bool hittedC =
false;
910 if( ( hitPacket[iLT].m_hitresult ==
true )
911 || ( hitPacket[iRT].m_hitresult ==
true )
912 || ( hitPacket[iLB].m_hitresult ==
true )
913 || ( hitPacket[iRB].m_hitresult ==
true ) )
915 oriC = ( oriLT + oriRB ) * 0.5f;
916 dirC = glm::normalize( ( dirLT + dirRB ) * 0.5f );
920 centerRay.
Init( oriC, dirC );
922 const unsigned int nodeLT = hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
923 const unsigned int nodeRT = hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
924 const unsigned int nodeLB = hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
925 const unsigned int nodeRB = hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
931 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
935 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) && ( nodeLB != nodeRT ) )
939 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) && ( nodeRB != nodeLT )
940 && ( nodeRB != nodeRT ) )
951 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
963 const SFVEC3F& oriRT = blockPacket.m_ray[ iRT ].m_Origin;
964 const SFVEC3F& dirRT = blockPacket.m_ray[ iRT ].m_Dir;
970 rayLRT.
Init( ( oriLT + oriRT ) * 0.5f,
971 glm::normalize( ( dirLT + dirRT ) * 0.5f ) );
974 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
976 if( hitPacket[iLT].m_hitresult && hitPacket[iRT].m_hitresult
977 && ( hitPacket[iLT].m_HitInfo.pHitObject
978 == hitPacket[iRT].m_HitInfo.pHitObject ) )
980 hitInfoLRT.
pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
981 hitInfoLRT.
m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
982 hitPacket[ iRT ].m_HitInfo.m_tHit ) * 0.5f;
984 glm::normalize( ( hitPacket[ iLT ].m_HitInfo.m_HitNormal +
985 hitPacket[ iRT ].m_HitInfo.m_HitNormal ) * 0.5f );
994 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iRT ].m_hitresult )
996 const unsigned int nodeLT =
997 hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
998 const unsigned int nodeRT =
999 hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1001 bool hittedLRT =
false;
1007 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
1013 false, 0,
false ) );
1016 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
1036 const SFVEC3F &oriLB = blockPacket.m_ray[ iLB ].m_Origin;
1037 const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
1041 rayLTB.
Init( ( oriLT + oriLB ) * 0.5f,
1042 glm::normalize( ( dirLT + dirLB ) * 0.5f ) );
1045 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1047 if( hitPacket[ iLT ].m_hitresult && hitPacket[ iLB ].m_hitresult
1048 && ( hitPacket[ iLT ].m_HitInfo.pHitObject ==
1049 hitPacket[ iLB ].m_HitInfo.pHitObject ) )
1051 hitInfoLTB.
pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
1052 hitInfoLTB.
m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
1053 hitPacket[ iLB ].m_HitInfo.m_tHit ) * 0.5f;
1055 glm::normalize( ( hitPacket[ iLT ].m_HitInfo.m_HitNormal +
1056 hitPacket[ iLB ].m_HitInfo.m_HitNormal ) * 0.5f );
1064 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iLB ].m_hitresult )
1066 const unsigned int nodeLT =
1067 hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
1068 const unsigned int nodeLB =
1069 hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1071 bool hittedLTB =
false;
1077 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) )
1083 false, 0,
false ) );
1086 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1106 rayRTB.
Init( ( oriRT + oriRB ) * 0.5f,
1107 glm::normalize( ( dirRT + dirRB ) * 0.5f ) );
1110 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1112 if( hitPacket[ iRT ].m_hitresult && hitPacket[ iRB ].m_hitresult
1113 && ( hitPacket[ iRT ].m_HitInfo.pHitObject ==
1114 hitPacket[ iRB ].m_HitInfo.pHitObject ) )
1116 hitInfoRTB.
pHitObject = hitPacket[ iRT ].m_HitInfo.pHitObject;
1118 hitInfoRTB.
m_tHit = ( hitPacket[ iRT ].m_HitInfo.m_tHit +
1119 hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
1122 glm::normalize( ( hitPacket[ iRT ].m_HitInfo.m_HitNormal +
1123 hitPacket[ iRB ].m_HitInfo.m_HitNormal ) * 0.5f );
1132 if( hitPacket[ iRT ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1134 const unsigned int nodeRT =
1135 hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1136 const unsigned int nodeRB =
1137 hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1139 bool hittedRTB =
false;
1145 if( ( nodeRB != 0 ) && ( nodeRB != nodeRT ) )
1156 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1160 false, 0,
false ) );
1170 const SFVEC3F& oriLB = blockPacket.m_ray[ iLB ].m_Origin;
1171 const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
1175 rayLRB.
Init( ( oriLB + oriRB ) * 0.5f,
1176 glm::normalize( ( dirLB + dirRB ) * 0.5f ) );
1179 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1181 if( hitPacket[iLB].m_hitresult && hitPacket[iRB].m_hitresult
1182 && ( hitPacket[iLB].m_HitInfo.pHitObject ==
1183 hitPacket[iRB].m_HitInfo.pHitObject ) )
1185 hitInfoLRB.
pHitObject = hitPacket[ iLB ].m_HitInfo.pHitObject;
1187 hitInfoLRB.
m_tHit = ( hitPacket[ iLB ].m_HitInfo.m_tHit +
1188 hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
1191 glm::normalize( ( hitPacket[ iLB ].m_HitInfo.m_HitNormal +
1192 hitPacket[ iRB ].m_HitInfo.m_HitNormal ) * 0.5f );
1201 if( hitPacket[ iLB ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1203 const unsigned int nodeLB =
1204 hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1205 const unsigned int nodeRB =
1206 hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1208 bool hittedLRB =
false;
1214 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) )
1221 false, 0,
false ) );
1225 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1229 false, 0,
false ) );
1239 if( hitPacket[ iLT ].m_hitresult || hittedC )
1243 rayLTC.
Init( ( oriLT + oriC ) * 0.5f,
1244 glm::normalize( ( dirLT + dirC ) * 0.5f ) );
1247 hitInfoLTC.
m_tHit = std::numeric_limits<float>::infinity();
1249 bool hitted =
false;
1253 else if( hitPacket[ iLT ].m_hitresult )
1254 hitted = hitPacket[ iLT ].m_HitInfo.pHitObject->Intersect(
1266 if( hitPacket[ iRT ].m_hitresult || hittedC )
1270 rayRTC.
Init( ( oriRT + oriC ) * 0.5f,
1271 glm::normalize( ( dirRT + dirC ) * 0.5f ) );
1274 hitInfoRTC.
m_tHit = std::numeric_limits<float>::infinity();
1276 bool hitted =
false;
1280 else if( hitPacket[ iRT ].m_hitresult )
1281 hitted = hitPacket[ iRT ].m_HitInfo.pHitObject->Intersect( rayRTC,
1292 if( hitPacket[ iLB ].m_hitresult || hittedC )
1296 rayLBC.
Init( ( oriLB + oriC ) * 0.5f,
1297 glm::normalize( ( dirLB + dirC ) * 0.5f ) );
1300 hitInfoLBC.
m_tHit = std::numeric_limits<float>::infinity();
1302 bool hitted =
false;
1306 else if( hitPacket[ iLB ].m_hitresult )
1307 hitted = hitPacket[ iLB ].m_HitInfo.pHitObject->Intersect( rayLBC,
1318 if( hitPacket[ iRB ].m_hitresult || hittedC )
1322 rayRBC.
Init( ( oriRB + oriC ) * 0.5f,
1323 glm::normalize( ( dirRB + dirC ) * 0.5f ) );
1326 hitInfoRBC.
m_tHit = std::numeric_limits<float>::infinity();
1328 bool hitted =
false;
1332 else if( hitPacket[ iRB ].m_hitresult )
1333 hitted = hitPacket[ iRB ].m_HitInfo.pHitObject->Intersect( rayRBC,
1378 while( threadsFinished < parallelThreadCount )
1379 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
1383#define USE_EXPERIMENTAL_SOFT_SHADOWS 1
1386 bool aIsInsideObject,
unsigned int aRecursiveLevel,
1387 bool is_testShadow )
const
1390 wxASSERT( objMaterial !=
nullptr );
1395 if( aRecursiveLevel > 7 )
1402 const SFVEC4F diffuseColorObj =
1405#if USE_EXPERIMENTAL_SOFT_SHADOWS
1409 float shadow_att_factor_sum = 0.0f;
1411 unsigned int nr_lights_that_can_cast_shadows = 0;
1419 light->GetLightParameters( hitPoint, vectorToLight, colorOfLight, distToLight );
1422 colorOfLight =
SFVEC3F( 1.0f );
1424 const float NdotL = glm::dot( aHitInfo.
m_HitNormal, vectorToLight );
1428 if( NdotL >= FLT_EPSILON )
1430 float shadow_att_factor_light = 1.0f;
1432 if( is_testShadow && light->GetCastShadows() )
1434 nr_lights_that_can_cast_shadows++;
1435#if USE_EXPERIMENTAL_SOFT_SHADOWS
1437 if( aRecursiveLevel > 0 )
1441 rayToLight.
Init( hitPoint, vectorToLight );
1446 shadow_att_factor_light = 0.0f;
1448#if USE_EXPERIMENTAL_SOFT_SHADOWS
1452 const unsigned int shadow_number_of_samples =
1454 const float shadow_inc_factor = 1.0f / (float) ( shadow_number_of_samples );
1456 for(
unsigned int i = 0; i < shadow_number_of_samples; ++i )
1462 rayToLight.
Init( hitPoint, vectorToLight );
1467 const SFVEC3F disturbed_vector_to_light =
1468 glm::normalize( vectorToLight + unifVector *
1471 rayToLight.
Init( hitPoint, disturbed_vector_to_light );
1479 shadow_att_factor_light -= shadow_inc_factor;
1484 shadow_att_factor_sum += shadow_att_factor_light;
1487 outColor +=
SFVEC4F( objMaterial->
Shade( aRay, aHitInfo, NdotL, diffuseColorObj,
1488 vectorToLight, colorOfLight,
1489 shadow_att_factor_light ),
1499 if( nr_lights_that_can_cast_shadows > 0 )
1502 shadow_att_factor_sum / (
float) ( nr_lights_that_can_cast_shadows * 1.0f ), 0.0f );
1510 outColor = glm::min( outColor,
SFVEC4F( 1.0f ) );
1517 && ( aRecursiveLevel < objMaterial->GetReflectionRecursionCount() ) )
1519 const unsigned int reflection_number_of_samples =
1527 for(
unsigned int i = 0; i < reflection_number_of_samples; ++i )
1533 reflectedRay.
Init( hitPoint, reflectVector );
1538 const SFVEC3F random_reflectVector =
1539 glm::normalize( reflectVector +
1543 reflectedRay.
Init( hitPoint, random_reflectVector );
1547 reflectedHit.
m_tHit = std::numeric_limits<float>::infinity();
1552 shadeHit( aBgColor, reflectedRay, reflectedHit,
false,
1553 aRecursiveLevel + 1, is_testShadow ) *
1556 (1.0f / ( 1.0f + 0.75f * reflectedHit.
m_tHit *
1557 reflectedHit.
m_tHit) ) );
1563 outColor += (sum_color /
SFVEC4F( (
float)reflection_number_of_samples) );
1572 const float airIndex = 1.000293f;
1573 const float glassIndex = 1.49f;
1574 const float air_over_glass = airIndex / glassIndex;
1575 const float glass_over_air = glassIndex / airIndex;
1577 const float refractionRatio = aIsInsideObject?glass_over_air:air_over_glass;
1589 const unsigned int refractions_number_of_samples =
1594 for(
unsigned int i = 0; i < refractions_number_of_samples; ++i )
1600 refractedRay.
Init( startPoint, refractedVector );
1605 const SFVEC3F randomizeRefractedVector =
1606 glm::normalize( refractedVector +
1610 refractedRay.
Init( startPoint, randomizeRefractedVector );
1614 refractedHit.
m_tHit = std::numeric_limits<float>::infinity();
1616 SFVEC4F refractedColor = aBgColor;
1620 refractedColor =
shadeHit( aBgColor, refractedRay, refractedHit,
1621 !aIsInsideObject, aRecursiveLevel + 1,
false );
1624 (1.0f - objTransparency ) *
1628 const SFVEC4F transparency = 1.0f / ( absorbance + 1.0f );
1630 sum_color += refractedColor * transparency;
1634 sum_color += refractedColor;
1638 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * sum_color
1639 /
SFVEC4F( (
float) refractions_number_of_samples );
1643 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * aBgColor;
1654 const float dx = (float) a.x - (
float) b.x;
1655 const float dy = (float) a.y - (
float) b.y;
1656 return hypotf( dx, dy );
1715 constexpr auto hilbert_get_pos = [](
size_t aT,
size_t& aX,
size_t& aY )
1717 static const size_t htab[] = { 0b0010, 0b0100, 0b1100, 0b1001, 0b1111, 0b0101,
1718 0b0001, 0b1000, 0b0000, 0b1010, 0b1110, 0b0111,
1719 0b1101, 0b1011, 0b0011, 0b0110 };
1720 static const size_t size =
sizeof( size_t ) * 8;
1725 for(
size_t i = 0; i < ( size / 2 ); ++i )
1727 size_t abi = aT >> ( size - 2 );
1730 size_t st = htab[( c01 << 2 ) | abi];
1733 yv = ( yv << 1 ) | ( ( st >> 2 ) & 1 );
1734 xv = ( xv << 1 ) | ( st >> 3 );
1741 size_t total_blocks = blocks_x * blocks_y;
1748 hilbert_get_pos( pos++, x, y );
1750 if( x < blocks_x && y < blocks_y )
1765 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
BS::thread_pool thread_pool
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.