CShaderDVR-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 CShaderDVRLUT8
46 #define CShaderDVRLUT8
47 //---------------------------------------------------------------------------
48 #include "system/CGlobals.h"
49 //---------------------------------------------------------------------------
50 
51 //---------------------------------------------------------------------------
52 namespace chai3d {
53 //---------------------------------------------------------------------------
54 
55 const std::string C_SHADER_DVR_LUT8_VERT =
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 
78 const std::string C_SHADER_DVR_LUT8_FRAG =
79 " \n"
80 " uniform vec3 uMinCorner; // lower bounding corner, model space \n"
81 " uniform vec3 uMaxCorner; // upper bounding corner, model space \n"
82 " uniform vec3 uTextureScale; // convert model to texture coordinates \n"
83 " uniform vec3 uGradientDelta; // one voxel step in texture space \n"
84 " uniform sampler3D uVolume; // volume texture \n"
85 " uniform sampler1D uColorLUT; // 1D transfer function texture \n"
86 " uniform float uIsosurface; // isosurface level \n"
87 " uniform float uOpacityThreshold; // opacity threshold \n"
88 " uniform float uOpticalDensityFactor; // Optical density factor \n"
89 " uniform float uResolution; // max samples to take on ray traversal \n"
90 " \n"
91 " varying vec4 vPosition; \n"
92 " \n"
93 " // deltas for gradient estimation along each axis in texture space \n"
94 " vec3 dx = vec3(uGradientDelta.x, 0.0, 0.0); \n"
95 " vec3 dy = vec3(0.0, uGradientDelta.y, 0.0); \n"
96 " vec3 dz = vec3(0.0, 0.0, uGradientDelta.z); \n"
97 " \n"
98 " \n"
99 " //---------------------------------------------------------------------- \n"
100 " // Finds the entering intersection between a ray e1+d and the volume's \n"
101 " // bounding box. \n"
102 " //---------------------------------------------------------------------- \n"
103 " \n"
104 " float entry(vec3 e1, vec3 d) \n"
105 " { \n"
106 " float t = distance(uMinCorner, uMaxCorner); \n"
107 " \n"
108 " vec3 a = (uMinCorner - e1) / d; \n"
109 " vec3 b = (uMaxCorner - e1) / d; \n"
110 " vec3 u = min(a, b); \n"
111 " \n"
112 " return max( max(-t, u.x), max(u.y, u.z) ); \n"
113 " } \n"
114 " \n"
115 " \n"
116 " //---------------------------------------------------------------------- \n"
117 " // Estimates the intensity gradient of the volume in model space \n"
118 " //---------------------------------------------------------------------- \n"
119 " \n"
120 " vec3 gradient(vec3 tc) \n"
121 " { \n"
122 " vec3 nabla = vec3( \n"
123 " texture3D(uVolume, tc + dx).r - texture3D(uVolume, tc - dx).r, \n"
124 " texture3D(uVolume, tc + dy).r - texture3D(uVolume, tc - dy).r, \n"
125 " texture3D(uVolume, tc + dz).r - texture3D(uVolume, tc - dz).r \n"
126 " ); \n"
127 " \n"
128 " return (nabla / uGradientDelta) * uTextureScale; \n"
129 " } \n"
130 " \n"
131 " \n"
132 " //---------------------------------------------------------------------- \n"
133 " // Computes phong shading based on current light and material \n"
134 " // properties. \n"
135 " //---------------------------------------------------------------------- \n"
136 " \n"
137 " vec3 shade(vec3 p, vec3 v, vec3 n) \n"
138 " { \n"
139 " vec4 lp = gl_ModelViewMatrixInverse * gl_LightSource[0].position; \n"
140 " vec3 l = normalize(lp.xyz - p * lp.w); \n"
141 " vec3 h = normalize(l+v); \n"
142 " float cos_i = max(dot(n, l), 0.0); \n"
143 " float cos_h = max(dot(n, h), 0.0); \n"
144 " \n"
145 " vec3 Ia = gl_FrontLightProduct[0].ambient.rgb; \n"
146 " vec3 Id = gl_FrontLightProduct[0].diffuse.rgb * cos_i; \n"
147 " vec3 Is = gl_FrontLightProduct[0].specular.rgb * pow(cos_h, gl_FrontMaterial.shininess); \n"
148 " return (Ia + Id + Is); \n"
149 " } \n"
150 " \n"
151 " \n"
152 " //---------------------------------------------------------------------- \n"
153 " // Main fragment shader code. \n"
154 " //---------------------------------------------------------------------- \n"
155 " \n"
156 " void main(void) \n"
157 " { \n"
158 " vec4 camera = gl_ModelViewMatrixInverse * vec4(0.0, 0.0, 0.0, 1.0); \n"
159 " vec3 raydir = normalize(vPosition.xyz - camera.xyz); \n"
160 " \n"
161 " float t_entry = entry(vPosition.xyz, raydir); \n"
162 " t_entry = max(t_entry, -distance(camera.xyz, vPosition.xyz)); \n"
163 " \n"
164 " // estimate a reasonable step size \n"
165 " float t_step = distance(uMinCorner, uMaxCorner) / uResolution; \n"
166 " vec3 tc_step = uTextureScale * (t_step * raydir); \n"
167 " \n"
168 " // cast the ray (in model space) \n"
169 " vec4 sum = vec4(0.0); \n"
170 " vec3 tc = gl_TexCoord[0].stp + t_entry * tc_step / t_step; \n"
171 " \n"
172 " for (float t = t_entry; t < 0.0; t += t_step, tc += tc_step) \n"
173 " { \n"
174 " // sample the volume for intensity (red channel) \n"
175 " float intensity = texture3D(uVolume, tc).r; \n"
176 " \n"
177 " // look up intensity in the colour LUT \n"
178 " vec4 colour = texture1D(uColorLUT, intensity); \n"
179 " \n"
180 " // skip empty space \n"
181 " if (colour.a < 0.001) continue; \n"
182 " \n"
183 " // estimate gradient \n"
184 " vec3 nabla = gradient(tc); \n"
185 " \n"
186 " // compute shading \n"
187 " vec3 position = vPosition.xyz + t * raydir; \n"
188 " vec3 normal = -normalize(nabla); \n"
189 " vec3 view = -raydir; \n"
190 " vec3 shaded = shade(position, view, normal); \n"
191 " colour.rgb *= shaded; \n"
192 " \n"
193 " // compute transmission for this segment \n"
194 " float Tr = exp(-colour.a * uOpticalDensityFactor); \n"
195 " colour.rgb *= 1.0 - Tr; \n"
196 " colour.a = 1.0 - Tr; \n"
197 " \n"
198 " // accumulate colour and opacity \n"
199 " sum += (1.0 - sum.a) * colour; \n"
200 " \n"
201 " // early ray termination test \n"
202 " if (sum.a > uOpacityThreshold) \n"
203 " { \n"
204 " // calculate fragment depth \n"
205 " vec4 clip = gl_ModelViewProjectionMatrix * vec4(position, 1.0); \n"
206 " gl_FragDepth = (gl_DepthRange.diff * clip.z / clip.w + \n"
207 " gl_DepthRange.near + gl_DepthRange.far) * 0.5; \n"
208 " \n"
209 " break; \n"
210 " } \n"
211 " } \n"
212 " \n"
213 " gl_FragColor = sum; \n"
214 " } \n"
215 " \n";
216 
217 //---------------------------------------------------------------------------
218 } // namespace chai3d
219 //---------------------------------------------------------------------------
220 
221 //---------------------------------------------------------------------------
222 #endif
223 //---------------------------------------------------------------------------
const std::string C_SHADER_DVR_LUT8_FRAG
Definition: CShaderDVR-LUT8.h:78
Implements option settings for CHAI3D.
const std::string C_SHADER_DVR_LUT8_VERT
Definition: CShaderDVR-LUT8.h:55
Definition: CAudioBuffer.cpp:56