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

Revision 3198, 4.3 KB checked in by mattausch, 16 years ago (diff)

removed some visual bugs (trees against sky), flickering much better now

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.oldWorldPos = float4(1e20f, 1e20f, 1e20f, oldOffs.w);
106        OUT.worldPos = offs;
107
108        return OUT;
109}
110
111/*
112
113pixel fragtex(fragin IN,
114                          uniform sampler2D tex: TEXUNIT0,
115                          uniform float4x4 viewMatrix
116                          )
117{
118        float4 texColor = tex2D(tex, IN.texCoord.xy);
119
120        // account for alpha blending
121        if (texColor.w < 0.5f) discard;
122
123        pixel pix;
124
125        // save color in first render target
126        // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
127        pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor;
128        // save world space normal in rt => transform back into world space by
129        // multiplying with inverse view. since transforming normal with T means to
130        // multiply with the inverse transpose of T, we multiple with
131        // Transp(Inv(Inv(view))) = Transp(view)
132        pix.norm = normalize(mul(transpose(viewMatrix), IN.normal).xyz);
133        //pix.norm = IN.normal.xyz;
134        // compute eye linear depth
135        pix.col.w = 1e20f;//length(IN.eyePos.xyz);
136
137        // the scene entity id
138        //pix.id = glstate.fog.color.xyz;
139        // the offset to the world pos from old frame
140        pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
141
142        return pix;
143}*/
Note: See TracBrowser for help on using the repository browser.