KiCad PCB EDA Suite
CPOSTSHADER_SSAO Class Reference

#include <cpostshader_ssao.h>

Inheritance diagram for CPOSTSHADER_SSAO:
CPOSTSHADER

Public Member Functions

 CPOSTSHADER_SSAO (const CCAMERA &aCamera)
 
SFVEC3F Shade (const SFVEC2I &aShaderPos) const override
 
SFVEC3F ApplyShadeColor (const SFVEC2I &aShaderPos, const SFVEC3F &aInputColor, const SFVEC3F &aShadeColor) const override
 ApplyShadeColor - 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 CCAMERAm_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
 giColorCurve - Apply a curve transformation to the original color it will atenuate the bright colors (works as a gamma function): http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIxLjAtKDEvKHgqMS4wKzEuMCkpK3gqMC4zMCIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMCwid2luZG93IjpbIi0wLjA2MjE4NDYxNTM4NDYxNTUwNSIsIjEuMTQyOTg0NjE1Mzg0NjE0NiIsIi0wLjEyNzA5OTk5OTk5OTk5OTc3IiwiMS4xMzI2Il19XQ&ndash; More...
 

Private Attributes

SFVEC3Fm_shadedBuffer
 
bool m_isUsingShadows
 

Detailed Description

Definition at line 37 of file cpostshader_ssao.h.

Constructor & Destructor Documentation

◆ CPOSTSHADER_SSAO()

CPOSTSHADER_SSAO::CPOSTSHADER_SSAO ( const CCAMERA aCamera)
explicit

Definition at line 34 of file cpostshader_ssao.cpp.

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

Member Function Documentation

◆ aoFF()

float CPOSTSHADER_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 cpostshader_ssao.cpp.

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

References CPOSTSHADER::GetNormalAt().

Referenced by Shade().

◆ ApplyShadeColor()

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

ApplyShadeColor - apply the final color process using a previous stage color.

Parameters
aShadeColor- The result of the shader
Returns
the result of the shade process

Implements CPOSTSHADER.

Definition at line 236 of file cpostshader_ssao.cpp.

237 {
238  SFVEC3F outColor;
239 
240  const SFVEC3F subtracted = aInputColor - aShadeColor;
241  const SFVEC3F mixed = glm::mix( aInputColor,
242  aInputColor * 0.50f - aShadeColor * 0.05f,
243  glm::min( aShadeColor, 1.0f ) );
244 
245  outColor.r = ( aShadeColor.r < 0.0f ) ? subtracted.r : mixed.r;
246  outColor.g = ( aShadeColor.g < 0.0f ) ? subtracted.g : mixed.g;
247  outColor.b = ( aShadeColor.b < 0.0f ) ? subtracted.b : mixed.b;
248 
249  return outColor;
250 }
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47

Referenced by C3D_RENDER_RAYTRACING::rt_render_post_process_blur_finish().

◆ Blur()

SFVEC3F CPOSTSHADER_SSAO::Blur ( const SFVEC2I aShaderPos) const

Definition at line 264 of file cpostshader_ssao.cpp.

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

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

Referenced by C3D_RENDER_RAYTRACING::rt_render_post_process_blur_finish().

◆ DebugBuffersOutputAsImages()

void CPOSTSHADER::DebugBuffersOutputAsImages ( ) const
inherited

Definition at line 191 of file cpostshader.cpp.

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 }
SFVEC2UI m_size
Definition: cpostshader.h:113
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:42
void DBG_SaveBuffer(const wxString &aFileName, const unsigned char *aInBuffer, unsigned int aXSize, unsigned int aYSize)
SFVEC3F * m_normals
Definition: cpostshader.h:114
SFVEC3F * m_color
Definition: cpostshader.h:115
float GetDepthNormalizedAt(const SFVEC2I &aPos) const
void DBG_SaveNormalsBuffer(const wxString &aFileName, const SFVEC3F *aInNormalsBuffer, unsigned int aXSize, unsigned int aYSize)
float * m_shadow_att_factor
Definition: cpostshader.h:118

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

◆ ec_depth()

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

◆ GetColorAt() [1/2]

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

Definition at line 126 of file cpostshader.cpp.

127 {
128  return m_color[GetIndex( aPos )];
129 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
SFVEC3F * m_color
Definition: cpostshader.h:115

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

Referenced by Shade().

◆ GetColorAt() [2/2]

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

Definition at line 150 of file cpostshader.cpp.

151 {
152  return m_color[GetIndex( aPos )];
153 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
SFVEC3F * m_color
Definition: cpostshader.h:115

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

◆ GetColorAtNotProtected()

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

Definition at line 156 of file cpostshader.cpp.

157 {
158  return m_color[ aPos.x + m_size.x * aPos.y ];
159 }
SFVEC2UI m_size
Definition: cpostshader.h:113
SFVEC3F * m_color
Definition: cpostshader.h:115

References CPOSTSHADER::m_color, and CPOSTSHADER::m_size.

Referenced by C3D_RENDER_RAYTRACING::rt_render_post_process_blur_finish().

◆ GetDepthAt() [1/2]

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

Definition at line 132 of file cpostshader.cpp.

133 {
134  return m_depth[GetIndex( aPos )];
135 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
float * m_depth
Definition: cpostshader.h:117

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

Referenced by Blur(), and Shade().

◆ GetDepthAt() [2/2]

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

Definition at line 162 of file cpostshader.cpp.

163 {
164  return m_depth[GetIndex( aPos )];
165 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
float * m_depth
Definition: cpostshader.h:117

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

◆ GetDepthNormalizedAt()

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

Definition at line 168 of file cpostshader.cpp.

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 }
float m_tmax
Definition: cpostshader.h:120
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
float * m_depth
Definition: cpostshader.h:117
float m_tmin
Definition: cpostshader.h:119

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

Referenced by CPOSTSHADER::DebugBuffersOutputAsImages().

◆ GetIndex() [1/2]

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

Definition at line 87 of file cpostshader.h.

88  {
89  SFVEC2F clampPos;
90 
91  clampPos.x = glm::clamp( aPos.x, 0.0f, 1.0f );
92  clampPos.y = glm::clamp( aPos.y, 0.0f, 1.0f );
93 
94  const unsigned int idx = (unsigned int)( (float)m_size.x * clampPos.x +
95  (float)m_size.x * (float)m_size.y *
96  clampPos.y );
97 
98  return glm::min( idx, m_size.x * m_size.y );
99  }
SFVEC2UI m_size
Definition: cpostshader.h:113
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45

References CPOSTSHADER::m_size.

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

◆ GetIndex() [2/2]

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

Definition at line 101 of file cpostshader.h.

102  {
103  SFVEC2I clampPos;
104  clampPos.x = glm::clamp( aPos.x, 0, (int)m_size.x - 1 );
105  clampPos.y = glm::clamp( aPos.y, 0, (int)m_size.y - 1 );
106 
107  return (unsigned int)( clampPos.x + m_size.x * clampPos.y );
108  }
SFVEC2UI m_size
Definition: cpostshader.h:113
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:42

References CPOSTSHADER::m_size.

◆ GetMaxDepth()

float CPOSTSHADER::GetMaxDepth ( ) const
inlineprotectedinherited

Definition at line 81 of file cpostshader.h.

81 { return m_tmax; }
float m_tmax
Definition: cpostshader.h:120

References CPOSTSHADER::m_tmax.

◆ GetNormalAt() [1/2]

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

Definition at line 120 of file cpostshader.cpp.

121 {
122  return m_normals[GetIndex( aPos )];
123 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
SFVEC3F * m_normals
Definition: cpostshader.h:114

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

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

◆ GetNormalAt() [2/2]

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

Definition at line 144 of file cpostshader.cpp.

145 {
146  return m_normals[GetIndex( aPos )];
147 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
SFVEC3F * m_normals
Definition: cpostshader.h:114

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

◆ GetPositionAt() [1/2]

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

Definition at line 138 of file cpostshader.cpp.

139 {
140  return m_wc_hitposition[GetIndex( aPos )];
141 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
SFVEC3F * m_wc_hitposition
Definition: cpostshader.h:116

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

Referenced by Shade().

◆ GetPositionAt() [2/2]

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

Definition at line 179 of file cpostshader.cpp.

180 {
181  return m_wc_hitposition[GetIndex( aPos )];
182 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
SFVEC3F * m_wc_hitposition
Definition: cpostshader.h:116

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

◆ GetShadowFactorAt()

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

Definition at line 185 of file cpostshader.cpp.

186 {
187  return m_shadow_att_factor[GetIndex( aPos )];
188 }
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
float * m_shadow_att_factor
Definition: cpostshader.h:118

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

Referenced by Shade().

◆ giColorCurve()

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

giColorCurve - Apply a curve transformation to the original color it will atenuate the bright colors (works as a gamma function): http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIxLjAtKDEvKHgqMS4wKzEuMCkpK3gqMC4zMCIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMCwid2luZG93IjpbIi0wLjA2MjE4NDYxNTM4NDYxNTUwNSIsIjEuMTQyOTg0NjE1Mzg0NjE0NiIsIi0wLjEyNzA5OTk5OTk5OTk5OTc3IiwiMS4xMzI2Il19XQ&ndash;

Parameters
aColorinput color
Returns
transformated color

Definition at line 253 of file cpostshader_ssao.cpp.

254 {
255  const SFVEC3F vec1 = SFVEC3F(1.0f);
256 
257  // This option actually apply a gama since we are using linear color space
258  // and the result shader will be applied after convert back to sRGB
259 
260  // http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIxLjAtKDEuMC8oeCo5LjArMS4wKSkreCowLjEiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyItMC4wNjIxODQ2MTUzODQ2MTU1MDUiLCIxLjE0Mjk4NDYxNTM4NDYxNDYiLCItMC4xMjcwOTk5OTk5OTk5OTk3NyIsIjEuMTMyNiJdfV0-
261  return vec1 - ( vec1 / (aColor * SFVEC3F(9.0f) + vec1) ) + aColor * SFVEC3F(0.10f);
262 }
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47

Referenced by Shade().

◆ giFF()

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

Definition at line 116 of file cpostshader_ssao.cpp.

122 {
123  if( (ddiff.x > FLT_EPSILON) ||
124  (ddiff.y > FLT_EPSILON) ||
125  (ddiff.z > FLT_EPSILON) )
126  {
127  const SFVEC3F vv = glm::normalize( ddiff );
128  const float rd = glm::length( ddiff );
129  const SFVEC2I vr = aShaderPos + SFVEC2I( c1, c2 );
130 
131  const float attDistFactor = 1.0f / ( rd * rd + 1.0f );
132 
133  return ( glm::clamp( glm::dot( GetNormalAt( vr ), -vv), 0.0f, 1.0f ) *
134  glm::clamp( glm::dot( cnorm, vv ), 0.0f, 1.0f ) * attDistFactor ) * ( 0.03f + aShadow ) * 3.0f;
135  }
136 
137  return 0.0f;
138 }
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:42
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
const SFVEC3F & GetNormalAt(const SFVEC2F &aPos) const

References CPOSTSHADER::GetNormalAt().

Referenced by Shade().

◆ InitFrame()

void CPOSTSHADER::InitFrame ( )
inlineinherited

Definition at line 54 of file cpostshader.h.

54 { m_tmin = FLT_MAX; m_tmax = 0.0f; }
float m_tmax
Definition: cpostshader.h:120
float m_tmin
Definition: cpostshader.h:119

References CPOSTSHADER::m_tmax, and CPOSTSHADER::m_tmin.

Referenced by C3D_RENDER_RAYTRACING::restart_render_state().

◆ posFromDepth()

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

◆ SetPixelData()

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

Definition at line 78 of file cpostshader.cpp.

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 }
float m_tmax
Definition: cpostshader.h:120
SFVEC2UI m_size
Definition: cpostshader.h:113
SFVEC3F * m_normals
Definition: cpostshader.h:114
float * m_depth
Definition: cpostshader.h:117
SFVEC3F * m_color
Definition: cpostshader.h:115
float m_tmin
Definition: cpostshader.h:119
SFVEC3F * m_wc_hitposition
Definition: cpostshader.h:116
float * m_shadow_att_factor
Definition: cpostshader.h:118

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

Referenced by C3D_RENDER_RAYTRACING::rt_render_trace_block().

◆ SetShadedBuffer()

void CPOSTSHADER_SSAO::SetShadedBuffer ( SFVEC3F aShadedBuffer)
inline

Definition at line 49 of file cpostshader_ssao.h.

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

References m_shadedBuffer.

Referenced by C3D_RENDER_RAYTRACING::rt_render_post_process_shade().

◆ SetShadowsEnabled()

void CPOSTSHADER_SSAO::SetShadowsEnabled ( bool  aIsShadowsEnabled)
inline

Definition at line 54 of file cpostshader_ssao.h.

55  {
56  m_isUsingShadows = aIsShadowsEnabled;
57  }

References m_isUsingShadows.

Referenced by C3D_RENDER_RAYTRACING::rt_render_post_process_shade().

◆ Shade()

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

Implements CPOSTSHADER.

Definition at line 141 of file cpostshader_ssao.cpp.

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

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

Referenced by C3D_RENDER_RAYTRACING::rt_render_post_process_shade().

◆ UpdateSize() [1/2]

void CPOSTSHADER::UpdateSize ( const SFVEC2UI aSize)
inherited

Definition at line 72 of file cpostshader.cpp.

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

Referenced by C3D_RENDER_RAYTRACING::initialize_block_positions().

◆ UpdateSize() [2/2]

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

Definition at line 55 of file cpostshader.cpp.

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 }
SFVEC2UI m_size
Definition: cpostshader.h:113
SFVEC3F * m_normals
Definition: cpostshader.h:114
void destroy_buffers()
float * m_depth
Definition: cpostshader.h:117
SFVEC3F * m_color
Definition: cpostshader.h:115
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
SFVEC3F * m_wc_hitposition
Definition: cpostshader.h:116
float * m_shadow_att_factor
Definition: cpostshader.h:118

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

Member Data Documentation

◆ m_camera

const CCAMERA& CPOSTSHADER::m_camera
protectedinherited

Definition at line 111 of file cpostshader.h.

◆ m_color

◆ m_depth

◆ m_isUsingShadows

bool CPOSTSHADER_SSAO::m_isUsingShadows
private

Definition at line 90 of file cpostshader_ssao.h.

Referenced by SetShadowsEnabled(), and Shade().

◆ m_normals

◆ m_shadedBuffer

SFVEC3F* CPOSTSHADER_SSAO::m_shadedBuffer
private

Definition at line 88 of file cpostshader_ssao.h.

Referenced by Blur(), and SetShadedBuffer().

◆ m_shadow_att_factor

◆ m_size

◆ m_tmax

float CPOSTSHADER::m_tmax
protectedinherited

◆ m_tmin

float CPOSTSHADER::m_tmin
protectedinherited

◆ m_wc_hitposition

SFVEC3F* CPOSTSHADER::m_wc_hitposition
protectedinherited

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