root/OpenSceneGraph-Data/trunk/shaders/volume_n_mip.frag

Revision 8959 (checked in by robert, 3 months ago)

Aded volume rendering shaders

Line 
1 uniform sampler3D baseTexture;
2 uniform sampler3D normalMap;
3 uniform float sampleDensity;
4 uniform float transparency;
5 uniform float alphaCutOff;
6
7 varying vec4 cameraPos;
8 varying vec4 vertexPos;
9 varying mat4 texgen;
10
11 void main(void)
12 {
13
14     vec3 t0 = (texgen * vertexPos).xyz;
15     vec3 te = (texgen * cameraPos).xyz;
16
17     vec3 eyeDirection = normalize(te-t0);
18
19     if (te.x>=0.0 && te.x<=1.0 &&
20         te.y>=0.0 && te.y<=1.0 &&
21         te.z>=0.0 && te.z<=1.0)
22     {
23         // do nothing... te inside volume
24     }
25     else
26     {
27         if (te.x<0.0)
28         {
29             float r = -te.x / (t0.x-te.x);
30             te = te + (t0-te)*r;
31         }
32
33         if (te.x>1.0)
34         {
35             float r = (1.0-te.x) / (t0.x-te.x);
36             te = te + (t0-te)*r;
37         }
38
39         if (te.y<0.0)
40         {
41             float r = -te.y / (t0.y-te.y);
42             te = te + (t0-te)*r;
43         }
44
45         if (te.y>1.0)
46         {
47             float r = (1.0-te.y) / (t0.y-te.y);
48             te = te + (t0-te)*r;
49         }
50
51         if (te.z<0.0)
52         {
53             float r = -te.z / (t0.z-te.z);
54             te = te + (t0-te)*r;
55         }
56
57         if (te.z>1.0)
58         {
59             float r = (1.0-te.z) / (t0.z-te.z);
60             te = te + (t0-te)*r;
61         }
62     }
63
64     const float max_iteratrions = 2048.0;
65     float num_iterations = ceil(length(te-t0)/sampleDensity);
66     if (num_iterations<2.0) num_iterations = 2.0;
67
68     if (num_iterations>max_iteratrions)
69     {
70         num_iterations = max_iteratrions;
71     }
72
73     vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);
74     vec3 texcoord = t0;
75
76     vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0);
77     while(num_iterations>0.0)
78     {
79         vec4 normal = texture3D( normalMap, texcoord);
80 #if 1
81         vec4 color = texture3D( baseTexture, texcoord);
82
83         normal.x = normal.x*2.0-1.0;
84         normal.y = normal.y*2.0-1.0;
85         normal.z = normal.z*2.0-1.0;
86        
87         float lightScale = 0.1 +  max(dot(normal.xyz, eyeDirection), 0.0);
88         color.x *= lightScale;
89         color.y *= lightScale;
90         color.z *= lightScale;
91
92         float r = normal[3]*transparency;
93 #else
94         vec4 color = texture3D( normalMap, texcoord);
95         color.x = color.x*2.0 - 1.0;
96         color.y = color.y*2.0 - 1.0;
97         color.z = color.z*2.0 - 1.0;
98        
99         float lightScale = 0.1 +  max(dot(color.xyz, eyeDirection), 0.0);
100         color.x = lightScale;
101         color.y = lightScale;
102         color.z = lightScale;
103
104         float r = color[3]*transparency;
105 #endif       
106         if (r>alphaCutOff)
107         {
108             fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;
109             fragColor.w += r;
110         }
111         texcoord += deltaTexCoord;
112
113         --num_iterations;
114     }
115
116     if (fragColor.w>1.0) fragColor.w = 1.0;
117     if (fragColor.w==0.0) discard;
118     gl_FragColor = fragColor;
119 }
Note: See TracBrowser for help on using the browser.