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

Last change on this file since 4880 was 3630, checked in by ldelgass, 7 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 */
8
9// constrain particle to be outside volume of a sphere
10void SphereConstraint(inout float3 x, float3 center, float r)
11{
12    float3 delta = x - center;
13    float dist = length(delta);
14    if (dist < r) {
15        x = center + delta*(r / dist);
16    }
17}
18
19// constrain particle to be above floor
20void FloorConstraint(inout float3 x, float level)
21{
22    if (x.y < level) {
23        x.y = level;
24    }
25}
26
27// constrain particle to heightfield stored in texture
28void TerrainConstraint(inout float3 pos, uniform sampler2D terrain_tex,
29                       float3 scale, float3 offset)
30{
31    float2 uv = (pos.xz - offset.xz) / scale.xz;
32    float h = tex2D(terrain_tex, uv).r*scale.y + offset.y;
33    if (pos.y < h) {
34        pos.y = h;
35    }
36}
37
38void SphereCollide(inout float3 x, inout float3 vel, float3 center, float r,
39                   float3 sphere_vel, float friction, inout float3 force)
40{
41    float3 delta = x - center;
42    float dist = length(delta);
43    if (dist < r) {
44        // x = center + delta*(r / dist);   
45        vel += (delta / dist) * friction;
46        vel += sphere_vel;
47    }
48}
49
50void FloorCollide(inout float3 x, inout float3 vel, float level,
51                  float friction, inout float3 force)
52{
53    if (x.y < level) {
54        // x.y = level;
55        // force.y += -vel.y*friction;
56        vel.y += -vel.y*friction;
57    }
58}
59
60void Gravitation(float3 pos, float3 mass_pos, inout float3 force, float epsilon)
61{
62    float3 delta = mass_pos - pos;
63    float dist = length(delta);
64    float3 dir = delta / dist;
65    force += dir * (1.0 / (epsilon + dist*dist));
66}
67
68void TerrainCollide(float3 pos, inout float3 vel, uniform sampler2D terrain_tex,
69                    float3 scale, float3 offset, float friction)
70{
71    const float2 texelSize = float2( 1.0 / 256.0, 1.0 / 256.0 );
72    float2 uv = (pos.xz - offset.xz) / scale.xz;
73    float h0 = tex2D(terrain_tex, uv).r;
74    float h = h0*scale.y + offset.y;
75    if (pos.y < h) {
76        // calculate normal (could precalc this)
77        float h1 = tex2D(terrain_tex, uv + texelSize*float2(1, 0) ).r;
78        float h2 = tex2D(terrain_tex, uv + texelSize*float2(0, 1) ).r;
79        float3 N = cross(float3(scale.x*texelSize.x, (h1-h0)*scale.y, 0),
80                         float3(0, (h2-h0)*scale.y, scale.z*texelSize.y));
81        N = normalize(N);
82        vel = reflect(vel, N);
83        vel *= friction;
84    }
85}
86
87struct a2v
88{
89    //For semantics, see pages 230-240 of Cg_Toolkit.pdf
90    float4 Position : POSITION;
91    float4 TexCoord : TEXCOORD0;
92};
93
94struct v2f
95{
96    float4 HPosition: HPOS;             //Clip space pos. Not readable in frag prog
97    float4 TexCoord : TEXCOORD0;
98    float3 EyeVector: TEXCOORD1;        //world space eye vector
99    float3 Light    : TEXCOORD2;    //world space light vector
100};
101
102struct PixelOut {
103    float4 Color:       COLOR0;
104    //float Depth:      DEPTH;
105};
Note: See TracBrowser for help on using the repository browser.