KiCad PCB EDA Suite
post_shader.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <[email protected]>
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 
31 #include "post_shader.h"
32 #include "buffers_debug.h"
33 #include <wx/debug.h>
34 
35 
36 POST_SHADER::POST_SHADER( const CAMERA& aCamera ) :
37  m_camera( aCamera )
38 {
39  m_size = SFVEC2UI( 0, 0 );
40  m_normals = nullptr;
41  m_color = nullptr;
42  m_depth = nullptr;
43  m_wc_hitposition = nullptr;
44  m_shadow_att_factor = nullptr;
45  m_tmin = FLT_MAX;
46  m_tmax = FLT_MIN;
47 }
48 
49 
51 {
53 }
54 
55 
56 void POST_SHADER::UpdateSize( unsigned int xSize, unsigned int ySize )
57 {
59 
60  m_size.x = xSize;
61  m_size.y = ySize;
62 
63  const unsigned int n_elements = xSize * ySize;
64 
65  m_normals = new SFVEC3F[n_elements];
66  m_color = new SFVEC3F[n_elements];
67  m_depth = new float[n_elements];
68  m_wc_hitposition = new SFVEC3F[n_elements];
69  m_shadow_att_factor = new float[n_elements];
70 }
71 
72 
73 void POST_SHADER::UpdateSize( const SFVEC2UI& aSize )
74 {
75  UpdateSize( aSize.x, aSize.y );
76 }
77 
78 
79 void POST_SHADER::SetPixelData( unsigned int x, unsigned int y, const SFVEC3F& aNormal,
80  const SFVEC3F& aColor, const SFVEC3F& aHitPosition,
81  float aDepth, float aShadowAttFactor )
82 {
83  wxASSERT( x < m_size.x );
84  wxASSERT( y < m_size.y );
85  wxASSERT( ( aShadowAttFactor >= 0.0f ) && ( aShadowAttFactor <= 1.0f ) );
86 
87  const unsigned int idx = x + y * m_size.x;
88 
89  m_normals[ idx ] = aNormal;
90  m_color [ idx ] = aColor;
91  m_depth [ idx ] = aDepth;
92  m_shadow_att_factor [ idx ] = aShadowAttFactor;
93  m_wc_hitposition[ idx ] = aHitPosition;
94 
95 
96  if( aDepth > FLT_EPSILON )
97  {
98  if( aDepth < m_tmin )
99  m_tmin = aDepth;
100 
101  if( aDepth > m_tmax )
102  m_tmax = aDepth;
103  }
104 }
105 
106 
108 {
109  delete[] m_normals;
110  m_normals = nullptr;
111  delete[] m_color;
112  m_color = nullptr;
113  delete[] m_depth;
114  m_depth = nullptr;
115  delete[] m_shadow_att_factor;
116  m_shadow_att_factor = nullptr;
117  delete[] m_wc_hitposition;
118  m_wc_hitposition = nullptr;
119 }
120 
121 
122 const SFVEC3F& POST_SHADER::GetNormalAt( const SFVEC2F& aPos ) const
123 {
124  return m_normals[GetIndex( aPos )];
125 }
126 
127 
128 const SFVEC3F& POST_SHADER::GetColorAt( const SFVEC2F& aPos ) const
129 {
130  return m_color[GetIndex( aPos )];
131 }
132 
133 
134 float POST_SHADER::GetDepthAt( const SFVEC2F& aPos ) const
135 {
136  return m_depth[GetIndex( aPos )];
137 }
138 
139 
140 const SFVEC3F& POST_SHADER::GetPositionAt( const SFVEC2F& aPos ) const
141 {
142  return m_wc_hitposition[GetIndex( aPos )];
143 }
144 
145 
146 const SFVEC3F& POST_SHADER::GetNormalAt( const SFVEC2I& aPos ) const
147 {
148  return m_normals[GetIndex( aPos )];
149 }
150 
151 
152 const SFVEC3F& POST_SHADER::GetColorAt( const SFVEC2I& aPos ) const
153 {
154  return m_color[GetIndex( aPos )];
155 }
156 
157 
159 {
160  return m_color[ aPos.x + m_size.x * aPos.y ];
161 }
162 
163 
164 float POST_SHADER::GetDepthAt( const SFVEC2I& aPos ) const
165 {
166  return m_depth[GetIndex( aPos )];
167 }
168 
169 
170 float POST_SHADER::GetDepthNormalizedAt( const SFVEC2I& aPos ) const
171 {
172  const float depth = m_depth[GetIndex( aPos )];
173 
174  if( depth >= m_tmin )
175  return (depth - m_tmin) / (m_tmax - m_tmin);
176 
177  return 0.0f;
178 }
179 
180 
181 const SFVEC3F& POST_SHADER::GetPositionAt( const SFVEC2I& aPos ) const
182 {
183  return m_wc_hitposition[GetIndex( aPos )];
184 }
185 
186 
187 const float& POST_SHADER::GetShadowFactorAt( const SFVEC2I& aPos ) const
188 {
189  return m_shadow_att_factor[GetIndex( aPos )];
190 }
191 
192 
194 {
195  DBG_SaveBuffer( "m_shadow_att_factor", m_shadow_att_factor, m_size.x, m_size.y );
196  DBG_SaveBuffer( "m_color", m_color, m_size.x, m_size.y );
197  DBG_SaveNormalsBuffer( "m_normals", m_normals, m_size.x, m_size.y );
198 
199  // Normalize depth
200  float *normalizedDepth = (float*) malloc( m_size.x * m_size.y * sizeof( float ) );
201 
202  float *normalizedDepthPTr = normalizedDepth;
203 
204  for( unsigned int iy = 0; iy < m_size.y; ++iy )
205  {
206  for( unsigned int ix = 0; ix < m_size.x; ++ix )
207  {
208  *normalizedDepthPTr = GetDepthNormalizedAt( SFVEC2I( ix, iy) );
209  normalizedDepthPTr++;
210  }
211  }
212 
213  DBG_SaveBuffer( "m_depthNormalized", normalizedDepth, m_size.x, m_size.y );
214 
215  free( normalizedDepth );
216 }
virtual ~POST_SHADER()
Definition: post_shader.cpp:50
const SFVEC3F & GetColorAt(const SFVEC2F &aPos) const
float GetDepthAt(const SFVEC2F &aPos) const
float * m_depth
Definition: post_shader.h:114
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
void DBG_SaveBuffer(const wxString &aFileName, const unsigned char *aInBuffer, unsigned int aXSize, unsigned int aYSize)
void SetPixelData(unsigned int x, unsigned int y, const SFVEC3F &aNormal, const SFVEC3F &aColor, const SFVEC3F &aHitPosition, float aDepth, float aShadowAttFactor)
Definition: post_shader.cpp:79
void DebugBuffersOutputAsImages() const
A class used to derive camera objects from.
Definition: camera.h:77
glm::uvec2 SFVEC2UI
Definition: xv3d_types.h:38
SFVEC3F * m_wc_hitposition
Definition: post_shader.h:113
const SFVEC3F & GetColorAtNotProtected(const SFVEC2I &aPos) const
float m_tmin
Definition: post_shader.h:116
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66
POST_SHADER(const CAMERA &aCamera)
Definition: post_shader.cpp:36
A base class to create post shaders.
SFVEC3F * m_normals
Definition: post_shader.h:111
float * m_shadow_att_factor
Definition: post_shader.h:115
void DBG_SaveNormalsBuffer(const wxString &aFileName, const SFVEC3F *aInNormalsBuffer, unsigned int aXSize, unsigned int aYSize)
SFVEC3F * m_color
Definition: post_shader.h:112
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
SFVEC2UI m_size
Definition: post_shader.h:110
void UpdateSize(const SFVEC2UI &aSize)
Definition: post_shader.cpp:73
float GetDepthNormalizedAt(const SFVEC2I &aPos) const
const SFVEC3F & GetPositionAt(const SFVEC2F &aPos) const
const SFVEC3F & GetNormalAt(const SFVEC2F &aPos) const
float m_tmax
Definition: post_shader.h:117
const float & GetShadowFactorAt(const SFVEC2I &aPos) const
void destroy_buffers()