| 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 | |
|---|
| 15 | in vec4 boneWeight0; |
|---|
| 16 | in vec4 boneWeight1; |
|---|
| 17 | in vec4 boneWeight2; |
|---|
| 18 | in vec4 boneWeight3; |
|---|
| 19 | |
|---|
| 20 | uniform int nbBonesPerVertex; |
|---|
| 21 | uniform mat4 matrixPalette[MAX_MATRIX]; |
|---|
| 22 | |
|---|
| 23 | vec4 position; |
|---|
| 24 | vec3 normal; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | // accumulate position and normal in global scope |
|---|
| 28 | void computeAcummulatedNormalAndPosition(vec4 boneWeight) |
|---|
| 29 | { |
|---|
| 30 | int matrixIndex; |
|---|
| 31 | float matrixWeight; |
|---|
| 32 | for (int i = 0; i < 2; i++) |
|---|
| 33 | { |
|---|
| 34 | matrixIndex = int(boneWeight[0]); |
|---|
| 35 | matrixWeight = boneWeight[1]; |
|---|
| 36 | mat4 matrix = matrixPalette[matrixIndex]; |
|---|
| 37 | // correct for normal if no scale in bone |
|---|
| 38 | mat3 matrixNormal = mat3(matrix); |
|---|
| 39 | position += matrixWeight * (matrix * gl_Vertex ); |
|---|
| 40 | normal += matrixWeight * (matrixNormal * gl_Normal ); |
|---|
| 41 | |
|---|
| 42 | boneWeight = boneWeight.zwxy; |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | void main( void ) |
|---|
| 47 | { |
|---|
| 48 | position = vec4(0.0,0.0,0.0,0.0); |
|---|
| 49 | normal = vec3(0.0,0.0,0.0); |
|---|
| 50 | |
|---|
| 51 | // there is 2 bone data per attributes |
|---|
| 52 | if (nbBonesPerVertex > 0) |
|---|
| 53 | computeAcummulatedNormalAndPosition(boneWeight0); |
|---|
| 54 | if (nbBonesPerVertex > 2) |
|---|
| 55 | computeAcummulatedNormalAndPosition(boneWeight1); |
|---|
| 56 | if (nbBonesPerVertex > 4) |
|---|
| 57 | computeAcummulatedNormalAndPosition(boneWeight2); |
|---|
| 58 | if (nbBonesPerVertex > 6) |
|---|
| 59 | computeAcummulatedNormalAndPosition(boneWeight3); |
|---|
| 60 | |
|---|
| 61 | normal = gl_NormalMatrix * normal; |
|---|
| 62 | |
|---|
| 63 | vec3 lightDir = normalize(vec3(gl_LightSource[0].position)); |
|---|
| 64 | float NdotL = max(dot(normal, lightDir), 0.0); |
|---|
| 65 | vec4 diffuse = NdotL * gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse; |
|---|
| 66 | |
|---|
| 67 | vec4 ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient; |
|---|
| 68 | vec4 globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient; |
|---|
| 69 | |
|---|
| 70 | float NdotHV = max(dot(normal, gl_LightSource[0].halfVector.xyz),0.0); |
|---|
| 71 | vec4 specular = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV,gl_FrontMaterial.shininess); |
|---|
| 72 | |
|---|
| 73 | gl_FrontColor = specular + diffuse + globalAmbient + ambient; |
|---|
| 74 | gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * position; |
|---|
| 75 | } |
|---|