You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
  5. * Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file post_shader.cpp
  26. * @brief A base class to create post shaders.
  27. */
  28. #include "post_shader.h"
  29. #include "buffers_debug.h"
  30. #include <wx/debug.h>
  31. POST_SHADER::POST_SHADER( const CAMERA& aCamera ) :
  32. m_camera( aCamera )
  33. {
  34. m_size = SFVEC2UI( 0, 0 );
  35. m_normals = nullptr;
  36. m_color = nullptr;
  37. m_depth = nullptr;
  38. m_wc_hitposition = nullptr;
  39. m_shadow_att_factor = nullptr;
  40. m_tmin = FLT_MAX;
  41. m_tmax = FLT_MIN;
  42. }
  43. POST_SHADER::~POST_SHADER()
  44. {
  45. destroy_buffers();
  46. }
  47. void POST_SHADER::UpdateSize( unsigned int xSize, unsigned int ySize )
  48. {
  49. destroy_buffers();
  50. m_size.x = xSize;
  51. m_size.y = ySize;
  52. const unsigned int n_elements = xSize * ySize;
  53. m_normals = new SFVEC3F[n_elements];
  54. m_color = new SFVEC3F[n_elements];
  55. m_depth = new float[n_elements];
  56. m_wc_hitposition = new SFVEC3F[n_elements];
  57. m_shadow_att_factor = new float[n_elements];
  58. }
  59. void POST_SHADER::UpdateSize( const SFVEC2UI& aSize )
  60. {
  61. UpdateSize( aSize.x, aSize.y );
  62. }
  63. void POST_SHADER::SetPixelData( unsigned int x, unsigned int y, const SFVEC3F& aNormal,
  64. const SFVEC3F& aColor, const SFVEC3F& aHitPosition,
  65. float aDepth, float aShadowAttFactor )
  66. {
  67. wxASSERT( x < m_size.x );
  68. wxASSERT( y < m_size.y );
  69. wxASSERT( ( aShadowAttFactor >= 0.0f ) && ( aShadowAttFactor <= 1.0f ) );
  70. const unsigned int idx = x + y * m_size.x;
  71. m_normals[ idx ] = aNormal;
  72. m_color [ idx ] = aColor;
  73. m_depth [ idx ] = aDepth;
  74. m_shadow_att_factor [ idx ] = aShadowAttFactor;
  75. m_wc_hitposition[ idx ] = aHitPosition;
  76. if( aDepth > FLT_EPSILON )
  77. {
  78. if( aDepth < m_tmin )
  79. m_tmin = aDepth;
  80. if( aDepth > m_tmax )
  81. m_tmax = aDepth;
  82. }
  83. }
  84. void POST_SHADER::destroy_buffers()
  85. {
  86. delete[] m_normals;
  87. m_normals = nullptr;
  88. delete[] m_color;
  89. m_color = nullptr;
  90. delete[] m_depth;
  91. m_depth = nullptr;
  92. delete[] m_shadow_att_factor;
  93. m_shadow_att_factor = nullptr;
  94. delete[] m_wc_hitposition;
  95. m_wc_hitposition = nullptr;
  96. }
  97. const SFVEC3F& POST_SHADER::GetNormalAt( const SFVEC2F& aPos ) const
  98. {
  99. return m_normals[GetIndex( aPos )];
  100. }
  101. const SFVEC3F& POST_SHADER::GetColorAt( const SFVEC2F& aPos ) const
  102. {
  103. return m_color[GetIndex( aPos )];
  104. }
  105. float POST_SHADER::GetDepthAt( const SFVEC2F& aPos ) const
  106. {
  107. return m_depth[GetIndex( aPos )];
  108. }
  109. const SFVEC3F& POST_SHADER::GetPositionAt( const SFVEC2F& aPos ) const
  110. {
  111. return m_wc_hitposition[GetIndex( aPos )];
  112. }
  113. const SFVEC3F& POST_SHADER::GetNormalAt( const SFVEC2I& aPos ) const
  114. {
  115. return m_normals[GetIndex( aPos )];
  116. }
  117. const SFVEC3F& POST_SHADER::GetColorAt( const SFVEC2I& aPos ) const
  118. {
  119. return m_color[GetIndex( aPos )];
  120. }
  121. const SFVEC3F& POST_SHADER::GetColorAtNotProtected( const SFVEC2I& aPos ) const
  122. {
  123. return m_color[ aPos.x + m_size.x * aPos.y ];
  124. }
  125. float POST_SHADER::GetDepthAt( const SFVEC2I& aPos ) const
  126. {
  127. return m_depth[GetIndex( aPos )];
  128. }
  129. float POST_SHADER::GetDepthNormalizedAt( const SFVEC2I& aPos ) const
  130. {
  131. const float depth = m_depth[GetIndex( aPos )];
  132. if( depth >= m_tmin )
  133. return (depth - m_tmin) / (m_tmax - m_tmin);
  134. return 0.0f;
  135. }
  136. const SFVEC3F& POST_SHADER::GetPositionAt( const SFVEC2I& aPos ) const
  137. {
  138. return m_wc_hitposition[GetIndex( aPos )];
  139. }
  140. const float& POST_SHADER::GetShadowFactorAt( const SFVEC2I& aPos ) const
  141. {
  142. return m_shadow_att_factor[GetIndex( aPos )];
  143. }
  144. void POST_SHADER::DebugBuffersOutputAsImages() const
  145. {
  146. DBG_SaveBuffer( wxT( "m_shadow_att_factor" ), m_shadow_att_factor, m_size.x, m_size.y );
  147. DBG_SaveBuffer( wxT( "m_color" ), m_color, m_size.x, m_size.y );
  148. DBG_SaveNormalsBuffer( wxT( "m_normals" ), m_normals, m_size.x, m_size.y );
  149. // Normalize depth
  150. float *normalizedDepth = (float*) malloc( m_size.x * m_size.y * sizeof( float ) );
  151. float *normalizedDepthPTr = normalizedDepth;
  152. for( unsigned int iy = 0; iy < m_size.y; ++iy )
  153. {
  154. for( unsigned int ix = 0; ix < m_size.x; ++ix )
  155. {
  156. *normalizedDepthPTr = GetDepthNormalizedAt( SFVEC2I( ix, iy) );
  157. normalizedDepthPTr++;
  158. }
  159. }
  160. DBG_SaveBuffer( wxT( "m_depthNormalized" ), normalizedDepth, m_size.x, m_size.y );
  161. free( normalizedDepth );
  162. }