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

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

tree animation works somehow with flow vector

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                                         //uniform float4x4 modelMatrix,
78                                         //uniform float4x4 oldModelMatrix
79                                         )
80{
81        vtxout OUT;
82
83        OUT.color = IN.color;
84        OUT.texCoord = IN.texCoord;
85               
86        const float pos = (minMaxPos.x - IN.position.z) / (minMaxPos.x - minMaxPos.y);
87
88        float factor = pos * pos * windStrength * sin(timer * frequency);
89        const float4 offs = float4(factor * windDir, 0);
90
91        float oldFactor = pos * pos * windStrength * sin(oldTimer * frequency);
92        const float4 oldOffs = float4(oldFactor * windDir, 0);
93
94        // transform the vertex position into post projection space
95        OUT.position = mul(glstate.matrix.mvp, IN.position);
96        // displace the input position
97        OUT.position += offs;
98
99        // transform the vertex position into eye space
100        OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
101        OUT.eyePos += offs;
102
103        OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal);
104       
105        // hack: no translational component anyway
106        OUT.oldWorldPos = oldOffs;
107        OUT.worldPos = offs;
108
109        return OUT;
110}
Note: See TracBrowser for help on using the repository browser.