35#include "../color_rgb.h"
38#include "../common_ogl/ogl_utils.h"
45 m_postShaderSsao( aCamera )
47 wxLogTrace(
m_logTrace, wxT(
"RENDER_3D_RAYTRACE::RENDER_3D_RAYTRACE" ) );
75 wxLogTrace(
m_logTrace, wxT(
"RENDER_3D_RAYTRACE::~RENDER_3D_RAYTRACE" ) );
105 glDeleteBuffers( 1, &
m_pboId );
152 bool requestRedraw =
false;
161 requestRedraw =
true;
174 if( aStatusReporter )
175 aStatusReporter->
Report(
_(
"Loading..." ) );
178 requestRedraw =
true;
179 Reload( aStatusReporter, aWarningReporter,
false );
188 requestRedraw =
true;
195 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
196 glClearDepth( 1.0f );
197 glClearStencil( 0x00 );
198 glClear( GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
201 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
203 glDisable( GL_STENCIL_TEST );
204 glDisable( GL_LIGHTING );
205 glDisable( GL_COLOR_MATERIAL );
206 glDisable( GL_DEPTH_TEST );
207 glDisable( GL_TEXTURE_2D );
208 glDisable( GL_BLEND );
209 glDisable( GL_MULTISAMPLE );
213 if( requestRedraw || aIsMoving || was_camera_changed )
218 if( aIsMoving || was_camera_changed )
228 glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB,
m_pboId );
231 GLubyte* ptrPBO = (GLubyte *)glMapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB,
239 glUnmapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB );
247 glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB,
m_pboId );
252 GLubyte* ptrPBO = (GLubyte *)glMapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB,
257 render( ptrPBO, aStatusReporter );
260 requestRedraw =
true;
263 glUnmapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB );
269 glClear( GL_COLOR_BUFFER_BIT );
277 glEnable( GL_BLEND );
278 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
279 glEnable( GL_ALPHA_TEST );
281 glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
283 return requestRedraw;
302 GLubyte* tmp_ptrPBO = ptrPBO + 3;
304 for(
unsigned int i = 0; i < nPixels; ++i )
330 wxASSERT_MSG(
false, wxT(
"Invalid state on m_renderState" ) );
349 auto startTime = std::chrono::steady_clock::now();
350 bool breakLoop =
false;
352 std::atomic<size_t> numBlocksRendered( 0 );
353 std::atomic<size_t> currentBlock( 0 );
354 std::atomic<size_t> threadsFinished( 0 );
356 size_t parallelThreadCount = std::min<size_t>(
357 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
360 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
362 std::thread t = std::thread( [&]()
364 for(
size_t iBlock = currentBlock.fetch_add( 1 );
366 iBlock = currentBlock.fetch_add( 1 ) )
368 if( !m_blockPositionsWasProcessed[iBlock] )
370 renderBlockTracing( ptrPBO, iBlock );
372 m_blockPositionsWasProcessed[iBlock] = 1;
376 if( std::chrono::duration_cast<std::chrono::milliseconds>(
377 std::chrono::steady_clock::now() - startTime ).count() > 150 )
388 while( threadsFinished < parallelThreadCount )
389 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
391 m_blockRenderProgressCount += numBlocksRendered;
393 if( aStatusReporter )
395 (
float) ( m_blockRenderProgressCount * 100 )
396 / (
float) m_blockPositions.size() ) );
400 if( m_blockRenderProgressCount >= m_blockPositions.size() )
402 if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
413#define SRGB_GAMA 2.4f
420 const float gammaCorrection = 1.0f /
SRGB_GAMA;
423 return glm::mix( glm::pow( clampedColor,
SFVEC3F(gammaCorrection) ) * 1.055f - 0.055f,
424 clampedColor * 12.92f,
425 glm::lessThan( clampedColor,
SFVEC3F(0.0031308f) ) );
435 return glm::mix( glm::pow( ( aSRGBcolor +
SFVEC3F( 0.055f ) )
436 *
SFVEC3F( 0.94786729857819905213270142180095f ),
438 aSRGBcolor *
SFVEC3F( 0.07739938080495356037151702786378f ),
439 glm::lessThanEqual( aSRGBcolor,
SFVEC3F( 0.04045f ) ) );
446 bool applyColorSpaceConversion )
455 if( applyColorSpaceConversion )
459 ptrPBO[0] = (
unsigned int) glm::clamp( (
int) (
color.r * 255 ), 0, 255 );
460 ptrPBO[1] = (
unsigned int) glm::clamp( (
int) (
color.g * 255 ), 0, 255 );
461 ptrPBO[2] = (
unsigned int) glm::clamp( (
int) (
color.b * 255 ), 0, 255 );
471 aHitPacket[i].
m_HitInfo.
m_tHit = std::numeric_limits<float>::infinity();
488 if( aHitPacket[i].m_hitresult ==
true )
490 aOutHitColor[i] =
shadeHit( bgColorY[y], aRayPkt[i], aHitPacket[i].m_HitInfo,
491 false, 0, is_testShadow );
495 aOutHitColor[i] = bgColorY[y];
513 const RAY& rayAA = aRayPck[i];
516 hitAA.
m_tHit = std::numeric_limits<float>::infinity();
521 const unsigned int idx0y1 = ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 );
522 const unsigned int idx1y1 = ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 );
528 unsigned int nodex1y0 = 0;
531 nodex1y0 = aHitPck_X0Y0[ i + 1 ].m_HitInfo.m_acc_node_info;
533 unsigned int nodex0y1 = 0;
536 nodex0y1 = aHitPck_X0Y0[idx0y1].m_HitInfo.m_acc_node_info;
538 unsigned int nodex1y1 = 0;
545 if( ( ( nodex0y0 == nodex1y0 ) || ( nodex1y0 == 0 ) )
546 && ( ( nodex0y0 == nodex0y1 ) || ( nodex0y1 == 0 ) )
547 && ( ( nodex0y0 == nodex1y1 ) || ( nodex1y1 == 0 ) )
548 && ( nodex0y0 == node_AA_x0y0 ) )
586 if( ( nodex1y0 != 0 ) && ( nodex0y0 != nodex1y0 ) )
589 if( ( nodex0y1 != 0 ) && ( nodex0y0 != nodex0y1 ) && ( nodex1y0 != nodex0y1 ) )
592 if( ( nodex1y1 != 0 ) && ( nodex0y0 != nodex1y1 ) && ( nodex0y1 != nodex1y1 ) &&
593 ( nodex1y0 != nodex1y1 ) )
596 if( (node_AA_x0y0 != 0 ) && ( nodex0y0 != node_AA_x0y0 ) &&
597 ( nodex0y1 != node_AA_x0y0 ) && ( nodex1y0 != node_AA_x0y0 ) &&
598 ( nodex1y1 != node_AA_x0y0 ) )
604 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0,
614 aOutHitColor[i] =
shadeHit( aBgColorY[y], rayAA, hitAA,
false, 0,
623#define DISP_FACTOR 0.075f
645 const float posYfactor = (float) ( blockPosI.y + y ) / (float)
m_windowSize.y;
659 const SFVEC3F& outColor = bgColor[y];
661 const unsigned int yBlockPos = blockPos.y + y;
679 const SFVEC3F& outColor = bgColor[y];
681 const unsigned int yConst = blockPos.x + ( ( y + blockPos.y ) *
m_realBufferSize.x );
685 GLubyte* ptr = &ptrPBO[( yConst + x ) * 4];
718 const SFVEC3F& outColor = bgColor[y];
721 hitColor_AA_X1Y1[i] = outColor;
736 SFVEC3F color_average = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] ) *
SFVEC3F( 0.5f );
738 hitColor_AA_X1Y0[i] = color_average;
739 hitColor_AA_X0Y1[i] = color_average;
740 hitColor_AA_X0Y1_half[i] = color_average;
766 blockRayPck_AA_X1Y1_half, hitColor_AA_X0Y1_half );
771 hitColor_X0Y0[i] = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] + hitColor_AA_X1Y0[i] +
772 hitColor_AA_X0Y1[i] + hitColor_AA_X0Y1_half[i] ) *
778 GLubyte* ptr = &ptrPBO[( blockPos.x + ( blockPos.y *
m_realBufferSize.x ) ) * 4];
793 const SFVEC3F& hColor = hitColor_X0Y0[i];
795 if( hitPacket_X0Y0[i].m_hitresult ==
true )
841 if( aStatusReporter )
842 aStatusReporter->
Report(
_(
"Rendering: Post processing shader" ) );
846 std::atomic<size_t> nextBlock( 0 );
847 std::atomic<size_t> threadsFinished( 0 );
849 size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
851 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
853 std::thread t = std::thread( [&]()
856 y = nextBlock.fetch_add( 1 ) )
858 SFVEC3F* ptr = &m_shaderBuffer[ y * m_realBufferSize.x ];
860 for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
862 *ptr = m_postShaderSsao.Shade( SFVEC2I( x, y ) );
873 while( threadsFinished < parallelThreadCount )
874 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
894 std::atomic<size_t> nextBlock( 0 );
895 std::atomic<size_t> threadsFinished( 0 );
897 size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
899 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
901 std::thread t = std::thread( [&]()
904 y = nextBlock.fetch_add( 1 ) )
906 GLubyte* ptr = &ptrPBO[ y * m_realBufferSize.x * 4 ];
908 for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
910 const SFVEC3F bluredShadeColor = m_postShaderSsao.Blur( SFVEC2I( x, y ) );
913 const SFVEC3F originColor = convertLinearToSRGB(
914 m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) ) );
916 const SFVEC3F originColor =
917 m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) );
919 const SFVEC3F shadedColor = m_postShaderSsao.ApplyShadeColor(
920 SFVEC2I( x, y ), originColor, bluredShadeColor );
922 renderFinalColor( ptr, shadedColor, false );
934 while( threadsFinished < parallelThreadCount )
935 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
950 std::atomic<size_t> nextBlock( 0 );
951 std::atomic<size_t> threadsFinished( 0 );
953 size_t parallelThreadCount = std::min<size_t>(
954 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
957 for(
size_t ii = 0; ii < parallelThreadCount; ++ii )
959 std::thread t = std::thread( [&]()
962 iBlock = nextBlock.fetch_add( 1 ) )
964 const SFVEC2UI& windowPosUI = m_blockPositionsFast[ iBlock ];
965 const SFVEC2I windowsPos = SFVEC2I( windowPosUI.x + m_xoffset,
966 windowPosUI.y + m_yoffset );
968 RAYPACKET blockPacket( m_camera, windowsPos, 4 );
970 HITINFO_PACKET hitPacket[RAYPACKET_RAYS_PER_PACKET];
973 for( HITINFO_PACKET& packet : hitPacket )
975 packet.m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
976 packet.m_HitInfo.m_acc_node_info = 0;
977 packet.m_hitresult = false;
988 const float posYfactor =
989 (float) ( windowsPos.y + y * 4.0f ) / (float) m_windowSize.y;
991 bgColor[y] = (SFVEC3F) m_boardAdapter.m_BgColorTop * SFVEC3F( posYfactor )
992 + (SFVEC3F) m_boardAdapter.m_BgColorBot
993 * ( SFVEC3F( 1.0f ) - SFVEC3F( posYfactor ) );
1000 const SFVEC3F bhColorY = bgColor[i / RAYPACKET_DIM];
1002 if( hitPacket[i].m_hitresult == true )
1004 const SFVEC3F hitColor = shadeHit( bhColorY, blockPacket.m_ray[i],
1005 hitPacket[i].m_HitInfo, false,
1008 hitColorShading[i] = COLOR_RGB( hitColor );
1011 hitColorShading[i] = bhColorY;
1018 const SFVEC3F bgColorY = bgColor[y];
1039 const unsigned int iLT = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 0 ) );
1040 const unsigned int iRT = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 0 ) );
1041 const unsigned int iLB = ( ( x + 0 ) +
RAYPACKET_DIM * ( y + 1 ) );
1042 const unsigned int iRB = ( ( x + 1 ) +
RAYPACKET_DIM * ( y + 1 ) );
1045 const COLOR_RGB& cLT = hitColorShading[ iLT ];
1046 const COLOR_RGB& cRT = hitColorShading[ iRT ];
1047 const COLOR_RGB& cLB = hitColorShading[ iLB ];
1048 const COLOR_RGB& cRB = hitColorShading[ iRB ];
1053 const SFVEC3F& oriLT = blockPacket.m_ray[ iLT ].m_Origin;
1054 const SFVEC3F& oriRB = blockPacket.m_ray[ iRB ].m_Origin;
1056 const SFVEC3F& dirLT = blockPacket.m_ray[ iLT ].m_Dir;
1057 const SFVEC3F& dirRB = blockPacket.m_ray[ iRB ].m_Dir;
1063 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
1065 bool hittedC =
false;
1067 if( ( hitPacket[iLT].m_hitresult ==
true )
1068 || ( hitPacket[iRT].m_hitresult ==
true )
1069 || ( hitPacket[iLB].m_hitresult ==
true )
1070 || ( hitPacket[iRB].m_hitresult ==
true ) )
1072 oriC = ( oriLT + oriRB ) * 0.5f;
1073 dirC = glm::normalize( ( dirLT + dirRB ) * 0.5f );
1077 centerRay.
Init( oriC, dirC );
1079 const unsigned int nodeLT = hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
1080 const unsigned int nodeRT = hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1081 const unsigned int nodeLB = hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1082 const unsigned int nodeRB = hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1088 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
1092 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) && ( nodeLB != nodeRT ) )
1096 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) && ( nodeRB != nodeLT )
1097 && ( nodeRB != nodeRT ) )
1104 false, 0,
false ) );
1108 centerHitInfo.
m_tHit = std::numeric_limits<float>::infinity();
1113 false, 0,
false ) );
1120 const SFVEC3F& oriRT = blockPacket.m_ray[ iRT ].m_Origin;
1121 const SFVEC3F& dirRT = blockPacket.m_ray[ iRT ].m_Dir;
1127 rayLRT.
Init( ( oriLT + oriRT ) * 0.5f,
1128 glm::normalize( ( dirLT + dirRT ) * 0.5f ) );
1131 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
1133 if( hitPacket[iLT].m_hitresult && hitPacket[iRT].m_hitresult
1134 && ( hitPacket[iLT].m_HitInfo.pHitObject
1135 == hitPacket[iRT].m_HitInfo.pHitObject ) )
1137 hitInfoLRT.
pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
1138 hitInfoLRT.
m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
1139 hitPacket[ iRT ].m_HitInfo.m_tHit ) * 0.5f;
1141 glm::normalize( ( hitPacket[ iLT ].m_HitInfo.m_HitNormal +
1142 hitPacket[ iRT ].m_HitInfo.m_HitNormal ) * 0.5f );
1151 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iRT ].m_hitresult )
1153 const unsigned int nodeLT =
1154 hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
1155 const unsigned int nodeRT =
1156 hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1158 bool hittedLRT =
false;
1164 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
1170 false, 0,
false ) );
1173 hitInfoLRT.
m_tHit = std::numeric_limits<float>::infinity();
1193 const SFVEC3F &oriLB = blockPacket.m_ray[ iLB ].m_Origin;
1194 const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
1198 rayLTB.
Init( ( oriLT + oriLB ) * 0.5f,
1199 glm::normalize( ( dirLT + dirLB ) * 0.5f ) );
1202 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1204 if( hitPacket[ iLT ].m_hitresult && hitPacket[ iLB ].m_hitresult
1205 && ( hitPacket[ iLT ].m_HitInfo.pHitObject ==
1206 hitPacket[ iLB ].m_HitInfo.pHitObject ) )
1208 hitInfoLTB.
pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
1209 hitInfoLTB.
m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
1210 hitPacket[ iLB ].m_HitInfo.m_tHit ) * 0.5f;
1212 glm::normalize( ( hitPacket[ iLT ].m_HitInfo.m_HitNormal +
1213 hitPacket[ iLB ].m_HitInfo.m_HitNormal ) * 0.5f );
1221 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iLB ].m_hitresult )
1223 const unsigned int nodeLT =
1224 hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
1225 const unsigned int nodeLB =
1226 hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1228 bool hittedLTB =
false;
1234 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) )
1240 false, 0,
false ) );
1243 hitInfoLTB.
m_tHit = std::numeric_limits<float>::infinity();
1263 rayRTB.
Init( ( oriRT + oriRB ) * 0.5f,
1264 glm::normalize( ( dirRT + dirRB ) * 0.5f ) );
1267 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1269 if( hitPacket[ iRT ].m_hitresult && hitPacket[ iRB ].m_hitresult
1270 && ( hitPacket[ iRT ].m_HitInfo.pHitObject ==
1271 hitPacket[ iRB ].m_HitInfo.pHitObject ) )
1273 hitInfoRTB.
pHitObject = hitPacket[ iRT ].m_HitInfo.pHitObject;
1275 hitInfoRTB.
m_tHit = ( hitPacket[ iRT ].m_HitInfo.m_tHit +
1276 hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
1279 glm::normalize( ( hitPacket[ iRT ].m_HitInfo.m_HitNormal +
1280 hitPacket[ iRB ].m_HitInfo.m_HitNormal ) * 0.5f );
1289 if( hitPacket[ iRT ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1291 const unsigned int nodeRT =
1292 hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1293 const unsigned int nodeRB =
1294 hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1296 bool hittedRTB =
false;
1302 if( ( nodeRB != 0 ) && ( nodeRB != nodeRT ) )
1313 hitInfoRTB.
m_tHit = std::numeric_limits<float>::infinity();
1317 false, 0,
false ) );
1327 const SFVEC3F& oriLB = blockPacket.m_ray[ iLB ].m_Origin;
1328 const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
1332 rayLRB.
Init( ( oriLB + oriRB ) * 0.5f,
1333 glm::normalize( ( dirLB + dirRB ) * 0.5f ) );
1336 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1338 if( hitPacket[iLB].m_hitresult && hitPacket[iRB].m_hitresult
1339 && ( hitPacket[iLB].m_HitInfo.pHitObject ==
1340 hitPacket[iRB].m_HitInfo.pHitObject ) )
1342 hitInfoLRB.
pHitObject = hitPacket[ iLB ].m_HitInfo.pHitObject;
1344 hitInfoLRB.
m_tHit = ( hitPacket[ iLB ].m_HitInfo.m_tHit +
1345 hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
1348 glm::normalize( ( hitPacket[ iLB ].m_HitInfo.m_HitNormal +
1349 hitPacket[ iRB ].m_HitInfo.m_HitNormal ) * 0.5f );
1358 if( hitPacket[ iLB ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1360 const unsigned int nodeLB =
1361 hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1362 const unsigned int nodeRB =
1363 hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1365 bool hittedLRB =
false;
1371 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) )
1378 false, 0,
false ) );
1382 hitInfoLRB.
m_tHit = std::numeric_limits<float>::infinity();
1386 false, 0,
false ) );
1396 if( hitPacket[ iLT ].m_hitresult || hittedC )
1400 rayLTC.
Init( ( oriLT + oriC ) * 0.5f,
1401 glm::normalize( ( dirLT + dirC ) * 0.5f ) );
1404 hitInfoLTC.
m_tHit = std::numeric_limits<float>::infinity();
1406 bool hitted =
false;
1410 else if( hitPacket[ iLT ].m_hitresult )
1411 hitted = hitPacket[ iLT ].m_HitInfo.pHitObject->Intersect(
1423 if( hitPacket[ iRT ].m_hitresult || hittedC )
1427 rayRTC.
Init( ( oriRT + oriC ) * 0.5f,
1428 glm::normalize( ( dirRT + dirC ) * 0.5f ) );
1431 hitInfoRTC.
m_tHit = std::numeric_limits<float>::infinity();
1433 bool hitted =
false;
1437 else if( hitPacket[ iRT ].m_hitresult )
1438 hitted = hitPacket[ iRT ].m_HitInfo.pHitObject->Intersect( rayRTC,
1449 if( hitPacket[ iLB ].m_hitresult || hittedC )
1453 rayLBC.
Init( ( oriLB + oriC ) * 0.5f,
1454 glm::normalize( ( dirLB + dirC ) * 0.5f ) );
1457 hitInfoLBC.
m_tHit = std::numeric_limits<float>::infinity();
1459 bool hitted =
false;
1463 else if( hitPacket[ iLB ].m_hitresult )
1464 hitted = hitPacket[ iLB ].m_HitInfo.pHitObject->Intersect( rayLBC,
1475 if( hitPacket[ iRB ].m_hitresult || hittedC )
1479 rayRBC.
Init( ( oriRB + oriC ) * 0.5f,
1480 glm::normalize( ( dirRB + dirC ) * 0.5f ) );
1483 hitInfoRBC.
m_tHit = std::numeric_limits<float>::infinity();
1485 bool hitted =
false;
1489 else if( hitPacket[ iRB ].m_hitresult )
1490 hitted = hitPacket[ iRB ].m_HitInfo.pHitObject->Intersect( rayRBC,
1535 while( threadsFinished < parallelThreadCount )
1536 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
1540#define USE_EXPERIMENTAL_SOFT_SHADOWS 1
1543 bool aIsInsideObject,
unsigned int aRecursiveLevel,
1544 bool is_testShadow )
const
1547 wxASSERT( objMaterial !=
nullptr );
1551 if( aRecursiveLevel > 7 )
1560#if USE_EXPERIMENTAL_SOFT_SHADOWS
1564 float shadow_att_factor_sum = 0.0f;
1566 unsigned int nr_lights_that_can_cast_shadows = 0;
1574 light->GetLightParameters( hitPoint, vectorToLight, colorOfLight, distToLight );
1577 colorOfLight =
SFVEC3F( 1.0f );
1579 const float NdotL = glm::dot( aHitInfo.
m_HitNormal, vectorToLight );
1583 if( NdotL >= FLT_EPSILON )
1585 float shadow_att_factor_light = 1.0f;
1587 if( is_testShadow && light->GetCastShadows() )
1589 nr_lights_that_can_cast_shadows++;
1590#if USE_EXPERIMENTAL_SOFT_SHADOWS
1592 if( aRecursiveLevel > 0 )
1596 rayToLight.
Init( hitPoint, vectorToLight );
1601 shadow_att_factor_light = 0.0f;
1603#if USE_EXPERIMENTAL_SOFT_SHADOWS
1607 const unsigned int shadow_number_of_samples =
1609 const float shadow_inc_factor = 1.0f / (float) ( shadow_number_of_samples );
1611 for(
unsigned int i = 0; i < shadow_number_of_samples; ++i )
1617 rayToLight.
Init( hitPoint, vectorToLight );
1622 const SFVEC3F disturbed_vector_to_light =
1623 glm::normalize( vectorToLight + unifVector *
1626 rayToLight.
Init( hitPoint, disturbed_vector_to_light );
1634 shadow_att_factor_light -= shadow_inc_factor;
1639 shadow_att_factor_sum += shadow_att_factor_light;
1642 outColor += objMaterial->
Shade( aRay, aHitInfo, NdotL, diffuseColorObj, vectorToLight,
1643 colorOfLight, shadow_att_factor_light );
1652 if( nr_lights_that_can_cast_shadows > 0 )
1655 shadow_att_factor_sum / (
float) ( nr_lights_that_can_cast_shadows * 1.0f ), 0.0f );
1663 outColor = glm::min( outColor,
SFVEC3F( 1.0f ) );
1670 && ( aRecursiveLevel < objMaterial->GetReflectionRecursionCount() ) )
1672 const unsigned int reflection_number_of_samples =
1680 for(
unsigned int i = 0; i < reflection_number_of_samples; ++i )
1686 reflectedRay.
Init( hitPoint, reflectVector );
1691 const SFVEC3F random_reflectVector =
1692 glm::normalize( reflectVector +
1696 reflectedRay.
Init( hitPoint, random_reflectVector );
1700 reflectedHit.
m_tHit = std::numeric_limits<float>::infinity();
1705 shadeHit( aBgColor, reflectedRay, reflectedHit,
false,
1706 aRecursiveLevel + 1, is_testShadow ) *
1709 (1.0f / ( 1.0f + 0.75f * reflectedHit.
m_tHit *
1710 reflectedHit.
m_tHit) ) );
1714 outColor += (sum_color /
SFVEC3F( (
float)reflection_number_of_samples) );
1723 const float airIndex = 1.000293f;
1724 const float glassIndex = 1.49f;
1725 const float air_over_glass = airIndex / glassIndex;
1726 const float glass_over_air = glassIndex / airIndex;
1728 const float refractionRatio = aIsInsideObject?glass_over_air:air_over_glass;
1740 const unsigned int refractions_number_of_samples =
1745 for(
unsigned int i = 0; i < refractions_number_of_samples; ++i )
1751 refractedRay.
Init( startPoint, refractedVector );
1756 const SFVEC3F randomizeRefractedVector =
1757 glm::normalize( refractedVector +
1761 refractedRay.
Init( startPoint, randomizeRefractedVector );
1765 refractedHit.
m_tHit = std::numeric_limits<float>::infinity();
1767 SFVEC3F refractedColor = aBgColor;
1771 refractedColor =
shadeHit( aBgColor, refractedRay, refractedHit,
1772 !aIsInsideObject, aRecursiveLevel + 1,
false );
1775 (1.0f - objTransparency ) *
1779 const SFVEC3F transparency = 1.0f / ( absorbance + 1.0f );
1781 sum_color += refractedColor * transparency;
1785 sum_color += refractedColor;
1789 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * sum_color
1790 /
SFVEC3F( (
float) refractions_number_of_samples );
1794 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * aBgColor;
1811 if( GLEW_ARB_pixel_buffer_object )
1827 glGenBuffersARB( 1, &
m_pboId );
1828 glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB,
m_pboId );
1829 glBufferDataARB( GL_PIXEL_UNPACK_BUFFER_ARB,
m_pboDataSize, 0, GL_STREAM_DRAW_ARB );
1830 glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
1833 wxT(
"RENDER_3D_RAYTRACE:: GLEW_ARB_pixel_buffer_object is supported" ) );
1848 const float dx = (float) a.x - (
float) b.x;
1849 const float dy = (float) a.y - (
float) b.y;
1850 return hypotf( dx, dy );
1906 for(
int x = 0; x < blocks_x; ++x )
1908 for(
int y = 0; y < blocks_y; ++y )
1916 return distance( a, center ) < distance( b, center );
1930 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.
Implement a canvas based on a wxGLCanvas.
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 UpdateSize(const SFVEC2UI &aSize)
void SetPixelData(unsigned int x, unsigned int y, const SFVEC3F &aNormal, const SFVEC3F &aColor, const SFVEC3F &aHitPosition, float aDepth, float aShadowAttFactor)
This is a base class to hold data and functions for render targets.
CAMERA & m_camera
Flag if the opengl specific for this render was already initialized.
std::unique_ptr< BUSY_INDICATOR > CreateBusyIndicator() const
Return a created busy indicator, if a factory has been set, else a null pointer.
bool m_reloadRequested
The window size that this camera is working.
bool m_is_opengl_initialized
BOARD_ADAPTER & m_boardAdapter
DIRECTIONAL_LIGHT * m_cameraLight
bool m_openglSupportsVertexBufferObjects
unsigned int m_converted2dRoundSegmentCount
std::vector< SFVEC2UI > m_blockPositionsFast
SFVEC2UI m_realBufferSize
int GetWaitForEditingTimeOut() override
Give the interface the time (in ms) that it should wait for editing or movements before (this works f...
void restartRenderState()
HITINFO_PACKET * m_firstHitinfo
void initializeNewWindowSize()
POST_SHADER_SSAO m_postShaderSsao
unsigned long int m_renderStartTime
Time that the render starts.
void SetCurWindowSize(const wxSize &aSize) override
Before each render, the canvas will tell the render what is the size of its windows,...
RENDER_3D_RAYTRACE(EDA_3D_CANVAS *aCanvas, BOARD_ADAPTER &aAdapter, CAMERA &aCamera)
BOARD_ITEM * IntersectBoardItem(const RAY &aRay)
CONTAINER_2D * m_outlineBoard2dObjects
std::vector< SFVEC2UI > m_blockPositions
Flag if a position was already processed (cleared each new render).
wxSize m_oldWindowsSize
Encode Morton code positions.
ACCELERATOR_3D * m_accelerator
unsigned int m_convertedDummyBlockCount
void postProcessShading(GLubyte *ptrPBO, REPORTER *aStatusReporter)
size_t m_blockRenderProgressCount
Save the number of blocks progress of the render.
void renderTracing(GLubyte *ptrPBO, REPORTER *aStatusReporter)
SFVEC3F shadeHit(const SFVEC3F &aBgColor, const RAY &aRay, HITINFO &aHitInfo, bool aIsInsideObject, unsigned int aRecursiveLevel, bool is_testShadow) const
SFVEC3F m_backgroundColorTop
void renderFinalColor(GLubyte *ptrPBO, const SFVEC3F &rgbColor, bool applyColorSpaceConversion)
std::vector< int > m_blockPositionsWasProcessed
Encode the Morton code positions (on fast preview mode).
void renderBlockTracing(GLubyte *ptrPBO, signed int iBlock)
void initializeBlockPositions()
void postProcessBlurFinish(GLubyte *ptrPBO, REPORTER *aStatusReporter)
void render(GLubyte *ptrPBO, REPORTER *aStatusReporter)
SFVEC2UI m_fastPreviewModeSize
SFVEC3F m_backgroundColorBottom
Used to see if the windows size changed.
RT_RENDER_STATE m_renderState
State used on quality render.
void renderRayPackets(const SFVEC3F *bgColorY, const RAY *aRayPkt, HITINFO_PACKET *aHitPacket, bool is_testShadow, SFVEC3F *aOutHitColor)
std::list< LIGHT * > m_lights
void renderPreview(GLubyte *ptrPBO)
void Reload(REPORTER *aStatusReporter, REPORTER *aWarningReporter, bool aOnlyLoadCopperAndShapes)
void renderAntiAliasPackets(const SFVEC3F *aBgColorY, const HITINFO_PACKET *aHitPck_X0Y0, const HITINFO_PACKET *aHitPck_AA_X1Y1, const RAY *aRayPck, SFVEC3F *aOutHitColor)
BVH_CONTAINER_2D * m_antioutlineBoard2dObjects
bool Redraw(bool aIsMoving, REPORTER *aStatusReporter, REPORTER *aWarningReporter) override
Redraw the view.
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_RGB BlendColor(const COLOR_RGB &aC1, const COLOR_RGB &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....
void OglDrawBackground(const SFVEC3F &aTopColor, const SFVEC3F &aBotColor)
unsigned GetRunningMicroSecs()
An alternate way to calculate an elapsed time (in microsecondes) to class PROF_COUNTER.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
void RAYPACKET_InitRays_with2DDisplacement(const CAMERA &aCamera, const SFVEC2F &aWindowsPosition, const SFVEC2F &a2DWindowsPosDisplacementFactor, RAY *aRayPck)
#define RAYPACKET_INVMASK
#define RAYPACKET_RAYS_PER_PACKET
SFVEC3F ConvertSRGBToLinear(const SFVEC3F &aSRGBcolor)
static void SetPixel(GLubyte *p, const COLOR_RGB &v)
static void HITINFO_PACKET_init(HITINFO_PACKET *aHitPacket)
static SFVEC3F convertLinearToSRGB(const SFVEC3F &aRGBcolor)
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
@ RT_RENDER_STATE_POST_PROCESS_SHADE
@ RT_RENDER_STATE_POST_PROCESS_BLUR_AND_FINISH
@ RT_RENDER_STATE_TRACING
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