source: nanovis/trunk/shaders/one_volume.cg @ 5722

Last change on this file since 5722 was 3630, checked in by ldelgass, 11 years ago

Nanovis refactoring to fix problems with scaling and multiple results.
Do rendering in world space to properly place and scale multiple data sets.
Also fix flows to reduce resets of animations. More work toward removing
Cg dependency. Fix panning to convert viewport coords to world coords.

File size: 2.9 KB
Line 
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 */
28PixelOut main(v2f IN,
29              uniform sampler3D volume,
30              uniform sampler1D tf,
31              uniform float4 material,
32              uniform float4 renderParams)
33{
34    PixelOut OUT;
35    float4 sample = tex3D(volume, IN.TexCoord.xyz);
36
37    //sample the transfer function texture
38    float4 color = tex1D(tf, sample.x);
39
40    if (renderParams.x == 0.0) {
41        //If single slice render, only flat shading, completely opaque.
42        color.w = 1;
43    } else {
44        //regular volume rendering, we do PHONG SHADING
45        //lighting parameters
46        float3 normal;
47        float diffuse, specular;
48        if (dot(sample.yzw, sample.yzw) < 5.0e-5) {
49            diffuse = 0;
50            specular = 0;
51        } else {
52            normal = normalize(sample.yzw * 2.0 - 1.0);
53            float3 light_vector = normalize(IN.Light);
54            float3 eye_vector = normalize(IN.EyeVector);
55            float3 half_vector = normalize(eye_vector+light_vector);
56
57            //lighting computation
58#ifndef LIGHT_TWO_SIDE
59            float normal_dot_light = dot(normal, light_vector);
60            float normal_dot_half = dot(normal, half_vector);
61            if (renderParams.w > 0.5) {
62                normal_dot_light = abs(normal_dot_light);
63                normal_dot_half = abs(normal_dot_half);
64            } else {
65                normal_dot_light = max(normal_dot_light, 0);
66                normal_dot_half = max(normal_dot_half, 0);
67            }
68#else
69            float normal_dot_light = abs(dot(normal, light_vector));
70            float normal_dot_half = abs(dot(normal, half_vector));
71#endif
72            diffuse = normal_dot_light * material.y;
73            if (diffuse > 1.0e-6)
74                specular = pow(normal_dot_half, material.w) * material.z;
75            else
76                specular = 0.0;
77        }
78        float ambient = material.x;
79
80        color.xyz = color.xyz * (ambient + diffuse) + float3(specular);
81
82        if (renderParams.y < 0.5) {
83            // Opacity scaling: equivalent to scaling transfer function
84            color.w = color.w * renderParams.z;
85            // Apply opacity correction for ratio of current sample distance
86            // to data set sample distance
87            color.w = (1.0 - pow((1.0 - color.w), renderParams.x));
88        }
89    }
90
91    OUT.Color = min(max(color, 0.0), 1.0);
92    return OUT;
93}
Note: See TracBrowser for help on using the repository browser.