Drake Designer
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
edl_shade.glsl
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: edl_shade.glsl
5 
6  Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
7  All rights reserved.
8 
9  ParaView is a free software; you can redistribute it and/or modify it
10  under the terms of the ParaView license version 1.2.
11 
12  See License_v1.2.txt for the full ParaView license.
13  A copy of this license can be obtained by contacting
14  Kitware Inc.
15  28 Corporate Drive
16  Clifton Park, NY 12065
17  USA
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 =========================================================================*/
32 /*----------------------------------------------------------------------
33 Acknowledgement:
34 This algorithm is the result of joint work by Electricité de France,
35 CNRS, Collège de France and Université J. Fourier as part of the
36 Ph.D. thesis of Christian BOUCHENY.
37 ------------------------------------------------------------------------*/
39 //
40 //
41 // EyeDome Lighting - Simplified version for use in VTK
42 // - oriented light
43 // - no focus
44 // - some uniforms transformed to local variables
45 //
46 // C.B. - 3 feb. 2009
47 //
48 // IN: Depth buffer of the scene
49 // r = recorded z, in [0:1]
50 // OUT: EDL shaded image
51 //
53 
54 /**************************************************/
55 uniform sampler2D s2_depth; // - Z Map
56 uniform float d; // [1.0 in full res - 2.0 at lower res]
57  //- Extension in image space, in pixels
58 uniform vec4 N[8]; //- Array of neighbours
59  // [No support for TabUniform in VTK
60  // --> constant array, hereafter]
61 uniform float F_scale; // [5.] - Shading amplification factor
62 
63 uniform float SX; // - pixel horizontal step (image distance: 1/w)
64 uniform float SY; //- pixel vertical step (image distance: 1/h)
65 uniform float Znear; // near clipping plane
66 uniform float Zfar; // far clipping plane
67 uniform float SceneSize; // typical scene size, to scale the depth by.
68 
69 uniform vec3 L; // [0.,0.,-1.] - Light direction [frontal]
70 /**************************************************/
71 
72 /**************************************************/
73 int Nnb = 1; // nombre de voisins par rayon
74 float Zm = 0.; // minimal z in image
75 float ZM = 1.; // maximal z in image
76 float Z; // initial Z
77 
78 vec3 WHITE3 = vec3(1.,1.,1.);
79 
80 float t;
81 vec4 Zn[8]; // profondeurs des voisins
82 float D[8]; // ombrage genere par les voisins
83 vec4 tn, tnw, tw, tsw, ts, tse, te, tne;
84 float dn, dnw, dw, dsw, ds, dse, de, dne;
85 float S; // image step, corresponds to one pixel size
86 /**************************************************/
87 
89 //
90 // Local shading functions
91 //
92 // Pseudo angle, avec S (distance pixel) valant l'unite
93 // zi elevation of current pixel
94 // zj elevation of its neighbour
95 // delta distance between the two
96 float angleP(float zi, float zj, float delta)
97 {
98  return max(0.,zj-zi) / (delta/S);
99 }
100 
101 // zi elevation of current pixel
102 // zj elevation of its neighbour
103 // delta distance between the two
104 float obscurance(float zi, float zj, float delta)
105 {
106  return angleP(zi,zj,delta);
107 }
108 //
109 // Local shading functions
110 //
112 
114 //
115 // Z transformation
116 //
117 float zflip(float z)
118 {
119  return 1. - z;
120 }
121 
122 float zscale(float z)
123 {
124  return clamp((z-Zm)/(ZM-Zm),0.,1.);
125 }
126 
127 // Inversion of OpenGL perspective projection
128 // (should be adapted for orthographic projection)
129 //
130 float ztransform(float z)
131 {
132  float Z;
133  Z = (z-0.5)*2.;
134  Z = -2.*Zfar*Znear/( (Zfar-Znear) * (Z-(Zfar+Znear)/(Zfar-Znear)) );
135  Z = (Z-Znear)/SceneSize;
136  return 1.-Z;
137 }
138 //
139 // Z transformation
140 //
142 
144 //
145 // NEIGHBORHOOD SHADING
146 //
147 void computeNeighbours8(float dist)
148 {
149  // Plan Lumiere-point
150  vec4 P = vec4( L.xyz , -dot(L.xyz,vec3(0.,0.,t)) );
151 
152  // 0 at the back of the scene
153  int c;
154  vec2 V; // pixel voisin
155  float di = dist;
156  float Znp[8]; // profondeur des 8 voisins sur le plan
157 
158  for(c=0; c<8;c++)
159  {
160  V = gl_TexCoord[0].st + di*vec2(SX,SY)*N[c].xy;
161  Zn[c].x = ztransform(texture2D(s2_depth,V).r);
162  // profondeur du voisin reel dans l'image
163 
164  // VERSION qui ombre le fond
165  Znp[c] = dot( vec4(di*vec2(SX,SY)*N[c].xy,Zn[c].x,1.) , P );
166  }
167  dn = obscurance( 0., Znp[0] ,di*SX);
168  dnw = obscurance( 0., Znp[1],di*SX);
169  dw = obscurance( 0., Znp[2] ,di*SX);
170  dsw = obscurance( 0., Znp[3],di*SX);
171  ds = obscurance( 0., Znp[4] ,di*SX);
172  dse = obscurance( 0., Znp[5],di*SX);
173  de = obscurance( 0., Znp[6] ,di*SX);
174  dne = obscurance( 0., Znp[7],di*SX);
175 }
176 
177 float computeObscurance(float F,float scale,float weight)
178 {
179  computeNeighbours8( scale );
180 
181  float S = F;
182  float WE = weight;
183 
184  S += dn * WE;
185  S += dnw * WE;
186  S += dw * WE;
187  S += dsw * WE;
188  S += ds * WE;
189  S += dse * WE;
190  S += de * WE;
191  S += dne * WE;
192 
193  return S;
194 }
195 
197 {
198  float F = 0.;
199  float weight = 20.; // 2. * 3.14159;
200  int filter = 0;
201  F = computeObscurance(F,d,weight);
202  F = exp(-F_scale*F);
203  vec3 color = WHITE3;
204 
205  if(false)
206  {
207  gl_FragColor = vec4(1.,1.,1.,Z);
208  }
209  else
210  {
211  gl_FragColor = vec4(F,F,F,Z);
212  }
213 }
214 
215 void main (void)
216 {
217  S = SX;
218  Z = texture2D(s2_depth,gl_TexCoord[0].st).r;
219  t = ztransform(Z);
220 
222 }
void ambientOcclusion()
Definition: edl_shade.glsl:196
float dse
Definition: edl_shade.glsl:84
float zflip(float z)
Definition: edl_shade.glsl:117
uniform float Zfar
Definition: edl_shade.glsl:66
float t
Definition: edl_shade.glsl:80
vec4 tnw
Definition: edl_shade.glsl:83
uniform vec4 N[8]
Definition: edl_shade.glsl:58
float obscurance(float zi, float zj, float delta)
Definition: edl_shade.glsl:104
uniform float Znear
Definition: edl_shade.glsl:65
float dn
Definition: edl_shade.glsl:84
void computeNeighbours8(float dist)
Definition: edl_shade.glsl:147
float dw
Definition: edl_shade.glsl:84
float ZM
Definition: edl_shade.glsl:75
vec4 Zn[8]
Definition: edl_shade.glsl:81
float de
Definition: edl_shade.glsl:84
float computeObscurance(float F, float scale, float weight)
Definition: edl_shade.glsl:177
vec4 tn
Definition: edl_shade.glsl:83
uniform float SX
Definition: edl_shade.glsl:63
float dnw
Definition: edl_shade.glsl:84
uniform float d
Definition: edl_shade.glsl:56
uniform float SceneSize
Definition: edl_shade.glsl:67
float ds
Definition: edl_shade.glsl:84
float angleP(float zi, float zj, float delta)
Definition: edl_shade.glsl:96
float Zm
Definition: edl_shade.glsl:74
vec4 tw
Definition: edl_shade.glsl:83
int Nnb
Definition: edl_shade.glsl:73
vec4 ts
Definition: edl_shade.glsl:83
float z
float ztransform(float z)
Definition: edl_shade.glsl:130
vec4 tse
Definition: edl_shade.glsl:83
uniform vec3 L
Definition: edl_shade.glsl:69
float Z
Definition: edl_shade.glsl:76
float zscale(float z)
Definition: edl_shade.glsl:122
void main(void)
Definition: edl_shade.glsl:215
float dne
Definition: edl_shade.glsl:84
float S
Definition: edl_shade.glsl:85
uniform float F_scale
Definition: edl_shade.glsl:61
float D[8]
Definition: edl_shade.glsl:82
vec4 te
Definition: edl_shade.glsl:83
vec4 tne
Definition: edl_shade.glsl:83
vec3 WHITE3
Definition: edl_shade.glsl:78
float dsw
Definition: edl_shade.glsl:84
uniform float SY
Definition: edl_shade.glsl:64
vec4 tsw
Definition: edl_shade.glsl:83
uniform sampler2D s2_depth
Definition: edl_shade.glsl:55