KiCad PCB EDA Suite
POST_SHADER_SSAO Class Reference

#include <post_shader_ssao.h>

Inheritance diagram for POST_SHADER_SSAO:
POST_SHADER

Public Member Functions

 POST_SHADER_SSAO (const CAMERA &aCamera)
 
SFVEC3F Shade (const SFVEC2I &aShaderPos) const override
 
SFVEC3F ApplyShadeColor (const SFVEC2I &aShaderPos, const SFVEC3F &aInputColor, const SFVEC3F &aShadeColor) const override
 Apply the final color process using a previous stage color. More...
 
SFVEC3F Blur (const SFVEC2I &aShaderPos) const
 
void SetShadedBuffer (SFVEC3F *aShadedBuffer)
 
void SetShadowsEnabled (bool aIsShadowsEnabled)
 
void UpdateSize (const SFVEC2UI &aSize)
 
void UpdateSize (unsigned int xSize, unsigned int ySize)
 
void InitFrame ()
 
void SetPixelData (unsigned int x, unsigned int y, const SFVEC3F &aNormal, const SFVEC3F &aColor, const SFVEC3F &aHitPosition, float aDepth, float aShadowAttFactor)
 
const SFVEC3FGetColorAtNotProtected (const SFVEC2I &aPos) const
 
void DebugBuffersOutputAsImages () const
 
unsigned int GetIndex (const SFVEC2F &aPos) const
 
unsigned int GetIndex (const SFVEC2I &aPos) const
 

Protected Member Functions

const SFVEC3FGetNormalAt (const SFVEC2F &aPos) const
 
const SFVEC3FGetNormalAt (const SFVEC2I &aPos) const
 
const SFVEC3FGetColorAt (const SFVEC2F &aPos) const
 
const SFVEC3FGetColorAt (const SFVEC2I &aPos) const
 
const SFVEC3FGetPositionAt (const SFVEC2F &aPos) const
 
const SFVEC3FGetPositionAt (const SFVEC2I &aPos) const
 
float GetDepthAt (const SFVEC2F &aPos) const
 
float GetDepthAt (const SFVEC2I &aPos) const
 
const float & GetShadowFactorAt (const SFVEC2I &aPos) const
 
float GetDepthNormalizedAt (const SFVEC2I &aPos) const
 
float GetMaxDepth () const
 

Protected Attributes

const CAMERAm_camera
 
SFVEC2UI m_size
 
SFVEC3Fm_normals
 
SFVEC3Fm_color
 
SFVEC3Fm_wc_hitposition
 
float * m_depth
 
float * m_shadow_att_factor
 
float m_tmin
 
float m_tmax
 

Private Member Functions

SFVEC3F posFromDepth (const SFVEC2F &coord) const
 
float ec_depth (const SFVEC2F &tc) const
 
float aoFF (const SFVEC2I &aShaderPos, const SFVEC3F &ddiff, const SFVEC3F &cnorm, const float aShadowAtSamplePos, const float aShadowAtCenterPos, int c1, int c2) const
 
float giFF (const SFVEC2I &aShaderPos, const SFVEC3F &ddiff, const SFVEC3F &cnorm, const float aShadow, int c1, int c2) const
 
SFVEC3F giColorCurve (const SFVEC3F &aColor) const
 Apply a curve transformation to the original color. More...
 

Private Attributes

SFVEC3Fm_shadedBuffer
 
bool m_isUsingShadows
 

Detailed Description

Definition at line 37 of file post_shader_ssao.h.

Constructor & Destructor Documentation

◆ POST_SHADER_SSAO()

POST_SHADER_SSAO::POST_SHADER_SSAO ( const CAMERA aCamera)
explicit

Definition at line 34 of file post_shader_ssao.cpp.

34  :
35  POST_SHADER( aCamera ),
36  m_shadedBuffer( nullptr ),
37  m_isUsingShadows( false )
38 {
39 }
POST_SHADER(const CAMERA &aCamera)
Definition: post_shader.cpp:36
SFVEC3F * m_shadedBuffer

Member Function Documentation

◆ aoFF()

float POST_SHADER_SSAO::aoFF ( const SFVEC2I aShaderPos,
const SFVEC3F ddiff,
const SFVEC3F cnorm,
const float  aShadowAtSamplePos,
const float  aShadowAtCenterPos,
int  c1,
int  c2 
) const
private

Definition at line 47 of file post_shader_ssao.cpp.

50 {
51  const float shadowGain = 0.60f;
52  const float aoGain = 1.0f;
53 
54  const float shadow_factor_at_sample = ( 1.0f - aShadowAtSamplePos ) * shadowGain;
55  const float shadow_factor_at_center = ( 1.0f - aShadowAtCenterPos ) * shadowGain;
56 
57  float return_value = shadow_factor_at_center;
58 
59  const float rd = glm::length( ddiff );
60 
61  // This limits the zero of the function (see below)
62  if( rd < 2.0f )
63  {
64  if( rd > FLT_EPSILON )
65  {
66  const SFVEC3F vv = glm::normalize( ddiff );
67 
68  // Calculate an attenuation distance factor, this was get the best
69  // results by experimentation
70  // Changing this factor will change how much shadow in relation to the
71  // distance of the hit it will be in shadow
72 
73  // http://www.fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIwLjgteCowLjYiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjAsImVxIjoiMS8oeCp4KjAuNSsxKSIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMCwid2luZG93IjpbIi0yLjU5Mjk0NTkyNTA5ODA0MSIsIjQuNTUzODc5NjU1NDQ1OTIzIiwiLTEuNzY3MDMwOTAzMjgxNjgxOCIsIjIuNjMxMDE1NjA3ODIyMjk3Il0sInNpemUiOls2NDksMzk5XX1d
74  const float attDistFactor = 1.0f / ( rd * rd * 8.0f + 1.0f );
75 
76  const SFVEC2I vr = aShaderPos + SFVEC2I( c1, c2 );
77 
78  float sampledNormalFactor = glm::max( glm::dot( GetNormalAt( vr ), cnorm ), 0.0f );
79 
80  sampledNormalFactor = glm::max( 1.0f - sampledNormalFactor *
81  sampledNormalFactor, 0.0f );
82 
83  const float shadowAttDistFactor = glm::max( glm::min( rd * 5.0f - 0.25f, 1.0f ), 0.0f );
84 
85  float shadowAttFactor = glm::min( sampledNormalFactor + shadowAttDistFactor, 1.0f );
86 
87  const float shadowFactor = glm::mix( shadow_factor_at_sample, shadow_factor_at_center,
88  shadowAttFactor );
89 
90  // This is a dot product threshold factor.
91  // it defines after which angle we consider that the point starts to occlude.
92  // if the value is high, it will discard low angles point
93  const float aDotThreshold = 0.15f;
94 
95  // This is the dot product between the center pixel normal (the one that is being
96  // shaded) and the vector from the center to the sampled point
97  const float localNormalFactor = glm::dot( cnorm, vv );
98 
99  const float localNormalFactorWithThreshold =
100  ( glm::max( localNormalFactor, aDotThreshold ) - aDotThreshold ) /
101  ( 1.0f - aDotThreshold );
102 
103  const float aoFactor = localNormalFactorWithThreshold * aoGain * attDistFactor;
104 
105  return_value = glm::min( aoFactor + shadowFactor, 1.0f );
106  }
107  }
108 
109  return return_value;
110 }
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
const SFVEC3F & GetNormalAt(const SFVEC2F &aPos) const

References POST_SHADER::GetNormalAt().

Referenced by Shade().

◆ ApplyShadeColor()

SFVEC3F POST_SHADER_SSAO::ApplyShadeColor ( const SFVEC2I aShaderPos,
const SFVEC3F aInputColor,
const SFVEC3F aShadeColor 
) const
overridevirtual

Apply the final color process using a previous stage color.

Parameters
aShadeColorThe result of the shader.
Returns
the result of the shade process

Implements POST_SHADER.

Definition at line 230 of file post_shader_ssao.cpp.

232 {
233  SFVEC3F outColor;
234 
235  const SFVEC3F subtracted = aInputColor - aShadeColor;
236  const SFVEC3F mixed = glm::mix( aInputColor, aInputColor * 0.50f - aShadeColor * 0.05f,
237  glm::min( aShadeColor, 1.0f ) );
238 
239  outColor.r = ( aShadeColor.r < 0.0f ) ? subtracted.r : mixed.r;
240  outColor.g = ( aShadeColor.g < 0.0f ) ? subtracted.g : mixed.g;
241  outColor.b = ( aShadeColor.b < 0.0f ) ? subtracted.b : mixed.b;
242 
243  return outColor;
244 }
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44

Referenced by RENDER_3D_RAYTRACE::postProcessBlurFinish().

◆ Blur()

SFVEC3F POST_SHADER_SSAO::Blur ( const SFVEC2I aShaderPos) const

Definition at line 259 of file post_shader_ssao.cpp.

260 {
261  const float dCenter = GetDepthAt( aShaderPos );
262 
263  SFVEC3F shadedOut = SFVEC3F( 0.0f );
264 
265  float totalWeight = 1.0f;
266 
267  for( int y = -3; y < 3; y++ )
268  {
269  for( int x = -3; x < 3; x++ )
270  {
271 
272  const unsigned int idx = GetIndex( SFVEC2I( aShaderPos.x + x, aShaderPos.y + y ) );
273 
274  const SFVEC3F s = m_shadedBuffer[idx];
275 
276  if( !( ( x == 0 ) && ( y == 0 ) ) )
277  {
278 
279  const float d = GetDepthAt( SFVEC2I( aShaderPos.x + x, aShaderPos.y + y ) );
280 
281  // Increasing the value will get more sharpness effect.
282  const float depthAtt = ( dCenter - d ) * dCenter * 25.0f;
283 
284  const float depthAttSqr = depthAtt * depthAtt;
285 
286  float weight = ( 1.0f / ( depthAttSqr + 1.0f ) ) - 0.02f * depthAttSqr;
287 
288  weight = glm::max( weight, 0.0f );
289 
290  shadedOut += s * weight;
291  totalWeight += weight;
292  }
293  else
294  {
295  shadedOut += s;
296  }
297  }
298  }
299 
300  return shadedOut / totalWeight;
301 }
float GetDepthAt(const SFVEC2F &aPos) const
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66
SFVEC3F * m_shadedBuffer
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44

References POST_SHADER::GetDepthAt(), POST_SHADER::GetIndex(), and m_shadedBuffer.

Referenced by RENDER_3D_RAYTRACE::postProcessBlurFinish().

◆ DebugBuffersOutputAsImages()

void POST_SHADER::DebugBuffersOutputAsImages ( ) const
inherited

Definition at line 193 of file post_shader.cpp.

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 }
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
void DBG_SaveBuffer(const wxString &aFileName, const unsigned char *aInBuffer, unsigned int aXSize, unsigned int aYSize)
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
SFVEC2UI m_size
Definition: post_shader.h:110
float GetDepthNormalizedAt(const SFVEC2I &aPos) const

References DBG_SaveBuffer(), DBG_SaveNormalsBuffer(), POST_SHADER::GetDepthNormalizedAt(), POST_SHADER::m_color, POST_SHADER::m_normals, POST_SHADER::m_shadow_att_factor, and POST_SHADER::m_size.

◆ ec_depth()

float POST_SHADER_SSAO::ec_depth ( const SFVEC2F tc) const
private

◆ GetColorAt() [1/2]

const SFVEC3F & POST_SHADER::GetColorAt ( const SFVEC2F aPos) const
protectedinherited

Definition at line 128 of file post_shader.cpp.

129 {
130  return m_color[GetIndex( aPos )];
131 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66
SFVEC3F * m_color
Definition: post_shader.h:112

References POST_SHADER::GetIndex(), and POST_SHADER::m_color.

Referenced by Shade().

◆ GetColorAt() [2/2]

const SFVEC3F & POST_SHADER::GetColorAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 152 of file post_shader.cpp.

153 {
154  return m_color[GetIndex( aPos )];
155 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66
SFVEC3F * m_color
Definition: post_shader.h:112

References POST_SHADER::GetIndex(), and POST_SHADER::m_color.

◆ GetColorAtNotProtected()

const SFVEC3F & POST_SHADER::GetColorAtNotProtected ( const SFVEC2I aPos) const
inherited

Definition at line 158 of file post_shader.cpp.

159 {
160  return m_color[ aPos.x + m_size.x * aPos.y ];
161 }
SFVEC3F * m_color
Definition: post_shader.h:112
SFVEC2UI m_size
Definition: post_shader.h:110

References POST_SHADER::m_color, and POST_SHADER::m_size.

Referenced by RENDER_3D_RAYTRACE::postProcessBlurFinish().

◆ GetDepthAt() [1/2]

float POST_SHADER::GetDepthAt ( const SFVEC2F aPos) const
protectedinherited

Definition at line 134 of file post_shader.cpp.

135 {
136  return m_depth[GetIndex( aPos )];
137 }
float * m_depth
Definition: post_shader.h:114
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66

References POST_SHADER::GetIndex(), and POST_SHADER::m_depth.

Referenced by Blur(), and Shade().

◆ GetDepthAt() [2/2]

float POST_SHADER::GetDepthAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 164 of file post_shader.cpp.

165 {
166  return m_depth[GetIndex( aPos )];
167 }
float * m_depth
Definition: post_shader.h:114
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66

References POST_SHADER::GetIndex(), and POST_SHADER::m_depth.

◆ GetDepthNormalizedAt()

float POST_SHADER::GetDepthNormalizedAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 170 of file post_shader.cpp.

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 }
float * m_depth
Definition: post_shader.h:114
float m_tmin
Definition: post_shader.h:116
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66
float m_tmax
Definition: post_shader.h:117

References POST_SHADER::GetIndex(), POST_SHADER::m_depth, POST_SHADER::m_tmax, and POST_SHADER::m_tmin.

Referenced by POST_SHADER::DebugBuffersOutputAsImages().

◆ GetIndex() [1/2]

unsigned int POST_SHADER::GetIndex ( const SFVEC2F aPos) const
inlineinherited

Definition at line 66 of file post_shader.h.

67  {
68  SFVEC2F clampPos;
69 
70  clampPos.x = glm::clamp( aPos.x, 0.0f, 1.0f );
71  clampPos.y = glm::clamp( aPos.y, 0.0f, 1.0f );
72 
73  const unsigned int idx = (unsigned int)( (float)m_size.x * clampPos.x +
74  (float)m_size.x * (float)m_size.y *
75  clampPos.y );
76 
77  return glm::min( idx, m_size.x * m_size.y );
78  }
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
SFVEC2UI m_size
Definition: post_shader.h:110

References POST_SHADER::m_size.

Referenced by Blur(), POST_SHADER::GetColorAt(), POST_SHADER::GetDepthAt(), POST_SHADER::GetDepthNormalizedAt(), POST_SHADER::GetNormalAt(), POST_SHADER::GetPositionAt(), and POST_SHADER::GetShadowFactorAt().

◆ GetIndex() [2/2]

unsigned int POST_SHADER::GetIndex ( const SFVEC2I aPos) const
inlineinherited

Definition at line 80 of file post_shader.h.

81  {
82  SFVEC2I clampPos;
83  clampPos.x = glm::clamp( aPos.x, 0, (int)m_size.x - 1 );
84  clampPos.y = glm::clamp( aPos.y, 0, (int)m_size.y - 1 );
85 
86  return (unsigned int)( clampPos.x + m_size.x * clampPos.y );
87  }
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
SFVEC2UI m_size
Definition: post_shader.h:110

References POST_SHADER::m_size.

◆ GetMaxDepth()

float POST_SHADER::GetMaxDepth ( ) const
inlineprotectedinherited

Definition at line 102 of file post_shader.h.

102 { return m_tmax; }
float m_tmax
Definition: post_shader.h:117

References POST_SHADER::m_tmax.

◆ GetNormalAt() [1/2]

const SFVEC3F & POST_SHADER::GetNormalAt ( const SFVEC2F aPos) const
protectedinherited

Definition at line 122 of file post_shader.cpp.

123 {
124  return m_normals[GetIndex( aPos )];
125 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66
SFVEC3F * m_normals
Definition: post_shader.h:111

References POST_SHADER::GetIndex(), and POST_SHADER::m_normals.

Referenced by aoFF(), giFF(), and Shade().

◆ GetNormalAt() [2/2]

const SFVEC3F & POST_SHADER::GetNormalAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 146 of file post_shader.cpp.

147 {
148  return m_normals[GetIndex( aPos )];
149 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66
SFVEC3F * m_normals
Definition: post_shader.h:111

References POST_SHADER::GetIndex(), and POST_SHADER::m_normals.

◆ GetPositionAt() [1/2]

const SFVEC3F & POST_SHADER::GetPositionAt ( const SFVEC2F aPos) const
protectedinherited

Definition at line 140 of file post_shader.cpp.

141 {
142  return m_wc_hitposition[GetIndex( aPos )];
143 }
SFVEC3F * m_wc_hitposition
Definition: post_shader.h:113
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66

References POST_SHADER::GetIndex(), and POST_SHADER::m_wc_hitposition.

Referenced by Shade().

◆ GetPositionAt() [2/2]

const SFVEC3F & POST_SHADER::GetPositionAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 181 of file post_shader.cpp.

182 {
183  return m_wc_hitposition[GetIndex( aPos )];
184 }
SFVEC3F * m_wc_hitposition
Definition: post_shader.h:113
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66

References POST_SHADER::GetIndex(), and POST_SHADER::m_wc_hitposition.

◆ GetShadowFactorAt()

const float & POST_SHADER::GetShadowFactorAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 187 of file post_shader.cpp.

188 {
189  return m_shadow_att_factor[GetIndex( aPos )];
190 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: post_shader.h:66
float * m_shadow_att_factor
Definition: post_shader.h:115

References POST_SHADER::GetIndex(), and POST_SHADER::m_shadow_att_factor.

Referenced by Shade().

◆ giColorCurve()

SFVEC3F POST_SHADER_SSAO::giColorCurve ( const SFVEC3F aColor) const
private

Apply a curve transformation to the original color.

It will attenuate the bright colors (works as a gamma function): http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIxLjAtKDEvKHgqMS4wKzEuMCkpK3gqMC4zMCIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMCwid2luZG93IjpbIi0wLjA2MjE4NDYxNTM4NDYxNTUwNSIsIjEuMTQyOTg0NjE1Mzg0NjE0NiIsIi0wLjEyNzA5OTk5OTk5OTk5OTc3IiwiMS4xMzI2Il19XQ&ndash;

Parameters
aColorinput color.
Returns
transformed color.

Definition at line 247 of file post_shader_ssao.cpp.

248 {
249  const SFVEC3F vec1 = SFVEC3F( 1.0f );
250 
251  // This option actually apply a gamma since we are using linear color space
252  // and the result shader will be applied after convert back to sRGB
253 
254  // http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIxLjAtKDEuMC8oeCo5LjArMS4wKSkreCowLjEiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyItMC4wNjIxODQ2MTUzODQ2MTU1MDUiLCIxLjE0Mjk4NDYxNTM4NDYxNDYiLCItMC4xMjcwOTk5OTk5OTk5OTk3NyIsIjEuMTMyNiJdfV0-
255  return vec1 - ( vec1 / (aColor * SFVEC3F(9.0f) + vec1) ) + aColor * SFVEC3F(0.10f);
256 }
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44

Referenced by Shade().

◆ giFF()

float POST_SHADER_SSAO::giFF ( const SFVEC2I aShaderPos,
const SFVEC3F ddiff,
const SFVEC3F cnorm,
const float  aShadow,
int  c1,
int  c2 
) const
private

Definition at line 113 of file post_shader_ssao.cpp.

115 {
116  if( ( ddiff.x > FLT_EPSILON ) || ( ddiff.y > FLT_EPSILON ) || ( ddiff.z > FLT_EPSILON ) )
117  {
118  const SFVEC3F vv = glm::normalize( ddiff );
119  const float rd = glm::length( ddiff );
120  const SFVEC2I vr = aShaderPos + SFVEC2I( c1, c2 );
121 
122  const float attDistFactor = 1.0f / ( rd * rd + 1.0f );
123 
124  return ( glm::clamp( glm::dot( GetNormalAt( vr ), -vv), 0.0f, 1.0f ) *
125  glm::clamp( glm::dot( cnorm, vv ), 0.0f, 1.0f ) * attDistFactor ) *
126  ( 0.03f + aShadow ) * 3.0f;
127  }
128 
129  return 0.0f;
130 }
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
const SFVEC3F & GetNormalAt(const SFVEC2F &aPos) const

References POST_SHADER::GetNormalAt().

Referenced by Shade().

◆ InitFrame()

void POST_SHADER::InitFrame ( )
inlineinherited

Definition at line 56 of file post_shader.h.

56 { m_tmin = FLT_MAX; m_tmax = 0.0f; }
float m_tmin
Definition: post_shader.h:116
float m_tmax
Definition: post_shader.h:117

References POST_SHADER::m_tmax, and POST_SHADER::m_tmin.

Referenced by RENDER_3D_RAYTRACE::restartRenderState().

◆ posFromDepth()

SFVEC3F POST_SHADER_SSAO::posFromDepth ( const SFVEC2F coord) const
private

◆ SetPixelData()

void POST_SHADER::SetPixelData ( unsigned int  x,
unsigned int  y,
const SFVEC3F aNormal,
const SFVEC3F aColor,
const SFVEC3F aHitPosition,
float  aDepth,
float  aShadowAttFactor 
)
inherited

Definition at line 79 of file post_shader.cpp.

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 }
float * m_depth
Definition: post_shader.h:114
SFVEC3F * m_wc_hitposition
Definition: post_shader.h:113
float m_tmin
Definition: post_shader.h:116
SFVEC3F * m_normals
Definition: post_shader.h:111
float * m_shadow_att_factor
Definition: post_shader.h:115
SFVEC3F * m_color
Definition: post_shader.h:112
SFVEC2UI m_size
Definition: post_shader.h:110
float m_tmax
Definition: post_shader.h:117

References POST_SHADER::m_color, POST_SHADER::m_depth, POST_SHADER::m_normals, POST_SHADER::m_shadow_att_factor, POST_SHADER::m_size, POST_SHADER::m_tmax, POST_SHADER::m_tmin, and POST_SHADER::m_wc_hitposition.

Referenced by RENDER_3D_RAYTRACE::renderBlockTracing().

◆ SetShadedBuffer()

void POST_SHADER_SSAO::SetShadedBuffer ( SFVEC3F aShadedBuffer)
inline

Definition at line 48 of file post_shader_ssao.h.

49  {
50  m_shadedBuffer = aShadedBuffer;
51  }
SFVEC3F * m_shadedBuffer

References m_shadedBuffer.

Referenced by RENDER_3D_RAYTRACE::postProcessShading().

◆ SetShadowsEnabled()

void POST_SHADER_SSAO::SetShadowsEnabled ( bool  aIsShadowsEnabled)
inline

Definition at line 53 of file post_shader_ssao.h.

54  {
55  m_isUsingShadows = aIsShadowsEnabled;
56  }

References m_isUsingShadows.

Referenced by RENDER_3D_RAYTRACE::postProcessShading().

◆ Shade()

SFVEC3F POST_SHADER_SSAO::Shade ( const SFVEC2I aShaderPos) const
overridevirtual

Implements POST_SHADER.

Definition at line 133 of file post_shader_ssao.cpp.

134 {
135  float cdepth = GetDepthAt( aShaderPos );
136 
137  if( cdepth > FLT_EPSILON )
138  {
139  cdepth = ( 30.0f / ( cdepth * 2.0f + 1.0f ) );
140 
141  // read current normal, position and color.
142  const SFVEC3F n = GetNormalAt( aShaderPos );
143  const SFVEC3F p = GetPositionAt( aShaderPos );
144 
145  const float shadowAt0 = GetShadowFactorAt( aShaderPos );
146 
147  // initialize variables:
148  float ao = 0.0f;
149  SFVEC3F gi = SFVEC3F( 0.0f );
150 
151 #define ROUNDS 3
152  for( unsigned int i = 0; i < ROUNDS; ++i )
153  {
154  static const int limit[ROUNDS] = { 0x01, 0x03, 0x03 };
155 
156  const int pw = Fast_rand() & limit[i];
157  const int ph = Fast_rand() & limit[i];
158 
159  const int npw = (int) ( ( pw + i ) * cdepth ) + ( i + 1 );
160  const int nph = (int) ( ( ph + i ) * cdepth ) + ( i + 1 );
161 
162  const SFVEC3F ddiff = GetPositionAt( aShaderPos + SFVEC2I( npw, nph ) ) - p;
163  const SFVEC3F ddiff2 = GetPositionAt( aShaderPos + SFVEC2I( npw, -nph ) ) - p;
164  const SFVEC3F ddiff3 = GetPositionAt( aShaderPos + SFVEC2I( -npw, nph ) ) - p;
165  const SFVEC3F ddiff4 = GetPositionAt( aShaderPos + SFVEC2I( -npw, -nph ) ) - p;
166  const SFVEC3F ddiff5 = GetPositionAt( aShaderPos + SFVEC2I( pw, nph ) ) - p;
167  const SFVEC3F ddiff6 = GetPositionAt( aShaderPos + SFVEC2I( pw, -nph ) ) - p;
168  const SFVEC3F ddiff7 = GetPositionAt( aShaderPos + SFVEC2I( npw, ph ) ) - p;
169  const SFVEC3F ddiff8 = GetPositionAt( aShaderPos + SFVEC2I(-npw, ph ) ) - p;
170 
171  const float shadowAt1 = GetShadowFactorAt( aShaderPos + SFVEC2I( +npw, nph ) );
172  const float shadowAt2 = GetShadowFactorAt( aShaderPos + SFVEC2I( +npw, -nph ) );
173  const float shadowAt3 = GetShadowFactorAt( aShaderPos + SFVEC2I( -npw, nph ) );
174  const float shadowAt4 = GetShadowFactorAt( aShaderPos + SFVEC2I( -npw, -nph ) );
175  const float shadowAt5 = GetShadowFactorAt( aShaderPos + SFVEC2I( +pw, nph ) );
176  const float shadowAt6 = GetShadowFactorAt( aShaderPos + SFVEC2I( pw, -nph ) );
177  const float shadowAt7 = GetShadowFactorAt( aShaderPos + SFVEC2I( npw, ph ) );
178  const float shadowAt8 = GetShadowFactorAt( aShaderPos + SFVEC2I( -npw, ph ) );
179 
180  ao += aoFF( aShaderPos, ddiff , n, shadowAt1, shadowAt0, npw, nph );
181  ao += aoFF( aShaderPos, ddiff2, n, shadowAt2, shadowAt0, npw, -nph );
182  ao += aoFF( aShaderPos, ddiff3, n, shadowAt3, shadowAt0, -npw, nph );
183  ao += aoFF( aShaderPos, ddiff4, n, shadowAt4, shadowAt0, -npw, -nph );
184  ao += aoFF( aShaderPos, ddiff5, n, shadowAt5, shadowAt0, pw, nph );
185  ao += aoFF( aShaderPos, ddiff6, n, shadowAt6, shadowAt0, pw, -nph );
186  ao += aoFF( aShaderPos, ddiff7, n, shadowAt7, shadowAt0, npw, ph );
187  ao += aoFF( aShaderPos, ddiff8, n, shadowAt8, shadowAt0, -npw, ph );
188 
189  gi += giFF( aShaderPos, ddiff , n, shadowAt1, npw, nph) *
190  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( npw, nph ) ) );
191  gi += giFF( aShaderPos, ddiff2, n, shadowAt2, npw, -nph) *
192  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( npw,-nph ) ) );
193  gi += giFF( aShaderPos, ddiff3, n, shadowAt3, -npw, nph) *
194  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( -npw, nph ) ) );
195  gi += giFF( aShaderPos, ddiff4, n, shadowAt4, -npw, -nph) *
196  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( -npw,-nph ) ) );
197  gi += giFF( aShaderPos, ddiff5, n, shadowAt5 , pw, nph) *
198  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( pw, nph ) ) );
199  gi += giFF( aShaderPos, ddiff6, n, shadowAt6, pw,-nph) *
200  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( pw,-nph ) ) );
201  gi += giFF( aShaderPos, ddiff7, n, shadowAt7, npw, ph) *
202  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( npw, ph ) ) );
203  gi += giFF( aShaderPos, ddiff8, n, shadowAt8, -npw, ph) *
204  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( -npw, ph ) ) );
205  }
206 
207  // If it received direct light, it shouldn't consider much AO
208  // shadowAt0 1.0 when no shadow
209  const float reduceAOwhenNoShadow = m_isUsingShadows ? ( 1.0f - shadowAt0 * 0.3f ) : 1.0f;
210 
211  ao = reduceAOwhenNoShadow * ( ao / ( ROUNDS * 8.0f ) );
212 
213  ao = ( 1.0f - 1.0f / ( ao * ao * 5.0f + 1.0f ) ) * 1.2f;
214 
215  gi = ( gi / ( ROUNDS * 8.0f ) );
216 
217  float giL = glm::min( glm::length( gi ) * 4.0f, 1.0f );
218 
219  giL = ( 1.0f - 1.0f / ( giL * 4.0f + 1.0f ) ) * 1.5f;
220 
221  return glm::mix( SFVEC3F( ao ), -gi, giL );
222  }
223  else
224  {
225  return SFVEC3F( 0.0f );
226  }
227 }
const SFVEC3F & GetColorAt(const SFVEC2F &aPos) const
#define ROUNDS
float GetDepthAt(const SFVEC2F &aPos) const
int Fast_rand(void)
Definition: 3d_fastmath.cpp:58
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
float giFF(const SFVEC2I &aShaderPos, const SFVEC3F &ddiff, const SFVEC3F &cnorm, const float aShadow, int c1, int c2) const
SFVEC3F giColorCurve(const SFVEC3F &aColor) const
Apply a curve transformation to the original color.
float aoFF(const SFVEC2I &aShaderPos, const SFVEC3F &ddiff, const SFVEC3F &cnorm, const float aShadowAtSamplePos, const float aShadowAtCenterPos, int c1, int c2) const
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
const SFVEC3F & GetPositionAt(const SFVEC2F &aPos) const
const SFVEC3F & GetNormalAt(const SFVEC2F &aPos) const
const float & GetShadowFactorAt(const SFVEC2I &aPos) const

References aoFF(), Fast_rand(), POST_SHADER::GetColorAt(), POST_SHADER::GetDepthAt(), POST_SHADER::GetNormalAt(), POST_SHADER::GetPositionAt(), POST_SHADER::GetShadowFactorAt(), giColorCurve(), giFF(), m_isUsingShadows, and ROUNDS.

Referenced by RENDER_3D_RAYTRACE::postProcessShading().

◆ UpdateSize() [1/2]

void POST_SHADER::UpdateSize ( const SFVEC2UI aSize)
inherited

Definition at line 73 of file post_shader.cpp.

74 {
75  UpdateSize( aSize.x, aSize.y );
76 }
void UpdateSize(const SFVEC2UI &aSize)
Definition: post_shader.cpp:73

Referenced by RENDER_3D_RAYTRACE::initializeBlockPositions().

◆ UpdateSize() [2/2]

void POST_SHADER::UpdateSize ( unsigned int  xSize,
unsigned int  ySize 
)
inherited

Definition at line 56 of file post_shader.cpp.

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 }
float * m_depth
Definition: post_shader.h:114
SFVEC3F * m_wc_hitposition
Definition: post_shader.h:113
SFVEC3F * m_normals
Definition: post_shader.h:111
float * m_shadow_att_factor
Definition: post_shader.h:115
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 destroy_buffers()

References POST_SHADER::destroy_buffers(), POST_SHADER::m_color, POST_SHADER::m_depth, POST_SHADER::m_normals, POST_SHADER::m_shadow_att_factor, POST_SHADER::m_size, and POST_SHADER::m_wc_hitposition.

Member Data Documentation

◆ m_camera

const CAMERA& POST_SHADER::m_camera
protectedinherited

Definition at line 108 of file post_shader.h.

◆ m_color

◆ m_depth

◆ m_isUsingShadows

bool POST_SHADER_SSAO::m_isUsingShadows
private

Definition at line 83 of file post_shader_ssao.h.

Referenced by SetShadowsEnabled(), and Shade().

◆ m_normals

◆ m_shadedBuffer

SFVEC3F* POST_SHADER_SSAO::m_shadedBuffer
private

Definition at line 81 of file post_shader_ssao.h.

Referenced by Blur(), and SetShadedBuffer().

◆ m_shadow_att_factor

◆ m_size

◆ m_tmax

float POST_SHADER::m_tmax
protectedinherited

◆ m_tmin

float POST_SHADER::m_tmin
protectedinherited

◆ m_wc_hitposition

SFVEC3F* POST_SHADER::m_wc_hitposition
protectedinherited

The documentation for this class was generated from the following files: