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

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

Aded volume rendering shaders

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