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...
 
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
 

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, int c1, int c2) const
 
float giFF (const SFVEC2I &aShaderPos, const SFVEC3F &ddiff, const SFVEC3F &cnorm, 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...
 

Detailed Description

Definition at line 37 of file cpostshader_ssao.h.

Constructor & Destructor Documentation

CPOSTSHADER_SSAO::CPOSTSHADER_SSAO ( const CCAMERA aCamera)
explicit

Definition at line 34 of file cpostshader_ssao.cpp.

34  : CPOSTSHADER( aCamera )
35 {
36 
37 }
CPOSTSHADER(const CCAMERA &aCamera)
Definition: cpostshader.cpp:36

Member Function Documentation

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

Definition at line 45 of file cpostshader_ssao.cpp.

References CPOSTSHADER::GetNormalAt(), CPOSTSHADER::GetShadowFactorAt(), and max.

Referenced by Shade().

50 {
51  const float shadowGain = 0.5f;
52  const float aoGain = 1.0f;
53  const float outGain = 0.80f;
54 
55  float return_value = 0.0f;
56 
57  const float rd = glm::length( ddiff );
58 
59  // This limits the zero of the function (see below)
60  if( rd < 1.0f )
61  {
62  const SFVEC2I vr = aShaderPos + SFVEC2I( c1, c2 );
63 
64  const float shadow_factor_at_sample = ( 1.0f - GetShadowFactorAt( vr ) ) * shadowGain;
65 
66  if( rd > FLT_EPSILON )
67  {
68  const SFVEC3F vv = glm::normalize( ddiff );
69 
70  // Calculate an attenuation distance factor, this was get the best
71  // results by experimentation
72  // Changing this factor will change how much shadow in relation to the
73  // distance of the hit it will be in shadow
74 
75  // http://www.fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIwLjYteCowLjQ1IiwiY29sb3IiOiIjMDAwMDAwIn0seyJ0eXBlIjowLCJlcSI6IiIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMCwid2luZG93IjpbIi0wLjIxNTcyODA1NTg4MzI1ODYiLCIyLjEyNjE0Mzc1MDM0OTM4ODciLCItMC4wOTM1NDA0NzY0MjczNjAzIiwiMS4zNDc2MTE0MDQzMzExOTIyIl0sInNpemUiOls2NDksMzk5XX1d
76  // zero: 1.0
77  const float attDistFactor = 0.6f - rd * 0.6f;
78 
79  // Original:
80  // http://www.fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIoMS0xL3NxcnQoMS8oeCp4KSsxKSkiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyItMC42ODY3NDc3NDcxMDg0MTQyIiwiMy44ODcyMjA2MjQ0Mzk3MzM0IiwiLTAuOTA5NTYyNzcyOTMyNDk2IiwiMS45MDUxODY5OTQxNzQwNTczIl19XQ--
81  // zero: inf
82  //const float attDistFactor = (1.0f - 1.0f / sqrt( 1.0f / ( rd * rd) + 1.0f) );
83 
84  //const float attDistFactor = 1.0f;
85 
86 
87  // Tool for visualize dot product:
88  // http://www.falstad.com/dotproduct/
89 
90  // This is a dot product threshold factor.
91  // it defines after wich angle we consider that the point starts to occlude.
92  // if the value is high, it will distart low angles point
93  const float aDotThreshold = 0.15f;
94 
95  // This is the normal factor using the normal at the sampled point (of the shader)
96  // agaisnt the vector from the center to the position at sampled point
97  const float sampledNormalFactor = glm::dot( GetNormalAt( vr ), -vv );
98 
99  // http://www.fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIobWF4KHgsMC4zKS0wLjMpLygxLTAuMykiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyItMC42ODY3NDc3NDcxMDg0MTQyIiwiMy44ODcyMjA2MjQ0Mzk3MzM0IiwiLTAuOTA5NTYyNzcyOTMyNDk2IiwiMS45MDUxODY5OTQxNzQwNTczIl19XQ--
100 
101  const float sampledNormalFactorWithThreshold = (glm::max( sampledNormalFactor, aDotThreshold ) - aDotThreshold) /
102  (1.0f - aDotThreshold);
103 
104 
105  // This is the dot product between the center pixel (the one that is being shaded)
106  // and the vector from the center to the sampled point
107  const float localNormalFactor = glm::dot( cnorm, vv );
108 
109  const float localNormalFactorWithThreshold = (glm::max( localNormalFactor, aDotThreshold ) - aDotThreshold) /
110  (1.0f - aDotThreshold);
111 
112 
113  const float aoFactor = (1.0f - sampledNormalFactorWithThreshold) *
114  localNormalFactorWithThreshold *
115  aoGain;
116 
117  return_value = ( ( aoFactor + shadow_factor_at_sample ) * attDistFactor );
118 
119  // Test / Debug code
120  //return_value = glm::max( aaFactor, shadow_factor );
121  //return_value = aaFactor;
122  //return_value = shadow_factor;
123  //return_value = glm::clamp( aaFactor, 0.0f, 1.0f );
124  }
125  else
126  {
127  return_value = shadow_factor_at_sample;
128  }
129  }
130 
131  return return_value * outGain;
132 }
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:42
const SFVEC3F & GetNormalAt(const SFVEC2F &aPos) const
#define max(a, b)
Definition: auxiliary.h:86
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
const float & GetShadowFactorAt(const SFVEC2I &aPos) const
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 259 of file cpostshader_ssao.cpp.

Referenced by C3D_RENDER_RAYTRACING::rt_render_post_process_blur_finish().

260 {
261  // This is the final stage of the shader and make the last calculation how to apply the shader
262  const SFVEC3F shadedColor = aInputColor - ( -aShadeColor * (aShadeColor * SFVEC3F(0.1f) - SFVEC3F(1.0f) ) );
263 
264  return shadedColor;
265 }
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
void CPOSTSHADER::DebugBuffersOutputAsImages ( ) const
inherited

Definition at line 191 of file cpostshader.cpp.

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

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:112
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:42
SFVEC3F * m_normals
Definition: cpostshader.h:113
float GetDepthNormalizedAt(const SFVEC2I &aPos) const
SFVEC3F * m_color
Definition: cpostshader.h:114
void DBG_SaveNormalsBuffer(wxString aFileName, const SFVEC3F *aInNormalsBuffer, unsigned int aXSize, unsigned int aYSize)
void DBG_SaveBuffer(wxString aFileName, const unsigned char *aInBuffer, unsigned int aXSize, unsigned int aYSize)
float * m_shadow_att_factor
Definition: cpostshader.h:117
float CPOSTSHADER_SSAO::ec_depth ( const SFVEC2F tc) const
private
const SFVEC3F & CPOSTSHADER::GetColorAt ( const SFVEC2F aPos) const
protectedinherited

Definition at line 126 of file cpostshader.cpp.

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

Referenced by Shade().

127 {
128  return m_color[ getIndex( aPos ) ];
129 }
SFVEC3F * m_color
Definition: cpostshader.h:114
unsigned int getIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:86
const SFVEC3F & CPOSTSHADER::GetColorAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 150 of file cpostshader.cpp.

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

151 {
152  return m_color[ getIndex( aPos ) ];
153 }
SFVEC3F * m_color
Definition: cpostshader.h:114
unsigned int getIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:86
const SFVEC3F & CPOSTSHADER::GetColorAtNotProtected ( const SFVEC2I aPos) const
inherited

Definition at line 156 of file cpostshader.cpp.

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

Referenced by C3D_RENDER_RAYTRACING::rt_render_post_process_blur_finish().

157 {
158  return m_color[ aPos.x + m_size.x * aPos.y ];
159 }
SFVEC2UI m_size
Definition: cpostshader.h:112
SFVEC3F * m_color
Definition: cpostshader.h:114
float CPOSTSHADER::GetDepthAt ( const SFVEC2F aPos) const
protectedinherited

Definition at line 132 of file cpostshader.cpp.

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

Referenced by Shade().

133 {
134  return m_depth[ getIndex( aPos ) ];
135 }
float * m_depth
Definition: cpostshader.h:116
unsigned int getIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:86
float CPOSTSHADER::GetDepthAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 162 of file cpostshader.cpp.

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

163 {
164  return m_depth[ getIndex( aPos ) ];
165 }
float * m_depth
Definition: cpostshader.h:116
unsigned int getIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:86
float CPOSTSHADER::GetDepthNormalizedAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 168 of file cpostshader.cpp.

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

Referenced by CPOSTSHADER::DebugBuffersOutputAsImages().

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:119
float * m_depth
Definition: cpostshader.h:116
float m_tmin
Definition: cpostshader.h:118
unsigned int getIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:86
float CPOSTSHADER::GetMaxDepth ( ) const
inlineprotectedinherited

Definition at line 81 of file cpostshader.h.

References CPOSTSHADER::m_tmax.

81 { return m_tmax; }
float m_tmax
Definition: cpostshader.h:119
const SFVEC3F & CPOSTSHADER::GetNormalAt ( const SFVEC2F aPos) const
protectedinherited

Definition at line 120 of file cpostshader.cpp.

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

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

121 {
122  return m_normals[ getIndex( aPos ) ];
123 }
SFVEC3F * m_normals
Definition: cpostshader.h:113
unsigned int getIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:86
const SFVEC3F & CPOSTSHADER::GetNormalAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 144 of file cpostshader.cpp.

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

145 {
146  return m_normals[ getIndex( aPos ) ];
147 }
SFVEC3F * m_normals
Definition: cpostshader.h:113
unsigned int getIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:86
const SFVEC3F & CPOSTSHADER::GetPositionAt ( const SFVEC2F aPos) const
protectedinherited

Definition at line 138 of file cpostshader.cpp.

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

Referenced by Shade().

139 {
140  return m_wc_hitposition[ getIndex( aPos ) ];
141 }
unsigned int getIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:86
SFVEC3F * m_wc_hitposition
Definition: cpostshader.h:115
const SFVEC3F & CPOSTSHADER::GetPositionAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 179 of file cpostshader.cpp.

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

180 {
181  return m_wc_hitposition[ getIndex( aPos ) ];
182 }
unsigned int getIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:86
SFVEC3F * m_wc_hitposition
Definition: cpostshader.h:115
const float & CPOSTSHADER::GetShadowFactorAt ( const SFVEC2I aPos) const
protectedinherited

Definition at line 185 of file cpostshader.cpp.

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

Referenced by aoFF().

186 {
187  return m_shadow_att_factor[ getIndex( aPos ) ];
188 }
unsigned int getIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:86
float * m_shadow_att_factor
Definition: cpostshader.h:117
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 268 of file cpostshader_ssao.cpp.

Referenced by Shade().

269 {
270  const SFVEC3F vec1 = SFVEC3F(1.0f);
271 
272  // http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIxLjAtKDEvKHgqMS4wKzEuMCkpK3gqMC4xIiwiY29sb3IiOiIjMDAwMDAwIn0seyJ0eXBlIjoxMDAwLCJ3aW5kb3ciOlsiLTAuMDYyMTg0NjE1Mzg0NjE1NTA1IiwiMS4xNDI5ODQ2MTUzODQ2MTQ2IiwiLTAuMTI3MDk5OTk5OTk5OTk5NzciLCIxLjEzMjYiXX1d
273  //return vec1 - ( vec1 / (aColor + vec1) ) + aColor * SFVEC3F(0.10f);
274 
275  // http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIxLjAtKDEuMC8oeCoyLjArMS4wKSkreCowLjEiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyItMC4wNjIxODQ2MTUzODQ2MTU1MDUiLCIxLjE0Mjk4NDYxNTM4NDYxNDYiLCItMC4xMjcwOTk5OTk5OTk5OTk3NyIsIjEuMTMyNiJdfV0-
276  //return vec1 - ( vec1 / (aColor * SFVEC3F(2.0f) + vec1) ) + aColor * SFVEC3F(0.10f);
277 
278  // This option actually apply a gama since we are using linear color space
279  // and the result shader will be applied after convert back to sRGB
280 
281  // http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIxLjAtKDEuMC8oeCo5LjArMS4wKSkreCowLjEiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyItMC4wNjIxODQ2MTUzODQ2MTU1MDUiLCIxLjE0Mjk4NDYxNTM4NDYxNDYiLCItMC4xMjcwOTk5OTk5OTk5OTk3NyIsIjEuMTMyNiJdfV0-
282  return vec1 - ( vec1 / (aColor * SFVEC3F(9.0f) + vec1) ) + aColor * SFVEC3F(0.10f);
283 
284  // return aColor;
285 }
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
float CPOSTSHADER_SSAO::giFF ( const SFVEC2I aShaderPos,
const SFVEC3F ddiff,
const SFVEC3F cnorm,
int  c1,
int  c2 
) const
private

Definition at line 135 of file cpostshader_ssao.cpp.

References CPOSTSHADER::GetNormalAt().

Referenced by Shade().

140 {
141  if( (ddiff.x > FLT_EPSILON) ||
142  (ddiff.y > FLT_EPSILON) ||
143  (ddiff.z > FLT_EPSILON) )
144  {
145  const SFVEC3F vv = glm::normalize( ddiff );
146  const float rd = glm::length( ddiff );
147  const SFVEC2I vr = aShaderPos + SFVEC2I( c1, c2 );
148 
149  return glm::clamp( glm::dot( GetNormalAt( vr ), -vv), 0.0f, 1.0f ) *
150  glm::clamp( glm::dot( cnorm, vv ), 0.0f, 1.0f ) / ( rd * rd + 1.0f );
151  }
152 
153  return 0.0f;
154 }
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:42
const SFVEC3F & GetNormalAt(const SFVEC2F &aPos) const
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
void CPOSTSHADER::InitFrame ( )
inlineinherited

Definition at line 54 of file cpostshader.h.

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

Referenced by C3D_RENDER_RAYTRACING::restart_render_state().

54 { m_tmin = FLT_MAX; m_tmax = 0.0f; }
float m_tmax
Definition: cpostshader.h:119
float m_tmin
Definition: cpostshader.h:118
SFVEC3F CPOSTSHADER_SSAO::posFromDepth ( const SFVEC2F coord) const
private
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.

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().

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:119
SFVEC2UI m_size
Definition: cpostshader.h:112
SFVEC3F * m_normals
Definition: cpostshader.h:113
float * m_depth
Definition: cpostshader.h:116
SFVEC3F * m_color
Definition: cpostshader.h:114
float m_tmin
Definition: cpostshader.h:118
SFVEC3F * m_wc_hitposition
Definition: cpostshader.h:115
float * m_shadow_att_factor
Definition: cpostshader.h:117
SFVEC3F CPOSTSHADER_SSAO::Shade ( const SFVEC2I aShaderPos) const
overridevirtual

Implements CPOSTSHADER.

Definition at line 157 of file cpostshader_ssao.cpp.

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

Referenced by C3D_RENDER_RAYTRACING::rt_render_post_process_shade().

158 {
159  // Test source code
160  //return SFVEC3F( GetShadowFactorAt( aShaderPos ) );
161  //return GetColorAt( aShaderPos );
162  //return SFVEC3F( 1.0f - GetDepthNormalizedAt( aShaderPos ) );
163  //return SFVEC3F( (1.0f / GetDepthAt( aShaderPos )) * 0.5f );
164  //return SFVEC3F( 1.0f - GetDepthNormalizedAt( aShaderPos ) +
165  // (1.0f / GetDepthAt( aShaderPos )) * 0.5f );
166 
167 #if 1
168  float cdepth = GetDepthAt( aShaderPos );
169 
170  if( cdepth > FLT_EPSILON )
171  {
172 
173  //const float cNormalizedDepth = GetDepthNormalizedAt( aShaderPos );
174  //wxASSERT( cNormalizedDepth <= 1.0f );
175  //wxASSERT( cNormalizedDepth >= 0.0f );
176 
177  cdepth = (10.0f / (cdepth + 1.0f) );
178 
179  // read current normal,position and color.
180  const SFVEC3F n = GetNormalAt( aShaderPos );
181  const SFVEC3F p = GetPositionAt( aShaderPos );
182  //const SFVEC3F col = GetColorAt( aShaderPos );
183 
184  // initialize variables:
185  float ao = 0.0f;
186  SFVEC3F gi = SFVEC3F(0.0f);
187 
188  // This calculated the "window range" of the shader. So it will get
189  // more or less sparsed samples
190  const int incx = 2;
191  const int incy = 2;
192 
193  //3 rounds of 8 samples each.
194  for( unsigned int i = 0; i < 3; ++i )
195  {
196  static const int mask[3] = { 0x01, 0x03, 0x03 };
197  const int pw = 0 + (Fast_rand() & mask[i]);
198  const int ph = 0 + (Fast_rand() & mask[i]);
199 
200  const int npw = (int)((pw + incx * i) * cdepth ) + (i + 1);
201  const int nph = (int)((ph + incy * i) * cdepth ) + (i + 1);
202 
203  const SFVEC3F ddiff = GetPositionAt( aShaderPos + SFVEC2I( npw, nph ) ) - p;
204  const SFVEC3F ddiff2 = GetPositionAt( aShaderPos + SFVEC2I( npw,-nph ) ) - p;
205  const SFVEC3F ddiff3 = GetPositionAt( aShaderPos + SFVEC2I(-npw, nph ) ) - p;
206  const SFVEC3F ddiff4 = GetPositionAt( aShaderPos + SFVEC2I(-npw,-nph ) ) - p;
207  const SFVEC3F ddiff5 = GetPositionAt( aShaderPos + SFVEC2I( pw, nph ) ) - p;
208  const SFVEC3F ddiff6 = GetPositionAt( aShaderPos + SFVEC2I( pw,-nph ) ) - p;
209  const SFVEC3F ddiff7 = GetPositionAt( aShaderPos + SFVEC2I( npw, ph ) ) - p;
210  const SFVEC3F ddiff8 = GetPositionAt( aShaderPos + SFVEC2I(-npw, ph ) ) - p;
211 
212  ao+= aoFF( aShaderPos, ddiff , n, npw, nph );
213  ao+= aoFF( aShaderPos, ddiff2, n, npw,-nph );
214  ao+= aoFF( aShaderPos, ddiff3, n, -npw, nph );
215  ao+= aoFF( aShaderPos, ddiff4, n, -npw,-nph );
216  ao+= aoFF( aShaderPos, ddiff5, n, pw, nph );
217  ao+= aoFF( aShaderPos, ddiff6, n, pw,-nph );
218  ao+= aoFF( aShaderPos, ddiff7, n, npw, ph );
219  ao+= aoFF( aShaderPos, ddiff8, n, -npw, ph );
220 
221  gi+= giFF( aShaderPos, ddiff , n, npw, nph) *
222  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( npw, nph ) ) );
223  gi+= giFF( aShaderPos, ddiff2, n, npw, -nph) *
224  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( npw,-nph ) ) );
225  gi+= giFF( aShaderPos, ddiff3, n,-npw, nph) *
226  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( -npw, nph ) ) );
227  gi+= giFF( aShaderPos, ddiff4, n,-npw, -nph) *
228  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( -npw,-nph ) ) );
229  gi+= giFF( aShaderPos, ddiff5, n, pw, nph) *
230  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( pw, nph ) ) );
231  gi+= giFF( aShaderPos, ddiff6, n, pw,-nph) *
232  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( pw,-nph ) ) );
233  gi+= giFF( aShaderPos, ddiff7, n, npw, ph) *
234  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( npw, ph ) ) );
235  gi+= giFF( aShaderPos, ddiff8, n,-npw, ph) *
236  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( -npw, ph ) ) );
237  }
238  ao = (ao / 24.0f) + 0.0f; // Apply a bias for the ambient oclusion
239  gi = (gi * 5.0f / 24.0f); // Apply a bias for the global illumination
240 
241  //return SFVEC3F(ao);
242  return SFVEC3F(ao) - gi;
243 
244  // Test source code
245  //return SFVEC3F( col );
246  //return SFVEC3F( col - SFVEC3F(ao) + gi * 5.0f );
247  //return SFVEC3F( SFVEC3F(1.0f) - SFVEC3F(ao) + gi * 5.0f );
248  //return SFVEC3F(cdepth);
249  //return SFVEC3F(cNormalizedDepth);
250  //return 1.0f - SFVEC3F(ao);
251  //return SFVEC3F(ao);
252  }
253  else
254  return SFVEC3F(0.0f);
255 #endif
256 }
int Fast_rand(void)
Definition: 3d_fastmath.cpp:58
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:42
const SFVEC3F & GetNormalAt(const SFVEC2F &aPos) const
const SFVEC3F & GetColorAt(const SFVEC2F &aPos) const
float aoFF(const SFVEC2I &aShaderPos, const SFVEC3F &ddiff, const SFVEC3F &cnorm, int c1, int c2) const
const SFVEC3F & GetPositionAt(const SFVEC2F &aPos) const
float GetDepthAt(const SFVEC2F &aPos) const
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
float giFF(const SFVEC2I &aShaderPos, const SFVEC3F &ddiff, const SFVEC3F &cnorm, 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 ...
void CPOSTSHADER::UpdateSize ( const SFVEC2UI aSize)
inherited

Definition at line 72 of file cpostshader.cpp.

Referenced by C3D_RENDER_RAYTRACING::initialize_block_positions().

73 {
74  UpdateSize( aSize.x, aSize.y );
75 }
void UpdateSize(const SFVEC2UI &aSize)
Definition: cpostshader.cpp:72
void CPOSTSHADER::UpdateSize ( unsigned int  xSize,
unsigned int  ySize 
)
inherited

Definition at line 55 of file cpostshader.cpp.

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.

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

Member Data Documentation

const CCAMERA& CPOSTSHADER::m_camera
protectedinherited

Definition at line 110 of file cpostshader.h.

float CPOSTSHADER::m_tmax
protectedinherited
float CPOSTSHADER::m_tmin
protectedinherited
SFVEC3F* CPOSTSHADER::m_wc_hitposition
protectedinherited

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