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.

213 lines
5.5 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) 1992-2016 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 cpostshader.cpp
  26. * @brief a base class to create post shaders
  27. */
  28. #include "cpostshader.h"
  29. #include "buffers_debug.h"
  30. #include <wx/debug.h>
  31. CPOSTSHADER::CPOSTSHADER( const CCAMERA &aCamera ) : m_camera(aCamera)
  32. {
  33. m_size = SFVEC2UI( 0, 0 );
  34. m_normals = NULL;
  35. m_color = NULL;
  36. m_depth = NULL;
  37. m_wc_hitposition = NULL;
  38. m_shadow_att_factor = NULL;
  39. m_tmin = FLT_MAX;
  40. m_tmax = FLT_MIN;
  41. }
  42. CPOSTSHADER::~CPOSTSHADER()
  43. {
  44. destroy_buffers();
  45. }
  46. void CPOSTSHADER::UpdateSize( unsigned int xSize, unsigned int ySize )
  47. {
  48. destroy_buffers();
  49. m_size.x = xSize;
  50. m_size.y = ySize;
  51. const unsigned int n_elements = xSize * ySize;
  52. m_normals = new SFVEC3F[n_elements];
  53. m_color = new SFVEC3F[n_elements];
  54. m_depth = new float[n_elements];
  55. m_wc_hitposition = new SFVEC3F[n_elements];
  56. m_shadow_att_factor = new float[n_elements];
  57. }
  58. void CPOSTSHADER::UpdateSize( const SFVEC2UI &aSize )
  59. {
  60. UpdateSize( aSize.x, aSize.y );
  61. }
  62. void CPOSTSHADER::SetPixelData( unsigned int x,
  63. unsigned int y,
  64. const SFVEC3F &aNormal,
  65. const SFVEC3F &aColor,
  66. const SFVEC3F &aHitPosition,
  67. float aDepth,
  68. float aShadowAttFactor )
  69. {
  70. wxASSERT( x < m_size.x );
  71. wxASSERT( y < m_size.y );
  72. wxASSERT( (aShadowAttFactor >= 0.0f) && (aShadowAttFactor <= 1.0f) );
  73. const unsigned int idx = x + y * m_size.x;
  74. m_normals[ idx ] = aNormal;
  75. m_color [ idx ] = aColor;
  76. m_depth [ idx ] = aDepth;
  77. m_shadow_att_factor [ idx ] = aShadowAttFactor;
  78. m_wc_hitposition[ idx ] = aHitPosition;
  79. if( aDepth > FLT_EPSILON )
  80. {
  81. if( aDepth < m_tmin )
  82. m_tmin = aDepth;
  83. if( aDepth > m_tmax )
  84. m_tmax = aDepth;
  85. }
  86. }
  87. void CPOSTSHADER::destroy_buffers()
  88. {
  89. delete m_normals; m_normals = 0;
  90. delete m_color; m_color = 0;
  91. delete m_depth; m_depth = 0;
  92. delete m_shadow_att_factor; m_shadow_att_factor = 0;
  93. delete m_wc_hitposition; m_wc_hitposition = 0;
  94. }
  95. const SFVEC3F &CPOSTSHADER::GetNormalAt( const SFVEC2F &aPos ) const
  96. {
  97. return m_normals[ getIndex( aPos ) ];
  98. }
  99. const SFVEC3F &CPOSTSHADER::GetColorAt( const SFVEC2F &aPos ) const
  100. {
  101. return m_color[ getIndex( aPos ) ];
  102. }
  103. float CPOSTSHADER::GetDepthAt( const SFVEC2F &aPos ) const
  104. {
  105. return m_depth[ getIndex( aPos ) ];
  106. }
  107. const SFVEC3F &CPOSTSHADER::GetPositionAt( const SFVEC2F &aPos ) const
  108. {
  109. return m_wc_hitposition[ getIndex( aPos ) ];
  110. }
  111. const SFVEC3F &CPOSTSHADER::GetNormalAt( const SFVEC2I &aPos ) const
  112. {
  113. return m_normals[ getIndex( aPos ) ];
  114. }
  115. const SFVEC3F &CPOSTSHADER::GetColorAt( const SFVEC2I &aPos ) const
  116. {
  117. return m_color[ getIndex( aPos ) ];
  118. }
  119. const SFVEC3F &CPOSTSHADER::GetColorAtNotProtected( const SFVEC2I &aPos ) const
  120. {
  121. return m_color[ aPos.x + m_size.x * aPos.y ];
  122. }
  123. float CPOSTSHADER::GetDepthAt( const SFVEC2I &aPos ) const
  124. {
  125. return m_depth[ getIndex( aPos ) ];
  126. }
  127. float CPOSTSHADER::GetDepthNormalizedAt( const SFVEC2I &aPos ) const
  128. {
  129. const float depth = m_depth[ getIndex( aPos ) ];
  130. if( depth >= m_tmin )
  131. return (depth - m_tmin) / (m_tmax - m_tmin);
  132. return 0.0f;
  133. }
  134. const SFVEC3F &CPOSTSHADER::GetPositionAt( const SFVEC2I &aPos ) const
  135. {
  136. return m_wc_hitposition[ getIndex( aPos ) ];
  137. }
  138. const float &CPOSTSHADER::GetShadowFactorAt( const SFVEC2I &aPos ) const
  139. {
  140. return m_shadow_att_factor[ getIndex( aPos ) ];
  141. }
  142. void CPOSTSHADER::DebugBuffersOutputAsImages() const
  143. {
  144. DBG_SaveBuffer( "m_shadow_att_factor", m_shadow_att_factor, m_size.x, m_size.y );
  145. DBG_SaveBuffer( "m_color", m_color, m_size.x, m_size.y );
  146. DBG_SaveNormalsBuffer( "m_normals", m_normals, m_size.x, m_size.y );
  147. // Normalize depth
  148. // /////////////////////////////////////////////////////////////////////////
  149. float *normalizedDepth = (float*) malloc( m_size.x * m_size.y * sizeof( float ) );
  150. float *normalizedDepthPTr = normalizedDepth;
  151. for( unsigned int iy = 0; iy < m_size.y; ++iy )
  152. for( unsigned int ix = 0; ix < m_size.x; ++ix )
  153. {
  154. *normalizedDepthPTr = GetDepthNormalizedAt( SFVEC2I( ix, iy) );
  155. normalizedDepthPTr++;
  156. }
  157. DBG_SaveBuffer( "m_depthNormalized", normalizedDepth, m_size.x, m_size.y );
  158. free( normalizedDepth );
  159. }