-
Notifications
You must be signed in to change notification settings - Fork 4
/
character.h
165 lines (140 loc) · 4.18 KB
/
character.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#pragma once
#include "vec.h"
#include "quat.h"
#include "array.h"
#include <assert.h>
#include <stdio.h>
//--------------------------------------
enum Bones
{
Bone_Entity = 0,
Bone_Hips = 1,
Bone_LeftUpLeg = 2,
Bone_LeftLeg = 3,
Bone_LeftFoot = 4,
Bone_LeftToe = 5,
Bone_RightUpLeg = 6,
Bone_RightLeg = 7,
Bone_RightFoot = 8,
Bone_RightToe = 9,
Bone_Spine = 10,
Bone_Spine1 = 11,
Bone_Spine2 = 12,
Bone_Neck = 13,
Bone_Head = 14,
Bone_LeftShoulder = 15,
Bone_LeftArm = 16,
Bone_LeftForeArm = 17,
Bone_LeftHand = 18,
Bone_RightShoulder = 19,
Bone_RightArm = 20,
Bone_RightForeArm = 21,
Bone_RightHand = 22,
};
const char* BoneNames[] =
{
"Entity",
"Hips",
"LeftUpLeg",
"LeftLeg",
"LeftFoot",
"LeftToe",
"RightUpLeg",
"RightLeg",
"RightFoot",
"RightToe",
"Spine",
"Spine1",
"Spine2",
"Neck",
"Head",
"LeftShoulder",
"LeftArm",
"LeftForeArm",
"LeftHand",
"RightShoulder",
"RightArm",
"RightForeArm",
"RightHand"
};
//--------------------------------------
struct character
{
array1d<vec3> positions;
array1d<vec3> normals;
array1d<vec2> texcoords;
array1d<unsigned short> triangles;
array2d<float> bone_weights;
array2d<unsigned short> bone_indices;
array1d<vec3> bone_rest_positions;
array1d<quat> bone_rest_rotations;
};
void character_load(character& c, const char* filename)
{
FILE* f = fopen(filename, "rb");
assert(f != NULL);
array1d_read(c.positions, f);
array1d_read(c.normals, f);
array1d_read(c.texcoords, f);
array1d_read(c.triangles, f);
array2d_read(c.bone_weights, f);
array2d_read(c.bone_indices, f);
array1d_read(c.bone_rest_positions, f);
array1d_read(c.bone_rest_rotations, f);
fclose(f);
}
//--------------------------------------
void linear_blend_skinning_positions(
slice1d<vec3> anim_positions,
const slice1d<vec3> rest_positions,
const slice2d<float> bone_weights,
const slice2d<unsigned short> bone_indices,
const slice1d<vec3> bone_rest_positions,
const slice1d<quat> bone_rest_rotations,
const slice1d<vec3> bone_anim_positions,
const slice1d<quat> bone_anim_rotations)
{
anim_positions.zero();
for (int i = 0; i < anim_positions.size; i++)
{
for (int j = 0; j < bone_indices.cols; j++)
{
if (bone_weights(i, j) > 0.0f)
{
int b = bone_indices(i, j);
vec3 position = rest_positions(i);
position = quat_inv_mul_vec3(bone_rest_rotations(b), position - bone_rest_positions(b));
position = quat_mul_vec3(bone_anim_rotations(b), position) + bone_anim_positions(b);
anim_positions(i) = anim_positions(i) + bone_weights(i, j) * position;
}
}
}
}
void linear_blend_skinning_normals(
slice1d<vec3> anim_normals,
const slice1d<vec3> rest_normals,
const slice2d<float> bone_weights,
const slice2d<unsigned short> bone_indices,
const slice1d<quat> bone_rest_rotations,
const slice1d<quat> bone_anim_rotations)
{
anim_normals.zero();
for (int i = 0; i < anim_normals.size; i++)
{
for (int j = 0; j < bone_indices.cols; j++)
{
if (bone_weights(i, j) > 0.0f)
{
int b = bone_indices(i, j);
vec3 normal = rest_normals(i);
normal = quat_inv_mul_vec3(bone_rest_rotations(b), normal);
normal = quat_mul_vec3(bone_anim_rotations(b), normal);
anim_normals(i) = anim_normals(i) + bone_weights(i, j) * normal;
}
}
}
for (int i = 0; i < anim_normals.size; i++)
{
anim_normals(i) = normalize(anim_normals(i));
}
}