45#ifndef CShaderIsosurfaceL8
46#define CShaderIsosurfaceL8
57" attribute vec3 aPosition; \n"
58" attribute vec3 aNormal; \n"
59" attribute vec3 aTexCoord; \n"
60" attribute vec4 aColor; \n"
61" attribute vec3 aTangent; \n"
62" attribute vec3 aBitangent; \n"
64" varying vec4 vPosition; \n"
66" //---------------------------------------------------------------------- \n"
67" // Main vertex shader code. \n"
68" //---------------------------------------------------------------------- \n"
72" gl_TexCoord[0] = gl_TextureMatrix[0] * vec4(aTexCoord, 1.0); \n"
73" vPosition = gl_Vertex; \n"
74" gl_Position = ftransform(); \n"
80" uniform vec3 uMinCorner; \n"
81" uniform vec3 uMaxCorner; \n"
82" uniform vec3 uTextureScale; \n"
83" uniform vec3 uGradientDelta; \n"
84" uniform sampler3D uVolume; \n"
85" uniform float uIsosurface; \n"
86" uniform float uResolution; \n"
88" varying vec4 vPosition; \n"
90" vec3 dx = vec3(uGradientDelta.x, 0.0, 0.0); \n"
91" vec3 dy = vec3(0.0, uGradientDelta.y, 0.0); \n"
92" vec3 dz = vec3(0.0, 0.0, uGradientDelta.z); \n"
95" //---------------------------------------------------------------------- \n"
96" // Finds the entering intersection between a ray e1+d and the volume's \n"
98" //---------------------------------------------------------------------- \n"
100" float entry(vec3 e1, vec3 d) \n"
102" float t = distance(uMinCorner, uMaxCorner); \n"
104" vec3 a = (uMinCorner - e1) / d; \n"
105" vec3 b = (uMaxCorner - e1) / d; \n"
106" vec3 u = min(a, b); \n"
108" return max( max(-t, u.x), max(u.y, u.z) ); \n"
113" //---------------------------------------------------------------------- \n"
114" // Estimates the intensity gradient of the volume in model space \n"
115" //---------------------------------------------------------------------- \n"
117" vec3 gradient(vec3 tc) \n"
119" vec3 nabla = vec3( \n"
120" texture3D(uVolume, tc + dx).r - texture3D(uVolume, tc - dx).r, \n"
121" texture3D(uVolume, tc + dy).r - texture3D(uVolume, tc - dy).r, \n"
122" texture3D(uVolume, tc + dz).r - texture3D(uVolume, tc - dz).r \n"
124" return (nabla / uGradientDelta) * uTextureScale; \n"
128" //---------------------------------------------------------------------- \n"
129" // Performs interval bisection and returns the value between a and b \n"
130" // closest to isosurface. When s(b) > s(a), direction should be +1.0, \n"
131" // and -1.0 otherwise. \n"
132" //---------------------------------------------------------------------- \n"
134" vec3 refine(vec3 a, vec3 b, float isosurface, float direction) \n"
136" for (int i = 0; i < 6; ++i) \n"
138" vec3 m = 0.5 * (a + b); \n"
139" float v = (texture3D(uVolume, m).r - isosurface) * direction; \n"
140" if (v >= 0.0) b = m; \n"
147" //---------------------------------------------------------------------- \n"
148" // Computes phong shading based on current light and material \n"
150" //---------------------------------------------------------------------- \n"
152" vec3 shade(vec3 p, vec3 v, vec3 n) \n"
154" vec4 lp = gl_ModelViewMatrixInverse * gl_LightSource[0].position; \n"
155" vec3 l = normalize(lp.xyz - p * lp.w); \n"
156" vec3 h = normalize(l+v); \n"
157" float cos_i = max(dot(n, l), 0.0); \n"
158" float cos_h = max(dot(n, h), 0.0); \n"
160" vec3 Ia = gl_FrontLightProduct[0].ambient.rgb; \n"
161" vec3 Id = gl_FrontLightProduct[0].diffuse.rgb * cos_i; \n"
162" vec3 Is = gl_FrontLightProduct[0].specular.rgb * pow(cos_h, gl_FrontMaterial.shininess); \n"
163" return (Ia + Id + Is); \n"
167" //---------------------------------------------------------------------- \n"
168" // Main fragment shader code. \n"
169" //---------------------------------------------------------------------- \n"
173" vec4 camera = gl_ModelViewMatrixInverse * vec4(0.0, 0.0, 0.0, 1.0); \n"
174" vec3 raydir = normalize(vPosition.xyz - camera.xyz); \n"
176" float t_entry = entry(vPosition.xyz, raydir); \n"
177" t_entry = max(t_entry, -distance(camera.xyz, vPosition.xyz)); \n"
179" // estimate a reasonable step size \n"
180" float t_step = distance(uMinCorner, uMaxCorner) / uResolution; \n"
181" vec3 tc_step = uTextureScale * (t_step * raydir); \n"
183" // cast the ray (in model space) \n"
184" vec4 sum = vec4(0.0); \n"
185" vec3 tc = gl_TexCoord[0].stp + t_entry * tc_step / t_step; \n"
187" for (float t = t_entry; t < 0.0; t += t_step, tc += tc_step) \n"
189" // sample the volume for intensity (red channel) \n"
190" float intensity = texture3D(uVolume, tc).r; \n"
192" if (intensity > uIsosurface) \n"
194" vec3 tcr = refine(tc - tc_step, tc, uIsosurface, 1.0); \n"
195" vec3 nabla = gradient(tcr); \n"
197" float dt = length(tcr - tc) / length(tc_step); \n"
198" vec3 position = vPosition.xyz + (t - dt * t_step) * raydir; \n"
199" vec3 normal = -normalize(nabla); \n"
200" vec3 view = -raydir; \n"
201" vec3 colour = shade(position, view, normal); \n"
203" sum = vec4(colour, 1.0); \n"
205" // calculate fragment depth \n"
206" vec4 clip = gl_ModelViewProjectionMatrix * vec4(position, 1.0); \n"
207" gl_FragDepth = (gl_DepthRange.diff * clip.z / clip.w + gl_DepthRange.near + gl_DepthRange.far) * 0.5; \n"
213" // discard the fragment if no geometry was intersected \n"
214" if (sum.a <= 0.0) discard; \n"
216" gl_FragColor = sum; \n"
Implements option settings for CHAI3D.
Definition CAudioBuffer.cpp:57
const std::string C_SHADER_ISOSURFACE_L8_VERT
Definition CShaderIsosurface-L8.h:55
const std::string C_SHADER_ISOSURFACE_L8_FRAG
Definition CShaderIsosurface-L8.h:78