Debian Codegraph @

src/vcglib/wrap/gl/splatting_apss/shaders/Raycasting.glsl

raw
1 /****************************************************************************
2 * MeshLab o o *
3 * An extendible mesh processor o o *
4 * _ O _ *
5 * Copyright(C) 2005, 2009 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23
24 // #version 110
25 // #extension all : enable
26
27 #pragma optimize(on)
28
29 #ifndef EXPE_EWA_HINT
30 #define EXPE_EWA_HINT 0
31 #endif
32
33 #ifndef EXPE_DEPTH_INTERPOLATION
34 #define EXPE_DEPTH_INTERPOLATION 0
35 #endif
36
37 //--------------------------------------------------------------------------------
38 // shared variables
39 //--------------------------------------------------------------------------------
40
41 // custom vertex attributes
42 //attribute float radius;
43
44 #ifdef CLIPPED_SPLAT
45 attribute vec3 secondNormal;
46 varying vec4 clipLine;
47 #endif
48
49 // standard uniforms
50 uniform float expeRadiusScale;
51 uniform float expePreComputeRadius;
52 uniform float expeDepthOffset;
53
54 // varying
55 varying vec4 covmat;
56 varying vec3 fragNormal;
57
58 varying vec3 fragNoverCdotN;
59 varying vec3 fragCenter;
60 varying float scaleSquaredDistance;
61
62 #ifdef EXPE_ATI_WORKAROUND
63 varying vec4 fragCenterAndRadius;
64 #endif
65
66 #ifdef EXPE_DEPTH_CORRECTION
67 varying float depthOffset;
68 #endif
69
70 uniform vec2 halfVp;
71 uniform float oneOverEwaRadius;
72
73
74 #ifdef EXPE_BACKFACE_SHADING
75 #undef EXPE_EARLY_BACK_FACE_CULLING
76 //#define EXPE_EWA_HINT 2
77 #endif
78
79 //--------------------------------------------------------------------------------
80 // Visibility Splatting
81 // Vertex Shader
82 //--------------------------------------------------------------------------------
83
84 #ifdef __VisibilityVP__
85 varying vec2 scaledFragCenter2d;
86 void VisibilityVP(void)
87 {
88 vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
89 // Point in eye space
90 vec4 ePos = gl_ModelViewMatrix * gl_Vertex;
91
92 float dotpn = dot(normal.xyz,ePos.xyz);
93
94 vec4 oPos;
95
96 #ifdef EXPE_EARLY_BACK_FACE_CULLING
97 // back_face culling
98 oPos = vec4(0,0,1,0);
99 if(dotpn<0.)
100 {
101 #endif
102
103 float radius = gl_MultiTexCoord2.x * expeRadiusScale;
104
105 vec4 pointSize;
106 pointSize.x = radius * expePreComputeRadius / ePos.z;
107 gl_PointSize = max(1.0, pointSize.x);
108
109 scaleSquaredDistance = 1.0 / (radius * radius);
110 //fragNormal = normal;
111 fragCenter = ePos.xyz;
112 fragNoverCdotN = normal/dot(ePos.xyz,normal);
113
114 #ifndef EXPE_DEPTH_CORRECTION
115 ePos.xyz += normalize(ePos.xyz) * expeDepthOffset * radius;
116 #else
117 //ePos.xyz += normalize(ePos.xyz) * expeDepthOffset * radius;
118 depthOffset = expeDepthOffset * radius;
119 #endif
120
121 oPos = gl_ProjectionMatrix * ePos;
122
123 #if (EXPE_EWA_HINT>0)
124 scaledFragCenter2d = 0.5*((oPos.xy/oPos.w)+1.0)*halfVp*oneOverEwaRadius;
125 #endif
126
127 #ifdef EXPE_ATI_WORKAROUND
128 fragCenterAndRadius.xyz = (oPos.xyz/oPos.w) + 1.0;
129 fragCenterAndRadius.xy = fragCenterAndRadius.xy*halfVp;
130 fragCenterAndRadius.z = fragCenterAndRadius.z*0.5;
131 fragCenterAndRadius.w = pointSize.x;
132 #endif
133
134 #ifndef EXPE_EARLY_BACK_FACE_CULLING
135 oPos.w = oPos.w * ( (dotpn<0.0) ? 1.0 : 0.0);
136 #else
137 }
138 #endif
139
140 gl_Position = oPos;
141 }
142
143 #endif
144
145 //--------------------------------------------------------------------------------
146 // Visibility Splatting
147 // Fragment Shader
148 //--------------------------------------------------------------------------------
149
150 #ifdef __VisibilityFP__
151 varying vec2 scaledFragCenter2d;
152 uniform vec3 rayCastParameter1;
153 uniform vec3 rayCastParameter2;
154 uniform vec2 depthParameterCast;
155
156 void VisibilityFP(void)
157 {
158 #ifdef EXPE_ATI_WORKAROUND
159 vec3 fragCoord;
160 fragCoord.xy = fragCenterAndRadius.xy + (gl_TexCoord[0].st-0.5) * fragCenterAndRadius.w;
161 fragCoord.z = fragCenterAndRadius.z;
162 #else
163 vec3 fragCoord = gl_FragCoord.xyz;
164 #endif
165 // compute q in object space
166 vec3 qOne = rayCastParameter1 * fragCoord + rayCastParameter2; // MAD
167 float oneOverDepth = dot(qOne,-fragNoverCdotN); // DP3
168 float depth = (1.0/oneOverDepth); // RCP
169 vec3 diff = fragCenter + qOne * depth; // MAD
170 float r2 = dot(diff,diff); // DP3
171
172 #if (EXPE_EWA_HINT>0)
173 vec2 d2 = oneOverEwaRadius*gl_FragCoord.xy - scaledFragCenter2d; // MAD
174 float r2d = dot(d2,d2); // DP3
175 gl_FragColor = vec4(min(r2d,r2*scaleSquaredDistance));
176 #else
177 gl_FragColor = vec4(r2*scaleSquaredDistance);
178 #endif
179
180 #ifdef EXPE_DEPTH_CORRECTION
181 oneOverDepth = 1.0/(-depth+depthOffset);
182 gl_FragDepth = depthParameterCast.x * oneOverDepth + depthParameterCast.y; // MAD
183 #endif
184 }
185
186 #endif
187
188 #ifdef __AttributeVP__
189
190 varying vec2 scaledFragCenter2d;
191
192 void AttributeVP(void)
193 {
194 // transform normal
195 vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
196 // Point in eye space
197 vec4 ePos = gl_ModelViewMatrix * gl_Vertex;
198
199 float dotpn = dot(normal.xyz,ePos.xyz);
200
201 vec4 oPos;
202
203 #ifdef EXPE_EARLY_BACK_FACE_CULLING
204 // back_face culling
205 oPos = vec4(0,0,1,0);
206 if(dotpn<0.)
207 {
208 #endif
209
210 #ifdef EXPE_BACKFACE_SHADING
211 if(dotpn>0.)
212 {
213 dotpn = -dotpn;
214 normal = -normal;
215 }
216 #endif
217
218 float radius = gl_MultiTexCoord2.x * expeRadiusScale * 1.05;
219
220 vec4 pointSize;
221 pointSize.x = radius * expePreComputeRadius / ePos.z;
222
223 #if (EXPE_EWA_HINT>0)
224 gl_PointSize = max(2.0, pointSize.x);
225 #else
226 gl_PointSize = max(1.0, pointSize.x);
227 #endif
228
229 scaleSquaredDistance = 1. / (radius * radius);
230 ///********* uncommented fragNormal....
231 //fragNormal = normal;
232 fragCenter = ePos.xyz;
233 fragNoverCdotN = normal/dot(ePos.xyz,normal);
234
235 // Output color
236 #ifdef EXPE_DEFERRED_SHADING
237 fragNormal.xyz = normal.xyz;
238 gl_FrontColor = gl_Color;
239 #else
240 // Output color
241 #ifdef EXPE_LIGHTING
242 gl_FrontColor = expeLighting(gl_Color, ePos.xyz, normal.xyz, 1.);
243 #else
244 gl_FrontColor = meshlabLighting(gl_Color, ePos.xyz, normal.xyz);
245 #endif
246 #endif
247
248 oPos = gl_ModelViewProjectionMatrix * gl_Vertex;
249
250 #ifdef EXPE_ATI_WORKAROUND
251 fragCenterAndRadius.xyz = (oPos.xyz/oPos.w) + 1.0;
252 fragCenterAndRadius.xy = fragCenterAndRadius.xy*halfVp;
253 fragCenterAndRadius.z = fragCenterAndRadius.z*0.5;
254 fragCenterAndRadius.w = pointSize.x;
255 #endif
256
257 #if (EXPE_EWA_HINT>0)
258 scaledFragCenter2d = ((oPos.xy/oPos.w)+1.0)*halfVp*oneOverEwaRadius;
259 #endif
260
261 #ifndef EXPE_EARLY_BACK_FACE_CULLING
262 oPos.w = oPos.w * (dotpn<0. ? 1.0 : 0.0);
263 #else
264 }
265 #endif
266
267 gl_Position = oPos;
268 }
269
270 #endif
271
272 //--------------------------------------------------------------------------------
273 // EWA Splatting
274 // Fragment Shader
275 //--------------------------------------------------------------------------------
276
277 #ifdef __AttributeFP__
278 // this sampler is only used by this fragment shader
279
280 varying vec2 scaledFragCenter2d;
281 uniform vec3 rayCastParameter1;
282 uniform vec3 rayCastParameter2;
283 uniform vec2 depthParameterCast;
284
285 // uniform sampler1D Kernel1dMap;
286
287 void AttributeFP(void)
288 {
289 #ifdef EXPE_ATI_WORKAROUND
290 vec3 fragCoord;
291 fragCoord.xy = fragCenterAndRadius.xy + (gl_TexCoord[0].st-0.5) * fragCenterAndRadius.w;
292 fragCoord.z = fragCenterAndRadius.z;
293 #else
294 vec3 fragCoord = gl_FragCoord.xyz;
295 #endif
296
297 #if 1
298 vec3 qOne = rayCastParameter1 * fragCoord + rayCastParameter2; // MAD
299 float oneOverDepth = dot(qOne,fragNoverCdotN); // DP3
300 float depth = (1.0/oneOverDepth); // RCP
301 vec3 diff = fragCenter - qOne * depth; // MAD
302 float r2 = dot(diff,diff); // DP3
303
304 #if (EXPE_EWA_HINT>0)
305 vec2 d2 = oneOverEwaRadius*gl_FragCoord.xy - scaledFragCenter2d; // MAD
306 float r2d = dot(d2,d2); // DP3
307 // float weight = texture1D(Kernel1dMap, min(r2d,r2*scaleSquaredDistance)).a; // MUL + MIN + TEX
308 float weight = min(r2d,r2*scaleSquaredDistance);
309 weight = clamp(1.-weight,0,1);
310 weight = weight*weight;
311 #else
312 //float weight = texture1D(Kernel1dMap, r2*scaleSquaredDistance).a; // MUL + TEX
313 float weight = clamp(1.-r2*scaleSquaredDistance,0.0,1.0);
314 weight = weight*weight;
315 #endif
316 weight *= 0.1; // limits overflow
317
318 #ifdef EXPE_DEPTH_CORRECTION
319 gl_FragDepth = depthParameterCast.x * oneOverDepth + depthParameterCast.y; // MAD
320 #endif
321
322 #ifdef EXPE_DEFERRED_SHADING
323 gl_FragData[0].rgb = gl_Color.rgb; // MOV
324 gl_FragData[1].xyz = fragNormal.xyz; // MOV
325 gl_FragData[1].w = weight; // MOV
326 gl_FragData[0].w = weight;
327
328 #if EXPE_DEPTH_INTERPOLATION==2 // linear space
329 gl_FragData[1].z = -depth; // MOV
330 #elif EXPE_DEPTH_INTERPOLATION==1 // window space
331 #ifdef EXPE_DEPTH_CORRECTION
332 gl_FragData[1].z = gl_FragDepth;
333 #else
334 gl_FragData[1].z = fragCoord.z;
335 #endif
336 #endif
337
338 #else
339 gl_FragColor.rgb = gl_Color.rgb; // MOV
340 gl_FragColor.w = weight;
341 #endif
342 #endif
343 }
344
345 #endif