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

Revision 3045, 3.6 KB checked in by mattausch, 16 years ago (diff)
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 color1: COLOR1;
13
14        float4 texCoord: TEXCOORD0;
15};
16
17// vtx output
18struct vtxout
19{
20        float4 position: POSITION;
21        float4 texCoord: TEXCOORD0;   
22
23        float4 color: COLOR0; 
24        //float4 color1: COLOR1; 
25        float4 eyePos: TEXCOORD1; // eye position
26        float3 normal: TEXCOORD2;
27};
28
29
30/** Vertex shader which conducts an simple tree animation
31        that bends the tree depending quadratically on the height
32*/
33vtxout animateVtx(vtxin IN,
34                                  uniform float3 windDir,
35                                  uniform float windStrength,
36                                  uniform float frequency,
37                                  uniform float2 minMaxPos,
38                                  uniform float timer,
39                                  uniform float3 lightDir)
40{
41        vtxout OUT;
42
43/*
44// Transform vertex position into homogenous clip-space.
45OUT.HPosition = mul(ModelViewProj, IN.Position);
46// Transform normal from model-space to view-space.
47float3 normalVec = normalize(mul(ModelViewIT,
48IN.Normal).xyz);
49// Store normalized light vector.
50float3 lightVec = normalize(LightVec.xyz);
51// Calculate half angle vector.
52float3 eyeVec = float3(0.0, 0.0, 1.0);
53float3 halfVec = normalize(lightVec + eyeVec);
54// Calculate diffuse component.
55float diffuse = dot(normalVec, lightVec);
56// Calculate specular component.
57float specular = dot(normalVec, halfVec);
58// Use the lit function to compute lighting vector from
59// diffuse and specular values.
60float4 lighting = lit(diffuse, specular, 32);
61// Blue diffuse material
62float3 diffuseMaterial = float3(0.0, 0.0, 1.0);
63// White specular material
64float3 specularMaterial = float3(1.0, 1.0, 1.0);
65// Combine diffuse and specular contributions and
66// output final vertex color.
67OUT.Color.rgb = lighting.y * diffuseMaterial +
68lighting.z * specularMaterial;
69OUT.Color.a = 1.0;
70return OUT;
71}
72*/
73        OUT.texCoord = IN.texCoord;
74               
75        const float pos = (minMaxPos.x - IN.position.z) / (minMaxPos.x - minMaxPos.y);
76        float factor = pos * pos * windStrength * sin(timer * frequency);
77
78        // transform the vertex position into post projection space
79        OUT.position = mul(glstate.matrix.mvp, IN.position);
80        // displace the input position
81        OUT.position += float4(factor * windDir, 0);
82
83        OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal);
84
85        Out.color = IN.color * max(0, dot(OUT.normal, lightDir));
86        //OUT.color1 = IN.color1;
87
88        return OUT;
89}
90
91/** vertex shader which conducts an simple tree animation
92        that bends the tree depending quadratically on the height
93        This version of the shader is used for deferred shading and thus only
94        displaces the vertices and outputs the color, put does not do any shading.
95*/
96vtxout animateVtxMrt(vtxin IN,
97                                         uniform float3 windDir,
98                                         uniform float windStrength,
99                                         uniform float frequency,
100                                         uniform float2 minMaxPos,
101                                         uniform float timer)
102{
103        vtxout OUT;
104
105        OUT.color = IN.color;
106        //OUT.color1 = IN.color1;
107
108        OUT.texCoord = IN.texCoord;
109               
110        const float pos = (minMaxPos.x - IN.position.z) / (minMaxPos.x - minMaxPos.y);
111        float factor = pos * pos * windStrength * sin(timer * frequency);
112
113        // transform the vertex position into post projection space
114        OUT.position = mul(glstate.matrix.mvp, IN.position);
115        // displace the input position
116        OUT.position += float4(factor * windDir, 0);
117
118        // transform the vertex position into eye space
119        OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
120        OUT.eyePos += float4(factor * windDir, 0);
121
122        OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal);
123       
124        return OUT;
125}
Note: See TracBrowser for help on using the repository browser.