source: nanovis/branches/1.1/shaders/one_volume.cg @ 4883

Last change on this file since 4883 was 3362, checked in by ldelgass, 11 years ago

Merge nanovis2 branch to trunk

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