[2852] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
[378] | 2 | /* |
---|
[4904] | 3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
[378] | 4 | * |
---|
[4904] | 5 | * Authors: |
---|
| 6 | * Wei Qiao <qiaow@purdue.edu> |
---|
| 7 | * Leif Delgass <ldelgass@purdue.edu> |
---|
[378] | 8 | */ |
---|
[386] | 9 | |
---|
[378] | 10 | #include "common.cg" |
---|
| 11 | |
---|
[3362] | 12 | //#define LIGHT_TWO_SIDE |
---|
| 13 | |
---|
[378] | 14 | /* |
---|
| 15 | * render one volume |
---|
[2877] | 16 | * |
---|
[3362] | 17 | * material: |
---|
| 18 | * x: ambient coefficient |
---|
| 19 | * y: diffuse coefficient |
---|
| 20 | * z: specular level/coefficient |
---|
[2877] | 21 | * w: specular exponent |
---|
[3362] | 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 |
---|
[378] | 27 | */ |
---|
[524] | 28 | PixelOut main(v2f IN, |
---|
[2852] | 29 | uniform sampler3D volume, |
---|
| 30 | uniform sampler1D tf, |
---|
| 31 | uniform float4x4 modelViewInv, |
---|
| 32 | uniform float4x4 modelView, |
---|
[3362] | 33 | uniform float4 material, |
---|
| 34 | uniform float4 renderParams) |
---|
[378] | 35 | { |
---|
[2852] | 36 | PixelOut OUT; |
---|
| 37 | float4 tex_coord = mul(modelViewInv, IN.TexCoord); |
---|
| 38 | float4 sample = tex3D(volume, tex_coord.xyz); |
---|
[378] | 39 | |
---|
[2852] | 40 | //sample the transfer function texture |
---|
| 41 | float4 color = tex1D(tf, sample.x); |
---|
[386] | 42 | |
---|
[3362] | 43 | if (renderParams.x == 0.0) { |
---|
[2852] | 44 | //If single slice render, only flat shading, completely opaque. |
---|
| 45 | color.w = 1; |
---|
| 46 | } else { |
---|
| 47 | //regular volume rendering, we do PHONG SHADING |
---|
[2877] | 48 | //lighting parameters |
---|
[2852] | 49 | float3 normal; |
---|
[2877] | 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); |
---|
[3362] | 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); |
---|
[2877] | 70 | } |
---|
[3362] | 71 | #else |
---|
| 72 | float normal_dot_light = abs(dot(normal, light_vector)); |
---|
| 73 | float normal_dot_half = abs(dot(normal, half_vector)); |
---|
[2877] | 74 | #endif |
---|
[3362] | 75 | diffuse = normal_dot_light * material.y; |
---|
[2877] | 76 | if (diffuse > 1.0e-6) |
---|
[3362] | 77 | specular = pow(normal_dot_half, material.w) * material.z; |
---|
[2877] | 78 | else |
---|
| 79 | specular = 0.0; |
---|
[2852] | 80 | } |
---|
[3362] | 81 | float ambient = material.x; |
---|
[392] | 82 | |
---|
[3362] | 83 | color.xyz = color.xyz * (ambient + diffuse) + float3(specular); |
---|
[870] | 84 | |
---|
[3362] | 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)); |
---|
[2852] | 91 | } |
---|
[1000] | 92 | } |
---|
[378] | 93 | |
---|
[3362] | 94 | OUT.Color = min(max(color, 0.0), 1.0); |
---|
| 95 | return OUT; |
---|
[378] | 96 | } |
---|