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