KiCad PCB EDA Suite
cpostshader_ssao.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 
30 #include "cpostshader_ssao.h"
31 #include "../3d_fastmath.h"
32 
33 
35 {
36 
37 }
38 
39 // There are differente sources for this shader on the web
40 //https://github.com/scanberg/hbao/blob/master/resources/shaders/ssao_frag.glsl
41 
42 //http://www.gamedev.net/topic/556187-the-best-ssao-ive-seen/
43 //http://www.gamedev.net/topic/556187-the-best-ssao-ive-seen/?view=findpost&p=4632208
44 
45 float CPOSTSHADER_SSAO::aoFF( const SFVEC2I &aShaderPos,
46  const SFVEC3F &ddiff,
47  const SFVEC3F &cnorm,
48  int c1,
49  int c2 ) const
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 }
133 
134 
135 float CPOSTSHADER_SSAO::giFF( const SFVEC2I &aShaderPos,
136  const SFVEC3F &ddiff,
137  const SFVEC3F &cnorm,
138  int c1,
139  int c2 ) const
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 }
155 
156 
157 SFVEC3F CPOSTSHADER_SSAO::Shade( const SFVEC2I &aShaderPos ) const
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 }
257 
258 
259 SFVEC3F CPOSTSHADER_SSAO::ApplyShadeColor( const SFVEC2I &aShaderPos, const SFVEC3F &aInputColor, const SFVEC3F &aShadeColor ) const
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 }
266 
267 
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 }
SFVEC3F ApplyShadeColor(const SFVEC2I &aShaderPos, const SFVEC3F &aInputColor, const SFVEC3F &aShadeColor) const override
ApplyShadeColor - apply the final color process using a previous stage color.
SFVEC3F Shade(const SFVEC2I &aShaderPos) const override
Class CCAMERA is a virtual class used to derive CCAMERA objects from.
Definition: ccamera.h:80
Implements a post shader screen space ambient occlusion on software.
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
CPOSTSHADER_SSAO(const CCAMERA &aCamera)
const SFVEC3F & GetPositionAt(const SFVEC2F &aPos) const
#define max(a, b)
Definition: auxiliary.h:86
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 ...
const float & GetShadowFactorAt(const SFVEC2I &aPos) const