forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkinned.vert
More file actions
64 lines (54 loc) · 2.12 KB
/
Skinned.vert
File metadata and controls
64 lines (54 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
// Request GLSL 3.3
#version 330
// Uniforms for world transform and view-proj
uniform mat4 uWorldTransform;
uniform mat4 uViewProj;
// Uniform for matrix palette
uniform mat4 uMatrixPalette[96];
// Attribute 0 is position, 1 is normal,
// 2 is bone indices, 3 is weights,
// 4 is tex coords.
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in uvec4 inSkinBones;
layout(location = 3) in vec4 inSkinWeights;
layout(location = 4) in vec2 inTexCoord;
// Any vertex outputs (other than position)
out vec2 fragTexCoord;
// Normal (in world space)
out vec3 fragNormal;
// Position (in world space)
out vec3 fragWorldPos;
void main()
{
// Convert position to homogeneous coordinates
vec4 pos = vec4(inPosition, 1.0);
// Skin the position
vec4 skinnedPos = (pos * uMatrixPalette[inSkinBones.x]) * inSkinWeights.x;
skinnedPos += (pos * uMatrixPalette[inSkinBones.y]) * inSkinWeights.y;
skinnedPos += (pos * uMatrixPalette[inSkinBones.z]) * inSkinWeights.z;
skinnedPos += (pos * uMatrixPalette[inSkinBones.w]) * inSkinWeights.w;
// Transform position to world space
skinnedPos = skinnedPos * uWorldTransform;
// Save world position
fragWorldPos = skinnedPos.xyz;
// Transform to clip space
gl_Position = skinnedPos * uViewProj;
// Skin the vertex normal
vec4 skinnedNormal = vec4(inNormal, 0.0f);
skinnedNormal = (skinnedNormal * uMatrixPalette[inSkinBones.x]) * inSkinWeights.x
+ (skinnedNormal * uMatrixPalette[inSkinBones.y]) * inSkinWeights.y
+ (skinnedNormal * uMatrixPalette[inSkinBones.z]) * inSkinWeights.z
+ (skinnedNormal * uMatrixPalette[inSkinBones.w]) * inSkinWeights.w;
// Transform normal into world space (w = 0)
fragNormal = (skinnedNormal * uWorldTransform).xyz;
// Pass along the texture coordinate to frag shader
fragTexCoord = inTexCoord;
}