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