CShaderIsosurfaceColor-LUT8.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, CHAI3D.
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Sonny Chan
40  \version 3.2.0 $Rev: 2015 $
41  */
42 //==============================================================================
43 
44 //---------------------------------------------------------------------------
45 #ifndef CShaderIsosurfaceColorLUT8
46 #define CShaderIsosurfaceColorLUT8
47 //---------------------------------------------------------------------------
48 #include "system/CGlobals.h"
49 //---------------------------------------------------------------------------
50 
51 //---------------------------------------------------------------------------
52 namespace chai3d {
53 //---------------------------------------------------------------------------
54 
56 " \n"
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"
63 " \n"
64 " varying vec4 vPosition; \n"
65 " \n"
66 " //---------------------------------------------------------------------- \n"
67 " // Main vertex shader code. \n"
68 " //---------------------------------------------------------------------- \n"
69 " \n"
70 " void main(void) \n"
71 " { \n"
72 " gl_TexCoord[0] = gl_TextureMatrix[0] * vec4(aTexCoord, 1.0); \n"
73 " vPosition = gl_Vertex; \n"
74 " gl_Position = ftransform(); \n"
75 " } \n"
76 " \n";
77 
79 " \n"
80 " varying vec4 vPosition; \n"
81 " \n"
82 " uniform vec3 uMinCorner; \n"
83 " uniform vec3 uMaxCorner; \n"
84 " uniform vec3 uTextureScale; \n"
85 " uniform vec3 uGradientDelta; \n"
86 " uniform sampler1D uColorLUT; \n"
87 " uniform sampler3D uVolume; \n"
88 " uniform float uIsosurface; \n"
89 " uniform float uResolution; \n"
90 " \n"
91 " vec3 dx = vec3(uGradientDelta.x, 0.0, 0.0); \n"
92 " vec3 dy = vec3(0.0, uGradientDelta.y, 0.0); \n"
93 " vec3 dz = vec3(0.0, 0.0, uGradientDelta.z); \n"
94 " \n"
95 " \n"
96 " //---------------------------------------------------------------------- \n"
97 " // Finds the entering intersection between a ray e1+d and the volume's \n"
98 " // bounding box. \n"
99 " //---------------------------------------------------------------------- \n"
100 " \n"
101 " float entry(vec3 e1, vec3 d) \n"
102 " { \n"
103 " float t = distance(uMinCorner, uMaxCorner); \n"
104 " \n"
105 " vec3 a = (uMinCorner - e1) / d; \n"
106 " vec3 b = (uMaxCorner - e1) / d; \n"
107 " vec3 u = min(a, b); \n"
108 " \n"
109 " return max( max(-t, u.x), max(u.y, u.z) ); \n"
110 " } \n"
111 " \n"
112 " \n"
113 " //---------------------------------------------------------------------- \n"
114 " // Estimates the intensity gradient of the volume in model space \n"
115 " //---------------------------------------------------------------------- \n"
116 " \n"
117 " vec3 gradient(vec3 tc) \n"
118 " { \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"
123 " ); \n"
124 " \n"
125 " return (nabla / uGradientDelta) * uTextureScale; \n"
126 " } \n"
127 " \n"
128 " \n"
129 " //---------------------------------------------------------------------- \n"
130 " // Performs interval bisection and returns the value between a and b \n"
131 " // closest to isosurface. When s(b) > s(a), direction should be +1.0, \n"
132 " // and -1.0 otherwise. \n"
133 " //---------------------------------------------------------------------- \n"
134 " \n"
135 " vec3 refine(vec3 a, vec3 b, float isosurface, float direction) \n"
136 " { \n"
137 " for (int i = 0; i < 6; ++i) \n"
138 " { \n"
139 " vec3 m = 0.5 * (a + b); \n"
140 " float v = (texture3D(uVolume, m).r - isosurface) * direction; \n"
141 " if (v >= 0.0) b = m; \n"
142 " else a = m; \n"
143 " } \n"
144 " return b; \n"
145 " } \n"
146 " \n"
147 " \n"
148 " //---------------------------------------------------------------------- \n"
149 " // Computes phong shading based on current light and material \n"
150 " // properties. \n"
151 " //---------------------------------------------------------------------- \n"
152 " \n"
153 " vec3 shade(vec3 p, vec3 v, vec3 n) \n"
154 " { \n"
155 " vec4 lp = gl_ModelViewMatrixInverse * gl_LightSource[0].position; \n"
156 " vec3 l = normalize(lp.xyz - p * lp.w); \n"
157 " vec3 h = normalize(l+v); \n"
158 " float cos_i = max(dot(n, l), 0.0); \n"
159 " float cos_h = max(dot(n, h), 0.0); \n"
160 " \n"
161 " vec3 Ia = gl_FrontLightProduct[0].ambient.rgb; \n"
162 " vec3 Id = gl_FrontLightProduct[0].diffuse.rgb * cos_i; \n"
163 " vec3 Is = gl_FrontLightProduct[0].specular.rgb * pow(cos_h, gl_FrontMaterial.shininess); \n"
164 " return (Ia + Id + Is); \n"
165 " } \n"
166 " \n"
167 " \n"
168 " //---------------------------------------------------------------------- \n"
169 " // Main fragment shader code. \n"
170 " //---------------------------------------------------------------------- \n"
171 " \n"
172 " void main(void) \n"
173 " { \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"
176 " \n"
177 " float t_entry = entry(vPosition.xyz, raydir); \n"
178 " t_entry = max(t_entry, -distance(camera.xyz, vPosition.xyz)); \n"
179 " \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"
183 " \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"
187 " \n"
188 " for (float t = t_entry; t < 0.0; t += t_step, tc += tc_step) \n"
189 " { \n"
190 " // sample the volume for intensity (red channel) \n"
191 " float intensity = texture3D(uVolume, tc).r; \n"
192 " \n"
193 " if (intensity > uIsosurface) \n"
194 " { \n"
195 " vec3 tcr = refine(tc - tc_step, tc, uIsosurface, 1.0); \n"
196 " vec3 nabla = gradient(tcr); \n"
197 " \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 = 0.5 * shade(position, view, normal) + 0.5 * texture1D(uColorLUT, intensity).rgb; \n"
203 " \n"
204 " sum = vec4(colour, 1.0); \n"
205 " \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"
209 " \n"
210 " break; \n"
211 " } \n"
212 " } \n"
213 " \n"
214 " // discard the fragment if no geometry was intersected \n"
215 " if (sum.a <= 0.0) discard; \n"
216 " \n"
217 " gl_FragColor = sum; \n"
218 " } \n"
219 " \n";
220 
221 
222 //---------------------------------------------------------------------------
223 } // namespace chai3d
224 //---------------------------------------------------------------------------
225 
226 //---------------------------------------------------------------------------
227 #endif
228 //---------------------------------------------------------------------------
const std::string C_SHADER_ISOSURFACE_COLOR_LUT8_VERT
Definition: CShaderIsosurfaceColor-LUT8.h:55
Implements option settings for CHAI3D.
const std::string C_SHADER_ISOSURFACE_COLOR_LUT8_FRAG
Definition: CShaderIsosurfaceColor-LUT8.h:78
Definition: CAudioBuffer.cpp:56