-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMD5Model.hpp
1262 lines (1096 loc) · 46.6 KB
/
MD5Model.hpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file MD5Model.hpp
* @brief Doom3 MD5 Model + Animation implementations
* @author Dr. Jeffrey Paone
*
* @copyright MIT License Copyright (c) 2023 Dr. Jeffrey Paone
*
* These functions, classes, and constants help minimize common
* code that needs to be written.
*/
#ifndef CSCI441_MD5_MODEL_HPP
#define CSCI441_MD5_MODEL_HPP
/*
* md5mesh model loader + animation
* last modification: Dr. Jeffrey Paone
* encapsulated into a class
* supports texturing
*
* Doom3's md5mesh viewer with animation. Mesh and Animation declaration
* See http://tfc.duke.free.fr/coding/md5-specs-en.html for more details
*
* Copyright (c) 2005-2007 David HENRY
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "TextureUtils.hpp"
#ifdef CSCI441_USE_GLEW
#include <GL/glew.h>
#else
#include <glad/gl.h>
#endif
#include <glm/exponential.hpp>
#include <glm/ext/quaternion_common.hpp>
#include <glm/ext/quaternion_float.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
namespace CSCI441 {
/**
* @class MD5Model
* @brief stores a Doom3 MD5 Mesh + Animation
*/
class [[maybe_unused]] MD5Model {
protected:
// md5mesh types
/**
* @brief a joint of the MD5 Skeleton
*/
struct MD5Joint {
/**
* @brief a non-existent joint. used to identify joints at the root of a skeleton
*/
static const GLint NULL_JOINT = -1;
/**
* @brief joint identifier
*/
char name[256] = "";
/**
* @brief index of the parent joint on skeletal tree
*/
GLint parent = NULL_JOINT;
/**
* @brief position of the joint in object space
*/
glm::vec3 position = {0.0f, 0.0f, 0.0f};
/**
* @brief joint orientation expressed as a quaternion in object space
*/
glm::quat orientation = {0.0f, 0.0f, 0.0f, 0.0f};
};
/**
* @brief a vertex on the mesh
*/
struct MD5Vertex {
/**
* @brief texture coordinate for vertex
*/
glm::vec2 texCoord = {0.0f, 0.0f};
/**
* @brief index of starting weight
*/
GLint start = 0;
/**
* @brief number of weights that determine vertex's position
*/
GLint count = 0;
};
/**
* @brief a triangle on the mesh
*/
struct MD5Triangle {
/**
* @brief vertex indices that make up triangle
*/
GLint index[3] = {0};
};
/**
* @brief the weight for a mesh vertex
*/
struct MD5Weight {
/**
* @brief index of joint the weight depends on
*/
GLint joint = MD5Joint::NULL_JOINT;
/**
* @brief contribution of the weight
*/
GLfloat bias = 0.f;
/**
* @brief weight's position in object space
*/
glm::vec3 position = {0.0f, 0.0f, 0.0f};
};
/**
* @brief texture handle for the model
*/
struct MD5Texture {
/**
* @brief handle of texture stored on the GPU
*/
GLuint texHandle = 0;
/**
* @brief filename texture was loaded from
*/
char filename[512] = "";
};
/**
* @brief mesh that comprises the model's skin
*/
struct MD5Mesh {
/**
* @brief array of vertices comprising the mesh
*/
MD5Vertex *vertices = nullptr;
/**
* @brief array triangles comprising the mesh
*/
MD5Triangle *triangles = nullptr;
/**
* @brief array of weights to determine vertex position based on joint positions
*/
MD5Weight *weights = nullptr;
/**
* @brief texture map array
*/
MD5Texture textures[4];
/**
* @brief named entities for different texture maps applied to the model
*/
enum TextureMap {
/**
* @brief diffuse map
*/
DIFFUSE,
/**
* @brief specular map
*/
SPECULAR,
/**
* @brief normal map
*/
NORMAL,
/**
* @brief height map
*/
HEIGHT
};
/**
* @brief number of vertices in the mesh vertex array
*/
GLint numVertices = 0;
/**
* @brief number of triangles in the mesh triangle array
*/
GLint numTriangles = 0;
/**
* @brief number of weights in the mesh weight array
*/
GLint numWeights = 0;
/**
* @brief base filename for all textures applied to mesh
*/
char shader[512] = "";
};
// md5anim types
/**
* @brief information pertaining to each animation joint
*/
struct MD5JointInfo {
/**
* @brief joint identifier
*/
char name[256] = "";
/**
* @brief index of parent joint on skeletal tree
*/
GLint parent = MD5Joint::NULL_JOINT;
/**
* @brief bit flags denoted how to compute the skeleton of a frame for this joint
*/
GLuint flags = 0;
/**
* @brief index of starting parameter
*/
GLint startIndex = 0;
};
/**
* @brief base frame joint
*/
struct MD5BaseFrameJoint {
/**
* @brief position of the joint in object space
*/
glm::vec3 position = {0.0f, 0.0f, 0.0f};
/**
* @brief joint orientation expressed as a quaternion in object space
*/
glm::quat orientation = {0.0f, 0.0f, 0.0f, 0.0f};
};
/**
* @brief bounding box containing the model during animation
* @note can be useful for computing AABB or OBB for frustum culling and basic collision detection
*/
struct MD5BoundingBox {
/**
* @brief minimum dimension bound
*/
glm::vec3 min = {0.0f, 0.0f, 0.0f};
/**
* @brief maximum dimension bound
*/
glm::vec3 max = {0.0f, 0.0f, 0.0f};
};
/**
* @brief stores an entire animation sequence for a given MD5 Model
*/
struct MD5Animation {
/**
* @brief number of frames in the animation
*/
GLint numFrames = 0;
/**
* @brief number of joints of the frame skeletons
* @note must be the same as the number of joints on the model the animation is applied to
*/
GLint numJoints = 0;
/**
* @brief number of frames per second to draw for the animation
* @note duration of a frame can be computed by inverting the frame rate
*/
GLint frameRate = 0;
/**
* @brief skeletal pose for each frame
*/
MD5Joint **skeletonFrames = nullptr;
/**
* @brief bounding box for each frame
*/
MD5BoundingBox *boundingBoxes = nullptr;
};
/**
* @brief stores state of current animation frame
*/
struct MD5AnimationState {
/**
* @brief index of current frame model is in
*/
GLint currFrame = 0;
/**
* @brief index of next frame model will move to
*/
GLint nextFrame = 0;
/**
* @brief time of last frame interpolation
*/
GLfloat lastTime = 0.0f;
/**
* @brief duration of a single frame
* @note equivalent to inverse of frame rate
*/
GLfloat maxTime = 0.0f;
};
public:
/**
* @brief initializes an empty MD5 Model
* @note need to call loadMD5Model() or readMD5Model() after construction to actually load in a mesh file
*/
MD5Model();
/**
* @brief deallocates any used memory on the CPU and GPU
*/
~MD5Model();
/**
* @brief do not allow MD5 models to be copied
*/
MD5Model(const MD5Model&) = delete;
/**
* @brief do not allow MD5 models to be copied
*/
MD5Model& operator=(const MD5Model&) = delete;
/**
* @brief loads a corresponding md5mesh and md5anim file to the object
* @param MD5_MESH_FILE name of file to load mesh from
* @param MD5_ANIM_FILE name of file to load animation from
* @returns true if both mesh and animation loaded successfully and are compatible
*/
[[maybe_unused]] bool loadMD5Model(const char* MD5_MESH_FILE, const char* MD5_ANIM_FILE = "");
/**
* @brief returns if the MD5 Model has an accompanying animation
* @returns true if animation exists for model
*/
[[nodiscard]] bool isAnimated() const { return _isAnimated; }
// md5mesh prototypes
/**
* @brief parses md5mesh file and allocates corresponding mesh data
* @param FILENAME *.md5mesh file to load
* @returns true if file parsed successfully
*/
[[nodiscard]] bool readMD5Model(const char* FILENAME);
/**
* @brief binds model VBOs to attribute pointer locations
* @param vPosAttribLoc location of vertex position attribute
* @param vColorAttribLoc location of vertex color attribute
* @param vTexCoordAttribLoc location of vertex texture coordinate attribute
* @note color attribute used when drawing the skeleton
* @note texCoord attribute used when drawing the mesh
*/
[[maybe_unused]]void allocVertexArrays(GLuint vPosAttribLoc, GLuint vColorAttribLoc, GLuint vTexCoordAttribLoc);
/**
* @brief draws all the meshes that make up the model
*/
[[maybe_unused]] void draw() const;
/**
* @brief draws the skeleton joints (as points) and bones (as lines)
*/
[[maybe_unused]] void drawSkeleton() const;
// md5anim prototypes
/**
* @brief reads in an animation sequence from an external file
* @param filename *.md5anim file to open
* @returns true iff mesh and animation are compatible
*/
[[nodiscard]] bool readMD5Anim(const char* filename);
/**
* @brief advances the model forward in its animation sequence the corresponding amount of time based on frame rate
* @param dt delta time since last frame
*/
void animate(GLfloat dt);
private:
MD5Joint* _baseSkeleton;
MD5Mesh* _meshes;
GLint _numJoints;
GLint _numMeshes;
// vertex array related stuff
GLint _maxVertices;
GLint _maxTriangles;
glm::vec3* _vertexArray;
glm::vec2* _texelArray;
GLuint* _vertexIndicesArray;
GLuint _vao;
GLuint _vbo[2];
GLuint _skeletonVAO;
GLuint _skeletonVBO;
/**
* @brief the MD5 skeletal joint data
*/
MD5Joint* _skeleton;
/**
* @brief the MD5 animation frame sequence
*/
MD5Animation _animation;
/**
* @brief flag stating if the loaded MD5 model has a corresponding animation or not
*/
bool _isAnimated;
/**
* @brief current animation frame state
*/
MD5AnimationState _animationInfo;
void _prepareMesh(const MD5Mesh* pMESH) const;
void _drawMesh(const MD5Mesh* pMESH) const;
[[nodiscard]] bool _checkAnimValidity() const;
static void _buildFrameSkeleton(const MD5JointInfo* pJOINT_INFOS,
const MD5BaseFrameJoint* pBASE_FRAME,
const GLfloat* pANIM_FRAME_DATA,
MD5Joint* pSkeletonFrame,
GLint NUM_JOINTS);
void _interpolateSkeletons(GLfloat interp);
void _freeModel();
void _freeVertexArrays();
void _freeAnim();
};
}
//----------------------------------------------------------------------------------------------------
inline CSCI441::MD5Model::MD5Model()
{
_baseSkeleton = nullptr;
_meshes = nullptr;
_numJoints = 0;
_numMeshes = 0;
_maxVertices = 0;
_maxTriangles = 0;
_vertexArray = nullptr;
_texelArray = nullptr;
_vertexIndicesArray = nullptr;
_vao = 0;
_vbo[0] = 0;
_vbo[1] = 0;
_skeletonVAO = 0;
_skeletonVBO = 0;
_animation = MD5Animation();
_skeleton = nullptr;
_animationInfo = MD5AnimationState();
_isAnimated = false;
}
inline CSCI441::MD5Model::~MD5Model()
{
_freeVertexArrays();
_freeAnim();
_freeModel();
}
// load our MD5 model
[[maybe_unused]]
inline bool
CSCI441::MD5Model::loadMD5Model(
const char* MD5_MESH_FILE,
const char* MD5_ANIM_FILE
) {
// Load MD5 _model file
if( readMD5Model(MD5_MESH_FILE) ) {
// if MD5 animation file name provided
if(strcmp(MD5_ANIM_FILE, "") != 0 ) {
// Load MD5 animation file
if( !readMD5Anim(MD5_ANIM_FILE) ) {
return false;
}
}
if( !isAnimated() ) {
printf ("[.MD5_ANIM_FILE]: no animation loaded.\n");
}
} else {
return false;
}
return true;
}
// Load an MD5 model from file.
inline bool
CSCI441::MD5Model::readMD5Model(
const char* FILENAME
) {
FILE *fp;
char buff[512];
GLint version;
GLint currentMesh = 0;
GLint i;
unsigned long uli;
GLint totalVertices = 0;
GLint totalWeights = 0;
GLint totalTriangles = 0;
GLfloat minX = 999999, minY = 999999, minZ = 999999;
GLfloat maxX = -999999, maxY = -999999, maxZ = -999999;
printf("[.md5mesh]: about to read %s\n", FILENAME );
fp = fopen(FILENAME, "rb" );
if( !fp ) {
fprintf (stderr, "[.md5mesh]: Error: couldn't open \"%s\"!\n", FILENAME);
return false;
}
while( !feof(fp) ) {
// Read whole line
fgets( buff, sizeof(buff), fp );
if( sscanf(buff, " MD5Version %d", &version) == 1 ) {
if( version != 10 ) {
// Bad version
fprintf (stderr, "[.md5mesh]: Error: bad model version\n");
fclose (fp);
return false;
}
} else if( sscanf(buff, " numJoints %d", &_numJoints) == 1 ) {
if( _numJoints > 0 ) {
// Allocate memory for base skeleton joints
_baseSkeleton = new MD5Joint[_numJoints];
}
} else if( sscanf(buff, " numMeshes %d", &_numMeshes) == 1 ) {
if( _numMeshes > 0 ) {
// Allocate memory for meshes
_meshes = new MD5Mesh[_numMeshes];
}
} else if( strncmp(buff, "joints {", 8) == 0 ) {
// Read each joint
for(i = 0; i < _numJoints; ++i) {
MD5Joint *joint = &_baseSkeleton[i];
// Read whole line
fgets( buff, sizeof(buff), fp );
if( sscanf(buff, "%s %d ( %f %f %f ) ( %f %f %f )",
joint->name, &joint->parent,
&joint->position[0], &joint->position[1], &joint->position[2],
&joint->orientation[0],&joint->orientation[1], &joint->orientation[2]) == 8
) {
// Compute the w component
joint->orientation.w = glm::extractRealComponent(joint->orientation);
}
}
} else if( strncmp(buff, "mesh {", 6) == 0 ) {
MD5Mesh *mesh = &_meshes[currentMesh];
GLint vert_index = 0;
GLint tri_index = 0;
GLint weight_index = 0;
GLfloat fdata[4];
GLint idata[3];
while( buff[0] != '}' && !feof(fp) ) {
// Read whole line
fgets( buff, sizeof(buff), fp );
if( strstr( buff, "shader ") ) {
GLint quote = 0, j = 0;
// Copy the shader name without the quote marks
for(uli = 0; uli < sizeof(buff) && (quote < 2); ++uli) {
if( buff[uli] == '\"' )
quote++;
if( (quote == 1) && (buff[uli] != '\"') ) {
mesh->shader[j] = buff[uli];
j++;
}
}
// there was a shader name
if( j > 0 ) {
// diffuse map
strcpy(mesh->textures[MD5Mesh::TextureMap::DIFFUSE].filename, mesh->shader);
strcat(mesh->textures[MD5Mesh::TextureMap::DIFFUSE].filename, ".tga");
mesh->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle = CSCI441::TextureUtils::loadAndRegisterTexture( mesh->textures[MD5Mesh::TextureMap::DIFFUSE].filename, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, GL_FALSE, GL_FALSE);
if( mesh->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle == 0 ) {
strcpy(mesh->textures[MD5Mesh::TextureMap::DIFFUSE].filename, mesh->shader);
strcat(mesh->textures[MD5Mesh::TextureMap::DIFFUSE].filename, "_d.tga");
mesh->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle = CSCI441::TextureUtils::loadAndRegisterTexture( mesh->textures[MD5Mesh::TextureMap::DIFFUSE].filename, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, GL_FALSE, GL_FALSE );
if( mesh->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle == 0 ) {
strcpy(mesh->textures[MD5Mesh::TextureMap::DIFFUSE].filename, mesh->shader);
strcat(mesh->textures[MD5Mesh::TextureMap::DIFFUSE].filename, ".png");
mesh->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle = CSCI441::TextureUtils::loadAndRegisterTexture(mesh->textures[MD5Mesh::TextureMap::DIFFUSE].filename, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, GL_FALSE, GL_FALSE );
if( mesh->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle == 0 ) {
printf("[.md5mesh | ERROR]: Could not load diffuse map %s\n", mesh->shader);
}
}
}
// specular map
strcpy(mesh->textures[MD5Mesh::TextureMap::SPECULAR].filename, mesh->shader);
strcat(mesh->textures[MD5Mesh::TextureMap::SPECULAR].filename, "_s.tga");
mesh->textures[MD5Mesh::TextureMap::SPECULAR].texHandle = CSCI441::TextureUtils::loadAndRegisterTexture( mesh->textures[MD5Mesh::TextureMap::SPECULAR].filename, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, GL_FALSE, GL_FALSE );
if( mesh->textures[MD5Mesh::TextureMap::SPECULAR].texHandle == 0 ) {
strcpy(mesh->textures[MD5Mesh::TextureMap::SPECULAR].filename, mesh->shader);
strcat(mesh->textures[MD5Mesh::TextureMap::SPECULAR].filename, "_s.png");
mesh->textures[MD5Mesh::TextureMap::SPECULAR].texHandle = CSCI441::TextureUtils::loadAndRegisterTexture( mesh->textures[MD5Mesh::TextureMap::SPECULAR].filename, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, GL_FALSE, GL_FALSE );
if( mesh->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle == 0 ) {
printf("[.md5mesh | ERROR]: Could not load specular map %s\n", mesh->shader);
}
}
// normal map
strcpy(mesh->textures[MD5Mesh::TextureMap::NORMAL].filename, mesh->shader);
strcat(mesh->textures[MD5Mesh::TextureMap::NORMAL].filename, "_local.tga");
mesh->textures[MD5Mesh::TextureMap::NORMAL].texHandle = CSCI441::TextureUtils::loadAndRegisterTexture( mesh->textures[MD5Mesh::TextureMap::NORMAL].filename, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, GL_FALSE, GL_FALSE );
if( mesh->textures[MD5Mesh::TextureMap::NORMAL].texHandle == 0 ) {
strcpy(mesh->textures[MD5Mesh::TextureMap::NORMAL].filename, mesh->shader);
strcat(mesh->textures[MD5Mesh::TextureMap::NORMAL].filename, "_local.png");
mesh->textures[MD5Mesh::TextureMap::NORMAL].texHandle = CSCI441::TextureUtils::loadAndRegisterTexture( mesh->textures[MD5Mesh::TextureMap::NORMAL].filename, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, GL_FALSE, GL_FALSE );
if( mesh->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle == 0 ) {
printf("[.md5mesh | ERROR]: Could not load normal map %s\n", mesh->shader);
}
}
// height map
strcpy(mesh->textures[MD5Mesh::TextureMap::HEIGHT].filename, mesh->shader);
strcat(mesh->textures[MD5Mesh::TextureMap::HEIGHT].filename, "_h.tga");
mesh->textures[MD5Mesh::TextureMap::HEIGHT].texHandle = CSCI441::TextureUtils::loadAndRegisterTexture( mesh->textures[MD5Mesh::TextureMap::HEIGHT].filename, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, GL_FALSE, GL_FALSE );
if( mesh->textures[MD5Mesh::TextureMap::HEIGHT].texHandle == 0 ) {
strcpy(mesh->textures[MD5Mesh::TextureMap::HEIGHT].filename, mesh->shader);
strcat(mesh->textures[MD5Mesh::TextureMap::HEIGHT].filename, "_h.png");
mesh->textures[MD5Mesh::TextureMap::HEIGHT].texHandle = CSCI441::TextureUtils::loadAndRegisterTexture( mesh->textures[MD5Mesh::TextureMap::HEIGHT].filename, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, GL_FALSE, GL_FALSE );
if( mesh->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle == 0 ) {
printf("[.md5mesh | ERROR]: Could not load height map %s\n", mesh->shader);
}
}
}
} else if( sscanf(buff, " numverts %d", &mesh->numVertices) == 1 ) {
if( mesh->numVertices > 0 ) {
// Allocate memory for vertices
mesh->vertices = new MD5Vertex[mesh->numVertices];
}
if( mesh->numVertices > _maxVertices )
_maxVertices = mesh->numVertices;
totalVertices += mesh->numVertices;
} else if( sscanf(buff, " numtris %d", &mesh->numTriangles) == 1 ) {
if( mesh->numTriangles > 0 ) {
// Allocate memory for triangles
mesh->triangles = new MD5Triangle[mesh->numTriangles];
}
if( mesh->numTriangles > _maxTriangles )
_maxTriangles = mesh->numTriangles;
totalTriangles += mesh->numTriangles;
} else if( sscanf(buff, " numweights %d", &mesh->numWeights) == 1 ) {
if( mesh->numWeights > 0 ) {
// Allocate memory for vertex weights
mesh->weights = new MD5Weight[mesh->numWeights];
}
totalWeights += mesh->numWeights;
} else if( sscanf(buff, " vert %d ( %f %f ) %d %d",
&vert_index,
&fdata[0], &fdata[1],
&idata[0], &idata[1]) == 5
) {
// Copy vertex data
mesh->vertices[vert_index].texCoord.s = fdata[0];
mesh->vertices[vert_index].texCoord.t = fdata[1];
mesh->vertices[vert_index].start = idata[0];
mesh->vertices[vert_index].count = idata[1];
} else if( sscanf(buff, " tri %d %d %d %d",
&tri_index,
&idata[0], &idata[1], &idata[2]) == 4
) {
// Copy triangle data
mesh->triangles[tri_index ].index[0] = idata[0];
mesh->triangles[tri_index ].index[1] = idata[1];
mesh->triangles[tri_index ].index[2] = idata[2];
} else if( sscanf(buff, " weight %d %d %f ( %f %f %f )",
&weight_index, &idata[0], &fdata[3],
&fdata[0], &fdata[1], &fdata[2]) == 6
) {
// Copy vertex data
mesh->weights[weight_index].joint = idata[0];
mesh->weights[weight_index].bias = fdata[3];
mesh->weights[weight_index].position[0] = fdata[0];
mesh->weights[weight_index].position[1] = fdata[1];
mesh->weights[weight_index].position[2] = fdata[2];
if( fdata[0] < minX ) { minX = fdata[0]; }
if( fdata[0] > maxX ) { maxX = fdata[0]; }
if( fdata[1] < minY ) { minY = fdata[1]; }
if( fdata[1] > maxY ) { maxY = fdata[1]; }
if( fdata[2] < minZ ) { minZ = fdata[2]; }
if( fdata[2] > maxZ ) { maxZ = fdata[2]; }
}
}
currentMesh++;
}
}
fclose(fp);
_skeleton = _baseSkeleton;
printf("[.md5mesh]: finished reading %s\n", FILENAME );
printf("[.md5mesh]: read in %d meshes, %d joints, %d vertices, %d weights, and %d triangles\n", _numMeshes, _numJoints, totalVertices, totalWeights, totalTriangles );
printf( "[.md5mesh]: base pose %f units across in X, %f units across in Y, %f units across in Z\n", (maxX - minX), (maxY-minY), (maxZ - minZ) );
printf( "\n" );
return true;
}
//Free resources allocated for the model.
inline void
CSCI441::MD5Model::_freeModel()
{
delete _baseSkeleton;
_baseSkeleton = nullptr;
// Free mesh data
for(GLint i = 0; i < _numMeshes; ++i) {
delete _meshes[i].vertices;
_meshes[i].vertices = nullptr;
delete _meshes[i].triangles;
_meshes[i].triangles = nullptr;
delete _meshes[i].weights;
_meshes[i].weights = nullptr;
}
delete _meshes;
_meshes = nullptr;
}
[[maybe_unused]]
inline void
CSCI441::MD5Model::draw() const
{
// Draw each mesh of the model
for(GLint i = 0; i < _numMeshes; ++i) {
MD5Mesh mesh = _meshes[i]; // get the mesh
_prepareMesh(&mesh); // do some preprocessing on it
_drawMesh(&mesh);
}
}
// Prepare a mesh for drawing. Compute mesh's final vertex positions
// given a skeleton. Put the vertices in vertex arrays.
inline void
CSCI441::MD5Model::_prepareMesh(
const MD5Mesh *pMESH
) const {
GLint i, j, k;
// Setup vertex indices
for(k = 0, i = 0; i < pMESH->numTriangles; ++i) {
for(j = 0; j < 3; ++j, ++k)
_vertexIndicesArray[k] = pMESH->triangles[i].index[j];
}
// Setup vertices
for(i = 0; i < pMESH->numVertices; ++i) {
glm::vec3 finalVertex = {0.0f, 0.0f, 0.0f };
// Calculate final vertex to draw with weights
for(j = 0; j < pMESH->vertices[i].count; ++j) {
const MD5Weight *weight = &pMESH->weights[pMESH->vertices[i].start + j];
const MD5Joint *joint = &_skeleton[weight->joint];
// Calculate transformed vertex for this weight
glm::vec3 weightedVertex;
weightedVertex = glm::rotate(joint->orientation, glm::vec4(weight->position, 0.0f));
// The sum of all weight->bias should be 1.0
finalVertex.x += (joint->position.x + weightedVertex.x) * weight->bias;
finalVertex.y += (joint->position.y + weightedVertex.y) * weight->bias;
finalVertex.z += (joint->position.z + weightedVertex.z) * weight->bias;
}
_vertexArray[i].x = finalVertex.x;
_vertexArray[i].y = finalVertex.y;
_vertexArray[i].z = finalVertex.z;
_texelArray[i].s = pMESH->vertices[i].texCoord.s;
_texelArray[i].t = pMESH->vertices[i].texCoord.t;
}
glBindVertexArray(_vao );
glBindBuffer(GL_ARRAY_BUFFER, _vbo[0] );
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::vec3) * pMESH->numVertices, &_vertexArray[0] );
glBufferSubData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * _maxVertices, sizeof(glm::vec2) * pMESH->numVertices, &_texelArray[0] );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo[1] );
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLuint) * pMESH->numTriangles * 3, _vertexIndicesArray );
}
inline void
CSCI441::MD5Model::_drawMesh(
const MD5Mesh *pMESH
) const {
// Bind Diffuse Map
glBindTexture(GL_TEXTURE_2D, pMESH->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle );
glBindVertexArray(_vao );
glDrawElements(GL_TRIANGLES, pMESH->numTriangles * 3, GL_UNSIGNED_INT, (void*)nullptr );
}
[[maybe_unused]]
inline void
CSCI441::MD5Model::allocVertexArrays(
GLuint vPosAttribLoc,
GLuint vColorAttribLoc,
GLuint vTexCoordAttribLoc
) {
_vertexArray = new glm::vec3[_maxVertices];
_texelArray = new glm::vec2[_maxVertices];
_vertexIndicesArray = new GLuint[_maxTriangles * 3];
glGenVertexArrays( 1, &_vao );
glBindVertexArray(_vao );
glGenBuffers(2, _vbo );
glBindBuffer(GL_ARRAY_BUFFER, _vbo[0] );
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * _maxVertices + sizeof(glm::vec2) * _maxVertices, nullptr, GL_DYNAMIC_DRAW );
glEnableVertexAttribArray( vPosAttribLoc );
glVertexAttribPointer( vPosAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, (void*)nullptr );
glEnableVertexAttribArray( vTexCoordAttribLoc );
glVertexAttribPointer( vTexCoordAttribLoc, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * _maxVertices) );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo[1] );
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * _maxTriangles * 3, nullptr, GL_DYNAMIC_DRAW );
printf("[.md5mesh]: Model VAO/VBO/IBO registered at %u/%u/%u\n", _vao, _vbo[0], _vbo[1] );
glGenVertexArrays( 1, &_skeletonVAO );
glBindVertexArray(_skeletonVAO );
glGenBuffers( 1, &_skeletonVBO );
glBindBuffer(GL_ARRAY_BUFFER, _skeletonVBO );
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * _numJoints * 3 * 2, nullptr, GL_DYNAMIC_DRAW );
glEnableVertexAttribArray( vPosAttribLoc ); // vPos
glVertexAttribPointer( vPosAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, (void*)nullptr );
glEnableVertexAttribArray( vColorAttribLoc ); // vColor
glVertexAttribPointer( vColorAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * _numJoints * 3) );
printf("[.md5mesh]: Skeleton VAO/VBO registered at %u/%u\n", _skeletonVAO, _skeletonVBO );
}
inline void
CSCI441::MD5Model::_freeVertexArrays()
{
delete[] _vertexArray;
_vertexArray = nullptr;
delete[] _vertexIndicesArray;
_vertexIndicesArray = nullptr;
delete[] _texelArray;
_texelArray = nullptr;
glDeleteVertexArrays( 1, &_vao );
glDeleteBuffers(2, _vbo );
glDeleteVertexArrays( 1, &_skeletonVAO );
glDeleteBuffers( 1, &_skeletonVBO );
}
// Draw the skeleton as lines and points (for joints).
[[maybe_unused]]
inline void
CSCI441::MD5Model::drawSkeleton() const
{
glBindVertexArray(_skeletonVAO );
glBindBuffer(GL_ARRAY_BUFFER, _skeletonVBO );
glm::vec3 jointColor = {1.0f, 1.0f, 0.0f };
glm::vec3 boneColor = {1.0f, 0.0f, 1.0f };
// put in points for joints
for(GLint i = 0; i < _numJoints; ++i ) {
glBufferSubData(GL_ARRAY_BUFFER, i * sizeof(glm::vec3), sizeof(glm::vec3), &(_skeleton[i].position) );
glBufferSubData(GL_ARRAY_BUFFER, i * sizeof(glm::vec3) + sizeof(glm::vec3) * _numJoints * 3, sizeof(glm::vec3), &jointColor[0]);
}
// put in lines for bones
GLint numBones = 0;
for(GLint i = 0; i < _numJoints; ++i ) {
if( _skeleton[i].parent != MD5Joint::NULL_JOINT ) {
glBufferSubData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * _numJoints + (i * 2) * sizeof(glm::vec3), sizeof(glm::vec3), &(_skeleton[_skeleton[i].parent].position) );
glBufferSubData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * _numJoints + (i * 2) * sizeof(glm::vec3) + sizeof(glm::vec3) * _numJoints * 3, sizeof(glm::vec3), &boneColor[0]);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * _numJoints + (i * 2) * sizeof(glm::vec3) + sizeof(glm::vec3), sizeof(glm::vec3), &(_skeleton[i].position) );
glBufferSubData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * _numJoints + (i * 2) * sizeof(glm::vec3) + sizeof(glm::vec3) + sizeof(glm::vec3) * _numJoints * 3, sizeof(glm::vec3), &boneColor[0]);
numBones++;
}
}
glPointSize (5.0f);
glDrawArrays(GL_POINTS, 0, _numJoints );
glPointSize(1.0f);
glLineWidth( 3.0f );
glDrawArrays(GL_LINES, _numJoints, numBones * 2 );
glLineWidth(1.0f);
}
// Check if an animation can be used for a given model. Model's
// skeleton and animation's skeleton must match.
inline bool
CSCI441::MD5Model::_checkAnimValidity() const
{
// md5mesh and md5anim must have the same number of joints
if( _numJoints != _animation.numJoints ) {
printf("\n[.md5anim]: skeleton and animation do not have same number of joints. cannot apply animation to skeleton\n\n");
return false;
}
// We just check with frame[0]
for(GLint i = 0; i < _numJoints; ++i) {
// Joints must have the same parent index
if (_baseSkeleton[i].parent != _animation.skeletonFrames[0][i].parent) {
printf("\n[.md5anim]: skeleton and animation joints do not have same parent index. cannot apply animation to skeleton\n\n");
return false;
}
// Joints must have the same name
if (strcmp (_baseSkeleton[i].name, _animation.skeletonFrames[0][i].name) != 0) {
printf("\n[.md5anim]: skeleton and animation joints do not have same name. cannot apply animation to skeleton\n\n");
return false;
}
}
printf("\n[.md5anim]: skeleton and animation match. animation can be applied to skeleton\n\n");
return true;
}
// Build _skeleton for a given frame data.
inline void
CSCI441::MD5Model::_buildFrameSkeleton(
const MD5JointInfo* pJOINT_INFOS,
const MD5BaseFrameJoint* pBASE_FRAME,
const GLfloat* pANIM_FRAME_DATA,
MD5Joint* pSkeletonFrame,
const GLint NUM_JOINTS
) {
if(pJOINT_INFOS == nullptr
|| pBASE_FRAME == nullptr
|| pANIM_FRAME_DATA == nullptr
|| pSkeletonFrame == nullptr) return;
GLint i;
for(i = 0; i < NUM_JOINTS; ++i) {
const MD5BaseFrameJoint *baseJoint = &pBASE_FRAME[i];
glm::vec3 animatedPosition = baseJoint->position;
glm::quat animatedOrientation = baseJoint->orientation;
GLint j = 0;
// Tx
if(pJOINT_INFOS[i].flags & 1 ) {
animatedPosition.x = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
++j;
}
// Ty
if(pJOINT_INFOS[i].flags & 2 ) {
animatedPosition.y = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
++j;
}