Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/Shader_HS18/programs/Example/GLSL/skinningTwoWeightsVp.glsl @ 12083

Last change on this file since 12083 was 12083, checked in by wiesep, 6 years ago

Reorganised shader programs

File size: 2.3 KB
Line 
1// Example GLSL program for skinning with two bone weights per vertex
2
3attribute vec4 vertex;
4attribute vec3 normal;
5attribute vec4 uv0;
6attribute vec4 blendIndices;
7attribute vec4 blendWeights;
8
9// 3x4 matrix, passed as vec4's for compatibility with GL 2.0
10// GL 2.0 supports 3x4 matrices
11// Support 24 bones ie 24*3, but use 72 since our parser can pick that out for sizing
12uniform vec4 worldMatrix3x4Array[72];
13uniform mat4 viewProjectionMatrix;
14uniform vec4 lightPos[2];
15uniform vec4 lightDiffuseColour[2];
16uniform vec4 ambient;
17uniform vec4 diffuse;
18
19varying vec4 colour;
20varying vec4 uv;
21
22void main()
23{
24        vec3 blendPos = vec3(0.0, 0.0, 0.0);
25        vec3 blendNorm = vec3(0.0, 0.0, 0.0);
26
27        for (int bone = 0; bone < 2; ++bone)
28        {
29                // perform matrix multiplication manually since no 3x4 matrices
30        // ATI GLSL compiler can't handle indexing an array within an array so calculate the inner index first
31                int idx = int(blendIndices[bone]) * 3;
32        // ATI GLSL compiler can't handle unrolling the loop so do it manually
33        // ATI GLSL has better performance when mat4 is used rather than using individual dot product
34        // There is a bug in ATI mat4 constructor (Cat 7.2) when indexed uniform array elements are used as vec4 parameter so manually assign
35                mat4 worldMatrix;
36                worldMatrix[0] = worldMatrix3x4Array[idx];
37                worldMatrix[1] = worldMatrix3x4Array[idx + 1];
38                worldMatrix[2] = worldMatrix3x4Array[idx + 2];
39                worldMatrix[3] = vec4(0);
40                // now weight this into final
41                float weight = blendWeights[bone];
42                blendPos += (vertex * worldMatrix).xyz * weight;
43
44                mat3 worldRotMatrix = mat3(worldMatrix[0].xyz, worldMatrix[1].xyz, worldMatrix[2].xyz);
45                blendNorm += (normal * worldRotMatrix) * weight;
46        }
47
48        blendNorm = normalize(blendNorm);
49
50        // apply view / projection to position
51        gl_Position = viewProjectionMatrix * vec4(blendPos, 1.0);
52
53        // simple vertex lighting model
54        vec3 lightDir0 = normalize(
55                lightPos[0].xyz -  (blendPos * lightPos[0].w));
56        vec3 lightDir1 = normalize(
57                lightPos[1].xyz -  (blendPos * lightPos[1].w));
58
59        colour = diffuse * (ambient + (clamp(dot(lightDir0, blendNorm), 0.0, 1.0) * lightDiffuseColour[0]) +
60                (clamp(dot(lightDir1, blendNorm), 0.0, 1.0) * lightDiffuseColour[1]));
61
62        uv = uv0;
63}
Note: See TracBrowser for help on using the repository browser.