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

RevLine 
[3045]1/***********************************************/
2/*     Vertex shaders for tree animation       */
3/***********************************************/
[3038]4
5
[3040]6struct vtxin
[3038]7{
8        float4 position: POSITION;
[3045]9        float4 normal: NORMAL;
10       
[3041]11        float4 color: COLOR;
12        float4 texCoord: TEXCOORD0;
[3038]13};
14
15// vtx output
16struct vtxout
17{
[3041]18        float4 position: POSITION;
[3038]19        float4 texCoord: TEXCOORD0;   
20
[3115]21        float4 color: COLOR0;
22        // eye position
23        float4 eyePos: TEXCOORD1;
[3046]24        float4 normal: TEXCOORD2;
[3115]25
26        float4 worldPos: TEXCOORD3;
27        float4 oldWorldPos: TEXCOORD4;
[3038]28};
29
30
[3045]31/** Vertex shader which conducts an simple tree animation
32        that bends the tree depending quadratically on the height
33*/
[3040]34vtxout animateVtx(vtxin IN,
35                                  uniform float3 windDir,
36                                  uniform float windStrength,
[3041]37                                  uniform float frequency,
38                                  uniform float2 minMaxPos,
[3045]39                                  uniform float timer,
40                                  uniform float3 lightDir)
[3038]41{
42        vtxout OUT;
[3040]43        OUT.texCoord = IN.texCoord;
[3041]44               
45        const float pos = (minMaxPos.x - IN.position.z) / (minMaxPos.x - minMaxPos.y);
[3045]46        float factor = pos * pos * windStrength * sin(timer * frequency);
[3038]47
[3045]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
[3046]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);
[3045]56
[3115]57        const float diffuse = max(.0f, dot(OUT.normal.xyz, l));
[3045]58
[3046]59        //OUT.color.xyz = IN.color.xyz * max(0, dot(OUT.normal.xyz, normalize(lightDir)));
[3115]60        OUT.color = glstate.material.ambient + glstate.material.front.diffuse * diffuse;
[3046]61        OUT.color.w = IN.color.w;
[3045]62        return OUT;
63}
64
[3046]65/** vertex shader which provides an simple tree animation
66        that bends the tree depending quadratically on the height using vertex displacement.
[3045]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,
[3115]75                                         uniform float timer,
76                                         uniform float oldTimer
77                                         )
[3045]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);
[3116]85
[3044]86        float factor = pos * pos * windStrength * sin(timer * frequency);
[3115]87        const float4 offs = float4(factor * windDir, 0);
88
[3116]89        float oldFactor = pos * pos * windStrength * sin(oldTimer * frequency);
90        const float4 oldOffs = float4(oldFactor * windDir, 0);
91
[3040]92        // transform the vertex position into post projection space
93        OUT.position = mul(glstate.matrix.mvp, IN.position);
[3041]94        // displace the input position
[3115]95        OUT.position += offs;
[3038]96
[3041]97        // transform the vertex position into eye space
98        OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
[3115]99        OUT.eyePos += offs;
[3041]100
[3168]101        OUT.normal = normalize(mul(glstate.matrix.invtrans.modelview[0], IN.normal));
[3045]102       
[3115]103        // hack: no translational component anyway
[3116]104        OUT.oldWorldPos = oldOffs;
[3198]105        //OUT.oldWorldPos = float4(1e20f, 1e20f, 1e20f, oldOffs.w);
[3115]106        OUT.worldPos = offs;
107
[3038]108        return OUT;
[3198]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.