WoW Model Viewer
Your premiere tool for viewing, equipping and animating World of Warcraft models.
Loading...
Searching...
No Matches
Bone.cpp
Go to the documentation of this file.
1#include "Bone.h"
2
3#include <glad/gl.h>
4#include <glm/gtc/matrix_transform.hpp>
5#include <glm/gtx/quaternion.hpp>
6
7#include "Logger.h"
8
9void Bone::calcMatrix(std::vector<Bone>& allbones, ssize_t anim, size_t time, bool rotate)
10{
11 if (calc)
12 return;
13
14 glm::mat4 m(1.0f);
15 const glm::fquat q(1.0f, 0.0f, 0.0f, 0.0f);
16
17 const bool tr = rot.uses(anim) || scale.uses(anim) || trans.uses(anim) || billboard;
18 if (tr)
19 {
20 m = glm::translate(m, pivot);
21
22 if (trans.uses(anim))
23 m = glm::translate(m, trans.getValue(anim, time));
24
25 if (rot.uses(anim) && rotate)
26 m = m * glm::toMat4(rot.getValue(anim, time));
27
28 if (scale.uses(anim))
29 m = glm::scale(m, scale.getValue(anim, time));
30
31 if (billboard)
32 {
33 float modelview[16];
34 glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
35
36 glm::vec3 vRight = glm::vec3(modelview[0], modelview[4], modelview[8]);
37 const glm::vec3 vUp = glm::vec3(modelview[1], modelview[5], modelview[9]); // Spherical billboarding
38 //glm::vec3 vUp = glm::vec3(0,1,0); // Cylindrical billboarding
39 vRight = vRight * -1.f;
40 m[0][2] = vRight.x;
41 m[1][2] = vRight.y;
42 m[2][2] = vRight.z;
43 m[0][1] = vUp.x;
44 m[1][1] = vUp.y;
45 m[2][1] = vUp.z;
46 }
47
48 m = glm::translate(m, pivot * -1.0f);
49 }
50
51 if (parent > -1)
52 {
53 allbones[parent].calcMatrix(allbones, anim, time, rotate);
54 mat = allbones[parent].mat * m;
55 }
56 else
57 {
58 mat = m;
59 }
60
61 // transform matrix for normal vectors ... ??
62 if (rot.uses(anim) && rotate)
63 {
64 if (parent >= 0)
65 mrot = allbones[parent].mrot * glm::toMat4(q);
66 else
67 mrot = glm::toMat4(q);
68 }
69 else
70 {
71 mrot = glm::mat4(1.0f);
72 }
73
74 transPivot = glm::vec3(mat * glm::vec4(pivot, 1.0f));
75
76 calc = true;
77}
78
80{
81 calc = false;
82
83 parent = b.parent;
84 pivot = b.pivot;
86
87 boneDef = b;
88
89 trans.init(b.translation, f, data);
90 rot.init(b.rotation, f, data);
91 scale.init(b.scaling, f, data);
92}
T getValue(ssize_t anim, size_t time)
Definition animated.h:178
void init(AnimationBlock &b, GameFile *f, std::vector< uint32 > &gs)
Definition animated.h:264
bool uses(ssize_t anim) const
Definition animated.h:171
void calcMatrix(std::vector< Bone > &allbones, ssize_t anim, size_t time, bool rotate=true)
Compute the bone matrix from parent bones and animation data.
Definition Bone.cpp:9
bool billboard
Whether this bone is billboarded.
Definition Bone.h:29
Animated< glm::fquat, PACK_QUATERNION, Quat16ToQuat32 > rot
Rotation animation track (packed 16-bit).
Definition Bone.h:23
glm::vec3 transPivot
Pivot point and translated pivot.
Definition Bone.h:26
int16 parent
Parent bone index (-1 if root).
Definition Bone.h:27
Animated< glm::vec3 > trans
Translation animation track.
Definition Bone.h:22
glm::mat4 mat
Computed transformation matrix.
Definition Bone.h:30
Animated< glm::vec3 > scale
Scale animation track.
Definition Bone.h:24
glm::mat4 mrot
Computed rotation-only matrix.
Definition Bone.h:31
ModelBoneDef boneDef
Raw bone definition from the M2 file.
Definition Bone.h:33
glm::vec3 pivot
Definition Bone.h:26
bool calc
Whether this bone has been calculated for the current frame.
Definition Bone.h:35
void initV3(GameFile &f, ModelBoneDef &b, const modelAnimData &data)
Initialise the bone from an M2 v3+ file.
Definition Bone.cpp:79
Abstract base class representing a file within the game data archive.
Definition GameFile.h:12
Holds per-model animation metadata: index-to-id mapping, external anim files, and global sequences.
Definition animated.h:18
@ MODELBONE_BILLBOARD
On-disk bone definition (block E) in an M2 file.
AnimationBlock translation
AnimationBlock rotation
glm::vec3 pivot
AnimationBlock scaling