1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Authors: |
---|
6 | * Wei Qiao <qiaow@purdue.edu> |
---|
7 | * Leif Delgass <ldelgass@purdue.edu> |
---|
8 | */ |
---|
9 | |
---|
10 | #include "common.cg" |
---|
11 | |
---|
12 | //#define LIGHT_TWO_SIDE |
---|
13 | |
---|
14 | /* |
---|
15 | * render one volume |
---|
16 | * |
---|
17 | * material: |
---|
18 | * x: ambient coefficient |
---|
19 | * y: diffuse coefficient |
---|
20 | * z: specular level/coefficient |
---|
21 | * w: specular exponent |
---|
22 | * renderParams: |
---|
23 | * x: sample distance ratio (0 = single slice) |
---|
24 | * y: isosurface flag (opacity scaling on/off) |
---|
25 | * z: opacity scale |
---|
26 | * w: two-sided lighting |
---|
27 | */ |
---|
28 | PixelOut main(v2f IN, |
---|
29 | uniform sampler3D volume, |
---|
30 | uniform sampler1D tf, |
---|
31 | uniform float4x4 modelViewInv, |
---|
32 | uniform float4x4 modelView, |
---|
33 | uniform float4 material, |
---|
34 | uniform float4 renderParams) |
---|
35 | { |
---|
36 | PixelOut OUT; |
---|
37 | float4 tex_coord = mul(modelViewInv, IN.TexCoord); |
---|
38 | float4 sample = tex3D(volume, tex_coord.xyz); |
---|
39 | |
---|
40 | //sample the transfer function texture |
---|
41 | float4 color = tex1D(tf, sample.x); |
---|
42 | |
---|
43 | if (renderParams.x == 0.0) { |
---|
44 | //If single slice render, only flat shading, completely opaque. |
---|
45 | color.w = 1; |
---|
46 | } else { |
---|
47 | //regular volume rendering, we do PHONG SHADING |
---|
48 | //lighting parameters |
---|
49 | float3 normal; |
---|
50 | float diffuse, specular; |
---|
51 | if (dot(sample.yzw, sample.yzw) < 5.0e-5) { |
---|
52 | diffuse = 0; |
---|
53 | specular = 0; |
---|
54 | } else { |
---|
55 | normal = normalize(sample.yzw * 2.0 - 1.0); |
---|
56 | float3 light_vector = normalize(IN.Light); |
---|
57 | float3 eye_vector = normalize(IN.EyeVector); |
---|
58 | float3 half_vector = normalize(eye_vector+light_vector); |
---|
59 | |
---|
60 | //lighting computation |
---|
61 | #ifndef LIGHT_TWO_SIDE |
---|
62 | float normal_dot_light = dot(normal, light_vector); |
---|
63 | float normal_dot_half = dot(normal, half_vector); |
---|
64 | if (renderParams.w > 0.5) { |
---|
65 | normal_dot_light = abs(normal_dot_light); |
---|
66 | normal_dot_half = abs(normal_dot_half); |
---|
67 | } else { |
---|
68 | normal_dot_light = max(normal_dot_light, 0); |
---|
69 | normal_dot_half = max(normal_dot_half, 0); |
---|
70 | } |
---|
71 | #else |
---|
72 | float normal_dot_light = abs(dot(normal, light_vector)); |
---|
73 | float normal_dot_half = abs(dot(normal, half_vector)); |
---|
74 | #endif |
---|
75 | diffuse = normal_dot_light * material.y; |
---|
76 | if (diffuse > 1.0e-6) |
---|
77 | specular = pow(normal_dot_half, material.w) * material.z; |
---|
78 | else |
---|
79 | specular = 0.0; |
---|
80 | } |
---|
81 | float ambient = material.x; |
---|
82 | |
---|
83 | color.xyz = color.xyz * (ambient + diffuse) + float3(specular); |
---|
84 | |
---|
85 | if (renderParams.y < 0.5) { |
---|
86 | // Opacity scaling: equivalent to scaling transfer function |
---|
87 | color.w = color.w * renderParams.z; |
---|
88 | // Apply opacity correction for ratio of current sample distance |
---|
89 | // to data set sample distance |
---|
90 | color.w = (1.0 - pow((1.0 - color.w), renderParams.x)); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | OUT.Color = min(max(color, 0.0), 1.0); |
---|
95 | return OUT; |
---|
96 | } |
---|