root/OpenSceneGraph-Data/trunk/shaders/skinning.vert @ 10696

Revision 10696, 2.7 kB (checked in by robert, 5 years ago)

From Cedric Pinson, updates to keep up to date with changes in osgAnimation such as hardware skinning

Line 
1/*  -*-c++-*-
2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
3 *
4 * This library is open source and may be redistributed and/or modified under 
5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 * (at your option) any later version.  The full license is in LICENSE file
7 * included with this distribution, and on the openscenegraph.org website.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * OpenSceneGraph Public License for more details.
13*/
14
15in vec4 boneWeight0;
16in vec4 boneWeight1;
17in vec4 boneWeight2;
18in vec4 boneWeight3;
19
20in vec4 gl_Vertex;
21in vec3 gl_Normal;
22
23uniform int nbBonesPerVertex;
24uniform mat4 matrixPalette[MAX_MATRIX];
25
26uniform mat4 gl_ModelViewMatrix;
27uniform mat4 gl_ProjectionMatrix;
28
29vec4 position;
30vec3 normal;
31
32
33// accumulate position and normal in global scope
34void computeAcummulatedNormalAndPosition(vec4 boneWeight)
35{
36    int matrixIndex;
37    float matrixWeight;
38    for (int i = 0; i < 2; i++)
39    {
40        matrixIndex =  int(boneWeight[0]);
41        matrixWeight = boneWeight[1];
42        mat4 matrix = matrixPalette[matrixIndex];
43        // correct for normal if no scale in bone
44        mat3 matrixNormal = mat3(matrix);
45        position += matrixWeight * (matrix * gl_Vertex );
46        normal += matrixWeight * (matrixNormal * gl_Normal );
47
48        boneWeight = boneWeight.zwxy;
49    }
50}
51
52void main( void )
53{
54    position =  vec4(0.0,0.0,0.0,0.0);
55                normal = vec3(0.0,0.0,0.0);
56
57    // there is 2 bone data per attributes
58    if (nbBonesPerVertex > 0)
59        computeAcummulatedNormalAndPosition(boneWeight0);
60    if (nbBonesPerVertex > 2)
61        computeAcummulatedNormalAndPosition(boneWeight1);
62    if (nbBonesPerVertex > 4)
63        computeAcummulatedNormalAndPosition(boneWeight2);
64    if (nbBonesPerVertex > 6)
65        computeAcummulatedNormalAndPosition(boneWeight3);
66
67    normal = gl_NormalMatrix * normal;
68
69    vec3 lightDir = normalize(vec3(gl_LightSource[0].position));
70    float NdotL = max(dot(normal, lightDir), 0.0);
71                vec4 diffuse = NdotL * gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
72
73                vec4 ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
74                vec4 globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;
75
76                float NdotHV = max(dot(normal, gl_LightSource[0].halfVector.xyz),0.0);
77                vec4 specular = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV,gl_FrontMaterial.shininess);
78
79                gl_FrontColor = specular + diffuse + globalAmbient + ambient;
80    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * position;
81}
Note: See TracBrowser for help on using the browser.