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