source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/treeanimation.cg @ 3168

Revision 3168, 3.2 KB checked in by mattausch, 16 years ago (diff)

removed bug (second aa output), normsl normalization now during mrt output

Line 
1/***********************************************/
2/*     Vertex shaders for tree animation       */
3/***********************************************/
4
5
6struct vtxin
7{
8        float4 position: POSITION;
9        float4 normal: NORMAL;
10       
11        float4 color: COLOR;
12        float4 texCoord: TEXCOORD0;
13};
14
15// vtx output
16struct vtxout
17{
18        float4 position: POSITION;
19        float4 texCoord: TEXCOORD0;   
20
21        float4 color: COLOR0;
22        // eye position
23        float4 eyePos: TEXCOORD1;
24        float4 normal: TEXCOORD2;
25
26        float4 worldPos: TEXCOORD3;
27        float4 oldWorldPos: TEXCOORD4;
28};
29
30
31/** Vertex shader which conducts an simple tree animation
32        that bends the tree depending quadratically on the height
33*/
34vtxout animateVtx(vtxin IN,
35                                  uniform float3 windDir,
36                                  uniform float windStrength,
37                                  uniform float frequency,
38                                  uniform float2 minMaxPos,
39                                  uniform float timer,
40                                  uniform float3 lightDir)
41{
42        vtxout OUT;
43        OUT.texCoord = IN.texCoord;
44               
45        const float pos = (minMaxPos.x - IN.position.z) / (minMaxPos.x - minMaxPos.y);
46        float factor = pos * pos * windStrength * sin(timer * frequency);
47
48        // transform the vertex position into post projection space
49        OUT.position = mul(glstate.matrix.mvp, IN.position);
50        // displace the input position
51        OUT.position += float4(factor * windDir, 0);
52
53        OUT.normal = normalize(mul(glstate.matrix.invtrans.modelview[0], IN.normal));
54        //const float3 l = normalize(mul(glstate.matrix.modelview[0], float4(lightDir, 0))).xyz;
55        const float3 l = normalize(lightDir);
56
57        const float diffuse = max(.0f, dot(OUT.normal.xyz, l));
58
59        //OUT.color.xyz = IN.color.xyz * max(0, dot(OUT.normal.xyz, normalize(lightDir)));
60        OUT.color = glstate.material.ambient + glstate.material.front.diffuse * diffuse;
61        OUT.color.w = IN.color.w;
62        return OUT;
63}
64
65/** vertex shader which provides an simple tree animation
66        that bends the tree depending quadratically on the height using vertex displacement.
67        This version of the shader is used for deferred shading and thus only
68        displaces the vertices and outputs the color, put does not do any shading.
69*/
70vtxout animateVtxMrt(vtxin IN,
71                                         uniform float3 windDir,
72                                         uniform float windStrength,
73                                         uniform float frequency,
74                                         uniform float2 minMaxPos,
75                                         uniform float timer,
76                                         uniform float oldTimer
77                                         )
78{
79        vtxout OUT;
80
81        OUT.color = IN.color;
82        OUT.texCoord = IN.texCoord;
83               
84        const float pos = (minMaxPos.x - IN.position.z) / (minMaxPos.x - minMaxPos.y);
85
86        float factor = pos * pos * windStrength * sin(timer * frequency);
87        const float4 offs = float4(factor * windDir, 0);
88
89        float oldFactor = pos * pos * windStrength * sin(oldTimer * frequency);
90        const float4 oldOffs = float4(oldFactor * windDir, 0);
91
92        // transform the vertex position into post projection space
93        OUT.position = mul(glstate.matrix.mvp, IN.position);
94        // displace the input position
95        OUT.position += offs;
96
97        // transform the vertex position into eye space
98        OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
99        OUT.eyePos += offs;
100
101        OUT.normal = normalize(mul(glstate.matrix.invtrans.modelview[0], IN.normal));
102       
103        // hack: no translational component anyway
104        OUT.oldWorldPos = oldOffs;
105        OUT.worldPos = offs;
106
107        return OUT;
108}
Note: See TracBrowser for help on using the repository browser.