45 #ifndef CShaderIsosurfaceRGBA8 46 #define CShaderIsosurfaceRGBA8 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" 97 " // bounding box. \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" 112 " //---------------------------------------------------------------------- \n" 113 " // Estimates the intensity gradient of the volume in model space \n" 114 " //---------------------------------------------------------------------- \n" 116 " vec3 gradient(vec3 tc) \n" 118 " vec3 nabla = vec3( \n" 119 " texture3D(uVolume, tc + dx).a - texture3D(uVolume, tc - dx).a, \n" 120 " texture3D(uVolume, tc + dy).a - texture3D(uVolume, tc - dy).a, \n" 121 " texture3D(uVolume, tc + dz).a - texture3D(uVolume, tc - dz).a \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).a - 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" 164 " return (Ia + Id + Is); \n" 168 " //---------------------------------------------------------------------- \n" 169 " // Main fragment shader code. \n" 170 " //---------------------------------------------------------------------- \n" 172 " void main(void) \n" 174 " vec4 camera = gl_ModelViewMatrixInverse * vec4(0.0, 0.0, 0.0, 1.0); \n" 175 " vec3 raydir = normalize(vPosition.xyz - camera.xyz); \n" 177 " float t_entry = entry(vPosition.xyz, raydir); \n" 178 " t_entry = max(t_entry, -distance(camera.xyz, vPosition.xyz)); \n" 180 " // estimate a reasonable step size \n" 181 " float t_step = distance(uMinCorner, uMaxCorner) / uResolution; \n" 182 " vec3 tc_step = uTextureScale * (t_step * raydir); \n" 184 " // cast the ray (in model space) \n" 185 " vec4 sum = vec4(0.0); \n" 186 " vec3 tc = gl_TexCoord[0].stp + t_entry * tc_step / t_step; \n" 188 " for (float t = t_entry; t < 0.0; t += t_step, tc += tc_step) \n" 190 " // sample the volume for intensity (red channel) \n" 191 " float intensity = texture3D(uVolume, tc).a; \n" 193 " if (intensity > uIsosurface) \n" 195 " vec3 tcr = refine(tc - tc_step, tc, uIsosurface, 1.0); \n" 196 " vec3 nabla = gradient(tcr); \n" 198 " float dt = length(tcr - tc) / length(tc_step); \n" 199 " vec3 position = vPosition.xyz + (t - dt * t_step) * raydir; \n" 200 " vec3 normal = -normalize(nabla); \n" 201 " vec3 view = -raydir; \n" 202 " vec3 colour = shade(position, view, normal); \n" 204 " sum = vec4(colour, 1.0); \n" 206 " // calculate fragment depth \n" 207 " vec4 clip = gl_ModelViewProjectionMatrix * vec4(position, 1.0); \n" 208 " gl_FragDepth = (gl_DepthRange.diff * clip.z / clip.w + gl_DepthRange.near + gl_DepthRange.far) * 0.5; \n" 214 " // discard the fragment if no geometry was intersected \n" 215 " if (sum.a <= 0.0) discard; \n" 217 " gl_FragColor = sum; \n" const std::string C_SHADER_ISOSURFACE_RGBA8_FRAG
Definition: CShaderIsosurface-RGBA8.h:78
const std::string C_SHADER_ISOSURFACE_RGBA8_VERT
Definition: CShaderIsosurface-RGBA8.h:55
Implements option settings for CHAI3D.
Definition: CAudioBuffer.cpp:56