12#ifndef CSCI441_MD5_MODEL_HPP
13#define CSCI441_MD5_MODEL_HPP
52#ifdef CSCI441_USE_GLEW
58#include <glm/exponential.hpp>
59#include <glm/ext/quaternion_common.hpp>
61#define GLM_ENABLE_EXPERIMENTAL
62#include <glm/gtx/quaternion.hpp>
87 _animations =
new CSCI441_INTERNAL::MD5Animation*[_numAnimations];
88 _animations[0] =
new CSCI441_INTERNAL::MD5Animation();
90 _animationInfos =
new CSCI441_INTERNAL::MD5AnimationState[_numAnimations];
128 [[maybe_unused]]
bool loadMD5Model(
const char* MD5_MESH_FILE,
const char* MD5_ANIM_FILE =
"");
134 [[nodiscard]]
bool isAnimated()
const {
return _isAnimated; }
142 [[nodiscard]]
bool readMD5Model(
const char* FILENAME);
154 [[maybe_unused]]
void allocVertexArrays(GLuint vPosAttribLoc, GLuint vColorAttribLoc, GLuint vTexCoordAttribLoc, GLuint vNormalAttribLoc = 0, GLuint vTangentAttribLoc = 0);
163 [[maybe_unused]]
void setActiveTextures(GLint diffuseMapActiveTexture, GLint specularMapActiveTexture, GLint normalMapActiveTexture, GLint heightMapActiveTexture);
168 [[maybe_unused]]
void draw()
const;
172 [[maybe_unused]]
void drawSkeleton()
const;
181 [[nodiscard]]
bool readMD5Anim(
const char* filename, GLushort targetAnimationIndex = 0);
187 [[nodiscard]]
bool addMD5Anim(
const char* filename);
196 if (!_isAnimated)
return 0;
198 return _numAnimations;
205 void useTargetAnimationIndex(GLushort targetAnimationIndex);
210 void animate(GLfloat dt);
219 static void readMD5Material(
const char* FILENAME,
const char* PATH =
"./");
223 static void releaseMD5Materials();
230 CSCI441_INTERNAL::MD5Joint* _baseSkeleton =
nullptr;
235 CSCI441_INTERNAL::MD5Mesh* _meshes =
nullptr;
240 GLint _numJoints = 0;
245 GLint _numMeshes = 0;
252 GLint _maxVertices = 0;
257 GLint _maxTriangles = 0;
262 glm::vec3* _vertexArray =
nullptr;
267 glm::vec3* _normalArray =
nullptr;
273 glm::vec4* _tangentArray =
nullptr;
278 glm::vec2* _texelArray =
nullptr;
283 GLuint* _vertexIndicesArray =
nullptr;
293 GLuint _vbo[2] = {0, 0};
299 GLuint _skeletonVAO = 0;
303 GLuint _skeletonVBO = 0;
307 CSCI441_INTERNAL::MD5Joint* _skeleton =
nullptr;
313 CSCI441_INTERNAL::MD5Animation** _animations =
nullptr;
317 GLushort _numAnimations = 0;
321 GLushort _currentAnimationIndex = 0;
325 bool _isAnimated =
false;
329 CSCI441_INTERNAL::MD5AnimationState* _animationInfos =
nullptr;
332 static std::map< std::string, CSCI441_INTERNAL::MD5MaterialShader* > _materials;
333 static std::map< std::string, GLuint > _textureMap;
338 GLint _diffuseActiveTexture = GL_TEXTURE0;
342 GLint _specularActiveTexture = GL_TEXTURE1;
346 GLint _normalActiveTexture = GL_TEXTURE2;
350 GLint _heightActiveTexture = GL_TEXTURE3;
358 void _prepareMesh(
const CSCI441_INTERNAL::MD5Mesh* pMESH)
const;
363 auto _drawMesh(
const CSCI441_INTERNAL::MD5Mesh* pMESH)
const -> void;
370 [[nodiscard]]
bool _checkAnimValidity(GLushort targetAnimationIndex)
const;
379 static void _buildFrameSkeleton(
const CSCI441_INTERNAL::MD5JointInfo* pJOINT_INFOS,
380 const CSCI441_INTERNAL::MD5BaseFrameJoint* pBASE_FRAME,
381 const GLfloat* pANIM_FRAME_DATA,
382 const CSCI441_INTERNAL::MD5Joint* pSkeletonFrame,
388 void _interpolateSkeletons(GLfloat interp);
396 void _freeVertexArrays();
406 void _moveFromSrc(MD5Model &src);
416 static size_t _trim(
char* out,
size_t len,
const char* str);
425 static bool _registerShaderTexture( CSCI441_INTERNAL::MD5Texture* texture );
443 const char* MD5_MESH_FILE,
444 const char* MD5_ANIM_FILE
447 if( readMD5Model(MD5_MESH_FILE) ) {
449 if(strcmp(MD5_ANIM_FILE,
"") != 0 ) {
451 if( !readMD5Anim(MD5_ANIM_FILE) ) {
455 if( !isAnimated() ) {
456 printf (
"[.MD5_ANIM_FILE]: no animation loaded.\n");
471 GLint currentMesh = 0;
473 GLint totalVertices = 0;
474 GLint totalWeights = 0;
475 GLint totalTriangles = 0;
477 GLfloat minX = 999999, minY = 999999, minZ = 999999;
478 GLfloat maxX = -999999, maxY = -999999, maxZ = -999999;
480 fprintf(stdout,
"[.md5mesh]: about to read %s\n", FILENAME );
482 FILE *fp = fopen(FILENAME,
"rb" );
484 fprintf (stderr,
"[.md5mesh]: Error: couldn't open \"%s\"!\n", FILENAME);
490 fgets( buff,
sizeof(buff), fp );
492 if( sscanf(buff,
" MD5Version %d", &version) == 1 ) {
493 if( version != 10 ) {
495 fprintf (stderr,
"[.md5mesh]: Error: bad model version\n");
499 }
else if( sscanf(buff,
" numJoints %d", &_numJoints) == 1 ) {
500 if( _numJoints > 0 ) {
502 _baseSkeleton =
new CSCI441_INTERNAL::MD5Joint[_numJoints];
504 }
else if( sscanf(buff,
" numMeshes %d", &_numMeshes) == 1 ) {
505 if( _numMeshes > 0 ) {
507 _meshes =
new CSCI441_INTERNAL::MD5Mesh[_numMeshes];
509 }
else if( strncmp(buff,
"joints {", 8) == 0 ) {
511 for(GLint i = 0; i < _numJoints; ++i) {
512 CSCI441_INTERNAL::MD5Joint *joint = &_baseSkeleton[i];
515 fgets( buff,
sizeof(buff), fp );
517 if( sscanf(buff,
"%s %d ( %f %f %f ) ( %f %f %f )",
518 joint->name, &joint->parent,
519 &joint->position[0], &joint->position[1], &joint->position[2],
520 &joint->orientation[0],&joint->orientation[1], &joint->orientation[2]) == 8
523 joint->orientation.w = glm::extractRealComponent(joint->orientation);
526 }
else if( strncmp(buff,
"mesh {", 6) == 0 ) {
527 CSCI441_INTERNAL::MD5Mesh *mesh = &_meshes[currentMesh];
528 GLint vert_index = 0;
530 GLint weight_index = 0;
531 GLfloat floatData[4];
534 while( buff[0] !=
'}' && !feof(fp) ) {
536 fgets( buff,
sizeof(buff), fp );
538 if( strstr( buff,
"shader ") ) {
539 GLint quote = 0, j = 0;
541 char shaderName[512] =
"";
544 for(
unsigned long uli = 0; uli <
sizeof(buff) && (quote < 2); ++uli) {
545 if( buff[uli] ==
'\"' )
548 if( (quote == 1) && (buff[uli] !=
'\"') ) {
549 shaderName[j] = buff[uli];
555 auto materialIter = _materials.find(shaderName);
556 if (materialIter != _materials.end()) {
557 mesh->shader = materialIter->second;
559 fprintf(stderr,
"[.md5mesh | ERROR]: Could not find material shader \"%s\"\n", shaderName);
562 }
else if( sscanf(buff,
" numverts %d", &mesh->numVertices) == 1 ) {
563 if( mesh->numVertices > 0 ) {
565 mesh->vertices =
new CSCI441_INTERNAL::MD5Vertex[mesh->numVertices];
568 if( mesh->numVertices > _maxVertices )
569 _maxVertices = mesh->numVertices;
571 totalVertices += mesh->numVertices;
572 }
else if( sscanf(buff,
" numtris %d", &mesh->numTriangles) == 1 ) {
573 if( mesh->numTriangles > 0 ) {
575 mesh->triangles =
new CSCI441_INTERNAL::MD5Triangle[mesh->numTriangles];
578 if( mesh->numTriangles > _maxTriangles )
579 _maxTriangles = mesh->numTriangles;
581 totalTriangles += mesh->numTriangles;
582 }
else if( sscanf(buff,
" numweights %d", &mesh->numWeights) == 1 ) {
583 if( mesh->numWeights > 0 ) {
585 mesh->weights =
new CSCI441_INTERNAL::MD5Weight[mesh->numWeights];
588 totalWeights += mesh->numWeights;
589 }
else if( sscanf(buff,
" vert %d ( %f %f ) %d %d",
591 &floatData[0], &floatData[1],
592 &intData[0], &intData[1]) == 5
595 mesh->vertices[vert_index].texCoord.s = floatData[0];
596 mesh->vertices[vert_index].texCoord.t = floatData[1];
597 mesh->vertices[vert_index].start = intData[0];
598 mesh->vertices[vert_index].count = intData[1];
599 }
else if( sscanf(buff,
" tri %d %d %d %d",
601 &intData[0], &intData[1], &intData[2]) == 4
604 mesh->triangles[tri_index ].index[0] = intData[0];
605 mesh->triangles[tri_index ].index[1] = intData[1];
606 mesh->triangles[tri_index ].index[2] = intData[2];
607 }
else if( sscanf(buff,
" weight %d %d %f ( %f %f %f )",
608 &weight_index, &intData[0], &floatData[3],
609 &floatData[0], &floatData[1], &floatData[2]) == 6
612 mesh->weights[weight_index].joint = intData[0];
613 mesh->weights[weight_index].bias = floatData[3];
614 mesh->weights[weight_index].position[0] = floatData[0];
615 mesh->weights[weight_index].position[1] = floatData[1];
616 mesh->weights[weight_index].position[2] = floatData[2];
618 if( floatData[0] < minX ) { minX = floatData[0]; }
619 if( floatData[0] > maxX ) { maxX = floatData[0]; }
620 if( floatData[1] < minY ) { minY = floatData[1]; }
621 if( floatData[1] > maxY ) { maxY = floatData[1]; }
622 if( floatData[2] < minZ ) { minZ = floatData[2]; }
623 if( floatData[2] > maxZ ) { maxZ = floatData[2]; }
633 _skeleton = _baseSkeleton;
635 fprintf(stdout,
"[.md5mesh]: finished reading %s\n", FILENAME );
636 fprintf(stdout,
"[.md5mesh]: read in %d meshes, %d joints, %d vertices, %d weights, and %d triangles\n", _numMeshes, _numJoints, totalVertices, totalWeights, totalTriangles );
637 fprintf(stdout,
"[.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) );
638 fprintf(stdout,
"\n" );
644CSCI441::MD5Model::_freeModel()
646 delete[] _baseSkeleton;
647 if (_baseSkeleton == _skeleton) _skeleton =
nullptr;
648 _baseSkeleton =
nullptr;
659 for(GLint i = 0; i < _numMeshes; ++i) {
660 CSCI441_INTERNAL::MD5Mesh& mesh = _meshes[i];
667CSCI441::MD5Model::_prepareMesh(
668 const CSCI441_INTERNAL::MD5Mesh *pMESH
673 for(k = 0, i = 0; i < pMESH->numTriangles; ++i) {
674 for(j = 0; j < 3; ++j, ++k)
675 _vertexIndicesArray[k] = pMESH->triangles[i].index[j];
679 auto normalAccum =
new glm::vec3[pMESH->numVertices];
680 auto tangentAccum =
new glm::vec3[pMESH->numVertices];
681 auto bitangentAccum =
new glm::vec3[pMESH->numVertices];
683 for(i = 0; i < pMESH->numVertices; ++i) {
684 normalAccum[i] = glm::vec3(0.0f);
685 tangentAccum[i] = glm::vec3(0.0f);
686 bitangentAccum[i] = glm::vec3(0.0f);
688 glm::vec3 finalVertex = {0.0f, 0.0f, 0.0f };
691 for(j = 0; j < pMESH->vertices[i].count; ++j) {
692 const CSCI441_INTERNAL::MD5Weight *weight = &pMESH->weights[pMESH->vertices[i].start + j];
693 const CSCI441_INTERNAL::MD5Joint *joint = &_skeleton[weight->joint];
696 const glm::vec3 weightedVertex = glm::rotate(joint->orientation, glm::vec4(weight->position, 0.0f));
699 finalVertex.x += (joint->position.x + weightedVertex.x) * weight->bias;
700 finalVertex.y += (joint->position.y + weightedVertex.y) * weight->bias;
701 finalVertex.z += (joint->position.z + weightedVertex.z) * weight->bias;
704 _vertexArray[i].x = finalVertex.x;
705 _vertexArray[i].y = finalVertex.y;
706 _vertexArray[i].z = finalVertex.z;
708 _texelArray[i].s = pMESH->vertices[i].texCoord.s;
709 _texelArray[i].t = pMESH->vertices[i].texCoord.t;
712 for(i = 0; i < pMESH->numTriangles; ++i) {
713 GLint idx0 = pMESH->triangles[i].index[0];
714 GLint idx1 = pMESH->triangles[i].index[1];
715 GLint idx2 = pMESH->triangles[i].index[2];
717 glm::vec3 v0 = _vertexArray[ idx0 ];
718 glm::vec3 v1 = _vertexArray[ idx1 ];
719 glm::vec3 v2 = _vertexArray[ idx2 ];
721 glm::vec2 uv0 = _texelArray[ idx0 ];
722 glm::vec2 uv1 = _texelArray[ idx1 ];
723 glm::vec2 uv2 = _texelArray[ idx2 ];
725 glm::vec3 edge1 = v1 - v0;
726 glm::vec3 edge2 = v2 - v0;
727 glm::vec2 deltaUV1 = uv1 - uv0;
728 glm::vec2 deltaUV2 = uv2 - uv0;
730 GLfloat f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
732 glm::vec3 normal = cross(edge1, edge2);
733 glm::vec3 tangent = f * (deltaUV2.y * edge1 - deltaUV1.y * edge2);
734 glm::vec3 bitangent = f * (deltaUV2.x * edge1 - deltaUV1.x * edge2);
736 normalAccum[ idx0 ] += normal; tangentAccum[ idx0 ] += tangent; bitangentAccum[ idx0 ] += bitangent;
737 normalAccum[ idx1 ] += normal; tangentAccum[ idx1 ] += tangent; bitangentAccum[ idx1 ] += bitangent;
738 normalAccum[ idx2 ] += normal; tangentAccum[ idx2 ] += tangent; bitangentAccum[ idx2 ] += bitangent;
741 for(i = 0; i < pMESH->numVertices; ++i) {
742 glm::vec3& n = normalAccum[i];
743 glm::vec3& t = tangentAccum[i];
744 glm::vec3& b = bitangentAccum[i];
746 glm::vec3 normal = -glm::normalize( n );
748 glm::vec3 tangent = -glm::normalize( t - (glm::dot(normal, t) * normal) );
749 glm::vec3 bitangent = glm::normalize( b );
751 _normalArray[ i ] = normal;
753 _tangentArray[ i ] = glm::vec4(tangent, 0.0f);
755 _tangentArray[ i ].w = (glm::dot( glm::cross(normal, tangent), bitangent) < 0.0f) ? -1.0f : 1.0f;
758 delete[] normalAccum;
759 delete[] tangentAccum;
760 delete[] bitangentAccum;
762 glBindVertexArray(_vao );
764 glBindBuffer(GL_ARRAY_BUFFER, _vbo[0] );
765 glBufferSubData(GL_ARRAY_BUFFER, 0,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * pMESH->numVertices, &_vertexArray[0] );
766 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _maxVertices * 1,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * pMESH->numVertices, &_normalArray[0] );
767 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _maxVertices * 2,
static_cast<GLsizeiptr
>(
sizeof(glm::vec4)) * pMESH->numVertices, &_tangentArray[0] );
768 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _maxVertices * 2 +
static_cast<GLsizeiptr
>(
sizeof(glm::vec4)) * _maxVertices,
static_cast<GLsizeiptr
>(
sizeof(glm::vec2)) * pMESH->numVertices, &_texelArray[0] );
770 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo[1] );
771 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0,
static_cast<GLsizeiptr
>(
sizeof(GLuint)) * pMESH->numTriangles * 3, _vertexIndicesArray );
775CSCI441::MD5Model::_drawMesh(
776 const CSCI441_INTERNAL::MD5Mesh *pMESH
778 if (pMESH->shader !=
nullptr) {
780 if (pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::SPECULAR].texHandle != 0) {
782 glActiveTexture(_specularActiveTexture);
783 glBindTexture(GL_TEXTURE_2D, pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::SPECULAR].texHandle );
786 if (pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::NORMAL].texHandle != 0) {
788 glActiveTexture(_normalActiveTexture);
789 glBindTexture(GL_TEXTURE_2D, pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::NORMAL].texHandle );
791 if (pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::HEIGHT].texHandle != 0) {
793 glActiveTexture(_heightActiveTexture);
794 glBindTexture(GL_TEXTURE_2D, pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::HEIGHT].texHandle );
797 if (pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::DIFFUSE].texHandle != 0) {
799 glActiveTexture(_diffuseActiveTexture);
800 glBindTexture(GL_TEXTURE_2D, pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::DIFFUSE].texHandle );
803 if (_diffuseActiveTexture != GL_TEXTURE0) {
805 glActiveTexture(GL_TEXTURE0);
809 glBindVertexArray(_vao );
810 glDrawElements(GL_TRIANGLES, pMESH->numTriangles * 3, GL_UNSIGNED_INT, (
void*)
nullptr );
816 const GLuint vPosAttribLoc,
817 const GLuint vColorAttribLoc,
818 const GLuint vTexCoordAttribLoc,
819 const GLuint vNormalAttribLoc,
820 const GLuint vTangentAttribLoc
825 _vertexArray =
new glm::vec3[_maxVertices];
826 _normalArray =
new glm::vec3[_maxVertices];
827 _tangentArray =
new glm::vec4[_maxVertices];
828 _texelArray =
new glm::vec2[_maxVertices];
829 _vertexIndicesArray =
new GLuint[_maxTriangles * 3];
831 glGenVertexArrays( 1, &_vao );
832 glBindVertexArray(_vao );
834 glGenBuffers(2, _vbo );
835 glBindBuffer(GL_ARRAY_BUFFER, _vbo[0] );
836 glBufferData(GL_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _maxVertices * 2 +
static_cast<GLsizeiptr
>(
sizeof(glm::vec4)) * _maxVertices +
static_cast<GLsizeiptr
>(
sizeof(glm::vec2)) * _maxVertices,
nullptr, GL_DYNAMIC_DRAW );
838 glEnableVertexAttribArray( vPosAttribLoc );
839 glVertexAttribPointer( vPosAttribLoc, 3, GL_FLOAT, GL_FALSE, 0,
static_cast<void *
>(
nullptr) );
841 if (vNormalAttribLoc != 0) {
842 glEnableVertexAttribArray( vNormalAttribLoc );
843 glVertexAttribPointer( vNormalAttribLoc, 3, GL_FLOAT, GL_FALSE, 0,
reinterpret_cast<void *
>(
sizeof(glm::vec3) * _maxVertices * 1) );
846 if (vTangentAttribLoc != 0) {
847 glEnableVertexAttribArray( vTangentAttribLoc );
848 glVertexAttribPointer( vTangentAttribLoc, 4, GL_FLOAT, GL_FALSE, 0,
reinterpret_cast<void *
>(
sizeof(glm::vec3) * _maxVertices * 2) );
851 if (vTexCoordAttribLoc != 0) {
852 glEnableVertexAttribArray( vTexCoordAttribLoc );
853 glVertexAttribPointer( vTexCoordAttribLoc, 2, GL_FLOAT, GL_FALSE, 0,
reinterpret_cast<void *
>(
sizeof(glm::vec3) * _maxVertices * 2 +
sizeof(glm::vec4) * _maxVertices) );
856 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo[1] );
857 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(GLuint)) * _maxTriangles * 3,
nullptr, GL_DYNAMIC_DRAW );
859 printf(
"[.md5mesh]: Model VAO/VBO/IBO registered at %u/%u/%u\n", _vao, _vbo[0], _vbo[1] );
861 glGenVertexArrays( 1, &_skeletonVAO );
862 glBindVertexArray(_skeletonVAO );
864 glGenBuffers( 1, &_skeletonVBO );
865 glBindBuffer(GL_ARRAY_BUFFER, _skeletonVBO );
866 glBufferData(GL_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints * 3 * 2,
nullptr, GL_DYNAMIC_DRAW );
868 glEnableVertexAttribArray( vPosAttribLoc );
869 glVertexAttribPointer( vPosAttribLoc, 3, GL_FLOAT, GL_FALSE, 0,
static_cast<void *
>(
nullptr) );
871 if (vColorAttribLoc != 0) {
872 glEnableVertexAttribArray( vColorAttribLoc );
873 glVertexAttribPointer( vColorAttribLoc, 3, GL_FLOAT, GL_FALSE, 0,
reinterpret_cast<void *
>(
sizeof(glm::vec3) * _numJoints * 3) );
876 printf(
"[.md5mesh]: Skeleton VAO/VBO registered at %u/%u\n", _skeletonVAO, _skeletonVBO );
881 const GLint diffuseMapActiveTexture = GL_TEXTURE0,
882 const GLint specularMapActiveTexture = GL_TEXTURE1,
883 const GLint normalMapActiveTexture = GL_TEXTURE2,
884 const GLint heightMapActiveTexture = GL_TEXTURE3
886 _diffuseActiveTexture = diffuseMapActiveTexture;
887 _specularActiveTexture = specularMapActiveTexture;
888 _normalActiveTexture = normalMapActiveTexture;
889 _heightActiveTexture = heightMapActiveTexture;
893CSCI441::MD5Model::_freeVertexArrays()
895 delete[] _vertexArray;
896 _vertexArray =
nullptr;
898 delete[] _normalArray;
899 _normalArray =
nullptr;
901 delete[] _tangentArray;
902 _tangentArray =
nullptr;
904 delete[] _vertexIndicesArray;
905 _vertexIndicesArray =
nullptr;
907 delete[] _texelArray;
908 _texelArray =
nullptr;
910 glDeleteVertexArrays( 1, &_vao );
913 glDeleteBuffers(2, _vbo );
917 glDeleteVertexArrays( 1, &_skeletonVAO );
920 glDeleteBuffers( 1, &_skeletonVBO );
928 glBindVertexArray(_skeletonVAO );
929 glBindBuffer(GL_ARRAY_BUFFER, _skeletonVBO );
931 constexpr glm::vec3 jointColor = {1.0f, 1.0f, 0.0f };
932 constexpr glm::vec3 boneColor = {1.0f, 0.0f, 1.0f };
935 for(GLint i = 0; i < _numJoints; ++i ) {
936 glBufferSubData(GL_ARRAY_BUFFER, i *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)),
sizeof(glm::vec3), &(_skeleton[i].position) );
937 glBufferSubData(GL_ARRAY_BUFFER, i *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) +
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints * 3,
sizeof(glm::vec3), &jointColor[0]);
942 for(GLint i = 0; i < _numJoints; ++i ) {
943 if( _skeleton[i].parent != CSCI441_INTERNAL::MD5Joint::NULL_JOINT ) {
946 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints + (i * 2) *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)),
947 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)),
948 &(_skeleton[_skeleton[i].parent].position)
952 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints + (i * 2) *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) +
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints * 3,
959 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints + (i * 2) *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) +
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)),
960 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)),
961 &(_skeleton[i].position)
965 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints + (i * 2) *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) +
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) +
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints * 3,
974 glDrawArrays(GL_POINTS, 0, _numJoints );
978 glDrawArrays(GL_LINES, _numJoints, numBones * 2 );
983CSCI441::MD5Model::_checkAnimValidity(
const GLushort targetAnimationIndex)
const
986 if( _numJoints != _animations[targetAnimationIndex]->getNumberOfJoints() ) {
987 fprintf(stderr,
"[.md5anim | ERROR]: skeleton and animation do not have same number of joints. cannot apply animation %u to skeleton\n", targetAnimationIndex);
990 if (_animations[targetAnimationIndex]->getNumberOfJoints() == 0 ) {
991 fprintf(stderr,
"[.md5anim | ERROR]: animation has zero joints. cannot apply animation %u to skeleton\n\n", targetAnimationIndex);
996 for(GLint i = 0; i < _numJoints; ++i) {
998 if (_baseSkeleton[i].parent != _animations[targetAnimationIndex]->getSkeletonFrameJoint(0, i).parent) {
999 fprintf(stderr,
"[.md5anim | ERROR]: skeleton and animation joints do not have same parent index. cannot apply animation %u to skeleton\n", targetAnimationIndex);
1004 if (strcmp (_baseSkeleton[i].name, _animations[targetAnimationIndex]->getSkeletonFrameJoint(0, i).name) != 0) {
1005 fprintf(stderr,
"[.md5anim | ERROR]: skeleton and animation joints do not have same name. cannot apply animation %u to skeleton\n", targetAnimationIndex);
1010 fprintf(stdout,
"[.md5anim]: skeleton and animation match. animation %u can be applied to skeleton\n", targetAnimationIndex);
1015CSCI441::MD5Model::_buildFrameSkeleton(
1016 const CSCI441_INTERNAL::MD5JointInfo* pJOINT_INFOS,
1017 const CSCI441_INTERNAL::MD5BaseFrameJoint* pBASE_FRAME,
1018 const GLfloat* pANIM_FRAME_DATA,
1019 const CSCI441_INTERNAL::MD5Joint* pSkeletonFrame,
1020 const GLint NUM_JOINTS
1022 if(pJOINT_INFOS ==
nullptr
1023 || pBASE_FRAME ==
nullptr
1024 || pANIM_FRAME_DATA ==
nullptr
1025 || pSkeletonFrame ==
nullptr)
return;
1027 for(GLint i = 0; i < NUM_JOINTS; ++i) {
1028 const CSCI441_INTERNAL::MD5BaseFrameJoint *baseJoint = &pBASE_FRAME[i];
1029 glm::vec3 animatedPosition = baseJoint->position;
1030 glm::quat animatedOrientation = baseJoint->orientation;
1034 if(pJOINT_INFOS[i].flags & 1 ) {
1035 animatedPosition.x = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1040 if(pJOINT_INFOS[i].flags & 2 ) {
1041 animatedPosition.y = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1046 if(pJOINT_INFOS[i].flags & 4 ) {
1047 animatedPosition.z = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1052 if(pJOINT_INFOS[i].flags & 8 ) {
1053 animatedOrientation.x = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1058 if(pJOINT_INFOS[i].flags & 16 ) {
1059 animatedOrientation.y = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1064 if(pJOINT_INFOS[i].flags & 32 ) {
1065 animatedOrientation.z = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1069 animatedOrientation.w = glm::extractRealComponent(animatedOrientation);
1074 const auto thisJoint =
const_cast<CSCI441_INTERNAL::MD5Joint *
>(&pSkeletonFrame[i]);
1076 const GLint parent = pJOINT_INFOS[i].parent;
1077 thisJoint->parent = parent;
1078 strcpy (thisJoint->name, pJOINT_INFOS[i].name);
1081 if( thisJoint->parent == CSCI441_INTERNAL::MD5Joint::NULL_JOINT ) {
1082 thisJoint->position = animatedPosition;
1083 thisJoint->orientation = animatedOrientation;
1085 const CSCI441_INTERNAL::MD5Joint *parentJoint = &pSkeletonFrame[parent];
1086 glm::vec3 rotatedPosition = glm::rotate(parentJoint->orientation, glm::vec4(animatedPosition, 0.0f));
1089 thisJoint->position = parentJoint->position + rotatedPosition;
1092 thisJoint->orientation = glm::normalize( glm::cross(parentJoint->orientation, animatedOrientation) );
1099 const char *filename,
1100 const GLushort targetAnimationIndex
1102 if (targetAnimationIndex >= _numAnimations) {
1103 fprintf (stderr,
"[.md5anim]: Error: target animation index %u is out of range for currently allocated animations (which is %u)\n", targetAnimationIndex, _numAnimations);
1104 fprintf (stderr,
"[.md5anim]: Hey Developer, if you wish to add an animation to the sequence, use CSCI441::MD5Model::addMD5Anim(const char*)\n");
1109 CSCI441_INTERNAL::MD5JointInfo *jointInfos =
nullptr;
1110 CSCI441_INTERNAL::MD5BaseFrameJoint *baseFrame =
nullptr;
1111 GLfloat *animFrameData =
nullptr;
1113 GLint numAnimatedComponents;
1114 GLint frameIndex, numFrames, numJoints;
1117 printf(
"[.md5anim]: about to read %s into animation %u\n", filename, targetAnimationIndex );
1119 FILE *fp = fopen( filename,
"rb" );
1121 fprintf (stderr,
"[.md5anim]: Error: couldn't open \"%s\"!\n", filename);
1125 while( !feof(fp) ) {
1127 fgets( buff,
sizeof(buff), fp );
1129 if( sscanf(buff,
" MD5Version %d", &version) == 1 ) {
1130 if( version != 10 ) {
1132 fprintf (stderr,
"[.md5anim]: Error: bad animation version\n");
1136 }
else if( sscanf(buff,
" numFrames %d", &numFrames) == 1 ) {
1138 _animations[targetAnimationIndex]->setNumberOfFrames(numFrames);
1139 }
else if( sscanf(buff,
" numJoints %d", &numJoints) == 1 ) {
1140 if (jointInfos !=
nullptr) {
1141 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numJoints already specified\n" );
1143 if( numJoints > 0 ) {
1144 _animations[targetAnimationIndex]->setNumberOfJoints(numJoints);
1147 jointInfos =
new CSCI441_INTERNAL::MD5JointInfo[numJoints];
1148 baseFrame =
new CSCI441_INTERNAL::MD5BaseFrameJoint[numJoints];
1150 }
else if( sscanf(buff,
" frameRate %d", &_animations[targetAnimationIndex]->frameRate) == 1 ) {
1152 }
else if( sscanf(buff,
" numAnimatedComponents %d", &numAnimatedComponents) == 1 ) {
1153 if (animFrameData !=
nullptr) {
1154 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numAnimatedComponents already specified\n" );
1156 if( numAnimatedComponents > 0 ) {
1158 animFrameData =
new GLfloat[numAnimatedComponents];
1160 }
else if( strncmp(buff,
"hierarchy {", 11) == 0 ) {
1161 if (jointInfos ==
nullptr) {
1162 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numJoints not specified prior to hierarchy\n" );
1164 for(i = 0; i < numJoints; ++i) {
1166 fgets( buff,
sizeof(buff), fp );
1169 sscanf(buff,
" %s %d %d %d",
1170 jointInfos[i].name, &jointInfos[i].parent,
1171 &jointInfos[i].flags, &jointInfos[i].startIndex);
1174 }
else if( strncmp(buff,
"bounds {", 8) == 0 ) {
1175 if (_animations[targetAnimationIndex]->getNumberOfFrames() == 0) {
1176 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numFrames not specified prior to bounds\n" );
1178 for(i = 0; i < _animations[targetAnimationIndex]->getNumberOfFrames(); ++i) {
1180 fgets( buff,
sizeof(buff), fp );
1183 sscanf(buff,
" ( %f %f %f ) ( %f %f %f )",
1184 &_animations[targetAnimationIndex]->getBoundingBox(i).min[0], &_animations[targetAnimationIndex]->getBoundingBox(i).min[1], &_animations[targetAnimationIndex]->getBoundingBox(i).min[2],
1185 &_animations[targetAnimationIndex]->getBoundingBox(i).max[0], &_animations[targetAnimationIndex]->getBoundingBox(i).max[1], &_animations[targetAnimationIndex]->getBoundingBox(i).max[2]);
1188 }
else if( strncmp(buff,
"baseframe {", 10) == 0 ) {
1189 if (baseFrame ==
nullptr) {
1190 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numJoints not specified prior to baseframe\n" );
1192 for(i = 0; i < numJoints; ++i) {
1194 fgets( buff,
sizeof(buff), fp );
1197 if( sscanf(buff,
" ( %f %f %f ) ( %f %f %f )",
1198 &baseFrame[i].position[0], &baseFrame[i].position[1], &baseFrame[i].position[2],
1199 &baseFrame[i].orientation[0], &baseFrame[i].orientation[1], &baseFrame[i].orientation[2]) == 6 ) {
1201 baseFrame[i].orientation.w = glm::extractRealComponent(baseFrame[i].orientation);
1205 }
else if(sscanf(buff,
" frame %d", &frameIndex) == 1 ) {
1206 if (animFrameData ==
nullptr) {
1207 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numAnimatedComponents not specified prior to frame\n" );
1208 }
else if (_animations[targetAnimationIndex]->getNumberOfFrames() == 0) {
1209 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numFrames not specified prior to frame\n" );
1210 }
else if (baseFrame ==
nullptr) {
1211 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. baseframe not specified prior to frame\n" );
1212 }
else if (jointInfos ==
nullptr) {
1213 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numJoints not specified prior to frame\n" );
1216 for(i = 0; i < numAnimatedComponents; ++i)
1217 fscanf( fp,
"%f", &animFrameData[i] );
1220 _buildFrameSkeleton(jointInfos, baseFrame, animFrameData,
1221 _animations[targetAnimationIndex]->getSkeletonFrame(frameIndex),
1229 printf(
"[.md5anim]: finished reading %s into animation %u\n", filename, targetAnimationIndex );
1230 printf(
"[.md5anim]: read in %d frames of %d joints with %d animated components\n", _animations[targetAnimationIndex]->getNumberOfFrames(), _animations[targetAnimationIndex]->getNumberOfJoints(), numAnimatedComponents );
1231 printf(
"[.md5anim]: animation's frame rate is %d\n", _animations[targetAnimationIndex]->frameRate );
1234 delete[] animFrameData;
1236 delete[] jointInfos;
1239 _animationInfos[targetAnimationIndex].currFrame = 0;
1240 _animationInfos[targetAnimationIndex].nextFrame = 1;
1242 _animationInfos[targetAnimationIndex].lastTime = 0.0f;
1243 _animationInfos[targetAnimationIndex].maxTime = 1.0f /
static_cast<GLfloat
>(_animations[targetAnimationIndex]->frameRate);
1246 if (_animations[targetAnimationIndex]->getNumberOfJoints() == 0) {
1247 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numJoints never specified\n" );
1249 _skeleton =
new CSCI441_INTERNAL::MD5Joint[_animations[targetAnimationIndex]->getNumberOfJoints()];
1252 if( _checkAnimValidity(targetAnimationIndex) ) {
1263 const char* filename
1266 auto newAnimations =
new CSCI441_INTERNAL::MD5Animation*[_numAnimations + 1];
1267 auto newAnimationInfos =
new CSCI441_INTERNAL::MD5AnimationState[_numAnimations + 1];
1269 for(
int i = 0; i < _numAnimations; ++i ) {
1270 newAnimations[i] = _animations[i];
1271 newAnimationInfos[i] = _animationInfos[i];
1273 newAnimations[_numAnimations] =
new CSCI441_INTERNAL::MD5Animation();
1275 delete[] _animations;
1276 delete[] _animationInfos;
1278 _animations = newAnimations;
1279 _animationInfos = newAnimationInfos;
1284 fprintf( stdout,
"\n[.md5anim]: preparing to read %s into new animation %u\n", filename, (_numAnimations-1) );
1285 const bool readSuccess = readMD5Anim(filename, _numAnimations - 1);
1288 fprintf( stdout,
"[.md5anim]: successfully read %s into new animation %u\n", filename, (_numAnimations-1) );
1290 fprintf( stderr,
"[.md5anim]: Error: could not read %s into new animation %u\n", filename, (_numAnimations-1) );
1293 newAnimations =
new CSCI441_INTERNAL::MD5Animation*[_numAnimations-1];
1294 newAnimationInfos =
new CSCI441_INTERNAL::MD5AnimationState[_numAnimations-1];
1296 for (
int i = 0; i < _numAnimations - 1; ++i) {
1297 newAnimations[i] = _animations[i];
1298 newAnimationInfos[i] = _animationInfos[i];
1300 delete _animations[_numAnimations-1];
1302 delete[] _animations;
1303 delete[] _animationInfos;
1305 _animations = newAnimations;
1306 _animationInfos = newAnimationInfos;
1316 const GLushort targetAnimationIndex
1319 if (targetAnimationIndex < _numAnimations) {
1321 _currentAnimationIndex = targetAnimationIndex;
1324 _animationInfos[targetAnimationIndex].currFrame = 0;
1325 _animationInfos[targetAnimationIndex].nextFrame = 1;
1327 _animationInfos[targetAnimationIndex].lastTime = 0.0f;
1328 _animationInfos[targetAnimationIndex].maxTime = 1.0f /
static_cast<GLfloat
>(_animations[targetAnimationIndex]->frameRate);
1335CSCI441::MD5Model::_freeAnim()
1338 _skeleton =
nullptr;
1340 for (
int i = 0; i < _numAnimations; i++) {
1341 delete _animations[i];
1343 delete[] _animations;
1344 _animations =
nullptr;
1346 delete[] _animationInfos;
1347 _animationInfos =
nullptr;
1350inline void CSCI441::MD5Model::_moveFromSrc(MD5Model &src) {
1351 this->_baseSkeleton = src._baseSkeleton;
1352 src._baseSkeleton =
nullptr;
1354 this->_meshes = src._meshes;
1355 src._meshes =
nullptr;
1357 this->_maxVertices = src._maxVertices;
1358 src._maxVertices = 0;
1360 this->_maxTriangles = src._maxTriangles;
1361 src._maxTriangles = 0;
1363 this->_vertexArray = src._vertexArray;
1364 src._vertexArray =
nullptr;
1366 this->_normalArray = src._normalArray;
1367 src._normalArray =
nullptr;
1369 this->_tangentArray = src._tangentArray;
1370 src._tangentArray =
nullptr;
1372 this->_texelArray = src._texelArray;
1373 src._texelArray =
nullptr;
1375 this->_vertexIndicesArray = src._vertexIndicesArray;
1376 src._vertexIndicesArray =
nullptr;
1378 this->_vao = src._vao;
1381 this->_vbo[0] = src._vbo[0];
1382 this->_vbo[1] = src._vbo[1];
1386 this->_skeletonVAO = src._skeletonVAO;
1387 src._skeletonVAO = 0;
1389 this->_skeletonVBO = src._skeletonVBO;
1390 src._skeletonVBO = 0;
1392 this->_skeleton = src._skeleton;
1393 src._skeleton =
nullptr;
1395 this->_animations = src._animations;
1396 src._animations =
nullptr;
1398 this->_numAnimations = src._numAnimations;
1399 src._numAnimations = 0;
1401 this->_currentAnimationIndex = src._currentAnimationIndex;
1402 src._currentAnimationIndex = 0;
1404 this->_isAnimated = src._isAnimated;
1405 src._isAnimated =
false;
1407 this->_animationInfos = src._animationInfos;
1408 src._animationInfos =
nullptr;
1412CSCI441::MD5Model::_interpolateSkeletons(
const GLfloat interp)
1414 const CSCI441_INTERNAL::MD5Joint *skeletonA = _animations[_currentAnimationIndex]->getSkeletonFrame(_animationInfos[_currentAnimationIndex].currFrame);
1415 const CSCI441_INTERNAL::MD5Joint *skeletonB = _animations[_currentAnimationIndex]->getSkeletonFrame(_animationInfos[_currentAnimationIndex].nextFrame);
1417 for(GLint i = 0; i < _animations[_currentAnimationIndex]->getNumberOfJoints(); ++i) {
1419 _skeleton[i].parent = skeletonA[i].parent;
1422 _skeleton[i].position = glm::mix(skeletonA[i].position, skeletonB[i].position, interp);
1425 _skeleton[i].orientation = glm::slerp(skeletonA[i].orientation, skeletonB[i].orientation, interp);
1434 const GLint maxFrames = _animations[_currentAnimationIndex]->getNumberOfFrames() - 1;
1435 if (maxFrames <= 0)
return;
1437 _animationInfos[_currentAnimationIndex].lastTime += dt;
1440 if( _animationInfos[_currentAnimationIndex].lastTime >= _animationInfos[_currentAnimationIndex].maxTime ) {
1441 _animationInfos[_currentAnimationIndex].currFrame++;
1442 _animationInfos[_currentAnimationIndex].nextFrame++;
1443 _animationInfos[_currentAnimationIndex].lastTime = 0.0;
1445 if( _animationInfos[_currentAnimationIndex].currFrame > maxFrames )
1446 _animationInfos[_currentAnimationIndex].currFrame = 0;
1448 if( _animationInfos[_currentAnimationIndex].nextFrame > maxFrames )
1449 _animationInfos[_currentAnimationIndex].nextFrame = 0;
1453 _interpolateSkeletons( _animationInfos[_currentAnimationIndex].lastTime *
static_cast<GLfloat
>(_animations[_currentAnimationIndex]->frameRate) );
1456inline std::map< std::string, CSCI441_INTERNAL::MD5MaterialShader* > CSCI441::MD5Model::_materials;
1457inline std::map< std::string, GLuint > CSCI441::MD5Model::_textureMap;
1460inline size_t CSCI441::MD5Model::_trim(
char* out,
const size_t len,
const char* str) {
1465 while( isspace(
static_cast<unsigned char>(*str)) ) str++;
1474 const char *end = str + strlen(str) - 1;
1475 while( end > str && isspace(
static_cast<unsigned char>(*end)) ) end--;
1479 const size_t out_size = (end - str) < len - 1 ? (end - str) : len - 1;
1482 memcpy(out, str, out_size);
1483 out[out_size] =
'\0';
1488inline bool CSCI441::MD5Model::_registerShaderTexture(CSCI441_INTERNAL::MD5Texture *texture) {
1489 if (
const auto textureIter = _textureMap.find( texture->filename ); textureIter == _textureMap.end()) {
1491 if( texture->texHandle == 0 ) {
1496 _textureMap.insert( std::pair( texture->filename, texture->texHandle) );
1500 texture->texHandle = textureIter->second;
1507 char buff[512], buff2[512];
1508 GLushort numTextures = 0;
1510 fprintf(stdout,
"\n[.md5mtr]: about to read %s\n", FILENAME );
1512 std::string path = PATH;
1513 if (path.back() !=
'/') path +=
"/";
1514 const std::string filename = path + FILENAME;
1515 FILE *fp = fopen(filename.c_str(),
"rb" );
1517 fprintf (stderr,
"[.md5mtr]: Error: couldn't open \"%s\"!\n", filename.c_str());
1521 while( !feof(fp) ) {
1523 fgets( buff,
sizeof(buff), fp );
1525 _trim(buff,
sizeof(buff), buff);
1527 if( sscanf(buff,
"table %s", buff2) == 1 ) {
1529 }
else if ( sscanf(buff,
"//%s", buff2) == 1 ) {
1531 }
else if (strnlen(buff,
sizeof(buff)) == 0) {
1535 auto shader =
new CSCI441_INTERNAL::MD5MaterialShader();
1537 strncpy(shader->name, buff, CSCI441_INTERNAL::MD5MaterialShader::MAX_NAME_LENGTH);
1538 shader->name[ strlen(buff) ] =
'\0';
1541 unsigned short numBlocks = 0;
1544 while ( fgets( buff,
sizeof(buff), fp ) !=
nullptr) {
1545 _trim(buff,
sizeof(buff), buff);
1547 if (strchr(buff,
'{') !=
nullptr) {
1551 else if (sscanf(buff,
" diffusemap %s", buff2) == 1) {
1553 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::DIFFUSE].filename, path.c_str(), CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1554 shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::DIFFUSE].filename[ path.length() ] =
'\0';
1555 strncat( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::DIFFUSE].filename, buff2, CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1556 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::DIFFUSE] )) {
1560 else if (sscanf(buff,
" specularmap %s", buff2) == 1) {
1562 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::SPECULAR].filename, path.c_str(), CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1563 shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::SPECULAR].filename[ path.length() ] =
'\0';
1564 strncat( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::SPECULAR].filename, buff2, CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1565 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::SPECULAR] )) {
1570 else if (numBlocks == 1 && sscanf(buff,
" bumpmap %s", buff2) == 1) {
1573 std::vector< char* > tokens;
1574 char* pch = strtok(buff,
" \t(),");
1575 while (pch != NULL) {
1576 tokens.push_back( pch );
1577 pch = strtok(NULL,
" \t(),");
1582 if (tokens.size() == 2) {
1583 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL].filename, path.c_str(), CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1584 shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL].filename[ path.length() ] =
'\0';
1585 strncat( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL].filename, tokens[1], CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1586 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL] )) {
1593 else if (tokens.size() == 4) {
1594 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT].filename, path.c_str(), CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1595 shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT].filename[ path.length() ] =
'\0';
1596 strncat( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT].filename, tokens[2], CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1597 shader->displacementScale = strtol(tokens[3], NULL, 10);
1598 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT] )) {
1606 else if (tokens.size() == 6) {
1607 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL].filename, path.c_str(), CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1608 shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL].filename[ path.length() ] =
'\0';
1609 strncat( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL].filename, tokens[2], CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1610 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL] )) {
1613 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT].filename, path.c_str(), CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1614 shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT].filename[ path.length() ] =
'\0';
1615 strncat( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT].filename, tokens[4], CSCI441_INTERNAL::MD5Texture::MAX_NAME_LENGTH );
1616 shader->displacementScale = strtol(tokens[5], NULL, 10);
1617 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT] )) {
1622 else if (strchr(buff,
'}') !=
nullptr) {
1625 if (numBlocks == 0) {
1631 _materials.insert( std::pair(shader->name, shader) );
1636 fprintf(stdout,
"[.md5mtr]: finished reading %s\n", FILENAME );
1637 fprintf(stdout,
"[.md5mtr]: read in %lu shaders and %u textures\n\n", _materials.size(), numTextures );
1642 for (
auto &[name, material] : _materials) {
1643 glDeleteTextures(1, &material->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::DIFFUSE].texHandle);
1644 glDeleteTextures(1, &material->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::SPECULAR].texHandle);
1645 glDeleteTextures(1, &material->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT].texHandle);
1646 glDeleteTextures(1, &material->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL].texHandle);
Doom3 MD5 Model + Animation type implementations.
Helper functions to work with OpenGL Textures.
GLuint loadAndRegisterTexture(const char *filename, GLint minFilter=GL_LINEAR, GLint magFilter=GL_LINEAR, GLint wrapS=GL_REPEAT, GLint wrapT=GL_REPEAT, GLboolean flipOnY=GL_TRUE, GLboolean printAllMessages=GL_TRUE, GLboolean enableMipmaps=GL_TRUE, GLboolean enableAniso=GL_TRUE)
loads and registers a texture into memory returning a texture handle
Definition: TextureUtils.hpp:171
stores a Doom3 MD5 Mesh + Animation
Definition: MD5Model.hpp:77
bool addMD5Anim(const char *filename)
adds another MD5 Animation sequence to the model's set of animation
Definition: MD5Model.hpp:1262
void draw() const
draws all the meshes that make up the model
Definition: MD5Model.hpp:656
GLushort getNumberOfAnimations() const
returns the number of animations that were successfully loaded against the model
Definition: MD5Model.hpp:194
bool readMD5Model(const char *FILENAME)
parses md5mesh file and allocates corresponding mesh data
Definition: MD5Model.hpp:466
bool readMD5Anim(const char *filename, GLushort targetAnimationIndex=0)
reads in an animation sequence from an external file
Definition: MD5Model.hpp:1098
bool isAnimated() const
returns if the MD5 Model has an accompanying animation
Definition: MD5Model.hpp:134
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:442
void setActiveTextures(GLint diffuseMapActiveTexture, GLint specularMapActiveTexture, GLint normalMapActiveTexture, GLint heightMapActiveTexture)
specify which texture targets each texture map should be bound to when rendering
Definition: MD5Model.hpp:880
MD5Model()
initializes an empty MD5 Model
Definition: MD5Model.hpp:83
static void readMD5Material(const char *FILENAME, const char *PATH="./")
loads textures corresponding to MD5 Shaders
Definition: MD5Model.hpp:1506
MD5Model & operator=(const MD5Model &)=delete
do not allow MD5 models to be copied
void allocVertexArrays(GLuint vPosAttribLoc, GLuint vColorAttribLoc, GLuint vTexCoordAttribLoc, GLuint vNormalAttribLoc=0, GLuint vTangentAttribLoc=0)
binds model VBOs to attribute pointer locations
Definition: MD5Model.hpp:815
void animate(GLfloat dt)
advances the model forward in its animation sequence the corresponding amount of time based on frame ...
Definition: MD5Model.hpp:1432
MD5Model & operator=(MD5Model &&src) noexcept
do not allow MD5 models to be moved
Definition: MD5Model.hpp:115
void useTargetAnimationIndex(GLushort targetAnimationIndex)
update current animation to be running through
Definition: MD5Model.hpp:1315
MD5Model(const MD5Model &)=delete
do not allow MD5 models to be copied
MD5Model(MD5Model &&src) noexcept
do not allow MD5 models to be moved
Definition: MD5Model.hpp:109
~MD5Model()
deallocates any used memory on the CPU and GPU
Definition: MD5Model.hpp:432
static void releaseMD5Materials()
deletes textures from GPU that were registered during parsing of *.mtr file
Definition: MD5Model.hpp:1641
void drawSkeleton() const
draws the skeleton joints (as points) and bones (as lines)
Definition: MD5Model.hpp:926
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17