-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMD5MeshAnimator.cpp
More file actions
128 lines (106 loc) · 3.86 KB
/
Copy pathMD5MeshAnimator.cpp
File metadata and controls
128 lines (106 loc) · 3.86 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
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
/***********************************************************************
MD5MeshAnimator - Quick 'n' dirty class to render an animated MD5 model.
Copyright (c) 2010-2018 Oliver Kreylos
This file is part of the Kinect 3D Video Capture Project (Kinect).
The Kinect 3D Video Capture Project is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
The Kinect 3D Video Capture Project is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Kinect 3D Video Capture Project; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#include "MD5MeshAnimator.h"
#include <IO/OpenFile.h>
#include <Geometry/OrthogonalTransformation.h>
#include <GL/gl.h>
#include <GL/GLMaterial.h>
#include <GL/GLTransformationWrappers.h>
#include <Vrui/Vrui.h>
/********************************
Methods of class MD5MeshAnimator:
********************************/
MD5MeshAnimator::MD5MeshAnimator(void)
:textureManager(fileManager),
materialManager(textureManager),
mesh(0),
numAnims(0),anims(0)
{
/* Find all pak???.pk4 files in the base/ directory: */
fileManager.addPakFiles(IO::openDirectory("/work/okreylos/GameData/Doom3/base/"),"pak");
/* Load the mesh: */
mesh=new SceneGraph::Doom3MD5Mesh(fileManager,materialManager,"models/md5/monsters/hellknight/hellknight.md5mesh");
bodyJointID=mesh->findJoint("Body");
/* Load all requested materials: */
materialManager.loadMaterials(fileManager);
/* Load the animations: */
numAnims=1;
anims=new SceneGraph::Doom3MD5Anim*[numAnims];
anims[0]=new SceneGraph::Doom3MD5Anim(fileManager,"models/md5/monsters/hellknight/idle2.md5anim");
// anims[1]=new SceneGraph::Doom3MD5Anim(fileManager,"models/md5/monsters/hellknight/roar1.md5anim");
if(numAnims>0)
{
animStartTime=Vrui::getApplicationTime();
currentAnimIndex=0;
currentFrameIndex=0;
nextFrameTime=animStartTime+double(anims[currentAnimIndex]->getFrameTime());
}
}
MD5MeshAnimator::~MD5MeshAnimator(void)
{
/* Delete the mesh and animations: */
delete mesh;
if(anims!=0)
{
for(int i=0;i<numAnims;++i)
delete anims[i];
delete[] anims;
}
}
void MD5MeshAnimator::frame(void)
{
if(numAnims>0)
{
double animTime=Vrui::getApplicationTime()-animStartTime;
if(animTime>=nextFrameTime)
{
/* Go to the next animation frame: */
++currentFrameIndex;
if(currentFrameIndex==anims[currentAnimIndex]->getNumFrames())
{
currentFrameIndex=0;
++currentAnimIndex;
if(currentAnimIndex==numAnims)
currentAnimIndex=0;
}
nextFrameTime+=double(anims[currentAnimIndex]->getFrameTime());
/* Apply the animation to the mesh: */
anims[currentAnimIndex]->animateMesh(mesh,currentFrameIndex);
}
Vrui::requestUpdate();
}
/* Re-pose the mesh: */
mesh->updatePose();
}
void MD5MeshAnimator::glRenderAction(GLContextData& contextData) const
{
glPushAttrib(GL_ENABLE_BIT|GL_LINE_BIT|GL_POINT_BIT);
if(numAnims>0&&bodyJointID.isValid())
{
/* Move the body joint to the origin: */
glPushMatrix();
glMultMatrix(Vrui::NavTransform::translateToOriginFrom(mesh->getJointTransform(bodyJointID).getOrigin()));
}
/* Draw the mesh's surface: */
glMaterial(GLMaterialEnums::FRONT_AND_BACK,GLMaterial(GLMaterial::Color(1.0f,1.0f,1.0f),GLMaterial::Color(0.4f,0.4f,0.4f),25.0f));
glDisable(GL_COLOR_MATERIAL);
mesh->drawSurface(contextData,false);
if(numAnims>0&&bodyJointID.isValid())
glPopMatrix();
glPopAttrib();
}