source: nanovis/trunk/shaders/distance.cg @ 4880

Last change on this file since 4880 was 2852, checked in by ldelgass, 8 years ago

Fix up line endings and formatting in shader code, also add emacs magic line
to get C++ mode highlighting

File size: 4.1 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2// Copyright (c) 2004-2005 Lutz Latta
3//
4// Permission is hereby granted, free of charge, to any person obtaining
5// a copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22
23// Supporting functions for adapting generic sorting algorithm to distance sorting.
24//
25// Note: Packing of two 16-bit halfs into one float is used.
26// This works only perfect with texture sizes up to 1024.
27// With size 2048 the higher coordinates shift from xxxx.5 to integer values,
28// then the algorithm should be changed to store only integers and always add 0.5.
29#define USE_RGB_FORMAT
30#ifndef USE_RGB_FORMAT
31float2 initSortIndex(float2 sortIndex : TEXCOORD0) : COLOR
32{
33    // TBD...
34    return float2(0, pack_2half(sortIndex));
35    //return float2(sortIndex.x, sortIndex.y);
36}
37
38float2 computeDistance(float2 texCoord : TEXCOORD0,
39                       uniform samplerRECT sortTexture : register(s0),
40                       uniform samplerRECT positionTexture : register(s1),
41                       uniform float3 viewerPosition) : COLOR
42{
43    float sortIndex = texRECT(sortTexture, texCoord.xy).y;
44    half2 particleIndex = unpack_2half(sortIndex);
45
46    float3 particlePos = (float3)texRECT(positionTexture, particleIndex);
47       
48    float3 delta = viewerPosition - particlePos;
49    float distanceSqr = dot(delta, delta);
50
51    // Prevent unused, far-away particles from destroying comparisons in sorting.
52    if (distanceSqr > 1e6 || isnan(distanceSqr))
53        distanceSqr = 1e6;
54
55    return float2(distanceSqr, sortIndex);
56}
57float4 lookupPosition(float2 texCoord : TEXCOORD0,
58                      uniform samplerRECT sortTexture : register(s0),
59                      uniform samplerRECT positionTexture : register(s1)) : COLOR
60{
61    float sortIndex = texRECT(sortTexture, texCoord.xy).y;
62    half2 particleIndex = unpack_2half(sortIndex);
63    float4 particlePos = (float4)texRECT(positionTexture, particleIndex);
64
65    return particlePos;
66}
67
68#else
69float3 initSortIndex(float2 sortIndex : TEXCOORD0) : COLOR
70{
71    //return float3(0, sortIndex.x, sortIndex.y);
72    return float3(1e6, sortIndex.x, sortIndex.y);
73}
74
75float3 computeDistance(float2 texCoord : TEXCOORD0,
76                       uniform samplerRECT sortTexture : register(s0),
77                       uniform samplerRECT positionTexture : register(s1),
78                       uniform float3 viewerPosition) : COLOR
79{
80    float2 particleIndex = texRECT(sortTexture, texCoord.xy).yz;
81    float3 particlePos = texRECT(positionTexture, particleIndex).xyz;
82
83    float3 delta = viewerPosition - particlePos;
84    float distanceSqr = dot(delta, delta);
85
86    // Prevent unused, far-away particles from destroying comparisons in sorting.
87    if (distanceSqr > 1e6 || isnan(distanceSqr))
88        distanceSqr = 1e6;
89
90    return float3(distanceSqr, particleIndex.x, particleIndex.y);
91}
92
93float3 lookupPosition(float2 texCoord : TEXCOORD0,
94                      uniform samplerRECT sortTexture : register(s0),
95                      uniform samplerRECT positionTexture : register(s1)) : COLOR
96{
97    float2 particleIndex = texRECT(sortTexture, texCoord.xy).yz;
98    float3 particlePos = texRECT(positionTexture, particleIndex).xyz;
99
100    return particlePos;
101}
102#endif
Note: See TracBrowser for help on using the repository browser.