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