2019-06-19 14:18:30 +00:00
|
|
|
/*
|
2016-07-19 17:35:25 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2020-04-25 14:38:41 +00:00
|
|
|
* Copyright (C) 2015-2020 Mario Luzeiro <mrluzeiro@ua.pt>
|
2023-11-20 18:57:41 +00:00
|
|
|
* Copyright (C) 2015-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-07-19 17:35:25 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2020-11-10 23:24:01 +00:00
|
|
|
#include <gal/opengl/kiglew.h> // Must be included first
|
|
|
|
|
2019-05-14 04:36:34 +00:00
|
|
|
#include <algorithm>
|
2018-09-21 04:23:15 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
2019-05-14 04:36:34 +00:00
|
|
|
#include <thread>
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "render_3d_raytrace.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
#include "mortoncodes.h"
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "../color_rgb.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
#include "3d_fastmath.h"
|
|
|
|
#include "3d_math.h"
|
|
|
|
#include "../common_ogl/ogl_utils.h"
|
2023-09-07 11:22:10 +00:00
|
|
|
#include <core/profile.h> // To use GetRunningMicroSecs or another profiling utility
|
2021-06-03 12:11:15 +00:00
|
|
|
#include <wx/log.h>
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-09-30 20:25:39 +00:00
|
|
|
|
2021-08-16 14:28:46 +00:00
|
|
|
RENDER_3D_RAYTRACE::RENDER_3D_RAYTRACE( EDA_3D_CANVAS* aCanvas, BOARD_ADAPTER& aAdapter, CAMERA& aCamera ) :
|
|
|
|
RENDER_3D_BASE( aCanvas, aAdapter, aCamera ),
|
2021-01-07 19:33:43 +00:00
|
|
|
m_postShaderSsao( aCamera )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxLogTrace( m_logTrace, wxT( "RENDER_3D_RAYTRACE::RENDER_3D_RAYTRACE" ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_openglSupportsVertexBufferObjects = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
m_pboId = GL_NONE;
|
|
|
|
m_pboDataSize = 0;
|
2021-01-02 21:05:29 +00:00
|
|
|
m_accelerator = nullptr;
|
2021-01-07 19:33:43 +00:00
|
|
|
m_convertedDummyBlockCount = 0;
|
|
|
|
m_converted2dRoundSegmentCount = 0;
|
2016-07-19 17:35:25 +00:00
|
|
|
m_oldWindowsSize.x = 0;
|
|
|
|
m_oldWindowsSize.y = 0;
|
2021-01-02 21:05:29 +00:00
|
|
|
m_outlineBoard2dObjects = nullptr;
|
|
|
|
m_antioutlineBoard2dObjects = nullptr;
|
|
|
|
m_firstHitinfo = nullptr;
|
|
|
|
m_shaderBuffer = nullptr;
|
2021-01-07 19:33:43 +00:00
|
|
|
m_cameraLight = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
m_xoffset = 0;
|
|
|
|
m_yoffset = 0;
|
|
|
|
|
|
|
|
m_isPreview = false;
|
2021-01-07 19:33:43 +00:00
|
|
|
m_renderState = RT_RENDER_STATE_MAX; // Set to an initial invalid state
|
|
|
|
m_renderStartTime = 0;
|
|
|
|
m_blockRenderProgressCount = 0;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
RENDER_3D_RAYTRACE::~RENDER_3D_RAYTRACE()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxLogTrace( m_logTrace, wxT( "RENDER_3D_RAYTRACE::~RENDER_3D_RAYTRACE" ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
delete m_accelerator;
|
2021-01-02 21:05:29 +00:00
|
|
|
m_accelerator = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
delete m_outlineBoard2dObjects;
|
2021-01-02 21:05:29 +00:00
|
|
|
m_outlineBoard2dObjects = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-10-29 12:48:45 +00:00
|
|
|
delete m_antioutlineBoard2dObjects;
|
2021-01-02 21:05:29 +00:00
|
|
|
m_antioutlineBoard2dObjects = nullptr;
|
2020-10-29 12:48:45 +00:00
|
|
|
|
2017-03-08 08:12:16 +00:00
|
|
|
delete[] m_shaderBuffer;
|
2021-01-02 21:05:29 +00:00
|
|
|
m_shaderBuffer = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
deletePbo();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
int RENDER_3D_RAYTRACE::GetWaitForEditingTimeOut()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2023-11-20 18:57:41 +00:00
|
|
|
return 200; // ms
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
void RENDER_3D_RAYTRACE::deletePbo()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Delete PBO if it was created
|
2021-01-07 19:33:43 +00:00
|
|
|
if( m_openglSupportsVertexBufferObjects )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( glIsBufferARB( m_pboId ) )
|
|
|
|
glDeleteBuffers( 1, &m_pboId );
|
|
|
|
|
|
|
|
m_pboId = GL_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void RENDER_3D_RAYTRACE::SetCurWindowSize( const wxSize& aSize )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( m_windowSize != aSize )
|
|
|
|
{
|
|
|
|
m_windowSize = aSize;
|
|
|
|
glViewport( 0, 0, m_windowSize.x, m_windowSize.y );
|
|
|
|
|
|
|
|
initializeNewWindowSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
void RENDER_3D_RAYTRACE::restartRenderState()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
m_renderStartTime = GetRunningMicroSecs();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_renderState = RT_RENDER_STATE_TRACING;
|
|
|
|
m_blockRenderProgressCount = 0;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_postShaderSsao.InitFrame();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
m_blockPositionsWasProcessed.resize( m_blockPositions.size() );
|
|
|
|
|
|
|
|
// Mark the blocks not processed yet
|
2020-12-20 18:23:50 +00:00
|
|
|
std::fill( m_blockPositionsWasProcessed.begin(), m_blockPositionsWasProcessed.end(), 0 );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
static inline void SetPixel( GLubyte* p, const COLOR_RGB& v )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
p[0] = v.c[0];
|
|
|
|
p[1] = v.c[1];
|
|
|
|
p[2] = v.c[2];
|
|
|
|
p[3] = 255;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool RENDER_3D_RAYTRACE::Redraw( bool aIsMoving, REPORTER* aStatusReporter,
|
|
|
|
REPORTER* aWarningReporter )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
bool requestRedraw = false;
|
|
|
|
|
|
|
|
// Initialize openGL if need
|
|
|
|
if( !m_is_opengl_initialized )
|
|
|
|
{
|
|
|
|
if( !initializeOpenGL() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//aIsMoving = true;
|
|
|
|
requestRedraw = true;
|
|
|
|
|
|
|
|
// It will assign the first time the windows size, so it will now
|
|
|
|
// revert to preview mode the first time the Redraw is called
|
|
|
|
m_oldWindowsSize = m_windowSize;
|
2021-01-07 19:33:43 +00:00
|
|
|
initializeBlockPositions();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-12 13:08:30 +00:00
|
|
|
std::unique_ptr<BUSY_INDICATOR> busy = CreateBusyIndicator();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Reload board if it was requested
|
|
|
|
if( m_reloadRequested )
|
|
|
|
{
|
2020-06-03 23:00:40 +00:00
|
|
|
if( aStatusReporter )
|
|
|
|
aStatusReporter->Report( _( "Loading..." ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
//aIsMoving = true;
|
|
|
|
requestRedraw = true;
|
2020-09-04 00:00:56 +00:00
|
|
|
Reload( aStatusReporter, aWarningReporter, false );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Recalculate constants if windows size was changed
|
|
|
|
if( m_windowSize != m_oldWindowsSize )
|
|
|
|
{
|
|
|
|
m_oldWindowsSize = m_windowSize;
|
|
|
|
aIsMoving = true;
|
|
|
|
requestRedraw = true;
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
initializeBlockPositions();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Clear buffers
|
|
|
|
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
|
|
|
|
glClearDepth( 1.0f );
|
|
|
|
glClearStencil( 0x00 );
|
|
|
|
glClear( GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
|
|
|
|
|
|
|
|
// 4-byte pixel alignment
|
|
|
|
glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
|
|
|
|
|
|
|
|
glDisable( GL_STENCIL_TEST );
|
|
|
|
glDisable( GL_LIGHTING );
|
|
|
|
glDisable( GL_COLOR_MATERIAL );
|
|
|
|
glDisable( GL_DEPTH_TEST );
|
|
|
|
glDisable( GL_TEXTURE_2D );
|
|
|
|
glDisable( GL_BLEND );
|
2020-03-28 19:59:41 +00:00
|
|
|
glDisable( GL_MULTISAMPLE );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-03-26 14:05:20 +00:00
|
|
|
const bool was_camera_changed = m_camera.ParametersChanged();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( requestRedraw || aIsMoving || was_camera_changed )
|
2021-01-07 19:33:43 +00:00
|
|
|
m_renderState = RT_RENDER_STATE_MAX; // Set to an invalid state,
|
|
|
|
// so it will restart again latter
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// This will only render if need, otherwise it will redraw the PBO on the screen again
|
|
|
|
if( aIsMoving || was_camera_changed )
|
|
|
|
{
|
2020-12-20 18:23:50 +00:00
|
|
|
// Set head light (camera view light) with the opposite direction of the camera
|
2021-01-07 19:33:43 +00:00
|
|
|
if( m_cameraLight )
|
|
|
|
m_cameraLight->SetDirection( -m_camera.GetDir() );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
OglDrawBackground( SFVEC3F( m_boardAdapter.m_BgColorTop),
|
|
|
|
SFVEC3F( m_boardAdapter.m_BgColorBot) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Bind PBO
|
|
|
|
glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, m_pboId );
|
|
|
|
|
|
|
|
// Get the PBO pixel pointer to write the data
|
2021-01-02 21:05:29 +00:00
|
|
|
GLubyte* ptrPBO = (GLubyte *)glMapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB,
|
2016-07-19 17:35:25 +00:00
|
|
|
GL_WRITE_ONLY_ARB );
|
|
|
|
|
|
|
|
if( ptrPBO )
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
renderPreview( ptrPBO );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// release pointer to mapping buffer, this initialize the coping to PBO
|
|
|
|
glUnmapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB );
|
|
|
|
}
|
|
|
|
|
|
|
|
glWindowPos2i( m_xoffset, m_yoffset );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Bind PBO
|
|
|
|
glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, m_pboId );
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( m_renderState != RT_RENDER_STATE_FINISH )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Get the PBO pixel pointer to write the data
|
2021-01-02 21:05:29 +00:00
|
|
|
GLubyte* ptrPBO = (GLubyte *)glMapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB,
|
2016-07-19 17:35:25 +00:00
|
|
|
GL_WRITE_ONLY_ARB );
|
|
|
|
|
|
|
|
if( ptrPBO )
|
|
|
|
{
|
2020-06-03 23:00:40 +00:00
|
|
|
render( ptrPBO, aStatusReporter );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( m_renderState != RT_RENDER_STATE_FINISH )
|
2016-07-19 17:35:25 +00:00
|
|
|
requestRedraw = true;
|
|
|
|
|
|
|
|
// release pointer to mapping buffer, this initialize the coping to PBO
|
2020-12-20 18:23:50 +00:00
|
|
|
glUnmapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( m_renderState == RT_RENDER_STATE_FINISH )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
glClear( GL_COLOR_BUFFER_BIT );
|
|
|
|
}
|
|
|
|
|
|
|
|
glWindowPos2i( m_xoffset, m_yoffset );
|
|
|
|
}
|
|
|
|
|
|
|
|
// This way it will blend the progress rendering with the last buffer. eg:
|
|
|
|
// if it was called after a openGL.
|
|
|
|
glEnable( GL_BLEND );
|
|
|
|
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
|
|
|
|
glEnable( GL_ALPHA_TEST );
|
2020-12-20 18:23:50 +00:00
|
|
|
glDrawPixels( m_realBufferSize.x, m_realBufferSize.y, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
|
2016-07-19 17:35:25 +00:00
|
|
|
glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
|
|
|
|
|
|
|
|
return requestRedraw;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void RENDER_3D_RAYTRACE::render( GLubyte* ptrPBO, REPORTER* aStatusReporter )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
if( ( m_renderState == RT_RENDER_STATE_FINISH ) || ( m_renderState >= RT_RENDER_STATE_MAX ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
restartRenderState();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( m_cameraLight )
|
|
|
|
m_cameraLight->SetDirection( -m_camera.GetDir() );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-08-30 20:30:18 +00:00
|
|
|
if( m_boardAdapter.m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Set all pixels of PBO transparent (Alpha to 0)
|
|
|
|
// This way it will draw the full buffer but only shows the updated (
|
|
|
|
// already calculated) squares
|
|
|
|
unsigned int nPixels = m_realBufferSize.x * m_realBufferSize.y;
|
2021-01-02 21:05:29 +00:00
|
|
|
GLubyte* tmp_ptrPBO = ptrPBO + 3; // PBO is RGBA
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( unsigned int i = 0; i < nPixels; ++i )
|
|
|
|
{
|
|
|
|
*tmp_ptrPBO = 0;
|
|
|
|
tmp_ptrPBO += 4; // PBO is RGBA
|
|
|
|
}
|
|
|
|
}
|
2016-11-26 14:38:51 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_backgroundColorTop = ConvertSRGBToLinear( (SFVEC3F)m_boardAdapter.m_BgColorTop );
|
|
|
|
m_backgroundColorBottom = ConvertSRGBToLinear( (SFVEC3F)m_boardAdapter.m_BgColorBot );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
switch( m_renderState )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
case RT_RENDER_STATE_TRACING:
|
2021-01-07 19:33:43 +00:00
|
|
|
renderTracing( ptrPBO, aStatusReporter );
|
2016-07-19 17:35:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_RENDER_STATE_POST_PROCESS_SHADE:
|
2021-01-07 19:33:43 +00:00
|
|
|
postProcessShading( ptrPBO, aStatusReporter );
|
2016-07-19 17:35:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_RENDER_STATE_POST_PROCESS_BLUR_AND_FINISH:
|
2021-01-07 19:33:43 +00:00
|
|
|
postProcessBlurFinish( ptrPBO, aStatusReporter );
|
2016-07-19 17:35:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-02-04 20:21:07 +00:00
|
|
|
wxASSERT_MSG( false, wxT( "Invalid state on m_renderState" ) );
|
2021-01-07 19:33:43 +00:00
|
|
|
restartRenderState();
|
2016-07-19 17:35:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( aStatusReporter && ( m_renderState == RT_RENDER_STATE_FINISH ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Calculation time in seconds
|
2021-01-07 19:33:43 +00:00
|
|
|
const double elapsed_time = (double)( GetRunningMicroSecs() - m_renderStartTime ) / 1e6;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
aStatusReporter->Report( wxString::Format( _( "Rendering time %.3f s" ), elapsed_time ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
void RENDER_3D_RAYTRACE::renderTracing( GLubyte* ptrPBO, REPORTER* aStatusReporter )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
m_isPreview = false;
|
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
auto startTime = std::chrono::steady_clock::now();
|
2016-08-28 06:05:49 +00:00
|
|
|
bool breakLoop = false;
|
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
std::atomic<size_t> numBlocksRendered( 0 );
|
|
|
|
std::atomic<size_t> currentBlock( 0 );
|
|
|
|
std::atomic<size_t> threadsFinished( 0 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
size_t parallelThreadCount = std::min<size_t>(
|
|
|
|
std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
|
|
|
|
m_blockPositions.size() );
|
2020-12-20 18:23:50 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
for( size_t ii = 0; ii < parallelThreadCount; ++ii )
|
|
|
|
{
|
|
|
|
std::thread t = std::thread( [&]()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
for( size_t iBlock = currentBlock.fetch_add( 1 );
|
2020-12-12 17:29:11 +00:00
|
|
|
iBlock < m_blockPositions.size() && !breakLoop;
|
|
|
|
iBlock = currentBlock.fetch_add( 1 ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
if( !m_blockPositionsWasProcessed[iBlock] )
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
renderBlockTracing( ptrPBO, iBlock );
|
2018-09-21 04:23:15 +00:00
|
|
|
numBlocksRendered++;
|
|
|
|
m_blockPositionsWasProcessed[iBlock] = 1;
|
|
|
|
|
|
|
|
// Check if it spend already some time render and request to exit
|
|
|
|
// to display the progress
|
|
|
|
if( std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now() - startTime ).count() > 150 )
|
|
|
|
breakLoop = true;
|
|
|
|
}
|
2016-08-28 06:05:49 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
threadsFinished++;
|
|
|
|
} );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
t.detach();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
while( threadsFinished < parallelThreadCount )
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_blockRenderProgressCount += numBlocksRendered;
|
2016-08-28 06:05:49 +00:00
|
|
|
|
2020-06-03 23:00:40 +00:00
|
|
|
if( aStatusReporter )
|
|
|
|
aStatusReporter->Report( wxString::Format( _( "Rendering: %.0f %%" ),
|
2021-01-07 19:33:43 +00:00
|
|
|
(float) ( m_blockRenderProgressCount * 100 )
|
2020-12-20 18:23:50 +00:00
|
|
|
/ (float) m_blockPositions.size() ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Check if it finish the rendering and if should continue to a post processing
|
|
|
|
// or mark it as finished
|
2021-01-07 19:33:43 +00:00
|
|
|
if( m_blockRenderProgressCount >= m_blockPositions.size() )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-08-30 20:30:18 +00:00
|
|
|
if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
|
2021-01-07 19:33:43 +00:00
|
|
|
m_renderState = RT_RENDER_STATE_POST_PROCESS_SHADE;
|
2016-07-19 17:35:25 +00:00
|
|
|
else
|
2021-01-07 19:33:43 +00:00
|
|
|
m_renderState = RT_RENDER_STATE_FINISH;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2016-09-30 20:25:39 +00:00
|
|
|
#ifdef USE_SRGB_SPACE
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
/// @todo This should be removed in future when KiCad supports a greater version of glm lib.
|
2016-11-26 14:38:51 +00:00
|
|
|
#define SRGB_GAMA 2.4f
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2016-09-30 20:25:39 +00:00
|
|
|
// This function implements the conversion from linear RGB to sRGB
|
|
|
|
// https://github.com/g-truc/glm/blob/master/glm/gtc/color_space.inl#L12
|
2020-12-20 18:23:50 +00:00
|
|
|
static SFVEC3F convertLinearToSRGB( const SFVEC3F& aRGBcolor )
|
2016-09-30 20:25:39 +00:00
|
|
|
{
|
2016-11-26 14:38:51 +00:00
|
|
|
const float gammaCorrection = 1.0f / SRGB_GAMA;
|
2020-12-20 18:23:50 +00:00
|
|
|
const SFVEC3F clampedColor = glm::clamp( aRGBcolor, SFVEC3F( 0.0f ), SFVEC3F( 1.0f ) );
|
2016-09-30 20:25:39 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
return glm::mix( glm::pow( clampedColor, SFVEC3F(gammaCorrection) ) * 1.055f - 0.055f,
|
|
|
|
clampedColor * 12.92f,
|
|
|
|
glm::lessThan( clampedColor, SFVEC3F(0.0031308f) ) );
|
2016-09-30 20:25:39 +00:00
|
|
|
}
|
2016-11-26 14:38:51 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2016-11-26 14:38:51 +00:00
|
|
|
// This function implements the conversion from sRGB to linear RGB
|
|
|
|
// https://github.com/g-truc/glm/blob/master/glm/gtc/color_space.inl#L35
|
2020-12-20 18:23:50 +00:00
|
|
|
SFVEC3F ConvertSRGBToLinear( const SFVEC3F& aSRGBcolor )
|
2016-11-26 14:38:51 +00:00
|
|
|
{
|
|
|
|
const float gammaCorrection = SRGB_GAMA;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
return glm::mix( glm::pow( ( aSRGBcolor + SFVEC3F( 0.055f ) )
|
|
|
|
* SFVEC3F( 0.94786729857819905213270142180095f ),
|
|
|
|
SFVEC3F( gammaCorrection ) ),
|
|
|
|
aSRGBcolor * SFVEC3F( 0.07739938080495356037151702786378f ),
|
|
|
|
glm::lessThanEqual( aSRGBcolor, SFVEC3F( 0.04045f ) ) );
|
2016-11-26 14:38:51 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 20:25:39 +00:00
|
|
|
#endif
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
void RENDER_3D_RAYTRACE::renderFinalColor( GLubyte* ptrPBO, const SFVEC3F& rgbColor,
|
2021-01-02 21:05:29 +00:00
|
|
|
bool applyColorSpaceConversion )
|
2016-09-30 20:25:39 +00:00
|
|
|
{
|
|
|
|
SFVEC3F color = rgbColor;
|
|
|
|
|
|
|
|
#ifdef USE_SRGB_SPACE
|
2020-12-20 18:23:50 +00:00
|
|
|
/// @note This should be used in future when the KiCad support a greater version of glm lib.
|
2016-09-30 20:25:39 +00:00
|
|
|
// if( applyColorSpaceConversion )
|
|
|
|
// rgbColor = glm::convertLinearToSRGB( rgbColor );
|
|
|
|
|
|
|
|
if( applyColorSpaceConversion )
|
|
|
|
color = convertLinearToSRGB( rgbColor );
|
|
|
|
#endif
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
ptrPBO[0] = (unsigned int) glm::clamp( (int) ( color.r * 255 ), 0, 255 );
|
|
|
|
ptrPBO[1] = (unsigned int) glm::clamp( (int) ( color.g * 255 ), 0, 255 );
|
|
|
|
ptrPBO[2] = (unsigned int) glm::clamp( (int) ( color.b * 255 ), 0, 255 );
|
2016-09-30 20:25:39 +00:00
|
|
|
ptrPBO[3] = 255;
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
static void HITINFO_PACKET_init( HITINFO_PACKET* aHitPacket )
|
2016-10-22 22:37:13 +00:00
|
|
|
{
|
|
|
|
// Initialize hitPacket with a "not hit" information
|
|
|
|
for( unsigned int i = 0; i < RAYPACKET_RAYS_PER_PACKET; ++i )
|
|
|
|
{
|
|
|
|
aHitPacket[i].m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
aHitPacket[i].m_HitInfo.m_acc_node_info = 0;
|
|
|
|
aHitPacket[i].m_hitresult = false;
|
2018-04-11 17:59:28 +00:00
|
|
|
aHitPacket[i].m_HitInfo.m_HitNormal = SFVEC3F( 0.0f );
|
2016-10-22 22:37:13 +00:00
|
|
|
aHitPacket[i].m_HitInfo.m_ShadowFactor = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
void RENDER_3D_RAYTRACE::renderRayPackets( const SFVEC3F* bgColorY, const RAY* aRayPkt,
|
2021-01-02 21:05:29 +00:00
|
|
|
HITINFO_PACKET* aHitPacket, bool is_testShadow,
|
|
|
|
SFVEC3F* aOutHitColor )
|
2016-10-22 22:37:13 +00:00
|
|
|
{
|
|
|
|
for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
|
|
|
|
{
|
|
|
|
for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
|
|
|
|
{
|
|
|
|
if( aHitPacket[i].m_hitresult == true )
|
|
|
|
{
|
2020-12-20 18:23:50 +00:00
|
|
|
aOutHitColor[i] = shadeHit( bgColorY[y], aRayPkt[i], aHitPacket[i].m_HitInfo,
|
|
|
|
false, 0, is_testShadow );
|
2016-10-22 22:37:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aOutHitColor[i] = bgColorY[y];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-13 16:23:09 +00:00
|
|
|
void RENDER_3D_RAYTRACE::renderAntiAliasPackets( const SFVEC3F* aBgColorY,
|
|
|
|
const HITINFO_PACKET* aHitPck_X0Y0,
|
|
|
|
const HITINFO_PACKET* aHitPck_AA_X1Y1,
|
|
|
|
const RAY* aRayPck, SFVEC3F* aOutHitColor )
|
2016-10-22 22:37:13 +00:00
|
|
|
{
|
2021-08-30 20:30:18 +00:00
|
|
|
const bool is_testShadow = m_boardAdapter.m_Cfg->m_Render.raytrace_shadows;
|
2016-10-22 22:37:13 +00:00
|
|
|
|
|
|
|
for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
|
|
|
|
{
|
|
|
|
for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const RAY& rayAA = aRayPck[i];
|
2016-10-22 22:37:13 +00:00
|
|
|
|
|
|
|
HITINFO hitAA;
|
|
|
|
hitAA.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
hitAA.m_acc_node_info = 0;
|
|
|
|
|
|
|
|
bool hitted = false;
|
|
|
|
|
|
|
|
const unsigned int idx0y1 = ( x + 0 ) + RAYPACKET_DIM * ( y + 1 );
|
|
|
|
const unsigned int idx1y1 = ( x + 1 ) + RAYPACKET_DIM * ( y + 1 );
|
|
|
|
|
|
|
|
// Gets the node info from the hit.
|
|
|
|
const unsigned int nodex0y0 = aHitPck_X0Y0[ i ].m_HitInfo.m_acc_node_info;
|
|
|
|
const unsigned int node_AA_x0y0 = aHitPck_AA_X1Y1[ i ].m_HitInfo.m_acc_node_info;
|
|
|
|
|
|
|
|
unsigned int nodex1y0 = 0;
|
|
|
|
|
|
|
|
if( x < (RAYPACKET_DIM - 1) )
|
|
|
|
nodex1y0 = aHitPck_X0Y0[ i + 1 ].m_HitInfo.m_acc_node_info;
|
|
|
|
|
|
|
|
unsigned int nodex0y1 = 0;
|
|
|
|
|
2021-01-13 16:23:09 +00:00
|
|
|
if( y < ( RAYPACKET_DIM - 1 ) && idx0y1 < RAYPACKET_RAYS_PER_PACKET )
|
|
|
|
nodex0y1 = aHitPck_X0Y0[idx0y1].m_HitInfo.m_acc_node_info;
|
2016-10-22 22:37:13 +00:00
|
|
|
|
|
|
|
unsigned int nodex1y1 = 0;
|
|
|
|
|
2021-01-13 16:23:09 +00:00
|
|
|
if( ( x < ( RAYPACKET_DIM - 1 ) ) && ( y < ( RAYPACKET_DIM - 1 ) )
|
|
|
|
&& idx1y1 < RAYPACKET_RAYS_PER_PACKET )
|
2020-12-12 17:29:11 +00:00
|
|
|
nodex1y1 = aHitPck_X0Y0[idx1y1].m_HitInfo.m_acc_node_info;
|
2016-10-22 22:37:13 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
// If all notes are equal we assume there was no change on the object hits.
|
|
|
|
if( ( ( nodex0y0 == nodex1y0 ) || ( nodex1y0 == 0 ) )
|
|
|
|
&& ( ( nodex0y0 == nodex0y1 ) || ( nodex0y1 == 0 ) )
|
|
|
|
&& ( ( nodex0y0 == nodex1y1 ) || ( nodex1y1 == 0 ) )
|
|
|
|
&& ( nodex0y0 == node_AA_x0y0 ) )
|
2016-10-22 22:37:13 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
/// @todo Either get rid of the if statement above or do something with the
|
|
|
|
/// commented out code below.
|
2016-10-22 22:37:13 +00:00
|
|
|
// Option 1
|
|
|
|
// This option will give a very good quality on reflections (slow)
|
|
|
|
/*
|
|
|
|
if( m_accelerator->Intersect( rayAA, hitAA, nodex0y0 ) )
|
|
|
|
{
|
|
|
|
aOutHitColor[i] += shadeHit( aBgColorY[y], rayAA, hitAA, false, 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( m_accelerator->Intersect( rayAA, hitAA ) )
|
|
|
|
aOutHitColor[i] += shadeHit( aBgColorY[y], rayAA, hitAA, false, 0 );
|
|
|
|
else
|
|
|
|
aOutHitColor[i] += hitColor[i];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Option 2
|
|
|
|
// Trace again with the same node,
|
|
|
|
// then if miss just give the same color as before
|
|
|
|
//if( m_accelerator->Intersect( rayAA, hitAA, nodex0y0 ) )
|
|
|
|
// aOutHitColor[i] += shadeHit( aBgColorY[y], rayAA, hitAA, false, 0 );
|
|
|
|
|
2016-11-26 14:38:51 +00:00
|
|
|
// Option 3
|
|
|
|
// Use same color
|
2016-10-22 22:37:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Try to intersect the different nodes
|
|
|
|
// It tests the possible combination of hitted or not hitted points
|
|
|
|
// This will try to get the best hit for this ray
|
|
|
|
|
|
|
|
if( nodex0y0 != 0 )
|
|
|
|
hitted |= m_accelerator->Intersect( rayAA, hitAA, nodex0y0 );
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( nodex1y0 != 0 ) && ( nodex0y0 != nodex1y0 ) )
|
2016-10-22 22:37:13 +00:00
|
|
|
hitted |= m_accelerator->Intersect( rayAA, hitAA, nodex1y0 );
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( nodex0y1 != 0 ) && ( nodex0y0 != nodex0y1 ) && ( nodex1y0 != nodex0y1 ) )
|
2016-10-22 22:37:13 +00:00
|
|
|
hitted |= m_accelerator->Intersect( rayAA, hitAA, nodex0y1 );
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( nodex1y1 != 0 ) && ( nodex0y0 != nodex1y1 ) && ( nodex0y1 != nodex1y1 ) &&
|
2016-10-22 22:37:13 +00:00
|
|
|
( nodex1y0 != nodex1y1 ) )
|
|
|
|
hitted |= m_accelerator->Intersect( rayAA, hitAA, nodex1y1 );
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( (node_AA_x0y0 != 0 ) && ( nodex0y0 != node_AA_x0y0 ) &&
|
|
|
|
( nodex0y1 != node_AA_x0y0 ) && ( nodex1y0 != node_AA_x0y0 ) &&
|
2016-10-22 22:37:13 +00:00
|
|
|
( nodex1y1 != node_AA_x0y0 ) )
|
|
|
|
hitted |= m_accelerator->Intersect( rayAA, hitAA, node_AA_x0y0 );
|
|
|
|
|
|
|
|
if( hitted )
|
|
|
|
{
|
|
|
|
// If we got any result, shade it
|
2020-12-12 17:29:11 +00:00
|
|
|
aOutHitColor[i] = shadeHit( aBgColorY[y], rayAA, hitAA, false, 0,
|
|
|
|
is_testShadow );
|
2016-10-22 22:37:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Note: There are very few cases that will end on this situation
|
|
|
|
// so it is not so expensive to trace a single ray from the beginning
|
|
|
|
|
|
|
|
// It was missed the 'last nodes' so, trace a ray from the beginning
|
|
|
|
if( m_accelerator->Intersect( rayAA, hitAA ) )
|
2020-12-12 17:29:11 +00:00
|
|
|
aOutHitColor[i] = shadeHit( aBgColorY[y], rayAA, hitAA, false, 0,
|
|
|
|
is_testShadow );
|
2016-10-22 22:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
#define DISP_FACTOR 0.075f
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
void RENDER_3D_RAYTRACE::renderBlockTracing( GLubyte* ptrPBO, signed int iBlock )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Initialize ray packets
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC2UI& blockPos = m_blockPositions[iBlock];
|
2020-12-20 18:23:50 +00:00
|
|
|
const SFVEC2I blockPosI = SFVEC2I( blockPos.x + m_xoffset, blockPos.y + m_yoffset );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
RAYPACKET blockPacket( m_camera, (SFVEC2F) blockPosI + SFVEC2F( DISP_FACTOR, DISP_FACTOR ),
|
|
|
|
SFVEC2F( DISP_FACTOR, DISP_FACTOR ) /* Displacement random factor */ );
|
2020-03-26 14:05:20 +00:00
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
HITINFO_PACKET hitPacket_X0Y0[RAYPACKET_RAYS_PER_PACKET];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
HITINFO_PACKET_init( hitPacket_X0Y0 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Calculate background gradient color
|
|
|
|
SFVEC3F bgColor[RAYPACKET_DIM];// Store a vertical gradient color
|
|
|
|
|
|
|
|
for( unsigned int y = 0; y < RAYPACKET_DIM; ++y )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const float posYfactor = (float) ( blockPosI.y + y ) / (float) m_windowSize.y;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
bgColor[y] = m_backgroundColorTop * SFVEC3F(posYfactor) +
|
|
|
|
m_backgroundColorBottom * ( SFVEC3F(1.0f) - SFVEC3F(posYfactor) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Intersect ray packets (calculate the intersection with rays and objects)
|
2016-10-22 22:37:13 +00:00
|
|
|
if( !m_accelerator->Intersect( blockPacket, hitPacket_X0Y0 ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// If block is empty then set shades and continue
|
2021-08-30 20:30:18 +00:00
|
|
|
if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
for( unsigned int y = 0; y < RAYPACKET_DIM; ++y )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& outColor = bgColor[y];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
const unsigned int yBlockPos = blockPos.y + y;
|
|
|
|
|
|
|
|
for( unsigned int x = 0; x < RAYPACKET_DIM; ++x )
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
m_postShaderSsao.SetPixelData( blockPos.x + x, yBlockPos,
|
|
|
|
SFVEC3F( 0.0f ), outColor,
|
|
|
|
SFVEC3F( 0.0f ), 0, 1.0f );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will set the output color to be displayed
|
2021-08-30 20:30:18 +00:00
|
|
|
// If post processing is enabled, it will not reflect the final result (as the final
|
|
|
|
// color will be computed on post processing) but it is used for report progress
|
|
|
|
const bool isFinalColor = !m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing;
|
2016-09-30 20:25:39 +00:00
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
for( unsigned int y = 0; y < RAYPACKET_DIM; ++y )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& outColor = bgColor[y];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const unsigned int yConst = blockPos.x + ( ( y + blockPos.y ) * m_realBufferSize.x );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( unsigned int x = 0; x < RAYPACKET_DIM; ++x )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
GLubyte* ptr = &ptrPBO[( yConst + x ) * 4];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
renderFinalColor( ptr, outColor, isFinalColor );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is nothing more here to do.. there are no hits ..
|
|
|
|
// just background so continue
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
SFVEC3F hitColor_X0Y0[RAYPACKET_RAYS_PER_PACKET];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
// Shade original (0, 0) hits ("paint" the intersected objects)
|
2021-01-07 19:33:43 +00:00
|
|
|
renderRayPackets( bgColor, blockPacket.m_ray, hitPacket_X0Y0,
|
2021-08-30 20:30:18 +00:00
|
|
|
m_boardAdapter.m_Cfg->m_Render.raytrace_shadows, hitColor_X0Y0 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-08-30 20:30:18 +00:00
|
|
|
if( m_boardAdapter.m_Cfg->m_Render.raytrace_anti_aliasing )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2016-10-22 22:37:13 +00:00
|
|
|
SFVEC3F hitColor_AA_X1Y1[RAYPACKET_RAYS_PER_PACKET];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
// Intersect one blockPosI + (0.5, 0.5) used for anti aliasing calculation
|
|
|
|
HITINFO_PACKET hitPacket_AA_X1Y1[RAYPACKET_RAYS_PER_PACKET];
|
|
|
|
HITINFO_PACKET_init( hitPacket_AA_X1Y1 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
RAYPACKET blockPacket_AA_X1Y1( m_camera, (SFVEC2F) blockPosI + SFVEC2F( 0.5f, 0.5f ),
|
|
|
|
SFVEC2F( DISP_FACTOR, DISP_FACTOR ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
if( !m_accelerator->Intersect( blockPacket_AA_X1Y1, hitPacket_AA_X1Y1 ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2016-10-22 22:37:13 +00:00
|
|
|
// Missed all the package
|
2016-07-19 17:35:25 +00:00
|
|
|
for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& outColor = bgColor[y];
|
2016-10-22 22:37:13 +00:00
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
|
2016-10-22 22:37:13 +00:00
|
|
|
hitColor_AA_X1Y1[i] = outColor;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-22 22:37:13 +00:00
|
|
|
else
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
renderRayPackets( bgColor, blockPacket_AA_X1Y1.m_ray, hitPacket_AA_X1Y1,
|
2021-08-30 20:30:18 +00:00
|
|
|
m_boardAdapter.m_Cfg->m_Render.raytrace_shadows, hitColor_AA_X1Y1 );
|
2016-10-22 22:37:13 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
SFVEC3F hitColor_AA_X1Y0[RAYPACKET_RAYS_PER_PACKET];
|
|
|
|
SFVEC3F hitColor_AA_X0Y1[RAYPACKET_RAYS_PER_PACKET];
|
|
|
|
SFVEC3F hitColor_AA_X0Y1_half[RAYPACKET_RAYS_PER_PACKET];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( unsigned int i = 0; i < RAYPACKET_RAYS_PER_PACKET; ++i )
|
|
|
|
{
|
2021-08-30 20:30:18 +00:00
|
|
|
SFVEC3F color_average = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] ) * SFVEC3F( 0.5f );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
hitColor_AA_X1Y0[i] = color_average;
|
|
|
|
hitColor_AA_X0Y1[i] = color_average;
|
|
|
|
hitColor_AA_X0Y1_half[i] = color_average;
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
RAY blockRayPck_AA_X1Y0[RAYPACKET_RAYS_PER_PACKET];
|
|
|
|
RAY blockRayPck_AA_X0Y1[RAYPACKET_RAYS_PER_PACKET];
|
|
|
|
RAY blockRayPck_AA_X1Y1_half[RAYPACKET_RAYS_PER_PACKET];
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
RAYPACKET_InitRays_with2DDisplacement(
|
2020-12-20 18:23:50 +00:00
|
|
|
m_camera, (SFVEC2F) blockPosI + SFVEC2F( 0.5f - DISP_FACTOR, DISP_FACTOR ),
|
2020-12-12 17:29:11 +00:00
|
|
|
SFVEC2F( DISP_FACTOR, DISP_FACTOR ), blockRayPck_AA_X1Y0 );
|
2016-10-22 22:37:13 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
RAYPACKET_InitRays_with2DDisplacement(
|
2020-12-20 18:23:50 +00:00
|
|
|
m_camera, (SFVEC2F) blockPosI + SFVEC2F( DISP_FACTOR, 0.5f - DISP_FACTOR ),
|
2020-12-12 17:29:11 +00:00
|
|
|
SFVEC2F( DISP_FACTOR, DISP_FACTOR ), blockRayPck_AA_X0Y1 );
|
2016-10-22 22:37:13 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
RAYPACKET_InitRays_with2DDisplacement(
|
2020-12-20 18:23:50 +00:00
|
|
|
m_camera, (SFVEC2F) blockPosI + SFVEC2F( 0.25f - DISP_FACTOR, 0.25f - DISP_FACTOR ),
|
2020-12-12 17:29:11 +00:00
|
|
|
SFVEC2F( DISP_FACTOR, DISP_FACTOR ), blockRayPck_AA_X1Y1_half );
|
2016-10-22 22:37:13 +00:00
|
|
|
|
2021-01-13 16:23:09 +00:00
|
|
|
renderAntiAliasPackets( bgColor, hitPacket_X0Y0, hitPacket_AA_X1Y1, blockRayPck_AA_X1Y0,
|
2021-01-07 19:33:43 +00:00
|
|
|
hitColor_AA_X1Y0 );
|
2016-10-22 22:37:13 +00:00
|
|
|
|
2021-01-13 16:23:09 +00:00
|
|
|
renderAntiAliasPackets( bgColor, hitPacket_X0Y0, hitPacket_AA_X1Y1, blockRayPck_AA_X0Y1,
|
2021-01-07 19:33:43 +00:00
|
|
|
hitColor_AA_X0Y1 );
|
2016-10-22 22:37:13 +00:00
|
|
|
|
2021-01-13 16:23:09 +00:00
|
|
|
renderAntiAliasPackets( bgColor, hitPacket_X0Y0, hitPacket_AA_X1Y1,
|
2021-01-07 19:33:43 +00:00
|
|
|
blockRayPck_AA_X1Y1_half, hitColor_AA_X0Y1_half );
|
2016-10-22 22:37:13 +00:00
|
|
|
|
|
|
|
// Average the result
|
|
|
|
for( unsigned int i = 0; i < RAYPACKET_RAYS_PER_PACKET; ++i )
|
|
|
|
{
|
2020-12-20 18:23:50 +00:00
|
|
|
hitColor_X0Y0[i] = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] + hitColor_AA_X1Y0[i] +
|
|
|
|
hitColor_AA_X0Y1[i] + hitColor_AA_X0Y1_half[i] ) *
|
|
|
|
SFVEC3F( 1.0f / 5.0f );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
// Copy results to the next stage
|
2020-12-12 17:29:11 +00:00
|
|
|
GLubyte* ptr = &ptrPBO[( blockPos.x + ( blockPos.y * m_realBufferSize.x ) ) * 4];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
const uint32_t ptrInc = ( m_realBufferSize.x - RAYPACKET_DIM ) * 4;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-08-30 20:30:18 +00:00
|
|
|
if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
|
2016-10-22 22:37:13 +00:00
|
|
|
{
|
|
|
|
SFVEC2I bPos;
|
|
|
|
bPos.y = blockPos.y;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
|
|
|
|
{
|
|
|
|
bPos.x = blockPos.x;
|
2016-09-30 20:25:39 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& hColor = hitColor_X0Y0[i];
|
2016-10-22 22:37:13 +00:00
|
|
|
|
|
|
|
if( hitPacket_X0Y0[i].m_hitresult == true )
|
2021-08-30 20:30:18 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
m_postShaderSsao.SetPixelData( bPos.x, bPos.y,
|
|
|
|
hitPacket_X0Y0[i].m_HitInfo.m_HitNormal,
|
|
|
|
hColor,
|
|
|
|
blockPacket.m_ray[i].at(
|
|
|
|
hitPacket_X0Y0[i].m_HitInfo.m_tHit ),
|
|
|
|
hitPacket_X0Y0[i].m_HitInfo.m_tHit,
|
|
|
|
hitPacket_X0Y0[i].m_HitInfo.m_ShadowFactor );
|
2021-08-30 20:30:18 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
else
|
2021-08-30 20:30:18 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
m_postShaderSsao.SetPixelData( bPos.x, bPos.y, SFVEC3F( 0.0f ), hColor,
|
|
|
|
SFVEC3F( 0.0f ), 0, 1.0f );
|
2021-08-30 20:30:18 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
renderFinalColor( ptr, hColor, false );
|
2016-10-22 22:37:13 +00:00
|
|
|
|
|
|
|
bPos.x++;
|
|
|
|
ptr += 4;
|
2016-09-30 20:25:39 +00:00
|
|
|
}
|
2016-10-22 22:37:13 +00:00
|
|
|
|
|
|
|
ptr += ptrInc;
|
|
|
|
bPos.y++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
|
|
|
|
{
|
|
|
|
for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
|
2016-09-30 20:25:39 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
renderFinalColor( ptr, hitColor_X0Y0[i], true );
|
2016-10-22 22:37:13 +00:00
|
|
|
ptr += 4;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
2016-10-22 22:37:13 +00:00
|
|
|
|
|
|
|
ptr += ptrInc;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-05 16:57:25 +00:00
|
|
|
void RENDER_3D_RAYTRACE::postProcessShading( GLubyte* /* ptrPBO */, REPORTER* aStatusReporter )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-08-30 20:30:18 +00:00
|
|
|
if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-06-03 23:00:40 +00:00
|
|
|
if( aStatusReporter )
|
|
|
|
aStatusReporter->Report( _( "Rendering: Post processing shader" ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-08-30 20:30:18 +00:00
|
|
|
m_postShaderSsao.SetShadowsEnabled( m_boardAdapter.m_Cfg->m_Render.raytrace_shadows );
|
2020-07-14 16:40:28 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
std::atomic<size_t> nextBlock( 0 );
|
|
|
|
std::atomic<size_t> threadsFinished( 0 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
|
2020-12-20 18:23:50 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
for( size_t ii = 0; ii < parallelThreadCount; ++ii )
|
|
|
|
{
|
|
|
|
std::thread t = std::thread( [&]()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
for( size_t y = nextBlock.fetch_add( 1 ); y < m_realBufferSize.y;
|
2020-12-20 18:23:50 +00:00
|
|
|
y = nextBlock.fetch_add( 1 ) )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
SFVEC3F* ptr = &m_shaderBuffer[ y * m_realBufferSize.x ];
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
*ptr = m_postShaderSsao.Shade( SFVEC2I( x, y ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
threadsFinished++;
|
|
|
|
} );
|
|
|
|
|
|
|
|
t.detach();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
while( threadsFinished < parallelThreadCount )
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_postShaderSsao.SetShadedBuffer( m_shaderBuffer );
|
2020-07-14 16:40:28 +00:00
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
// Set next state
|
2021-01-07 19:33:43 +00:00
|
|
|
m_renderState = RT_RENDER_STATE_POST_PROCESS_BLUR_AND_FINISH;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// As this was an invalid state, set to finish
|
2021-01-07 19:33:43 +00:00
|
|
|
m_renderState = RT_RENDER_STATE_FINISH;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-05 16:57:25 +00:00
|
|
|
void RENDER_3D_RAYTRACE::postProcessBlurFinish( GLubyte* ptrPBO, REPORTER* /* aStatusReporter */ )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-08-30 20:30:18 +00:00
|
|
|
if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// Now blurs the shader result and compute the final color
|
2018-09-21 04:23:15 +00:00
|
|
|
std::atomic<size_t> nextBlock( 0 );
|
|
|
|
std::atomic<size_t> threadsFinished( 0 );
|
|
|
|
|
|
|
|
size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
for( size_t ii = 0; ii < parallelThreadCount; ++ii )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
std::thread t = std::thread( [&]()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-12-20 18:23:50 +00:00
|
|
|
for( size_t y = nextBlock.fetch_add( 1 ); y < m_realBufferSize.y;
|
|
|
|
y = nextBlock.fetch_add( 1 ) )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
GLubyte* ptr = &ptrPBO[ y * m_realBufferSize.x * 4 ];
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
const SFVEC3F bluredShadeColor = m_postShaderSsao.Blur( SFVEC2I( x, y ) );
|
2020-07-14 16:40:28 +00:00
|
|
|
|
|
|
|
#ifdef USE_SRGB_SPACE
|
2020-12-12 17:29:11 +00:00
|
|
|
const SFVEC3F originColor = convertLinearToSRGB(
|
2021-01-07 19:33:43 +00:00
|
|
|
m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) ) );
|
2020-07-14 16:40:28 +00:00
|
|
|
#else
|
2020-12-12 17:29:11 +00:00
|
|
|
const SFVEC3F originColor =
|
2021-01-07 19:33:43 +00:00
|
|
|
m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) );
|
2020-07-14 16:40:28 +00:00
|
|
|
#endif
|
2021-01-07 19:33:43 +00:00
|
|
|
const SFVEC3F shadedColor = m_postShaderSsao.ApplyShadeColor(
|
2020-12-12 17:29:11 +00:00
|
|
|
SFVEC2I( x, y ), originColor, bluredShadeColor );
|
2018-09-21 04:23:15 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
renderFinalColor( ptr, shadedColor, false );
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
ptr += 4;
|
|
|
|
}
|
|
|
|
}
|
2016-09-30 20:25:39 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
threadsFinished++;
|
|
|
|
} );
|
2016-09-30 20:25:39 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
t.detach();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
while( threadsFinished < parallelThreadCount )
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
// Debug code
|
2021-01-07 19:33:43 +00:00
|
|
|
//m_postShaderSsao.DebugBuffersOutputAsImages();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// End rendering
|
2021-01-07 19:33:43 +00:00
|
|
|
m_renderState = RT_RENDER_STATE_FINISH;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
void RENDER_3D_RAYTRACE::renderPreview( GLubyte* ptrPBO )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
m_isPreview = true;
|
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
std::atomic<size_t> nextBlock( 0 );
|
|
|
|
std::atomic<size_t> threadsFinished( 0 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
size_t parallelThreadCount = std::min<size_t>(
|
|
|
|
std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
|
|
|
|
m_blockPositions.size() );
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
for( size_t ii = 0; ii < parallelThreadCount; ++ii )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
std::thread t = std::thread( [&]()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-12-20 18:23:50 +00:00
|
|
|
for( size_t iBlock = nextBlock.fetch_add( 1 ); iBlock < m_blockPositionsFast.size();
|
|
|
|
iBlock = nextBlock.fetch_add( 1 ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC2UI& windowPosUI = m_blockPositionsFast[ iBlock ];
|
2018-09-21 04:23:15 +00:00
|
|
|
const SFVEC2I windowsPos = SFVEC2I( windowPosUI.x + m_xoffset,
|
|
|
|
windowPosUI.y + m_yoffset );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-03-26 14:05:20 +00:00
|
|
|
RAYPACKET blockPacket( m_camera, windowsPos, 4 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
HITINFO_PACKET hitPacket[RAYPACKET_RAYS_PER_PACKET];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Initialize hitPacket with a "not hit" information
|
2020-03-26 14:05:20 +00:00
|
|
|
for( HITINFO_PACKET& packet : hitPacket )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
2020-03-26 14:05:20 +00:00
|
|
|
packet.m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
packet.m_HitInfo.m_acc_node_info = 0;
|
|
|
|
packet.m_hitresult = false;
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Intersect packet block
|
|
|
|
m_accelerator->Intersect( blockPacket, hitPacket );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Calculate background gradient color
|
|
|
|
SFVEC3F bgColor[RAYPACKET_DIM];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
for( unsigned int y = 0; y < RAYPACKET_DIM; ++y )
|
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
const float posYfactor =
|
|
|
|
(float) ( windowsPos.y + y * 4.0f ) / (float) m_windowSize.y;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
bgColor[y] = (SFVEC3F) m_boardAdapter.m_BgColorTop * SFVEC3F( posYfactor )
|
|
|
|
+ (SFVEC3F) m_boardAdapter.m_BgColorBot
|
|
|
|
* ( SFVEC3F( 1.0f ) - SFVEC3F( posYfactor ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB hitColorShading[RAYPACKET_RAYS_PER_PACKET];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
for( unsigned int i = 0; i < RAYPACKET_RAYS_PER_PACKET; ++i )
|
|
|
|
{
|
|
|
|
const SFVEC3F bhColorY = bgColor[i / RAYPACKET_DIM];
|
|
|
|
|
|
|
|
if( hitPacket[i].m_hitresult == true )
|
|
|
|
{
|
2020-12-20 18:23:50 +00:00
|
|
|
const SFVEC3F hitColor = shadeHit( bhColorY, blockPacket.m_ray[i],
|
|
|
|
hitPacket[i].m_HitInfo, false,
|
|
|
|
0, false );
|
2018-09-21 04:23:15 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
hitColorShading[i] = COLOR_RGB( hitColor );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
hitColorShading[i] = bhColorY;
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cLRB_old[(RAYPACKET_DIM - 1)];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
for( unsigned int y = 0; y < (RAYPACKET_DIM - 1); ++y )
|
|
|
|
{
|
|
|
|
const SFVEC3F bgColorY = bgColor[y];
|
2021-01-02 21:05:29 +00:00
|
|
|
const COLOR_RGB bgColorYRGB = COLOR_RGB( bgColorY );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// This stores cRTB from the last block to be reused next time in a cLTB pixel
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cRTB_old;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
//RAY cRTB_ray;
|
|
|
|
//HITINFO cRTB_hitInfo;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
for( unsigned int x = 0; x < ( RAYPACKET_DIM - 1 ); ++x )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
|
|
|
// pxl 0 pxl 1 pxl 2 pxl 3 pxl 4
|
|
|
|
// x0 x1 ...
|
|
|
|
// .---------------------------.
|
|
|
|
// y0 | cLT | cxxx | cLRT | cxxx | cRT |
|
|
|
|
// | cxxx | cLTC | cxxx | cRTC | cxxx |
|
|
|
|
// | cLTB | cxxx | cC | cxxx | cRTB |
|
|
|
|
// | cxxx | cLBC | cxxx | cRBC | cxxx |
|
|
|
|
// '---------------------------'
|
|
|
|
// y1 | cLB | cxxx | cLRB | cxxx | cRB |
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
const unsigned int iLT = ( ( x + 0 ) + RAYPACKET_DIM * ( y + 0 ) );
|
|
|
|
const unsigned int iRT = ( ( x + 1 ) + RAYPACKET_DIM * ( y + 0 ) );
|
|
|
|
const unsigned int iLB = ( ( x + 0 ) + RAYPACKET_DIM * ( y + 1 ) );
|
|
|
|
const unsigned int iRB = ( ( x + 1 ) + RAYPACKET_DIM * ( y + 1 ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// !TODO: skip when there are no hits
|
2021-01-02 21:05:29 +00:00
|
|
|
const COLOR_RGB& cLT = hitColorShading[ iLT ];
|
|
|
|
const COLOR_RGB& cRT = hitColorShading[ iRT ];
|
|
|
|
const COLOR_RGB& cLB = hitColorShading[ iLB ];
|
|
|
|
const COLOR_RGB& cRB = hitColorShading[ iRB ];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace and shade cC
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cC = bgColorYRGB;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& oriLT = blockPacket.m_ray[ iLT ].m_Origin;
|
|
|
|
const SFVEC3F& oriRB = blockPacket.m_ray[ iRB ].m_Origin;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& dirLT = blockPacket.m_ray[ iLT ].m_Dir;
|
|
|
|
const SFVEC3F& dirRB = blockPacket.m_ray[ iRB ].m_Dir;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
SFVEC3F oriC;
|
|
|
|
SFVEC3F dirC;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
HITINFO centerHitInfo;
|
2016-07-19 17:35:25 +00:00
|
|
|
centerHitInfo.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
bool hittedC = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( hitPacket[iLT].m_hitresult == true )
|
|
|
|
|| ( hitPacket[iRT].m_hitresult == true )
|
|
|
|
|| ( hitPacket[iLB].m_hitresult == true )
|
|
|
|
|| ( hitPacket[iRB].m_hitresult == true ) )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
|
|
|
oriC = ( oriLT + oriRB ) * 0.5f;
|
|
|
|
dirC = glm::normalize( ( dirLT + dirRB ) * 0.5f );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace the center ray
|
|
|
|
RAY centerRay;
|
|
|
|
centerRay.Init( oriC, dirC );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-10-22 22:37:13 +00:00
|
|
|
const unsigned int nodeLT = hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
|
|
|
|
const unsigned int nodeRT = hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
|
2018-09-21 04:23:15 +00:00
|
|
|
const unsigned int nodeLB = hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
|
|
|
|
const unsigned int nodeRB = hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( nodeLT != 0 )
|
2020-12-12 17:29:11 +00:00
|
|
|
hittedC |= m_accelerator->Intersect( centerRay, centerHitInfo,
|
|
|
|
nodeLT );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
|
2020-12-12 17:29:11 +00:00
|
|
|
hittedC |= m_accelerator->Intersect( centerRay, centerHitInfo,
|
|
|
|
nodeRT );
|
2018-09-21 04:23:15 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) && ( nodeLB != nodeRT ) )
|
|
|
|
hittedC |= m_accelerator->Intersect( centerRay, centerHitInfo,
|
|
|
|
nodeLB );
|
2018-09-21 04:23:15 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) && ( nodeRB != nodeLT )
|
|
|
|
&& ( nodeRB != nodeRT ) )
|
|
|
|
hittedC |= m_accelerator->Intersect( centerRay, centerHitInfo,
|
|
|
|
nodeRB );
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
if( hittedC )
|
2020-12-12 17:29:11 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
cC = COLOR_RGB( shadeHit( bgColorY, centerRay, centerHitInfo,
|
2020-12-12 17:29:11 +00:00
|
|
|
false, 0, false ) );
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
else
|
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
centerHitInfo.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
hittedC = m_accelerator->Intersect( centerRay, centerHitInfo );
|
|
|
|
|
|
|
|
if( hittedC )
|
2021-01-02 21:05:29 +00:00
|
|
|
cC = COLOR_RGB( shadeHit( bgColorY, centerRay, centerHitInfo,
|
2020-12-20 18:23:50 +00:00
|
|
|
false, 0, false ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace and shade cLRT
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cLRT = bgColorYRGB;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& oriRT = blockPacket.m_ray[ iRT ].m_Origin;
|
|
|
|
const SFVEC3F& dirRT = blockPacket.m_ray[ iRT ].m_Dir;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( y == 0 )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace the center ray
|
|
|
|
RAY rayLRT;
|
|
|
|
rayLRT.Init( ( oriLT + oriRT ) * 0.5f,
|
2020-12-20 18:23:50 +00:00
|
|
|
glm::normalize( ( dirLT + dirRT ) * 0.5f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
HITINFO hitInfoLRT;
|
|
|
|
hitInfoLRT.m_tHit = std::numeric_limits<float>::infinity();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( hitPacket[iLT].m_hitresult && hitPacket[iRT].m_hitresult
|
|
|
|
&& ( hitPacket[iLT].m_HitInfo.pHitObject
|
|
|
|
== hitPacket[iRT].m_HitInfo.pHitObject ) )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
|
|
|
hitInfoLRT.pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
|
|
|
|
hitInfoLRT.m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
|
|
|
|
hitPacket[ iRT ].m_HitInfo.m_tHit ) * 0.5f;
|
|
|
|
hitInfoLRT.m_HitNormal =
|
|
|
|
glm::normalize( ( hitPacket[ iLT ].m_HitInfo.m_HitNormal +
|
|
|
|
hitPacket[ iRT ].m_HitInfo.m_HitNormal ) * 0.5f );
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
cLRT = COLOR_RGB( shadeHit( bgColorY, rayLRT, hitInfoLRT, false,
|
2020-12-20 18:23:50 +00:00
|
|
|
0, false ) );
|
|
|
|
cLRT = BlendColor( cLRT, BlendColor( cLT, cRT ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
else
|
|
|
|
{
|
2020-12-20 18:23:50 +00:00
|
|
|
// If any hits
|
|
|
|
if( hitPacket[ iLT ].m_hitresult || hitPacket[ iRT ].m_hitresult )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
2020-12-20 18:23:50 +00:00
|
|
|
const unsigned int nodeLT =
|
|
|
|
hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
|
|
|
|
const unsigned int nodeRT =
|
|
|
|
hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
bool hittedLRT = false;
|
|
|
|
|
|
|
|
if( nodeLT != 0 )
|
2020-12-20 18:23:50 +00:00
|
|
|
hittedLRT |= m_accelerator->Intersect( rayLRT, hitInfoLRT,
|
|
|
|
nodeLT );
|
2018-09-21 04:23:15 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
|
|
|
|
hittedLRT |= m_accelerator->Intersect( rayLRT, hitInfoLRT,
|
2018-09-21 04:23:15 +00:00
|
|
|
nodeRT );
|
|
|
|
|
|
|
|
if( hittedLRT )
|
2021-01-02 21:05:29 +00:00
|
|
|
cLRT = COLOR_RGB( shadeHit( bgColorY, rayLRT, hitInfoLRT,
|
2020-12-20 18:23:50 +00:00
|
|
|
false, 0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
hitInfoLRT.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
|
|
|
|
if( m_accelerator->Intersect( rayLRT,hitInfoLRT ) )
|
2021-01-02 21:05:29 +00:00
|
|
|
cLRT = COLOR_RGB( shadeHit( bgColorY, rayLRT,
|
2020-12-20 18:23:50 +00:00
|
|
|
hitInfoLRT, false,
|
|
|
|
0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-21 04:23:15 +00:00
|
|
|
else
|
2020-12-12 17:29:11 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
cLRT = cLRB_old[x];
|
2020-12-12 17:29:11 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace and shade cLTB
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cLTB = bgColorYRGB;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( x == 0 )
|
|
|
|
{
|
|
|
|
const SFVEC3F &oriLB = blockPacket.m_ray[ iLB ].m_Origin;
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace the center ray
|
|
|
|
RAY rayLTB;
|
|
|
|
rayLTB.Init( ( oriLT + oriLB ) * 0.5f,
|
|
|
|
glm::normalize( ( dirLT + dirLB ) * 0.5f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
HITINFO hitInfoLTB;
|
|
|
|
hitInfoLTB.m_tHit = std::numeric_limits<float>::infinity();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( hitPacket[ iLT ].m_hitresult && hitPacket[ iLB ].m_hitresult
|
|
|
|
&& ( hitPacket[ iLT ].m_HitInfo.pHitObject ==
|
|
|
|
hitPacket[ iLB ].m_HitInfo.pHitObject ) )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
|
|
|
hitInfoLTB.pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
|
|
|
|
hitInfoLTB.m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
|
|
|
|
hitPacket[ iLB ].m_HitInfo.m_tHit ) * 0.5f;
|
|
|
|
hitInfoLTB.m_HitNormal =
|
|
|
|
glm::normalize( ( hitPacket[ iLT ].m_HitInfo.m_HitNormal +
|
|
|
|
hitPacket[ iLB ].m_HitInfo.m_HitNormal ) * 0.5f );
|
2021-01-02 21:05:29 +00:00
|
|
|
cLTB = COLOR_RGB( shadeHit( bgColorY, rayLTB, hitInfoLTB, false,
|
2020-12-12 17:29:11 +00:00
|
|
|
0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
cLTB = BlendColor( cLTB, BlendColor( cLT, cLB) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
// If any hits
|
|
|
|
if( hitPacket[ iLT ].m_hitresult || hitPacket[ iLB ].m_hitresult )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
const unsigned int nodeLT =
|
|
|
|
hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
|
|
|
|
const unsigned int nodeLB =
|
|
|
|
hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
bool hittedLTB = false;
|
|
|
|
|
|
|
|
if( nodeLT != 0 )
|
2020-12-20 18:23:50 +00:00
|
|
|
hittedLTB |= m_accelerator->Intersect( rayLTB, hitInfoLTB,
|
2018-09-21 04:23:15 +00:00
|
|
|
nodeLT );
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) )
|
|
|
|
hittedLTB |= m_accelerator->Intersect( rayLTB, hitInfoLTB,
|
2018-09-21 04:23:15 +00:00
|
|
|
nodeLB );
|
|
|
|
|
|
|
|
if( hittedLTB )
|
2021-01-02 21:05:29 +00:00
|
|
|
cLTB = COLOR_RGB( shadeHit( bgColorY, rayLTB, hitInfoLTB,
|
2020-12-20 18:23:50 +00:00
|
|
|
false, 0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
hitInfoLTB.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
|
|
|
|
if( m_accelerator->Intersect( rayLTB, hitInfoLTB ) )
|
2021-01-02 21:05:29 +00:00
|
|
|
cLTB = COLOR_RGB( shadeHit( bgColorY, rayLTB,
|
2020-12-20 18:23:50 +00:00
|
|
|
hitInfoLTB, false,
|
|
|
|
0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-12-12 17:29:11 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
cLTB = cRTB_old;
|
2020-12-12 17:29:11 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace and shade cRTB
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cRTB = bgColorYRGB;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace the center ray
|
|
|
|
RAY rayRTB;
|
|
|
|
rayRTB.Init( ( oriRT + oriRB ) * 0.5f,
|
2020-12-20 18:23:50 +00:00
|
|
|
glm::normalize( ( dirRT + dirRB ) * 0.5f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
HITINFO hitInfoRTB;
|
|
|
|
hitInfoRTB.m_tHit = std::numeric_limits<float>::infinity();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( hitPacket[ iRT ].m_hitresult && hitPacket[ iRB ].m_hitresult
|
|
|
|
&& ( hitPacket[ iRT ].m_HitInfo.pHitObject ==
|
|
|
|
hitPacket[ iRB ].m_HitInfo.pHitObject ) )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
|
|
|
hitInfoRTB.pHitObject = hitPacket[ iRT ].m_HitInfo.pHitObject;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
hitInfoRTB.m_tHit = ( hitPacket[ iRT ].m_HitInfo.m_tHit +
|
|
|
|
hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
|
|
|
|
|
|
|
|
hitInfoRTB.m_HitNormal =
|
|
|
|
glm::normalize( ( hitPacket[ iRT ].m_HitInfo.m_HitNormal +
|
|
|
|
hitPacket[ iRB ].m_HitInfo.m_HitNormal ) * 0.5f );
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
cRTB = COLOR_RGB( shadeHit( bgColorY, rayRTB, hitInfoRTB, false, 0,
|
2020-12-12 17:29:11 +00:00
|
|
|
false ) );
|
2020-12-20 18:23:50 +00:00
|
|
|
cRTB = BlendColor( cRTB, BlendColor( cRT, cRB ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
else
|
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
// If any hits
|
|
|
|
if( hitPacket[ iRT ].m_hitresult || hitPacket[ iRB ].m_hitresult )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
const unsigned int nodeRT =
|
|
|
|
hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
|
|
|
|
const unsigned int nodeRB =
|
|
|
|
hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
bool hittedRTB = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( nodeRT != 0 )
|
2020-12-12 17:29:11 +00:00
|
|
|
hittedRTB |= m_accelerator->Intersect( rayRTB, hitInfoRTB,
|
|
|
|
nodeRT );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( nodeRB != 0 ) && ( nodeRB != nodeRT ) )
|
2020-12-12 17:29:11 +00:00
|
|
|
hittedRTB |= m_accelerator->Intersect( rayRTB, hitInfoRTB,
|
|
|
|
nodeRB );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hittedRTB )
|
2020-12-12 17:29:11 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
cRTB = COLOR_RGB( shadeHit( bgColorY, rayRTB, hitInfoRTB,
|
2020-12-20 18:23:50 +00:00
|
|
|
false, 0, false) );
|
2020-12-12 17:29:11 +00:00
|
|
|
}
|
2018-09-21 04:23:15 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
hitInfoRTB.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
|
|
|
|
if( m_accelerator->Intersect( rayRTB, hitInfoRTB ) )
|
2021-01-02 21:05:29 +00:00
|
|
|
cRTB = COLOR_RGB( shadeHit( bgColorY, rayRTB, hitInfoRTB,
|
2020-12-20 18:23:50 +00:00
|
|
|
false, 0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
cRTB_old = cRTB;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace and shade cLRB
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cLRB = bgColorYRGB;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& oriLB = blockPacket.m_ray[ iLB ].m_Origin;
|
|
|
|
const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace the center ray
|
|
|
|
RAY rayLRB;
|
|
|
|
rayLRB.Init( ( oriLB + oriRB ) * 0.5f,
|
2020-12-12 17:29:11 +00:00
|
|
|
glm::normalize( ( dirLB + dirRB ) * 0.5f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
HITINFO hitInfoLRB;
|
|
|
|
hitInfoLRB.m_tHit = std::numeric_limits<float>::infinity();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( hitPacket[iLB].m_hitresult && hitPacket[iRB].m_hitresult
|
|
|
|
&& ( hitPacket[iLB].m_HitInfo.pHitObject ==
|
|
|
|
hitPacket[iRB].m_HitInfo.pHitObject ) )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
|
|
|
hitInfoLRB.pHitObject = hitPacket[ iLB ].m_HitInfo.pHitObject;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
hitInfoLRB.m_tHit = ( hitPacket[ iLB ].m_HitInfo.m_tHit +
|
|
|
|
hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
hitInfoLRB.m_HitNormal =
|
|
|
|
glm::normalize( ( hitPacket[ iLB ].m_HitInfo.m_HitNormal +
|
|
|
|
hitPacket[ iRB ].m_HitInfo.m_HitNormal ) * 0.5f );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
cLRB = COLOR_RGB( shadeHit( bgColorY, rayLRB, hitInfoLRB, false, 0,
|
2020-12-12 17:29:11 +00:00
|
|
|
false ) );
|
2020-12-20 18:23:50 +00:00
|
|
|
cLRB = BlendColor( cLRB, BlendColor( cLB, cRB ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
else
|
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
// If any hits
|
|
|
|
if( hitPacket[ iLB ].m_hitresult || hitPacket[ iRB ].m_hitresult )
|
2018-09-21 04:23:15 +00:00
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
const unsigned int nodeLB =
|
|
|
|
hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
|
|
|
|
const unsigned int nodeRB =
|
|
|
|
hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
bool hittedLRB = false;
|
|
|
|
|
|
|
|
if( nodeLB != 0 )
|
2020-12-12 17:29:11 +00:00
|
|
|
hittedLRB |= m_accelerator->Intersect( rayLRB, hitInfoLRB,
|
|
|
|
nodeLB );
|
2018-09-21 04:23:15 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) )
|
|
|
|
hittedLRB |= m_accelerator->Intersect( rayLRB, hitInfoLRB,
|
|
|
|
nodeRB );
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
if( hittedLRB )
|
2020-12-12 17:29:11 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
cLRB = COLOR_RGB( shadeHit( bgColorY, rayLRB, hitInfoLRB,
|
2020-12-12 17:29:11 +00:00
|
|
|
false, 0, false ) );
|
|
|
|
}
|
2018-09-21 04:23:15 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
hitInfoLRB.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
|
|
|
|
if( m_accelerator->Intersect( rayLRB, hitInfoLRB ) )
|
2021-01-02 21:05:29 +00:00
|
|
|
cLRB = COLOR_RGB( shadeHit( bgColorY, rayLRB, hitInfoLRB,
|
2020-12-20 18:23:50 +00:00
|
|
|
false, 0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
cLRB_old[x] = cLRB;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace and shade cLTC
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cLTC = BlendColor( cLT , cC );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hitPacket[ iLT ].m_hitresult || hittedC )
|
|
|
|
{
|
|
|
|
// Trace the center ray
|
|
|
|
RAY rayLTC;
|
|
|
|
rayLTC.Init( ( oriLT + oriC ) * 0.5f,
|
|
|
|
glm::normalize( ( dirLT + dirC ) * 0.5f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
HITINFO hitInfoLTC;
|
|
|
|
hitInfoLTC.m_tHit = std::numeric_limits<float>::infinity();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
bool hitted = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hittedC )
|
|
|
|
hitted = centerHitInfo.pHitObject->Intersect( rayLTC, hitInfoLTC );
|
2020-12-12 17:29:11 +00:00
|
|
|
else if( hitPacket[ iLT ].m_hitresult )
|
|
|
|
hitted = hitPacket[ iLT ].m_HitInfo.pHitObject->Intersect(
|
|
|
|
rayLTC,
|
|
|
|
hitInfoLTC );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hitted )
|
2021-01-02 21:05:29 +00:00
|
|
|
cLTC = COLOR_RGB( shadeHit( bgColorY, rayLTC, hitInfoLTC, false,
|
2020-12-12 17:29:11 +00:00
|
|
|
0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace and shade cRTC
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cRTC = BlendColor( cRT , cC );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hitPacket[ iRT ].m_hitresult || hittedC )
|
|
|
|
{
|
|
|
|
// Trace the center ray
|
|
|
|
RAY rayRTC;
|
|
|
|
rayRTC.Init( ( oriRT + oriC ) * 0.5f,
|
|
|
|
glm::normalize( ( dirRT + dirC ) * 0.5f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
HITINFO hitInfoRTC;
|
|
|
|
hitInfoRTC.m_tHit = std::numeric_limits<float>::infinity();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
bool hitted = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hittedC )
|
|
|
|
hitted = centerHitInfo.pHitObject->Intersect( rayRTC, hitInfoRTC );
|
2020-12-12 17:29:11 +00:00
|
|
|
else if( hitPacket[ iRT ].m_hitresult )
|
|
|
|
hitted = hitPacket[ iRT ].m_HitInfo.pHitObject->Intersect( rayRTC,
|
|
|
|
hitInfoRTC );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hitted )
|
2021-01-02 21:05:29 +00:00
|
|
|
cRTC = COLOR_RGB( shadeHit( bgColorY, rayRTC, hitInfoRTC, false,
|
2020-12-12 17:29:11 +00:00
|
|
|
0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace and shade cLBC
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cLBC = BlendColor( cLB , cC );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hitPacket[ iLB ].m_hitresult || hittedC )
|
|
|
|
{
|
|
|
|
// Trace the center ray
|
|
|
|
RAY rayLBC;
|
|
|
|
rayLBC.Init( ( oriLB + oriC ) * 0.5f,
|
|
|
|
glm::normalize( ( dirLB + dirC ) * 0.5f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
HITINFO hitInfoLBC;
|
|
|
|
hitInfoLBC.m_tHit = std::numeric_limits<float>::infinity();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
bool hitted = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hittedC )
|
|
|
|
hitted = centerHitInfo.pHitObject->Intersect( rayLBC, hitInfoLBC );
|
2020-12-12 17:29:11 +00:00
|
|
|
else if( hitPacket[ iLB ].m_hitresult )
|
|
|
|
hitted = hitPacket[ iLB ].m_HitInfo.pHitObject->Intersect( rayLBC,
|
|
|
|
hitInfoLBC );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hitted )
|
2021-01-02 21:05:29 +00:00
|
|
|
cLBC = COLOR_RGB( shadeHit( bgColorY, rayLBC, hitInfoLBC, false,
|
2020-12-12 17:29:11 +00:00
|
|
|
0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Trace and shade cRBC
|
2021-01-02 21:05:29 +00:00
|
|
|
COLOR_RGB cRBC = BlendColor( cRB , cC );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hitPacket[ iRB ].m_hitresult || hittedC )
|
|
|
|
{
|
|
|
|
// Trace the center ray
|
|
|
|
RAY rayRBC;
|
|
|
|
rayRBC.Init( ( oriRB + oriC ) * 0.5f,
|
|
|
|
glm::normalize( ( dirRB + dirC ) * 0.5f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
HITINFO hitInfoRBC;
|
|
|
|
hitInfoRBC.m_tHit = std::numeric_limits<float>::infinity();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
bool hitted = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hittedC )
|
|
|
|
hitted = centerHitInfo.pHitObject->Intersect( rayRBC, hitInfoRBC );
|
2020-12-12 17:29:11 +00:00
|
|
|
else if( hitPacket[ iRB ].m_hitresult )
|
|
|
|
hitted = hitPacket[ iRB ].m_HitInfo.pHitObject->Intersect( rayRBC,
|
|
|
|
hitInfoRBC );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
if( hitted )
|
2021-01-02 21:05:29 +00:00
|
|
|
cRBC = COLOR_RGB( shadeHit( bgColorY, rayRBC, hitInfoRBC, false,
|
2020-12-12 17:29:11 +00:00
|
|
|
0, false ) );
|
2018-09-21 04:23:15 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
// Set pixel colors
|
2021-01-07 19:33:43 +00:00
|
|
|
GLubyte* ptr =
|
|
|
|
&ptrPBO[( 4 * x + m_blockPositionsFast[iBlock].x
|
|
|
|
+ m_realBufferSize.x
|
|
|
|
* ( m_blockPositionsFast[iBlock].y + 4 * y ) ) * 4];
|
|
|
|
SetPixel( ptr + 0, cLT );
|
2018-09-21 04:23:15 +00:00
|
|
|
SetPixel( ptr + 4, BlendColor( cLT, cLRT, cLTC ) );
|
|
|
|
SetPixel( ptr + 8, cLRT );
|
|
|
|
SetPixel( ptr + 12, BlendColor( cLRT, cRT, cRTC ) );
|
|
|
|
|
|
|
|
ptr += m_realBufferSize.x * 4;
|
|
|
|
SetPixel( ptr + 0, BlendColor( cLT , cLTB, cLTC ) );
|
|
|
|
SetPixel( ptr + 4, BlendColor( cLTC, BlendColor( cLT , cC ) ) );
|
|
|
|
SetPixel( ptr + 8, BlendColor( cC, BlendColor( cLRT, cLTC, cRTC ) ) );
|
|
|
|
SetPixel( ptr + 12, BlendColor( cRTC, BlendColor( cRT , cC ) ) );
|
|
|
|
|
|
|
|
ptr += m_realBufferSize.x * 4;
|
|
|
|
SetPixel( ptr + 0, cLTB );
|
|
|
|
SetPixel( ptr + 4, BlendColor( cC, BlendColor( cLTB, cLTC, cLBC ) ) );
|
|
|
|
SetPixel( ptr + 8, cC );
|
|
|
|
SetPixel( ptr + 12, BlendColor( cC, BlendColor( cRTB, cRTC, cRBC ) ) );
|
|
|
|
|
|
|
|
ptr += m_realBufferSize.x * 4;
|
|
|
|
SetPixel( ptr + 0, BlendColor( cLB , cLTB, cLBC ) );
|
|
|
|
SetPixel( ptr + 4, BlendColor( cLBC, BlendColor( cLB , cC ) ) );
|
|
|
|
SetPixel( ptr + 8, BlendColor( cC, BlendColor( cLRB, cLBC, cRBC ) ) );
|
|
|
|
SetPixel( ptr + 12, BlendColor( cRBC, BlendColor( cRB , cC ) ) );
|
|
|
|
}
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
threadsFinished++;
|
|
|
|
} );
|
|
|
|
|
|
|
|
t.detach();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
while( threadsFinished < parallelThreadCount )
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define USE_EXPERIMENTAL_SOFT_SHADOWS 1
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
SFVEC3F RENDER_3D_RAYTRACE::shadeHit( const SFVEC3F& aBgColor, const RAY& aRay, HITINFO& aHitInfo,
|
|
|
|
bool aIsInsideObject, unsigned int aRecursiveLevel,
|
|
|
|
bool is_testShadow ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const MATERIAL* objMaterial = aHitInfo.pHitObject->GetMaterial();
|
|
|
|
wxASSERT( objMaterial != nullptr );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-08-25 20:33:51 +00:00
|
|
|
SFVEC3F outColor = objMaterial->GetEmissiveColor() + objMaterial->GetAmbientColor();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-09-04 15:12:01 +00:00
|
|
|
if( aRecursiveLevel > 7 )
|
2020-08-25 20:33:51 +00:00
|
|
|
return outColor;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-08-25 20:33:51 +00:00
|
|
|
SFVEC3F hitPoint = aHitInfo.m_HitPoint;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-05 22:19:00 +00:00
|
|
|
hitPoint += aHitInfo.m_HitNormal * m_boardAdapter.GetNonCopperLayerThickness() * 0.6f;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-08-25 20:33:51 +00:00
|
|
|
const SFVEC3F diffuseColorObj = aHitInfo.pHitObject->GetDiffuseColor( aHitInfo );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
#if USE_EXPERIMENTAL_SOFT_SHADOWS
|
2021-08-30 20:30:18 +00:00
|
|
|
bool is_aa_enabled = m_boardAdapter.m_Cfg->m_Render.raytrace_anti_aliasing && !m_isPreview;
|
2016-07-19 17:35:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
float shadow_att_factor_sum = 0.0f;
|
|
|
|
|
|
|
|
unsigned int nr_lights_that_can_cast_shadows = 0;
|
|
|
|
|
2021-07-28 20:04:53 +00:00
|
|
|
for( const LIGHT* light : m_lights )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
SFVEC3F vectorToLight;
|
|
|
|
SFVEC3F colorOfLight;
|
|
|
|
float distToLight;
|
|
|
|
|
|
|
|
light->GetLightParameters( hitPoint, vectorToLight, colorOfLight, distToLight );
|
|
|
|
|
|
|
|
if( m_isPreview )
|
|
|
|
colorOfLight = SFVEC3F( 1.0f );
|
|
|
|
|
|
|
|
const float NdotL = glm::dot( aHitInfo.m_HitNormal, vectorToLight );
|
|
|
|
|
|
|
|
// Only calc shade if the normal is facing the direction of light,
|
|
|
|
// otherwise it is in the shadow
|
|
|
|
if( NdotL >= FLT_EPSILON )
|
|
|
|
{
|
2016-11-26 14:38:51 +00:00
|
|
|
float shadow_att_factor_light = 1.0f;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( is_testShadow && light->GetCastShadows() )
|
|
|
|
{
|
|
|
|
nr_lights_that_can_cast_shadows++;
|
|
|
|
#if USE_EXPERIMENTAL_SOFT_SHADOWS
|
2020-09-04 15:12:01 +00:00
|
|
|
// For rays that are recursive, just calculate one hit shadow
|
|
|
|
if( aRecursiveLevel > 0 )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
#endif
|
|
|
|
RAY rayToLight;
|
|
|
|
rayToLight.Init( hitPoint, vectorToLight );
|
|
|
|
|
|
|
|
// Test if point is not in the shadow.
|
|
|
|
// Test for any hit from the point in the direction of light
|
|
|
|
if( m_accelerator->IntersectP( rayToLight, distToLight ) )
|
|
|
|
shadow_att_factor_light = 0.0f;
|
|
|
|
|
|
|
|
#if USE_EXPERIMENTAL_SOFT_SHADOWS
|
|
|
|
}
|
2020-12-20 18:23:50 +00:00
|
|
|
else // Experimental softshadow calculation
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
const unsigned int shadow_number_of_samples =
|
2021-08-30 20:30:18 +00:00
|
|
|
m_boardAdapter.m_Cfg->m_Render.raytrace_nrsamples_shadows;
|
2020-12-20 18:23:50 +00:00
|
|
|
const float shadow_inc_factor = 1.0f / (float) ( shadow_number_of_samples );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2016-11-26 14:38:51 +00:00
|
|
|
for( unsigned int i = 0; i < shadow_number_of_samples; ++i )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
RAY rayToLight;
|
2020-09-04 15:12:01 +00:00
|
|
|
|
|
|
|
if( i == 0 )
|
|
|
|
{
|
|
|
|
rayToLight.Init( hitPoint, vectorToLight );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const SFVEC3F unifVector = UniformRandomHemisphereDirection();
|
2020-12-12 17:29:11 +00:00
|
|
|
const SFVEC3F disturbed_vector_to_light =
|
|
|
|
glm::normalize( vectorToLight + unifVector *
|
2021-08-30 20:30:18 +00:00
|
|
|
m_boardAdapter.m_Cfg->m_Render.raytrace_spread_shadows );
|
2020-09-04 15:12:01 +00:00
|
|
|
|
|
|
|
rayToLight.Init( hitPoint, disturbed_vector_to_light );
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// !TODO: there are multiple ways that this tests can be
|
|
|
|
// optimized. Eg: by packing rays or to test against the
|
|
|
|
// latest hit object.
|
|
|
|
if( m_accelerator->IntersectP( rayToLight, distToLight ) )
|
|
|
|
{
|
|
|
|
shadow_att_factor_light -= shadow_inc_factor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
shadow_att_factor_sum += shadow_att_factor_light;
|
|
|
|
}
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
outColor += objMaterial->Shade( aRay, aHitInfo, NdotL, diffuseColorObj, vectorToLight,
|
|
|
|
colorOfLight, shadow_att_factor_light );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only use the headlight for preview
|
|
|
|
if( m_isPreview )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-26 14:38:51 +00:00
|
|
|
// Improvement: this is not taking in account the lightcolor
|
|
|
|
if( nr_lights_that_can_cast_shadows > 0 )
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
aHitInfo.m_ShadowFactor = glm::max(
|
|
|
|
shadow_att_factor_sum / (float) ( nr_lights_that_can_cast_shadows * 1.0f ), 0.0f );
|
2016-11-26 14:38:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aHitInfo.m_ShadowFactor = 1.0f;
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
// Clamp color to not be brighter than 1.0f
|
|
|
|
outColor = glm::min( outColor, SFVEC3F( 1.0f ) );
|
|
|
|
|
|
|
|
if( !m_isPreview )
|
|
|
|
{
|
|
|
|
// Reflections
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( objMaterial->GetReflection() > 0.0f )
|
2021-08-30 20:30:18 +00:00
|
|
|
&& m_boardAdapter.m_Cfg->m_Render.raytrace_reflections
|
2021-01-07 19:33:43 +00:00
|
|
|
&& ( aRecursiveLevel < objMaterial->GetReflectionRecursionCount() ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
const unsigned int reflection_number_of_samples =
|
2021-01-07 19:33:43 +00:00
|
|
|
objMaterial->GetReflectionRayCount();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
SFVEC3F sum_color = SFVEC3F( 0.0f );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
const SFVEC3F reflectVector = aRay.m_Dir - 2.0f *
|
|
|
|
glm::dot( aRay.m_Dir, aHitInfo.m_HitNormal ) * aHitInfo.m_HitNormal;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( unsigned int i = 0; i < reflection_number_of_samples; ++i )
|
|
|
|
{
|
|
|
|
RAY reflectedRay;
|
2020-09-04 15:12:01 +00:00
|
|
|
|
|
|
|
if( i == 0 )
|
|
|
|
{
|
|
|
|
reflectedRay.Init( hitPoint, reflectVector );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Apply some randomize to the reflected vector
|
|
|
|
const SFVEC3F random_reflectVector =
|
|
|
|
glm::normalize( reflectVector +
|
|
|
|
UniformRandomHemisphereDirection() *
|
2021-08-30 20:30:18 +00:00
|
|
|
m_boardAdapter.m_Cfg->m_Render.raytrace_spread_reflections );
|
2020-09-04 15:12:01 +00:00
|
|
|
|
|
|
|
reflectedRay.Init( hitPoint, random_reflectVector );
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
HITINFO reflectedHit;
|
|
|
|
reflectedHit.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
|
|
|
|
if( m_accelerator->Intersect( reflectedRay, reflectedHit ) )
|
|
|
|
{
|
2017-01-21 12:30:05 +00:00
|
|
|
sum_color += ( diffuseColorObj + objMaterial->GetSpecularColor() ) *
|
2020-12-20 18:23:50 +00:00
|
|
|
shadeHit( aBgColor, reflectedRay, reflectedHit, false,
|
|
|
|
aRecursiveLevel + 1, is_testShadow ) *
|
2017-01-21 12:30:05 +00:00
|
|
|
SFVEC3F( objMaterial->GetReflection() *
|
|
|
|
// Falloff factor
|
|
|
|
(1.0f / ( 1.0f + 0.75f * reflectedHit.m_tHit *
|
|
|
|
reflectedHit.m_tHit) ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outColor += (sum_color / SFVEC3F( (float)reflection_number_of_samples) );
|
|
|
|
}
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
// Refraction
|
2020-04-25 14:38:41 +00:00
|
|
|
const float objTransparency = aHitInfo.pHitObject->GetModelTransparency();
|
|
|
|
|
2021-08-30 20:30:18 +00:00
|
|
|
if( ( objTransparency > 0.0f ) && m_boardAdapter.m_Cfg->m_Render.raytrace_refractions
|
2021-01-07 19:33:43 +00:00
|
|
|
&& ( aRecursiveLevel < objMaterial->GetRefractionRecursionCount() ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
const float airIndex = 1.000293f;
|
|
|
|
const float glassIndex = 1.49f;
|
|
|
|
const float air_over_glass = airIndex / glassIndex;
|
|
|
|
const float glass_over_air = glassIndex / airIndex;
|
|
|
|
|
2017-01-21 22:17:12 +00:00
|
|
|
const float refractionRatio = aIsInsideObject?glass_over_air:air_over_glass;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
SFVEC3F refractedVector;
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( Refract( aRay.m_Dir, aHitInfo.m_HitNormal, refractionRatio, refractedVector ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// This increase the start point by a "fixed" factor so it will work the
|
|
|
|
// same for all distances
|
2020-12-12 17:29:11 +00:00
|
|
|
const SFVEC3F startPoint =
|
2021-01-05 22:19:00 +00:00
|
|
|
aRay.at( aHitInfo.m_tHit + m_boardAdapter.GetNonCopperLayerThickness() *
|
2020-12-12 17:29:11 +00:00
|
|
|
0.25f );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
const unsigned int refractions_number_of_samples =
|
2021-01-07 19:33:43 +00:00
|
|
|
objMaterial->GetRefractionRayCount();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2017-01-21 22:17:12 +00:00
|
|
|
SFVEC3F sum_color = SFVEC3F(0.0f);
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2017-01-21 22:17:12 +00:00
|
|
|
for( unsigned int i = 0; i < refractions_number_of_samples; ++i )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2017-01-21 22:17:12 +00:00
|
|
|
RAY refractedRay;
|
|
|
|
|
2020-09-04 15:12:01 +00:00
|
|
|
if( i == 0 )
|
|
|
|
{
|
|
|
|
refractedRay.Init( startPoint, refractedVector );
|
|
|
|
}
|
|
|
|
else
|
2017-01-21 22:17:12 +00:00
|
|
|
{
|
|
|
|
// apply some randomize to the refracted vector
|
2020-12-12 17:29:11 +00:00
|
|
|
const SFVEC3F randomizeRefractedVector =
|
|
|
|
glm::normalize( refractedVector +
|
|
|
|
UniformRandomHemisphereDirection() *
|
2021-08-30 20:30:18 +00:00
|
|
|
m_boardAdapter.m_Cfg->m_Render.raytrace_spread_refractions );
|
2017-01-21 22:17:12 +00:00
|
|
|
|
|
|
|
refractedRay.Init( startPoint, randomizeRefractedVector );
|
|
|
|
}
|
|
|
|
|
|
|
|
HITINFO refractedHit;
|
|
|
|
refractedHit.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
|
2020-08-25 20:33:51 +00:00
|
|
|
SFVEC3F refractedColor = aBgColor;
|
2017-01-21 22:17:12 +00:00
|
|
|
|
|
|
|
if( m_accelerator->Intersect( refractedRay, refractedHit ) )
|
|
|
|
{
|
2020-12-20 18:23:50 +00:00
|
|
|
refractedColor = shadeHit( aBgColor, refractedRay, refractedHit,
|
|
|
|
!aIsInsideObject, aRecursiveLevel + 1, false );
|
2017-01-21 22:17:12 +00:00
|
|
|
|
|
|
|
const SFVEC3F absorbance = ( SFVEC3F(1.0f) - diffuseColorObj ) *
|
|
|
|
(1.0f - objTransparency ) *
|
2020-12-12 17:29:11 +00:00
|
|
|
objMaterial->GetAbsorvance() *
|
2020-08-25 20:33:51 +00:00
|
|
|
refractedHit.m_tHit;
|
2017-01-21 22:17:12 +00:00
|
|
|
|
2020-08-25 20:33:51 +00:00
|
|
|
const SFVEC3F transparency = 1.0f / ( absorbance + 1.0f );
|
2017-01-21 22:17:12 +00:00
|
|
|
|
2020-08-25 20:33:51 +00:00
|
|
|
sum_color += refractedColor * transparency;
|
2017-01-21 22:17:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-25 20:33:51 +00:00
|
|
|
sum_color += refractedColor;
|
2017-01-21 22:17:12 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
2017-01-21 22:17:12 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * sum_color
|
|
|
|
/ SFVEC3F( (float) refractions_number_of_samples );
|
2020-08-25 20:33:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * aBgColor;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void RENDER_3D_RAYTRACE::initializeNewWindowSize()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
initPbo();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
void RENDER_3D_RAYTRACE::initPbo()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( GLEW_ARB_pixel_buffer_object )
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
m_openglSupportsVertexBufferObjects = true;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Try to delete vbo if it was already initialized
|
2021-01-07 19:33:43 +00:00
|
|
|
deletePbo();
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Learn about Pixel buffer objects at:
|
|
|
|
// http://www.songho.ca/opengl/gl_pbo.html
|
|
|
|
// http://web.eecs.umich.edu/~sugih/courses/eecs487/lectures/25-PBO+Mipmapping.pdf
|
|
|
|
// "create 2 pixel buffer objects, you need to delete them when program exits.
|
|
|
|
// glBufferDataARB with NULL pointer reserves only memory space."
|
|
|
|
|
|
|
|
// This sets the number of RGBA pixels
|
|
|
|
m_pboDataSize = m_realBufferSize.x * m_realBufferSize.y * 4;
|
|
|
|
|
|
|
|
glGenBuffersARB( 1, &m_pboId );
|
|
|
|
glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, m_pboId );
|
|
|
|
glBufferDataARB( GL_PIXEL_UNPACK_BUFFER_ARB, m_pboDataSize, 0, GL_STREAM_DRAW_ARB );
|
|
|
|
glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
|
|
|
|
|
|
|
|
wxLogTrace( m_logTrace,
|
2021-01-02 21:05:29 +00:00
|
|
|
wxT( "RENDER_3D_RAYTRACE:: GLEW_ARB_pixel_buffer_object is supported" ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool RENDER_3D_RAYTRACE::initializeOpenGL()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
m_is_opengl_initialized = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 04:36:34 +00:00
|
|
|
static float distance( const SFVEC2UI& a, const SFVEC2UI& b )
|
|
|
|
{
|
|
|
|
const float dx = (float) a.x - (float) b.x;
|
|
|
|
const float dy = (float) a.y - (float) b.y;
|
|
|
|
return hypotf( dx, dy );
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
void RENDER_3D_RAYTRACE::initializeBlockPositions()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2018-04-11 17:59:28 +00:00
|
|
|
m_realBufferSize = SFVEC2UI( 0 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Calc block positions for fast preview mode
|
|
|
|
m_blockPositionsFast.clear();
|
|
|
|
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
const unsigned int mX = DecodeMorton2X(i);
|
|
|
|
const unsigned int mY = DecodeMorton2Y(i);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
const SFVEC2UI blockPos( mX * 4 * RAYPACKET_DIM - mX * 4,
|
2020-12-20 18:23:50 +00:00
|
|
|
mY * 4 * RAYPACKET_DIM - mY * 4 );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( ( blockPos.x >= ( (unsigned int)m_windowSize.x - ( 4 * RAYPACKET_DIM + 4 ) ) ) &&
|
|
|
|
( blockPos.y >= ( (unsigned int)m_windowSize.y - ( 4 * RAYPACKET_DIM + 4 ) ) ) )
|
|
|
|
break;
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
if( ( blockPos.x < ( (unsigned int)m_windowSize.x - ( 4 * RAYPACKET_DIM + 4 ) ) ) &&
|
|
|
|
( blockPos.y < ( (unsigned int)m_windowSize.y - ( 4 * RAYPACKET_DIM + 4 ) ) ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
m_blockPositionsFast.push_back( blockPos );
|
|
|
|
|
|
|
|
if( blockPos.x > m_realBufferSize.x )
|
|
|
|
m_realBufferSize.x = blockPos.x;
|
|
|
|
|
|
|
|
if( blockPos.y > m_realBufferSize.y )
|
|
|
|
m_realBufferSize.y = blockPos.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_fastPreviewModeSize = m_realBufferSize;
|
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
m_realBufferSize.x = ( ( m_realBufferSize.x + RAYPACKET_DIM * 4 ) & RAYPACKET_INVMASK );
|
|
|
|
m_realBufferSize.y = ( ( m_realBufferSize.y + RAYPACKET_DIM * 4 ) & RAYPACKET_INVMASK );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
m_xoffset = ( m_windowSize.x - m_realBufferSize.x ) / 2;
|
|
|
|
m_yoffset = ( m_windowSize.y - m_realBufferSize.y ) / 2;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_postShaderSsao.UpdateSize( m_realBufferSize );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-20 18:23:50 +00:00
|
|
|
// Calc block positions for regular rendering. Choose an 'inside out' style of rendering.
|
2016-07-19 17:35:25 +00:00
|
|
|
m_blockPositions.clear();
|
2019-05-14 04:36:34 +00:00
|
|
|
const int blocks_x = m_realBufferSize.x / RAYPACKET_DIM;
|
|
|
|
const int blocks_y = m_realBufferSize.y / RAYPACKET_DIM;
|
|
|
|
m_blockPositions.reserve( blocks_x * blocks_y );
|
|
|
|
|
|
|
|
for( int x = 0; x < blocks_x; ++x )
|
2020-12-20 18:23:50 +00:00
|
|
|
{
|
2019-05-14 04:36:34 +00:00
|
|
|
for( int y = 0; y < blocks_y; ++y )
|
2019-12-05 15:41:21 +00:00
|
|
|
m_blockPositions.emplace_back( x * RAYPACKET_DIM, y * RAYPACKET_DIM );
|
2020-12-20 18:23:50 +00:00
|
|
|
}
|
2019-05-14 04:36:34 +00:00
|
|
|
|
|
|
|
const SFVEC2UI center( m_realBufferSize.x / 2, m_realBufferSize.y / 2 );
|
|
|
|
std::sort( m_blockPositions.begin(), m_blockPositions.end(),
|
2023-10-04 17:43:36 +00:00
|
|
|
[&]( const SFVEC2UI& a, const SFVEC2UI& b )
|
|
|
|
{
|
2019-05-14 04:36:34 +00:00
|
|
|
// Sort order: inside out.
|
2023-10-04 17:43:36 +00:00
|
|
|
float distanceA = distance( a, center );
|
|
|
|
float distanceB = distance( b, center );
|
|
|
|
|
|
|
|
if( distanceA != distanceB )
|
|
|
|
return distanceA < distanceB;
|
|
|
|
|
|
|
|
if( a[0] != b[0] )
|
|
|
|
return a[0] < b[0];
|
|
|
|
|
|
|
|
return a[1] < b[1];
|
2019-05-14 04:36:34 +00:00
|
|
|
} );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Create m_shader buffer
|
2017-03-08 08:12:16 +00:00
|
|
|
delete[] m_shaderBuffer;
|
2016-07-19 17:35:25 +00:00
|
|
|
m_shaderBuffer = new SFVEC3F[m_realBufferSize.x * m_realBufferSize.y];
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
initPbo();
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
2020-09-04 00:00:56 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BOARD_ITEM* RENDER_3D_RAYTRACE::IntersectBoardItem( const RAY& aRay )
|
2020-09-04 00:00:56 +00:00
|
|
|
{
|
|
|
|
HITINFO hitInfo;
|
|
|
|
hitInfo.m_tHit = std::numeric_limits<float>::infinity();
|
|
|
|
|
|
|
|
if( m_accelerator )
|
2020-12-12 17:29:11 +00:00
|
|
|
{
|
2020-09-04 00:00:56 +00:00
|
|
|
if( m_accelerator->Intersect( aRay, hitInfo ) )
|
|
|
|
{
|
|
|
|
if( hitInfo.pHitObject )
|
|
|
|
return hitInfo.pHitObject->GetBoardItem();
|
|
|
|
}
|
2020-12-12 17:29:11 +00:00
|
|
|
}
|
2020-09-04 00:00:56 +00:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|