12#ifndef CSCI441_MD5_MODEL_HPP
13#define CSCI441_MD5_MODEL_HPP
50#ifdef CSCI441_USE_GLEW
56#include <glm/exponential.hpp>
57#include <glm/ext/quaternion_common.hpp>
58#include <glm/ext/quaternion_float.hpp>
60#define GLM_ENABLE_EXPERIMENTAL
61#include <glm/gtx/quaternion.hpp>
85 static const GLint NULL_JOINT = -1;
93 GLint parent = NULL_JOINT;
97 glm::vec3 position = {0.0f, 0.0f, 0.0f};
101 glm::quat orientation = {0.0f, 0.0f, 0.0f, 0.0f};
111 glm::vec2 texCoord = {0.0f, 0.0f};
129 GLint index[3] = {0};
139 GLint joint = MD5Joint::NULL_JOINT;
147 glm::vec3 position = {0.0f, 0.0f, 0.0f};
157 GLuint texHandle = 0;
161 char filename[512] =
"";
209 GLint numVertices = 0;
213 GLint numTriangles = 0;
217 GLint numWeights = 0;
222 char shader[512] =
"";
237 GLint parent = MD5Joint::NULL_JOINT;
245 GLint startIndex = 0;
255 glm::vec3 position = {0.0f, 0.0f, 0.0f};
259 glm::quat orientation = {0.0f, 0.0f, 0.0f, 0.0f};
270 glm::vec3 min = {0.0f, 0.0f, 0.0f};
274 glm::vec3 max = {0.0f, 0.0f, 0.0f};
322 GLfloat lastTime = 0.0f;
327 GLfloat maxTime = 0.0f;
356 [[maybe_unused]]
bool loadMD5Model(
const char* MD5_MESH_FILE,
const char* MD5_ANIM_FILE =
"");
362 [[nodiscard]]
bool isAnimated()
const {
return _isAnimated; }
370 [[nodiscard]]
bool readMD5Model(
const char* FILENAME);
379 [[maybe_unused]]
void allocVertexArrays(GLuint vPosAttribLoc, GLuint vColorAttribLoc, GLuint vTexCoordAttribLoc);
383 [[maybe_unused]]
void draw()
const;
387 [[maybe_unused]]
void drawSkeleton()
const;
395 [[nodiscard]]
bool readMD5Anim(
const char* filename);
400 void animate(GLfloat dt);
403 MD5Joint* _baseSkeleton;
413 glm::vec3* _vertexArray;
414 glm::vec2* _texelArray;
415 GLuint* _vertexIndicesArray;
430 MD5Animation _animation;
438 MD5AnimationState _animationInfo;
440 void _prepareMesh(
const MD5Mesh* pMESH)
const;
441 void _drawMesh(
const MD5Mesh* pMESH)
const;
442 [[nodiscard]]
bool _checkAnimValidity()
const;
443 static void _buildFrameSkeleton(
const MD5JointInfo* pJOINT_INFOS,
444 const MD5BaseFrameJoint* pBASE_FRAME,
445 const GLfloat* pANIM_FRAME_DATA,
446 MD5Joint* pSkeletonFrame,
448 void _interpolateSkeletons(GLfloat interp);
450 void _freeVertexArrays();
459 _baseSkeleton =
nullptr;
465 _vertexArray =
nullptr;
466 _texelArray =
nullptr;
467 _vertexIndicesArray =
nullptr;
490 const char* MD5_MESH_FILE,
491 const char* MD5_ANIM_FILE
494 if( readMD5Model(MD5_MESH_FILE) ) {
496 if(strcmp(MD5_ANIM_FILE,
"") != 0 ) {
498 if( !readMD5Anim(MD5_ANIM_FILE) ) {
502 if( !isAnimated() ) {
503 printf (
"[.MD5_ANIM_FILE]: no animation loaded.\n");
519 GLint currentMesh = 0;
523 GLint totalVertices = 0;
524 GLint totalWeights = 0;
525 GLint totalTriangles = 0;
527 GLfloat minX = 999999, minY = 999999, minZ = 999999;
528 GLfloat maxX = -999999, maxY = -999999, maxZ = -999999;
530 printf(
"[.md5mesh]: about to read %s\n", FILENAME );
532 fp = fopen(FILENAME,
"rb" );
534 fprintf (stderr,
"[.md5mesh]: Error: couldn't open \"%s\"!\n", FILENAME);
540 fgets( buff,
sizeof(buff), fp );
542 if( sscanf(buff,
" MD5Version %d", &version) == 1 ) {
543 if( version != 10 ) {
545 fprintf (stderr,
"[.md5mesh]: Error: bad model version\n");
549 }
else if( sscanf(buff,
" numJoints %d", &_numJoints) == 1 ) {
550 if( _numJoints > 0 ) {
552 _baseSkeleton =
new MD5Joint[_numJoints];
554 }
else if( sscanf(buff,
" numMeshes %d", &_numMeshes) == 1 ) {
555 if( _numMeshes > 0 ) {
557 _meshes =
new MD5Mesh[_numMeshes];
559 }
else if( strncmp(buff,
"joints {", 8) == 0 ) {
561 for(i = 0; i < _numJoints; ++i) {
562 MD5Joint *joint = &_baseSkeleton[i];
565 fgets( buff,
sizeof(buff), fp );
567 if( sscanf(buff,
"%s %d ( %f %f %f ) ( %f %f %f )",
576 }
else if( strncmp(buff,
"mesh {", 6) == 0 ) {
577 MD5Mesh *mesh = &_meshes[currentMesh];
578 GLint vert_index = 0;
580 GLint weight_index = 0;
584 while( buff[0] !=
'}' && !feof(fp) ) {
586 fgets( buff,
sizeof(buff), fp );
588 if( strstr( buff,
"shader ") ) {
589 GLint quote = 0, j = 0;
592 for(uli = 0; uli <
sizeof(buff) && (quote < 2); ++uli) {
593 if( buff[uli] ==
'\"' )
596 if( (quote == 1) && (buff[uli] !=
'\"') ) {
597 mesh->
shader[j] = buff[uli];
606 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);
610 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 );
614 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 );
616 printf(
"[.md5mesh | ERROR]: Could not load diffuse map %s\n", mesh->
shader);
624 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 );
628 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 );
630 printf(
"[.md5mesh | ERROR]: Could not load specular map %s\n", mesh->
shader);
637 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 );
641 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 );
643 printf(
"[.md5mesh | ERROR]: Could not load normal map %s\n", mesh->
shader);
650 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 );
654 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 );
656 printf(
"[.md5mesh | ERROR]: Could not load height map %s\n", mesh->
shader);
660 }
else if( sscanf(buff,
" numverts %d", &mesh->
numVertices) == 1 ) {
670 }
else if( sscanf(buff,
" numtris %d", &mesh->
numTriangles) == 1 ) {
680 }
else if( sscanf(buff,
" numweights %d", &mesh->
numWeights) == 1 ) {
687 }
else if( sscanf(buff,
" vert %d ( %f %f ) %d %d",
689 &fdata[0], &fdata[1],
690 &idata[0], &idata[1]) == 5
697 }
else if( sscanf(buff,
" tri %d %d %d %d",
699 &idata[0], &idata[1], &idata[2]) == 4
705 }
else if( sscanf(buff,
" weight %d %d %f ( %f %f %f )",
706 &weight_index, &idata[0], &fdata[3],
707 &fdata[0], &fdata[1], &fdata[2]) == 6
716 if( fdata[0] < minX ) { minX = fdata[0]; }
717 if( fdata[0] > maxX ) { maxX = fdata[0]; }
718 if( fdata[1] < minY ) { minY = fdata[1]; }
719 if( fdata[1] > maxY ) { maxY = fdata[1]; }
720 if( fdata[2] < minZ ) { minZ = fdata[2]; }
721 if( fdata[2] > maxZ ) { maxZ = fdata[2]; }
731 _skeleton = _baseSkeleton;
733 printf(
"[.md5mesh]: finished reading %s\n", FILENAME );
734 printf(
"[.md5mesh]: read in %d meshes, %d joints, %d vertices, %d weights, and %d triangles\n", _numMeshes, _numJoints, totalVertices, totalWeights, totalTriangles );
735 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) );
743CSCI441::MD5Model::_freeModel()
745 delete _baseSkeleton;
746 _baseSkeleton =
nullptr;
749 for(GLint i = 0; i < _numMeshes; ++i) {
750 delete _meshes[i].vertices;
751 _meshes[i].vertices =
nullptr;
753 delete _meshes[i].triangles;
754 _meshes[i].triangles =
nullptr;
756 delete _meshes[i].weights;
757 _meshes[i].weights =
nullptr;
769 for(GLint i = 0; i < _numMeshes; ++i) {
779CSCI441::MD5Model::_prepareMesh(
785 for(k = 0, i = 0; i < pMESH->numTriangles; ++i) {
786 for(j = 0; j < 3; ++j, ++k)
787 _vertexIndicesArray[k] = pMESH->triangles[i].index[j];
791 for(i = 0; i < pMESH->numVertices; ++i) {
792 glm::vec3 finalVertex = {0.0f, 0.0f, 0.0f };
795 for(j = 0; j < pMESH->vertices[i].count; ++j) {
796 const MD5Weight *weight = &pMESH->weights[pMESH->vertices[i].start + j];
797 const MD5Joint *joint = &_skeleton[weight->joint];
800 glm::vec3 weightedVertex;
801 weightedVertex = glm::rotate(joint->orientation, glm::vec4(weight->position, 0.0f));
804 finalVertex.x += (joint->position.x + weightedVertex.x) * weight->bias;
805 finalVertex.y += (joint->position.y + weightedVertex.y) * weight->bias;
806 finalVertex.z += (joint->position.z + weightedVertex.z) * weight->bias;
809 _vertexArray[i].x = finalVertex.x;
810 _vertexArray[i].y = finalVertex.y;
811 _vertexArray[i].z = finalVertex.z;
813 _texelArray[i].s = pMESH->vertices[i].texCoord.s;
814 _texelArray[i].t = pMESH->vertices[i].texCoord.t;
817 glBindVertexArray(_vao );
818 glBindBuffer(GL_ARRAY_BUFFER, _vbo[0] );
819 glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(glm::vec3) * pMESH->numVertices, &_vertexArray[0] );
820 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * _maxVertices,
sizeof(glm::vec2) * pMESH->numVertices, &_texelArray[0] );
821 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo[1] );
822 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0,
sizeof(GLuint) * pMESH->numTriangles * 3, _vertexIndicesArray );
826CSCI441::MD5Model::_drawMesh(
830 glBindTexture(GL_TEXTURE_2D, pMESH->textures[MD5Mesh::TextureMap::DIFFUSE].texHandle );
832 glBindVertexArray(_vao );
833 glDrawElements(GL_TRIANGLES, pMESH->numTriangles * 3, GL_UNSIGNED_INT, (
void*)
nullptr );
839 GLuint vPosAttribLoc,
840 GLuint vColorAttribLoc,
841 GLuint vTexCoordAttribLoc
843 _vertexArray =
new glm::vec3[_maxVertices];
844 _texelArray =
new glm::vec2[_maxVertices];
845 _vertexIndicesArray =
new GLuint[_maxTriangles * 3];
847 glGenVertexArrays( 1, &_vao );
848 glBindVertexArray(_vao );
850 glGenBuffers(2, _vbo );
851 glBindBuffer(GL_ARRAY_BUFFER, _vbo[0] );
852 glBufferData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * _maxVertices +
sizeof(glm::vec2) * _maxVertices,
nullptr, GL_DYNAMIC_DRAW );
854 glEnableVertexAttribArray( vPosAttribLoc );
855 glVertexAttribPointer( vPosAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr );
857 glEnableVertexAttribArray( vTexCoordAttribLoc );
858 glVertexAttribPointer( vTexCoordAttribLoc, 2, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * _maxVertices) );
860 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo[1] );
861 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(GLuint) * _maxTriangles * 3,
nullptr, GL_DYNAMIC_DRAW );
863 printf(
"[.md5mesh]: Model VAO/VBO/IBO registered at %u/%u/%u\n", _vao, _vbo[0], _vbo[1] );
865 glGenVertexArrays( 1, &_skeletonVAO );
866 glBindVertexArray(_skeletonVAO );
868 glGenBuffers( 1, &_skeletonVBO );
869 glBindBuffer(GL_ARRAY_BUFFER, _skeletonVBO );
870 glBufferData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * _numJoints * 3 * 2,
nullptr, GL_DYNAMIC_DRAW );
872 glEnableVertexAttribArray( vPosAttribLoc );
873 glVertexAttribPointer( vPosAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr );
875 glEnableVertexAttribArray( vColorAttribLoc );
876 glVertexAttribPointer( vColorAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * _numJoints * 3) );
878 printf(
"[.md5mesh]: Skeleton VAO/VBO registered at %u/%u\n", _skeletonVAO, _skeletonVBO );
882CSCI441::MD5Model::_freeVertexArrays()
884 delete[] _vertexArray;
885 _vertexArray =
nullptr;
887 delete[] _vertexIndicesArray;
888 _vertexIndicesArray =
nullptr;
890 delete[] _texelArray;
891 _texelArray =
nullptr;
893 glDeleteVertexArrays( 1, &_vao );
894 glDeleteBuffers(2, _vbo );
895 glDeleteVertexArrays( 1, &_skeletonVAO );
896 glDeleteBuffers( 1, &_skeletonVBO );
904 glBindVertexArray(_skeletonVAO );
905 glBindBuffer(GL_ARRAY_BUFFER, _skeletonVBO );
907 glm::vec3 jointColor = {1.0f, 1.0f, 0.0f };
908 glm::vec3 boneColor = {1.0f, 0.0f, 1.0f };
911 for(GLint i = 0; i < _numJoints; ++i ) {
912 glBufferSubData(GL_ARRAY_BUFFER, i *
sizeof(glm::vec3),
sizeof(glm::vec3), &(_skeleton[i].position) );
913 glBufferSubData(GL_ARRAY_BUFFER, i *
sizeof(glm::vec3) +
sizeof(glm::vec3) * _numJoints * 3,
sizeof(glm::vec3), &jointColor[0]);
918 for(GLint i = 0; i < _numJoints; ++i ) {
919 if( _skeleton[i].parent != MD5Joint::NULL_JOINT ) {
920 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * _numJoints + (i * 2) *
sizeof(glm::vec3),
sizeof(glm::vec3), &(_skeleton[_skeleton[i].parent].position) );
921 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * _numJoints + (i * 2) *
sizeof(glm::vec3) +
sizeof(glm::vec3) * _numJoints * 3,
sizeof(glm::vec3), &boneColor[0]);
923 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * _numJoints + (i * 2) *
sizeof(glm::vec3) +
sizeof(glm::vec3),
sizeof(glm::vec3), &(_skeleton[i].position) );
924 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]);
930 glDrawArrays(GL_POINTS, 0, _numJoints );
934 glDrawArrays(GL_LINES, _numJoints, numBones * 2 );
941CSCI441::MD5Model::_checkAnimValidity()
const
944 if( _numJoints != _animation.numJoints ) {
945 printf(
"\n[.md5anim]: skeleton and animation do not have same number of joints. cannot apply animation to skeleton\n\n");
950 for(GLint i = 0; i < _numJoints; ++i) {
952 if (_baseSkeleton[i].parent != _animation.skeletonFrames[0][i].parent) {
953 printf(
"\n[.md5anim]: skeleton and animation joints do not have same parent index. cannot apply animation to skeleton\n\n");
958 if (strcmp (_baseSkeleton[i].name, _animation.skeletonFrames[0][i].name) != 0) {
959 printf(
"\n[.md5anim]: skeleton and animation joints do not have same name. cannot apply animation to skeleton\n\n");
964 printf(
"\n[.md5anim]: skeleton and animation match. animation can be applied to skeleton\n\n");
970CSCI441::MD5Model::_buildFrameSkeleton(
971 const MD5JointInfo* pJOINT_INFOS,
972 const MD5BaseFrameJoint* pBASE_FRAME,
973 const GLfloat* pANIM_FRAME_DATA,
974 MD5Joint* pSkeletonFrame,
975 const GLint NUM_JOINTS
977 if(pJOINT_INFOS ==
nullptr
978 || pBASE_FRAME ==
nullptr
979 || pANIM_FRAME_DATA ==
nullptr
980 || pSkeletonFrame ==
nullptr)
return;
984 for(i = 0; i < NUM_JOINTS; ++i) {
985 const MD5BaseFrameJoint *baseJoint = &pBASE_FRAME[i];
986 glm::vec3 animatedPosition = baseJoint->position;
987 glm::quat animatedOrientation = baseJoint->orientation;
991 if(pJOINT_INFOS[i].flags & 1 ) {
992 animatedPosition.x = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
997 if(pJOINT_INFOS[i].flags & 2 ) {
998 animatedPosition.y = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1003 if(pJOINT_INFOS[i].flags & 4 ) {
1004 animatedPosition.z = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1009 if(pJOINT_INFOS[i].flags & 8 ) {
1010 animatedOrientation.x = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1015 if(pJOINT_INFOS[i].flags & 16 ) {
1016 animatedOrientation.y = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1021 if(pJOINT_INFOS[i].flags & 32 ) {
1022 animatedOrientation.z = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1026 animatedOrientation.w = glm::extractRealComponent(animatedOrientation);
1031 MD5Joint *thisJoint = &pSkeletonFrame[i];
1033 GLint parent = pJOINT_INFOS[i].parent;
1034 thisJoint->parent = parent;
1035 strcpy (thisJoint->name, pJOINT_INFOS[i].name);
1038 if( thisJoint->parent == MD5Joint::NULL_JOINT ) {
1039 thisJoint->position = animatedPosition;
1040 thisJoint->orientation = animatedOrientation;
1042 MD5Joint *parentJoint = &pSkeletonFrame[parent];
1043 glm::vec3 rotatedPosition = glm::rotate(parentJoint->orientation, glm::vec4(animatedPosition, 0.0f));
1046 thisJoint->position = parentJoint->position + rotatedPosition;
1049 thisJoint->orientation = glm::normalize( glm::cross(parentJoint->orientation, animatedOrientation) );
1057 const char *filename
1062 GLfloat *animFrameData =
nullptr;
1064 GLint numAnimatedComponents;
1068 printf(
"[.md5anim]: about to read %s\n", filename );
1070 FILE *fp = fopen( filename,
"rb" );
1072 fprintf (stderr,
"[.md5anim]: Error: couldn't open \"%s\"!\n", filename);
1076 while( !feof(fp) ) {
1078 fgets( buff,
sizeof(buff), fp );
1080 if( sscanf(buff,
" MD5Version %d", &version) == 1 ) {
1081 if( version != 10 ) {
1083 fprintf (stderr,
"[.md5anim]: Error: bad animation version\n");
1087 }
else if( sscanf(buff,
" numFrames %d", &_animation.numFrames) == 1 ) {
1089 if( _animation.numFrames > 0 ) {
1090 _animation.skeletonFrames =
new MD5Joint*[_animation.numFrames];
1091 _animation.boundingBoxes =
new MD5BoundingBox[_animation.numFrames];
1093 }
else if( sscanf(buff,
" numJoints %d", &_animation.numJoints) == 1 ) {
1094 if( _animation.numJoints > 0 ) {
1095 for(i = 0; i < _animation.numFrames; ++i) {
1097 _animation.skeletonFrames[i] =
new MD5Joint[_animation.numJoints];
1105 }
else if( sscanf(buff,
" frameRate %d", &_animation.frameRate) == 1 ) {
1107 }
else if( sscanf(buff,
" numAnimatedComponents %d", &numAnimatedComponents) == 1 ) {
1108 if( numAnimatedComponents > 0 ) {
1110 animFrameData =
new GLfloat[numAnimatedComponents];
1112 }
else if( strncmp(buff,
"hierarchy {", 11) == 0 ) {
1113 for(i = 0; i < _animation.numJoints; ++i) {
1115 fgets( buff,
sizeof(buff), fp );
1118 sscanf(buff,
" %s %d %d %d",
1119 jointInfos[i].name, &jointInfos[i].parent,
1120 &jointInfos[i].flags, &jointInfos[i].startIndex);
1122 }
else if( strncmp(buff,
"bounds {", 8) == 0 ) {
1123 for(i = 0; i < _animation.numFrames; ++i) {
1125 fgets( buff,
sizeof(buff), fp );
1128 sscanf(buff,
" ( %f %f %f ) ( %f %f %f )",
1129 &_animation.boundingBoxes[i].min[0], &_animation.boundingBoxes[i].min[1], &_animation.boundingBoxes[i].min[2],
1130 &_animation.boundingBoxes[i].max[0], &_animation.boundingBoxes[i].max[1], &_animation.boundingBoxes[i].max[2]);
1132 }
else if( strncmp(buff,
"baseframe {", 10) == 0 ) {
1133 for(i = 0; i < _animation.numJoints; ++i) {
1135 fgets( buff,
sizeof(buff), fp );
1138 if( sscanf(buff,
" ( %f %f %f ) ( %f %f %f )",
1139 &baseFrame[i].position[0], &baseFrame[i].position[1], &baseFrame[i].position[2],
1140 &baseFrame[i].orientation[0], &baseFrame[i].orientation[1], &baseFrame[i].orientation[2]) == 6 ) {
1142 baseFrame[i].
orientation.w = glm::extractRealComponent(baseFrame[i].orientation);
1145 }
else if(sscanf(buff,
" frame %d", &frameIndex) == 1 ) {
1147 for(i = 0; i < numAnimatedComponents; ++i)
1148 fscanf( fp,
"%f", &animFrameData[i] );
1151 _buildFrameSkeleton(jointInfos, baseFrame, animFrameData,
1152 _animation.skeletonFrames[frameIndex],
1153 _animation.numJoints);
1159 printf(
"[.md5anim]: finished reading %s\n", filename );
1160 printf(
"[.md5anim]: read in %d frames of %d joints with %d animated components\n", _animation.numFrames, _animation.numJoints, numAnimatedComponents );
1161 printf(
"[.md5anim]: animation's frame rate is %d\n", _animation.frameRate );
1165 free( animFrameData );
1174 _animationInfo.currFrame = 0;
1175 _animationInfo.nextFrame = 1;
1177 _animationInfo.lastTime = 0.0f;
1178 _animationInfo.maxTime = 1.0f / (GLfloat)_animation.frameRate;
1181 _skeleton =
new MD5Joint[_animation.numJoints];
1183 if( _checkAnimValidity() ) {
1194CSCI441::MD5Model::_freeAnim()
1198 for(i = 0; i < _animation.numFrames; ++i) {
1199 delete _animation.skeletonFrames[i];
1200 _animation.skeletonFrames[i] =
nullptr;
1203 delete[] _animation.skeletonFrames;
1204 _animation.skeletonFrames =
nullptr;
1206 delete _animation.boundingBoxes;
1207 _animation.boundingBoxes =
nullptr;
1210 _skeleton =
nullptr;
1215CSCI441::MD5Model::_interpolateSkeletons(GLfloat interp)
1217 const MD5Joint *skeletonA = _animation.skeletonFrames[_animationInfo.currFrame];
1218 const MD5Joint *skeletonB = _animation.skeletonFrames[_animationInfo.nextFrame];
1222 for(i = 0; i < _animation.numJoints; ++i) {
1224 _skeleton[i].parent = skeletonA[i].parent;
1227 _skeleton[i].position[0] = skeletonA[i].position[0] + interp * (skeletonB[i].position[0] - skeletonA[i].position[0]);
1228 _skeleton[i].position[1] = skeletonA[i].position[1] + interp * (skeletonB[i].position[1] - skeletonA[i].position[1]);
1229 _skeleton[i].position[2] = skeletonA[i].position[2] + interp * (skeletonB[i].position[2] - skeletonA[i].position[2]);
1232 _skeleton[i].orientation = glm::slerp(skeletonA[i].orientation, skeletonB[i].orientation, interp);
1241 GLint maxFrames = _animation.numFrames - 1;
1243 _animationInfo.lastTime += dt;
1246 if( _animationInfo.lastTime >= _animationInfo.maxTime ) {
1247 _animationInfo.currFrame++;
1248 _animationInfo.nextFrame++;
1249 _animationInfo.lastTime = 0.0;
1251 if( _animationInfo.currFrame > maxFrames )
1252 _animationInfo.currFrame = 0;
1254 if( _animationInfo.nextFrame > maxFrames )
1255 _animationInfo.nextFrame = 0;
1259 _interpolateSkeletons( _animationInfo.lastTime * _animation.frameRate );
Helper functions to work with OpenGL Textures.
stores a Doom3 MD5 Mesh + Animation
Definition: MD5Model.hpp:75
void draw() const
draws all the meshes that make up the model
Definition: MD5Model.hpp:766
bool readMD5Model(const char *FILENAME)
parses md5mesh file and allocates corresponding mesh data
Definition: MD5Model.hpp:513
bool isAnimated() const
returns if the MD5 Model has an accompanying animation
Definition: MD5Model.hpp:362
bool loadMD5Model(const char *MD5_MESH_FILE, const char *MD5_ANIM_FILE="")
loads a corresponding md5mesh and md5anim file to the object
Definition: MD5Model.hpp:489
MD5Model()
initializes an empty MD5 Model
Definition: MD5Model.hpp:457
MD5Model & operator=(const MD5Model &)=delete
do not allow MD5 models to be copied
bool readMD5Anim(const char *filename)
reads in an animation sequence from an external file
Definition: MD5Model.hpp:1056
void allocVertexArrays(GLuint vPosAttribLoc, GLuint vColorAttribLoc, GLuint vTexCoordAttribLoc)
binds model VBOs to attribute pointer locations
Definition: MD5Model.hpp:838
void animate(GLfloat dt)
advances the model forward in its animation sequence the corresponding amount of time based on frame ...
Definition: MD5Model.hpp:1239
MD5Model(const MD5Model &)=delete
do not allow MD5 models to be copied
~MD5Model()
deallocates any used memory on the CPU and GPU
Definition: MD5Model.hpp:479
void drawSkeleton() const
draws the skeleton joints (as points) and bones (as lines)
Definition: MD5Model.hpp:902
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17
stores an entire animation sequence for a given MD5 Model
Definition: MD5Model.hpp:280
stores state of current animation frame
Definition: MD5Model.hpp:309
base frame joint
Definition: MD5Model.hpp:251
glm::quat orientation
joint orientation expressed as a quaternion in object space
Definition: MD5Model.hpp:259
bounding box containing the model during animation
Definition: MD5Model.hpp:266
a joint of the MD5 Skeleton
Definition: MD5Model.hpp:81
glm::vec3 position
position of the joint in object space
Definition: MD5Model.hpp:97
glm::quat orientation
joint orientation expressed as a quaternion in object space
Definition: MD5Model.hpp:101
char name[256]
joint identifier
Definition: MD5Model.hpp:89
GLint parent
index of the parent joint on skeletal tree
Definition: MD5Model.hpp:93
information pertaining to each animation joint
Definition: MD5Model.hpp:229
mesh that comprises the model's skin
Definition: MD5Model.hpp:167
GLint numTriangles
number of triangles in the mesh triangle array
Definition: MD5Model.hpp:213
TextureMap
named entities for different texture maps applied to the model
Definition: MD5Model.hpp:187
@ DIFFUSE
diffuse map
Definition: MD5Model.hpp:191
@ SPECULAR
specular map
Definition: MD5Model.hpp:195
@ NORMAL
normal map
Definition: MD5Model.hpp:199
MD5Texture textures[4]
texture map array
Definition: MD5Model.hpp:183
GLint numVertices
number of vertices in the mesh vertex array
Definition: MD5Model.hpp:209
MD5Triangle * triangles
array triangles comprising the mesh
Definition: MD5Model.hpp:175
MD5Vertex * vertices
array of vertices comprising the mesh
Definition: MD5Model.hpp:171
GLint numWeights
number of weights in the mesh weight array
Definition: MD5Model.hpp:217
MD5Weight * weights
array of weights to determine vertex position based on joint positions
Definition: MD5Model.hpp:179
char shader[512]
base filename for all textures applied to mesh
Definition: MD5Model.hpp:222
texture handle for the model
Definition: MD5Model.hpp:153
GLuint texHandle
handle of texture stored on the GPU
Definition: MD5Model.hpp:157
char filename[512]
filename texture was loaded from
Definition: MD5Model.hpp:161
a triangle on the mesh
Definition: MD5Model.hpp:125
GLint index[3]
vertex indices that make up triangle
Definition: MD5Model.hpp:129
a vertex on the mesh
Definition: MD5Model.hpp:107
glm::vec2 texCoord
texture coordinate for vertex
Definition: MD5Model.hpp:111
GLint start
index of starting weight
Definition: MD5Model.hpp:115
GLint count
number of weights that determine vertex's position
Definition: MD5Model.hpp:119
the weight for a mesh vertex
Definition: MD5Model.hpp:135
GLfloat bias
contribution of the weight
Definition: MD5Model.hpp:143
GLint joint
index of joint the weight depends on
Definition: MD5Model.hpp:139
glm::vec3 position
weight's position in object space
Definition: MD5Model.hpp:147