Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/Shader_HS18/programs/Cg/Example_Basic.cg @ 12115

Last change on this file since 12115 was 12115, checked in by wiesep, 5 years ago

Changed folder structure, deletet some unused files and cleaned up code

File size: 11.3 KB
Line 
1/*
2  Basic ambient lighting vertex program
3*/
4void ambientOneTexture_vp(float4 position : POSITION,
5                                                  float2 uv               : TEXCOORD0,
6                                                 
7                                                  out float4 oPosition : POSITION,
8                                                  out float2 oUv           : TEXCOORD0,
9                                                  out float4 colour    : COLOR,
10
11                                                  uniform float4x4 worldViewProj,
12                                                  uniform float4 ambient)
13{
14        oPosition = mul(worldViewProj, position);
15        oUv = uv;
16        colour = ambient;
17}
18
19/*
20  Basic fragment program using texture and diffuse colour.
21*/
22void diffuseOneTexture_fp(float4 position          : POSITION,
23                                                  float2 uv                : TEXCOORD0,
24                                                  float4 diffuse           : COLOR,
25                                                  out float4 colour        : COLOR,
26                                                  uniform sampler2D texMap : register(s0))
27{
28        colour = tex2D(texMap,uv) * diffuse;
29}
30                         
31/*
32  Single-weight-per-vertex hardware skinning, 2 lights
33  The trouble with vertex programs is they're not general purpose, but
34  fixed function hardware skinning is very poorly supported
35*/
36void hardwareSkinningOneWeight_vp(
37        float4 position : POSITION,
38        float3 normal   : NORMAL,
39        float2 uv       : TEXCOORD0,
40        float  blendIdx : BLENDINDICES,
41       
42
43        out float4 oPosition : POSITION,
44        out float2 oUv       : TEXCOORD0,
45        out float4 colour           : COLOR,
46        // Support up to 24 bones of float3x4
47        // vs_1_1 only supports 96 params so more than this is not feasible
48        uniform float3x4   worldMatrix3x4Array[24],
49        uniform float4x4 viewProjectionMatrix,
50        uniform float4   lightPos[2],
51        uniform float4   lightDiffuseColour[2],
52        uniform float4   ambient)
53{
54        // transform by indexed matrix
55        float4 blendPos = float4(mul(worldMatrix3x4Array[blendIdx], position).xyz, 1.0);
56        // view / projection
57        oPosition = mul(viewProjectionMatrix, blendPos);
58        // transform normal
59        float3 norm = mul((float3x3)worldMatrix3x4Array[blendIdx], normal);
60        // Lighting - support point and directional
61        float3 lightDir0 =      normalize(
62                lightPos[0].xyz -  (blendPos.xyz * lightPos[0].w));
63        float3 lightDir1 =      normalize(
64                lightPos[1].xyz -  (blendPos.xyz * lightPos[1].w));
65
66        oUv = uv;
67        colour = ambient +
68                (saturate(dot(lightDir0, norm)) * lightDiffuseColour[0]) +
69                (saturate(dot(lightDir1, norm)) * lightDiffuseColour[1]);
70       
71}       
72
73/*
74  Single-weight-per-vertex hardware skinning, shadow-caster pass
75*/
76void hardwareSkinningOneWeightCaster_vp(
77        float4 position : POSITION,
78        float3 normal   : NORMAL,
79        float  blendIdx : BLENDINDICES,
80       
81
82        out float4 oPosition : POSITION,
83        out float4 colour    : COLOR,
84        // Support up to 24 bones of float3x4
85        // vs_1_1 only supports 96 params so more than this is not feasible
86        uniform float3x4   worldMatrix3x4Array[24],
87        uniform float4x4 viewProjectionMatrix,
88        uniform float4   ambient)
89{
90        // transform by indexed matrix
91        float4 blendPos = float4(mul(worldMatrix3x4Array[blendIdx], position).xyz, 1.0);
92        // view / projection
93        oPosition = mul(viewProjectionMatrix, blendPos);
94       
95        colour = ambient;
96       
97}       
98
99/*
100  Two-weight-per-vertex hardware skinning, 2 lights
101  The trouble with vertex programs is they're not general purpose, but
102  fixed function hardware skinning is very poorly supported
103*/
104void hardwareSkinningTwoWeights_vp(
105        float4 position : POSITION,
106        float3 normal   : NORMAL,
107        float2 uv       : TEXCOORD0,
108        float4 blendIdx : BLENDINDICES,
109        float4 blendWgt : BLENDWEIGHT,
110       
111
112        out float4 oPosition : POSITION,
113        out float2 oUv       : TEXCOORD0,
114        out float4 colour           : COLOR,
115        // Support up to 24 bones of float3x4
116        // vs_1_1 only supports 96 params so more than this is not feasible
117        uniform float3x4   worldMatrix3x4Array[24],
118        uniform float4x4 viewProjectionMatrix,
119        uniform float4   lightPos[2],
120        uniform float4   lightDiffuseColour[2],
121        uniform float4   ambient,
122        uniform float4   diffuse)
123{
124        // transform by indexed matrix
125        float4 blendPos = float4(0,0,0,0);
126        int i;
127        for (i = 0; i < 2; ++i)
128        {
129                blendPos += float4(mul(worldMatrix3x4Array[blendIdx[i]], position).xyz, 1.0) * blendWgt[i];
130        }
131        // view / projection
132        oPosition = mul(viewProjectionMatrix, blendPos);
133        // transform normal
134        float3 norm = float3(0,0,0);
135        for (i = 0; i < 2; ++i)
136        {
137                norm += mul((float3x3)worldMatrix3x4Array[blendIdx[i]], normal) *
138                blendWgt[i];
139        }
140        norm = normalize(norm);
141        // Lighting - support point and directional
142        float3 lightDir0 =      normalize(
143                lightPos[0].xyz -  (blendPos.xyz * lightPos[0].w));
144        float3 lightDir1 =      normalize(
145                lightPos[1].xyz -  (blendPos.xyz * lightPos[1].w));
146
147       
148        oUv = uv;
149        colour = diffuse * (ambient +
150                (saturate(dot(lightDir0, norm)) * lightDiffuseColour[0]) +
151                (saturate(dot(lightDir1, norm)) * lightDiffuseColour[1]));
152       
153}
154
155/*
156  Two-weight-per-vertex hardware skinning, shadow caster pass
157*/
158void hardwareSkinningTwoWeightsCaster_vp(
159        float4 position : POSITION,
160        float3 normal   : NORMAL,
161        float2 uv       : TEXCOORD0,
162        float4 blendIdx : BLENDINDICES,
163        float4 blendWgt : BLENDWEIGHT,
164       
165
166        out float4 oPosition : POSITION,
167        out float4 colour           : COLOR,
168        // Support up to 24 bones of float3x4
169        // vs_1_1 only supports 96 params so more than this is not feasible
170        uniform float3x4   worldMatrix3x4Array[24],
171        uniform float4x4 viewProjectionMatrix,
172        uniform float4   ambient)
173{
174        // transform by indexed matrix
175        float4 blendPos = float4(0,0,0,0);
176        int i;
177        for (i = 0; i < 2; ++i)
178        {
179                blendPos += float4(mul(worldMatrix3x4Array[blendIdx[i]], position).xyz, 1.0) * blendWgt[i];
180        }
181        // view / projection
182        oPosition = mul(viewProjectionMatrix, blendPos);
183       
184
185        colour = ambient;
186}
187
188
189/*
190  Four-weight-per-vertex hardware skinning, 2 lights
191  The trouble with vertex programs is they're not general purpose, but
192  fixed function hardware skinning is very poorly supported
193*/
194void hardwareSkinningFourWeights_vp(
195        float4 position : POSITION,
196        float3 normal   : NORMAL,
197        float2 uv       : TEXCOORD0,
198        float4 blendIdx : BLENDINDICES,
199        float4 blendWgt : BLENDWEIGHT,
200       
201
202        out float4 oPosition : POSITION,
203        out float2 oUv       : TEXCOORD0,
204        out float4 colour           : COLOR,
205        // Support up to 24 bones of float3x4
206        // vs_1_1 only supports 96 params so more than this is not feasible
207        uniform float3x4   worldMatrix3x4Array[24],
208        uniform float4x4 viewProjectionMatrix,
209        uniform float4   lightPos[2],
210        uniform float4   lightDiffuseColour[2],
211        uniform float4   ambient)
212{
213        // transform by indexed matrix
214        float4 blendPos = float4(0,0,0,0);
215        int i;
216        for (i = 0; i < 4; ++i)
217        {
218                blendPos += float4(mul(worldMatrix3x4Array[blendIdx[i]], position).xyz, 1.0) * blendWgt[i];
219        }
220        // view / projection
221        oPosition = mul(viewProjectionMatrix, blendPos);
222        // transform normal
223        float3 norm = float3(0,0,0);
224        for (i = 0; i < 4; ++i)
225        {
226                float3x3 d = (float3x3)worldMatrix3x4Array[blendIdx[i]];
227                norm += mul(d, normal) *
228                blendWgt[i];
229        }
230        norm = normalize(norm);
231        // Lighting - support point and directional
232        float3 lightDir0 =      normalize(
233                lightPos[0].xyz -  (blendPos.xyz * lightPos[0].w));
234        float3 lightDir1 =      normalize(
235                lightPos[1].xyz -  (blendPos.xyz * lightPos[1].w));
236
237       
238        oUv = uv;
239        colour = ambient +
240                (saturate(dot(lightDir0, norm)) * lightDiffuseColour[0]) +
241                (saturate(dot(lightDir1, norm)) * lightDiffuseColour[1]);
242       
243}
244
245// hardware morph animation (no normals)
246void hardwareMorphAnimation(float3 pos1 : POSITION,
247                          float4 normal         : NORMAL,
248                          float2 uv               : TEXCOORD0,
249                          float3 pos2     : TEXCOORD1,
250                                                 
251                          out float4 oPosition : POSITION,
252                          out float2 oUv           : TEXCOORD0,
253                          out float4 colour    : COLOR,
254
255                          uniform float4x4 worldViewProj,
256                          uniform float4 anim_t)
257{
258        // interpolate
259        float4 interp = float4(pos1 + anim_t.x*(pos2 - pos1), 1.0f);
260       
261        oPosition = mul(worldViewProj, interp);
262        oUv = uv;
263        colour = float4(1,0,0,1);
264}
265
266// hardware pose animation (no normals)
267void hardwarePoseAnimation(float3 pos : POSITION,
268                          float4 normal         : NORMAL,
269                          float2 uv               : TEXCOORD0,
270                          float3 pose1    : TEXCOORD1,
271                          float3 pose2    : TEXCOORD2,
272                                                 
273                          out float4 oPosition : POSITION,
274                          out float2 oUv           : TEXCOORD0,
275                          out float4 colour    : COLOR,
276
277                          uniform float4x4 worldViewProj,
278                          uniform float4 anim_t)
279{
280        // interpolate
281        float4 interp = float4(pos + anim_t.x*pose1 + anim_t.y*pose2, 1.0f);
282       
283        oPosition = mul(worldViewProj, interp);
284        oUv = uv;
285        colour = float4(1,0,0,1);
286}
287
288// hardware morph animation (with normals)
289void hardwareMorphAnimationWithNormals(float3 pos1 : POSITION,
290                          float3 normal1  : NORMAL,
291                          float2 uv               : TEXCOORD0,
292                          float3 pos2     : TEXCOORD1,
293                          float3 normal2  : TEXCOORD2,
294                                                 
295                          out float4 oPosition : POSITION,
296                          out float2 oUv           : TEXCOORD0,
297                          out float4 colour    : COLOR,
298
299                          uniform float4x4 worldViewProj,
300                          uniform float4 objSpaceLightPos,
301                          uniform float4 ambient,
302                          uniform float4 anim_t)
303{
304        // interpolate position
305        float4 posinterp = float4(pos1 + anim_t.x*(pos2 - pos1), 1.0f);
306
307        // nlerp normal
308        float3 ninterp = normal1 + anim_t.x*(normal2 - normal1);
309        ninterp = normalize(ninterp);
310       
311        oPosition = mul(worldViewProj, posinterp);
312        oUv = uv;
313       
314        float3 lightDir = normalize(
315                objSpaceLightPos.xyz -  (posinterp.xyz * objSpaceLightPos.w));
316
317        // Colour it red to make it easy to identify
318        float lit = saturate(dot(lightDir, ninterp));
319        colour = float4((ambient.rgb + float3(lit,lit,lit)) * float3(1,0,0), 1);
320}
321
322// hardware pose animation (with normals)
323void hardwarePoseAnimationWithNormals(float3 pos : POSITION,
324                          float3 normal    : NORMAL,
325                          float2 uv                : TEXCOORD0,
326                          float3 pose1pos  : TEXCOORD1,
327                          float3 pose1norm : TEXCOORD2,
328                          float3 pose2pos  : TEXCOORD3,
329                          float3 pose2norm : TEXCOORD4,
330                                                 
331                          out float4 oPosition : POSITION,
332                          out float2 oUv           : TEXCOORD0,
333                          out float4 colour    : COLOR,
334
335                          uniform float4x4 worldViewProj,
336                          uniform float4 objSpaceLightPos,
337                          uniform float4 ambient,
338                          uniform float4 anim_t)
339{
340        // interpolate
341        float4 posinterp = float4(pos + anim_t.x*pose1pos + anim_t.y*pose2pos, 1.0f);
342       
343        // nlerp normal
344        // First apply the pose normals (these are actual normals, not offsets)
345        float3 ninterp = anim_t.x*pose1norm + anim_t.y*pose2norm;
346
347        // Now add back any influence of the original normal
348        // This depends on what the cumulative weighting left the normal at, if it's lacking or cancelled out
349        //float remainder = 1.0 - min(anim_t.x + anim_t.y, 1.0);
350        float remainder = 1.0 - min(length(ninterp), 1.0);
351        ninterp = ninterp + (normal * remainder);
352        ninterp = normalize(ninterp);
353
354        oPosition = mul(worldViewProj, posinterp);
355        oUv = uv;
356       
357        float3 lightDir = normalize(
358                objSpaceLightPos.xyz -  (posinterp.xyz * objSpaceLightPos.w));
359
360        // Colour it red to make it easy to identify
361        float lit = saturate(dot(lightDir, ninterp));
362        colour = float4((ambient.rgb + float3(lit,lit,lit)) * float3(1,0,0), 1);
363}
364
365void basicPassthroughTangent_v(float4 position : POSITION,
366                                                float3 tangent       : TANGENT,
367                                                 
368                                                  out float4 oPosition : POSITION,
369                                                  out float3 oTangent  : TEXCOORD0,
370
371                                                  uniform float4x4 worldViewProj)
372{
373        oPosition = mul(worldViewProj, position);
374        oTangent = tangent;
375}
376void basicPassthroughNormal_v(float4 position : POSITION,
377                                                float3 normal       : NORMAL,
378                                                 
379                                                  out float4 oPosition : POSITION,
380                                                  out float3 oNormal  : TEXCOORD0,
381
382                                                  uniform float4x4 worldViewProj)
383{
384        oPosition = mul(worldViewProj, position);
385        oNormal = normal;
386}
387// Basic fragment program to display UV
388float4 showuv_p (float4 position : POSITION, float2 uv : TEXCOORD0) : COLOR
389{
390        // wrap values using frac
391        return float4(frac(uv.x), frac(uv.y), 0, 1);
392}
393// Basic fragment program to display 3d uv
394float4 showuvdir3d_p (float4 position : POSITION, float3 uv : TEXCOORD0) : COLOR
395{
396        float3 n = normalize(uv);
397        return float4(n.x, n.y, n.z, 1);
398}
399
400
Note: See TracBrowser for help on using the repository browser.