CShaderBasicVoxel-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 CShaderBasicVoxelLUT8
46 #define CShaderBasicVoxelLUT8
47 //---------------------------------------------------------------------------
48 #include "system/CGlobals.h"
49 //---------------------------------------------------------------------------
50 
51 //---------------------------------------------------------------------------
52 namespace chai3d {
53 //---------------------------------------------------------------------------
54 
55 const std::string C_SHADER_BASIC_VOXEL_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_BASIC_VOXEL_LUT8_FRAG =
79 " \n"
80 " uniform vec3 uMinCorner; \n"
81 " uniform vec3 uMaxCorner; \n"
82 " uniform vec3 uTextureScale; \n"
83 " uniform sampler3D uVolume; \n"
84 " uniform sampler1D uColorLUT; \n"
85 " uniform float uResolution; \n"
86 " uniform float uOpacity; \n"
87 " \n"
88 " varying vec4 vPosition; \n"
89 " \n"
90 " \n"
91 " //---------------------------------------------------------------------- \n"
92 " // Finds the entering intersection between a ray e1+d and the volume's \n"
93 " // bounding box. \n"
94 " //---------------------------------------------------------------------- \n"
95 " \n"
96 " float entry(vec3 e1, vec3 d) \n"
97 " { \n"
98 " float t = distance(uMinCorner, uMaxCorner); \n"
99 " \n"
100 " vec3 a = (uMinCorner - e1) / d; \n"
101 " vec3 b = (uMaxCorner - e1) / d; \n"
102 " vec3 u = min(a, b); \n"
103 " \n"
104 " return max( max(-t, u.x), max(u.y, u.z) ); \n"
105 " } \n"
106 " \n"
107 " \n"
108 " //---------------------------------------------------------------------- \n"
109 " // Performs interval bisection and returns the value between a and b \n"
110 " // closest to isosurface. When s(b) > s(a), direction should be +1.0, \n"
111 " // and -1.0 otherwise. \n"
112 " //---------------------------------------------------------------------- \n"
113 " \n"
114 " vec3 refine(vec3 a, vec3 b, float direction) \n"
115 " { \n"
116 " for (int i = 0; i < 6; ++i) \n"
117 " { \n"
118 " vec3 m = 0.5 * (a + b); \n"
119 " float v = texture3D(uVolume, m).a * direction; \n"
120 " if (v >= 0.0) b = m; \n"
121 " else a = m; \n"
122 " } \n"
123 " return b; \n"
124 " } \n"
125 " \n"
126 " \n"
127 " //---------------------------------------------------------------------- \n"
128 " // Main fragment shader code. \n"
129 " //---------------------------------------------------------------------- \n"
130 " \n"
131 " void main(void) \n"
132 " { \n"
133 " vec4 camera = gl_ModelViewMatrixInverse * vec4(0.0, 0.0, 0.0, 1.0); \n"
134 " vec3 raydir = normalize(vPosition.xyz - camera.xyz); \n"
135 " \n"
136 " float t_entry = entry(vPosition.xyz, raydir); \n"
137 " t_entry = max(t_entry, -distance(camera.xyz, vPosition.xyz)); \n"
138 " \n"
139 " // estimate a reasonable step size \n"
140 " float t_step = distance(uMinCorner, uMaxCorner) / uResolution; \n"
141 " vec3 tc_step = uTextureScale * (t_step * raydir); \n"
142 " \n"
143 " float sigma = 100.0 * uOpacity; \n"
144 " if (uOpacity == 1.0) \n"
145 " { \n"
146 " sigma = 1000.0; \n"
147 " } \n"
148 " \n"
149 " // cast the ray (in model space) \n"
150 " vec4 sum = vec4(0.0); \n"
151 " vec3 tc = gl_TexCoord[0].stp + t_entry * tc_step / t_step; \n"
152 " bool flag = true; \n"
153 " for (float t = t_entry; t < 0.0; t += t_step, tc += tc_step) \n"
154 " { \n"
155 " vec4 voxel = texture3D(uVolume, tc); \n"
156 " vec4 colour = texture1D(uColorLUT, voxel.r); \n"
157 " \n"
158 " float Tr = exp(-sigma * t_step); \n"
159 " colour *= 1.0 - Tr; \n"
160 " \n"
161 " if (flag) \n"
162 " { \n"
163 " if (voxel.r > 0.0) \n"
164 " { \n"
165 " vec3 tcr = refine(tc - tc_step, tc, 1.0); \n"
166 " \n"
167 " float dt = length(tcr - tc) / length(tc_step); \n"
168 " vec3 position = vPosition.xyz + (t - dt * t_step) * raydir; \n"
169 " vec4 clip = gl_ModelViewProjectionMatrix * vec4(position, 1.0); \n"
170 " gl_FragDepth=(gl_DepthRange.diff * clip.z / clip.w + gl_DepthRange.near + gl_DepthRange.far) * 0.5; \n"
171 " flag = false; \n"
172 " } \n"
173 " } \n"
174 " if (voxel.r > 0.0) \n"
175 " { \n"
176 " sum += (1.0 - sum.a) * colour; \n"
177 " } \n"
178 " } \n"
179 " \n"
180 " // discard the fragment if no geometry was intersected \n"
181 " if (sum.a <= 0.0) discard; \n"
182 " \n"
183 " gl_FragColor = sum; \n"
184 " } \n"
185 " \n";
186 
187 //---------------------------------------------------------------------------
188 } // namespace chai3d
189 //---------------------------------------------------------------------------
190 
191 //---------------------------------------------------------------------------
192 #endif
193 //---------------------------------------------------------------------------
const std::string C_SHADER_BASIC_VOXEL_LUT8_FRAG
Definition: CShaderBasicVoxel-LUT8.h:78
Implements option settings for CHAI3D.
const std::string C_SHADER_BASIC_VOXEL_LUT8_VERT
Definition: CShaderBasicVoxel-LUT8.h:55
Definition: CAudioBuffer.cpp:56