2016-07-19 17:35:25 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
|
2024-02-29 20:39:48 +00:00
|
|
|
* Copyright (C) 2015-2024 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-01-03 22:23:00 +00:00
|
|
|
* @file post_shader.cpp
|
|
|
|
* @brief A base class to create post shaders.
|
2016-07-19 17:35:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "post_shader.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
#include "buffers_debug.h"
|
|
|
|
#include <wx/debug.h>
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
POST_SHADER::POST_SHADER( const CAMERA& aCamera ) :
|
|
|
|
m_camera( aCamera )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
m_size = SFVEC2UI( 0, 0 );
|
2021-01-02 21:05:29 +00:00
|
|
|
m_normals = nullptr;
|
|
|
|
m_color = nullptr;
|
|
|
|
m_depth = nullptr;
|
|
|
|
m_wc_hitposition = nullptr;
|
|
|
|
m_shadow_att_factor = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
m_tmin = FLT_MAX;
|
|
|
|
m_tmax = FLT_MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
POST_SHADER::~POST_SHADER()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
destroy_buffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void POST_SHADER::UpdateSize( unsigned int xSize, unsigned int ySize )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
destroy_buffers();
|
|
|
|
|
|
|
|
m_size.x = xSize;
|
|
|
|
m_size.y = ySize;
|
|
|
|
|
|
|
|
const unsigned int n_elements = xSize * ySize;
|
|
|
|
|
|
|
|
m_normals = new SFVEC3F[n_elements];
|
2024-02-29 20:39:48 +00:00
|
|
|
m_color = new SFVEC4F[n_elements];
|
2016-07-19 17:35:25 +00:00
|
|
|
m_depth = new float[n_elements];
|
|
|
|
m_wc_hitposition = new SFVEC3F[n_elements];
|
|
|
|
m_shadow_att_factor = new float[n_elements];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void POST_SHADER::UpdateSize( const SFVEC2UI& aSize )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
UpdateSize( aSize.x, aSize.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void POST_SHADER::SetPixelData( unsigned int x, unsigned int y, const SFVEC3F& aNormal,
|
2024-02-29 20:39:48 +00:00
|
|
|
const SFVEC4F& aColor, const SFVEC3F& aHitPosition,
|
2020-12-13 23:02:57 +00:00
|
|
|
float aDepth, float aShadowAttFactor )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
wxASSERT( x < m_size.x );
|
|
|
|
wxASSERT( y < m_size.y );
|
2020-12-13 23:02:57 +00:00
|
|
|
wxASSERT( ( aShadowAttFactor >= 0.0f ) && ( aShadowAttFactor <= 1.0f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
const unsigned int idx = x + y * m_size.x;
|
|
|
|
|
|
|
|
m_normals[ idx ] = aNormal;
|
|
|
|
m_color [ idx ] = aColor;
|
|
|
|
m_depth [ idx ] = aDepth;
|
|
|
|
m_shadow_att_factor [ idx ] = aShadowAttFactor;
|
|
|
|
m_wc_hitposition[ idx ] = aHitPosition;
|
|
|
|
|
|
|
|
|
|
|
|
if( aDepth > FLT_EPSILON )
|
|
|
|
{
|
|
|
|
if( aDepth < m_tmin )
|
|
|
|
m_tmin = aDepth;
|
|
|
|
|
|
|
|
if( aDepth > m_tmax )
|
|
|
|
m_tmax = aDepth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void POST_SHADER::destroy_buffers()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-12-13 23:02:57 +00:00
|
|
|
delete[] m_normals;
|
|
|
|
m_normals = nullptr;
|
|
|
|
delete[] m_color;
|
|
|
|
m_color = nullptr;
|
|
|
|
delete[] m_depth;
|
|
|
|
m_depth = nullptr;
|
|
|
|
delete[] m_shadow_att_factor;
|
|
|
|
m_shadow_att_factor = nullptr;
|
|
|
|
delete[] m_wc_hitposition;
|
|
|
|
m_wc_hitposition = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& POST_SHADER::GetNormalAt( const SFVEC2F& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-07-14 16:40:28 +00:00
|
|
|
return m_normals[GetIndex( aPos )];
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-29 20:39:48 +00:00
|
|
|
const SFVEC4F& POST_SHADER::GetColorAt( const SFVEC2F& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-07-14 16:40:28 +00:00
|
|
|
return m_color[GetIndex( aPos )];
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
float POST_SHADER::GetDepthAt( const SFVEC2F& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-07-14 16:40:28 +00:00
|
|
|
return m_depth[GetIndex( aPos )];
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& POST_SHADER::GetPositionAt( const SFVEC2F& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-07-14 16:40:28 +00:00
|
|
|
return m_wc_hitposition[GetIndex( aPos )];
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& POST_SHADER::GetNormalAt( const SFVEC2I& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-07-14 16:40:28 +00:00
|
|
|
return m_normals[GetIndex( aPos )];
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-29 20:39:48 +00:00
|
|
|
const SFVEC4F& POST_SHADER::GetColorAt( const SFVEC2I& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-07-14 16:40:28 +00:00
|
|
|
return m_color[GetIndex( aPos )];
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-29 20:39:48 +00:00
|
|
|
const SFVEC4F& POST_SHADER::GetColorAtNotProtected( const SFVEC2I& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
return m_color[ aPos.x + m_size.x * aPos.y ];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
float POST_SHADER::GetDepthAt( const SFVEC2I& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-07-14 16:40:28 +00:00
|
|
|
return m_depth[GetIndex( aPos )];
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
float POST_SHADER::GetDepthNormalizedAt( const SFVEC2I& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-07-14 16:40:28 +00:00
|
|
|
const float depth = m_depth[GetIndex( aPos )];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( depth >= m_tmin )
|
|
|
|
return (depth - m_tmin) / (m_tmax - m_tmin);
|
|
|
|
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F& POST_SHADER::GetPositionAt( const SFVEC2I& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-07-14 16:40:28 +00:00
|
|
|
return m_wc_hitposition[GetIndex( aPos )];
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const float& POST_SHADER::GetShadowFactorAt( const SFVEC2I& aPos ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-07-14 16:40:28 +00:00
|
|
|
return m_shadow_att_factor[GetIndex( aPos )];
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void POST_SHADER::DebugBuffersOutputAsImages() const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2022-02-04 20:21:07 +00:00
|
|
|
DBG_SaveBuffer( wxT( "m_shadow_att_factor" ), m_shadow_att_factor, m_size.x, m_size.y );
|
|
|
|
DBG_SaveBuffer( wxT( "m_color" ), m_color, m_size.x, m_size.y );
|
|
|
|
DBG_SaveNormalsBuffer( wxT( "m_normals" ), m_normals, m_size.x, m_size.y );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Normalize depth
|
|
|
|
float *normalizedDepth = (float*) malloc( m_size.x * m_size.y * sizeof( float ) );
|
|
|
|
|
|
|
|
float *normalizedDepthPTr = normalizedDepth;
|
|
|
|
|
|
|
|
for( unsigned int iy = 0; iy < m_size.y; ++iy )
|
|
|
|
{
|
2021-01-03 22:23:00 +00:00
|
|
|
for( unsigned int ix = 0; ix < m_size.x; ++ix )
|
|
|
|
{
|
|
|
|
*normalizedDepthPTr = GetDepthNormalizedAt( SFVEC2I( ix, iy) );
|
|
|
|
normalizedDepthPTr++;
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 20:21:07 +00:00
|
|
|
DBG_SaveBuffer( wxT( "m_depthNormalized" ), normalizedDepth, m_size.x, m_size.y );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
free( normalizedDepth );
|
|
|
|
}
|