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>
86 _animations =
new CSCI441_INTERNAL::MD5Animation*[_numAnimations];
87 _animations[0] =
new CSCI441_INTERNAL::MD5Animation();
89 _animationInfos =
new CSCI441_INTERNAL::MD5AnimationState[_numAnimations];
127 [[maybe_unused]]
bool loadMD5Model(
const char* MD5_MESH_FILE,
const char* MD5_ANIM_FILE =
"");
133 [[nodiscard]]
bool isAnimated()
const {
return _isAnimated; }
141 [[nodiscard]]
bool readMD5Model(
const char* FILENAME);
153 [[maybe_unused]]
void allocVertexArrays(GLuint vPosAttribLoc, GLuint vColorAttribLoc, GLuint vTexCoordAttribLoc, GLuint vNormalAttribLoc = 0, GLuint vTangentAttribLoc = 0);
162 [[maybe_unused]]
void setActiveTextures(GLint diffuseMapActiveTexture, GLint specularMapActiveTexture, GLint normalMapActiveTexture, GLint heightMapActiveTexture);
167 [[maybe_unused]]
void draw()
const;
171 [[maybe_unused]]
void drawSkeleton()
const;
180 [[nodiscard]]
bool readMD5Anim(
const char* filename, GLushort targetAnimationIndex = 0);
186 [[nodiscard]]
bool addMD5Anim(
const char* filename);
195 if (!_isAnimated)
return 0;
197 return _numAnimations;
204 void useTargetAnimationIndex(GLushort targetAnimationIndex);
209 void animate(GLfloat dt);
217 static void readMD5Material(
const char* FILENAME);
221 static void releaseMD5Materials();
228 CSCI441_INTERNAL::MD5Joint* _baseSkeleton =
nullptr;
233 CSCI441_INTERNAL::MD5Mesh* _meshes =
nullptr;
238 GLint _numJoints = 0;
243 GLint _numMeshes = 0;
250 GLint _maxVertices = 0;
255 GLint _maxTriangles = 0;
260 glm::vec3* _vertexArray =
nullptr;
265 glm::vec3* _normalArray =
nullptr;
271 glm::vec4* _tangentArray =
nullptr;
276 glm::vec2* _texelArray =
nullptr;
281 GLuint* _vertexIndicesArray =
nullptr;
291 GLuint _vbo[2] = {0, 0};
297 GLuint _skeletonVAO = 0;
301 GLuint _skeletonVBO = 0;
305 CSCI441_INTERNAL::MD5Joint* _skeleton =
nullptr;
311 CSCI441_INTERNAL::MD5Animation** _animations =
nullptr;
315 GLushort _numAnimations = 0;
319 GLushort _currentAnimationIndex = 0;
323 bool _isAnimated =
false;
327 CSCI441_INTERNAL::MD5AnimationState* _animationInfos =
nullptr;
330 static std::map< std::string, CSCI441_INTERNAL::MD5MaterialShader* > _materials;
331 static std::map< std::string, GLuint > _textureMap;
336 GLint _diffuseActiveTexture = GL_TEXTURE0;
340 GLint _specularActiveTexture = GL_TEXTURE1;
344 GLint _normalActiveTexture = GL_TEXTURE2;
348 GLint _heightActiveTexture = GL_TEXTURE3;
356 void _prepareMesh(
const CSCI441_INTERNAL::MD5Mesh* pMESH)
const;
361 auto _drawMesh(
const CSCI441_INTERNAL::MD5Mesh* pMESH)
const -> void;
368 [[nodiscard]]
bool _checkAnimValidity(GLushort targetAnimationIndex)
const;
377 static void _buildFrameSkeleton(
const CSCI441_INTERNAL::MD5JointInfo* pJOINT_INFOS,
378 const CSCI441_INTERNAL::MD5BaseFrameJoint* pBASE_FRAME,
379 const GLfloat* pANIM_FRAME_DATA,
380 const CSCI441_INTERNAL::MD5Joint* pSkeletonFrame,
386 void _interpolateSkeletons(GLfloat interp);
394 void _freeVertexArrays();
404 void _moveFromSrc(MD5Model &src);
414 static size_t _trim(
char* out,
size_t len,
const char* str);
423 static bool _registerShaderTexture( CSCI441_INTERNAL::MD5Texture* texture );
441 const char* MD5_MESH_FILE,
442 const char* MD5_ANIM_FILE
445 if( readMD5Model(MD5_MESH_FILE) ) {
447 if(strcmp(MD5_ANIM_FILE,
"") != 0 ) {
449 if( !readMD5Anim(MD5_ANIM_FILE) ) {
453 if( !isAnimated() ) {
454 printf (
"[.MD5_ANIM_FILE]: no animation loaded.\n");
469 GLint currentMesh = 0;
471 GLint totalVertices = 0;
472 GLint totalWeights = 0;
473 GLint totalTriangles = 0;
475 GLfloat minX = 999999, minY = 999999, minZ = 999999;
476 GLfloat maxX = -999999, maxY = -999999, maxZ = -999999;
478 fprintf(stdout,
"[.md5mesh]: about to read %s\n", FILENAME );
480 FILE *fp = fopen(FILENAME,
"rb" );
482 fprintf (stderr,
"[.md5mesh]: Error: couldn't open \"%s\"!\n", FILENAME);
488 fgets( buff,
sizeof(buff), fp );
490 if( sscanf(buff,
" MD5Version %d", &version) == 1 ) {
491 if( version != 10 ) {
493 fprintf (stderr,
"[.md5mesh]: Error: bad model version\n");
497 }
else if( sscanf(buff,
" numJoints %d", &_numJoints) == 1 ) {
498 if( _numJoints > 0 ) {
500 _baseSkeleton =
new CSCI441_INTERNAL::MD5Joint[_numJoints];
502 }
else if( sscanf(buff,
" numMeshes %d", &_numMeshes) == 1 ) {
503 if( _numMeshes > 0 ) {
505 _meshes =
new CSCI441_INTERNAL::MD5Mesh[_numMeshes];
507 }
else if( strncmp(buff,
"joints {", 8) == 0 ) {
509 for(GLint i = 0; i < _numJoints; ++i) {
510 CSCI441_INTERNAL::MD5Joint *joint = &_baseSkeleton[i];
513 fgets( buff,
sizeof(buff), fp );
515 if( sscanf(buff,
"%s %d ( %f %f %f ) ( %f %f %f )",
516 joint->name, &joint->parent,
517 &joint->position[0], &joint->position[1], &joint->position[2],
518 &joint->orientation[0],&joint->orientation[1], &joint->orientation[2]) == 8
521 joint->orientation.w = glm::extractRealComponent(joint->orientation);
524 }
else if( strncmp(buff,
"mesh {", 6) == 0 ) {
525 CSCI441_INTERNAL::MD5Mesh *mesh = &_meshes[currentMesh];
526 GLint vert_index = 0;
528 GLint weight_index = 0;
529 GLfloat floatData[4];
532 while( buff[0] !=
'}' && !feof(fp) ) {
534 fgets( buff,
sizeof(buff), fp );
536 if( strstr( buff,
"shader ") ) {
537 GLint quote = 0, j = 0;
539 char shaderName[512] =
"";
542 for(
unsigned long uli = 0; uli <
sizeof(buff) && (quote < 2); ++uli) {
543 if( buff[uli] ==
'\"' )
546 if( (quote == 1) && (buff[uli] !=
'\"') ) {
547 shaderName[j] = buff[uli];
553 auto materialIter = _materials.find(shaderName);
554 if (materialIter != _materials.end()) {
555 mesh->shader = materialIter->second;
557 fprintf(stderr,
"[.md5mesh | ERROR]: Could not find material shader \"%s\"\n", shaderName);
560 }
else if( sscanf(buff,
" numverts %d", &mesh->numVertices) == 1 ) {
561 if( mesh->numVertices > 0 ) {
563 mesh->vertices =
new CSCI441_INTERNAL::MD5Vertex[mesh->numVertices];
566 if( mesh->numVertices > _maxVertices )
567 _maxVertices = mesh->numVertices;
569 totalVertices += mesh->numVertices;
570 }
else if( sscanf(buff,
" numtris %d", &mesh->numTriangles) == 1 ) {
571 if( mesh->numTriangles > 0 ) {
573 mesh->triangles =
new CSCI441_INTERNAL::MD5Triangle[mesh->numTriangles];
576 if( mesh->numTriangles > _maxTriangles )
577 _maxTriangles = mesh->numTriangles;
579 totalTriangles += mesh->numTriangles;
580 }
else if( sscanf(buff,
" numweights %d", &mesh->numWeights) == 1 ) {
581 if( mesh->numWeights > 0 ) {
583 mesh->weights =
new CSCI441_INTERNAL::MD5Weight[mesh->numWeights];
586 totalWeights += mesh->numWeights;
587 }
else if( sscanf(buff,
" vert %d ( %f %f ) %d %d",
589 &floatData[0], &floatData[1],
590 &intData[0], &intData[1]) == 5
593 mesh->vertices[vert_index].texCoord.s = floatData[0];
594 mesh->vertices[vert_index].texCoord.t = floatData[1];
595 mesh->vertices[vert_index].start = intData[0];
596 mesh->vertices[vert_index].count = intData[1];
597 }
else if( sscanf(buff,
" tri %d %d %d %d",
599 &intData[0], &intData[1], &intData[2]) == 4
602 mesh->triangles[tri_index ].index[0] = intData[0];
603 mesh->triangles[tri_index ].index[1] = intData[1];
604 mesh->triangles[tri_index ].index[2] = intData[2];
605 }
else if( sscanf(buff,
" weight %d %d %f ( %f %f %f )",
606 &weight_index, &intData[0], &floatData[3],
607 &floatData[0], &floatData[1], &floatData[2]) == 6
610 mesh->weights[weight_index].joint = intData[0];
611 mesh->weights[weight_index].bias = floatData[3];
612 mesh->weights[weight_index].position[0] = floatData[0];
613 mesh->weights[weight_index].position[1] = floatData[1];
614 mesh->weights[weight_index].position[2] = floatData[2];
616 if( floatData[0] < minX ) { minX = floatData[0]; }
617 if( floatData[0] > maxX ) { maxX = floatData[0]; }
618 if( floatData[1] < minY ) { minY = floatData[1]; }
619 if( floatData[1] > maxY ) { maxY = floatData[1]; }
620 if( floatData[2] < minZ ) { minZ = floatData[2]; }
621 if( floatData[2] > maxZ ) { maxZ = floatData[2]; }
631 _skeleton = _baseSkeleton;
633 fprintf(stdout,
"[.md5mesh]: finished reading %s\n", FILENAME );
634 fprintf(stdout,
"[.md5mesh]: read in %d meshes, %d joints, %d vertices, %d weights, and %d triangles\n", _numMeshes, _numJoints, totalVertices, totalWeights, totalTriangles );
635 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) );
636 fprintf(stdout,
"\n" );
642CSCI441::MD5Model::_freeModel()
644 delete[] _baseSkeleton;
645 if (_baseSkeleton == _skeleton) _skeleton =
nullptr;
646 _baseSkeleton =
nullptr;
657 for(GLint i = 0; i < _numMeshes; ++i) {
658 CSCI441_INTERNAL::MD5Mesh& mesh = _meshes[i];
665CSCI441::MD5Model::_prepareMesh(
666 const CSCI441_INTERNAL::MD5Mesh *pMESH
671 for(k = 0, i = 0; i < pMESH->numTriangles; ++i) {
672 for(j = 0; j < 3; ++j, ++k)
673 _vertexIndicesArray[k] = pMESH->triangles[i].index[j];
677 auto normalAccum =
new glm::vec3[pMESH->numVertices];
678 auto tangentAccum =
new glm::vec3[pMESH->numVertices];
679 auto bitangentAccum =
new glm::vec3[pMESH->numVertices];
681 for(i = 0; i < pMESH->numVertices; ++i) {
682 normalAccum[i] = glm::vec3(0.0f);
683 tangentAccum[i] = glm::vec3(0.0f);
684 bitangentAccum[i] = glm::vec3(0.0f);
686 glm::vec3 finalVertex = {0.0f, 0.0f, 0.0f };
689 for(j = 0; j < pMESH->vertices[i].count; ++j) {
690 const CSCI441_INTERNAL::MD5Weight *weight = &pMESH->weights[pMESH->vertices[i].start + j];
691 const CSCI441_INTERNAL::MD5Joint *joint = &_skeleton[weight->joint];
694 const glm::vec3 weightedVertex = glm::rotate(joint->orientation, glm::vec4(weight->position, 0.0f));
697 finalVertex.x += (joint->position.x + weightedVertex.x) * weight->bias;
698 finalVertex.y += (joint->position.y + weightedVertex.y) * weight->bias;
699 finalVertex.z += (joint->position.z + weightedVertex.z) * weight->bias;
702 _vertexArray[i].x = finalVertex.x;
703 _vertexArray[i].y = finalVertex.y;
704 _vertexArray[i].z = finalVertex.z;
706 _texelArray[i].s = pMESH->vertices[i].texCoord.s;
707 _texelArray[i].t = pMESH->vertices[i].texCoord.t;
710 for(i = 0; i < pMESH->numTriangles; ++i) {
711 GLint idx0 = pMESH->triangles[i].index[0];
712 GLint idx1 = pMESH->triangles[i].index[1];
713 GLint idx2 = pMESH->triangles[i].index[2];
715 glm::vec3 v0 = _vertexArray[ idx0 ];
716 glm::vec3 v1 = _vertexArray[ idx1 ];
717 glm::vec3 v2 = _vertexArray[ idx2 ];
719 glm::vec2 uv0 = _texelArray[ idx0 ];
720 glm::vec2 uv1 = _texelArray[ idx1 ];
721 glm::vec2 uv2 = _texelArray[ idx2 ];
723 glm::vec3 edge1 = v1 - v0;
724 glm::vec3 edge2 = v2 - v0;
725 glm::vec2 deltaUV1 = uv1 - uv0;
726 glm::vec2 deltaUV2 = uv2 - uv0;
728 GLfloat f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
730 glm::vec3 normal = cross(edge1, edge2);
731 glm::vec3 tangent = f * (deltaUV2.y * edge1 - deltaUV1.y * edge2);
732 glm::vec3 bitangent = f * (deltaUV2.x * edge1 - deltaUV1.x * edge2);
734 normalAccum[ idx0 ] += normal; tangentAccum[ idx0 ] += tangent; bitangentAccum[ idx0 ] += bitangent;
735 normalAccum[ idx1 ] += normal; tangentAccum[ idx1 ] += tangent; bitangentAccum[ idx1 ] += bitangent;
736 normalAccum[ idx2 ] += normal; tangentAccum[ idx2 ] += tangent; bitangentAccum[ idx2 ] += bitangent;
739 for(i = 0; i < pMESH->numVertices; ++i) {
740 glm::vec3& n = normalAccum[i];
741 glm::vec3& t = tangentAccum[i];
742 glm::vec3& b = bitangentAccum[i];
744 glm::vec3 normal = glm::normalize( n );
746 glm::vec3 tangent = glm::normalize( t - (glm::dot(normal, t) * normal) );
747 glm::vec3 bitangent = glm::normalize( b );
749 _normalArray[ i ] = normal;
751 _tangentArray[ i ] = glm::vec4(tangent, 0.0f);
753 _tangentArray[ i ].w = (glm::dot( glm::cross(normal, tangent), bitangent) < 0.0f) ? -1.0f : 1.0f;
756 delete[] normalAccum;
757 delete[] tangentAccum;
758 delete[] bitangentAccum;
760 glBindVertexArray(_vao );
762 glBindBuffer(GL_ARRAY_BUFFER, _vbo[0] );
763 glBufferSubData(GL_ARRAY_BUFFER, 0,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * pMESH->numVertices, &_vertexArray[0] );
764 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _maxVertices * 1,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * pMESH->numVertices, &_normalArray[0] );
765 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _maxVertices * 2,
static_cast<GLsizeiptr
>(
sizeof(glm::vec4)) * pMESH->numVertices, &_tangentArray[0] );
766 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] );
768 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo[1] );
769 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0,
static_cast<GLsizeiptr
>(
sizeof(GLuint)) * pMESH->numTriangles * 3, _vertexIndicesArray );
773CSCI441::MD5Model::_drawMesh(
774 const CSCI441_INTERNAL::MD5Mesh *pMESH
776 if (pMESH->shader !=
nullptr) {
778 if (pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::SPECULAR].texHandle != 0) {
780 glActiveTexture(_specularActiveTexture);
781 glBindTexture(GL_TEXTURE_2D, pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::SPECULAR].texHandle );
784 if (pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::NORMAL].texHandle != 0) {
786 glActiveTexture(_normalActiveTexture);
787 glBindTexture(GL_TEXTURE_2D, pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::NORMAL].texHandle );
789 if (pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::HEIGHT].texHandle != 0) {
791 glActiveTexture(_heightActiveTexture);
792 glBindTexture(GL_TEXTURE_2D, pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::HEIGHT].texHandle );
795 if (pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::DIFFUSE].texHandle != 0) {
797 glActiveTexture(_diffuseActiveTexture);
798 glBindTexture(GL_TEXTURE_2D, pMESH->shader->textures[CSCI441_INTERNAL::MD5MaterialShader::DIFFUSE].texHandle );
801 if (_diffuseActiveTexture != GL_TEXTURE0) {
803 glActiveTexture(GL_TEXTURE0);
807 glBindVertexArray(_vao );
808 glDrawElements(GL_TRIANGLES, pMESH->numTriangles * 3, GL_UNSIGNED_INT, (
void*)
nullptr );
814 const GLuint vPosAttribLoc,
815 const GLuint vColorAttribLoc,
816 const GLuint vTexCoordAttribLoc,
817 const GLuint vNormalAttribLoc,
818 const GLuint vTangentAttribLoc
823 _vertexArray =
new glm::vec3[_maxVertices];
824 _normalArray =
new glm::vec3[_maxVertices];
825 _tangentArray =
new glm::vec4[_maxVertices];
826 _texelArray =
new glm::vec2[_maxVertices];
827 _vertexIndicesArray =
new GLuint[_maxTriangles * 3];
829 glGenVertexArrays( 1, &_vao );
830 glBindVertexArray(_vao );
832 glGenBuffers(2, _vbo );
833 glBindBuffer(GL_ARRAY_BUFFER, _vbo[0] );
834 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 );
836 glEnableVertexAttribArray( vPosAttribLoc );
837 glVertexAttribPointer( vPosAttribLoc, 3, GL_FLOAT, GL_FALSE, 0,
static_cast<void *
>(
nullptr) );
839 if (vNormalAttribLoc != 0) {
840 glEnableVertexAttribArray( vNormalAttribLoc );
841 glVertexAttribPointer( vNormalAttribLoc, 3, GL_FLOAT, GL_FALSE, 0,
reinterpret_cast<void *
>(
sizeof(glm::vec3) * _maxVertices * 1) );
844 if (vTangentAttribLoc != 0) {
845 glEnableVertexAttribArray( vTangentAttribLoc );
846 glVertexAttribPointer( vTangentAttribLoc, 4, GL_FLOAT, GL_FALSE, 0,
reinterpret_cast<void *
>(
sizeof(glm::vec3) * _maxVertices * 2) );
849 if (vTexCoordAttribLoc != 0) {
850 glEnableVertexAttribArray( vTexCoordAttribLoc );
851 glVertexAttribPointer( vTexCoordAttribLoc, 2, GL_FLOAT, GL_FALSE, 0,
reinterpret_cast<void *
>(
sizeof(glm::vec3) * _maxVertices * 2 +
sizeof(glm::vec4) * _maxVertices) );
854 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo[1] );
855 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(GLuint)) * _maxTriangles * 3,
nullptr, GL_DYNAMIC_DRAW );
857 printf(
"[.md5mesh]: Model VAO/VBO/IBO registered at %u/%u/%u\n", _vao, _vbo[0], _vbo[1] );
859 glGenVertexArrays( 1, &_skeletonVAO );
860 glBindVertexArray(_skeletonVAO );
862 glGenBuffers( 1, &_skeletonVBO );
863 glBindBuffer(GL_ARRAY_BUFFER, _skeletonVBO );
864 glBufferData(GL_ARRAY_BUFFER,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints * 3 * 2,
nullptr, GL_DYNAMIC_DRAW );
866 glEnableVertexAttribArray( vPosAttribLoc );
867 glVertexAttribPointer( vPosAttribLoc, 3, GL_FLOAT, GL_FALSE, 0,
static_cast<void *
>(
nullptr) );
869 if (vColorAttribLoc != 0) {
870 glEnableVertexAttribArray( vColorAttribLoc );
871 glVertexAttribPointer( vColorAttribLoc, 3, GL_FLOAT, GL_FALSE, 0,
reinterpret_cast<void *
>(
sizeof(glm::vec3) * _numJoints * 3) );
874 printf(
"[.md5mesh]: Skeleton VAO/VBO registered at %u/%u\n", _skeletonVAO, _skeletonVBO );
879 const GLint diffuseMapActiveTexture = GL_TEXTURE0,
880 const GLint specularMapActiveTexture = GL_TEXTURE1,
881 const GLint normalMapActiveTexture = GL_TEXTURE2,
882 const GLint heightMapActiveTexture = GL_TEXTURE3
884 _diffuseActiveTexture = diffuseMapActiveTexture;
885 _specularActiveTexture = specularMapActiveTexture;
886 _normalActiveTexture = normalMapActiveTexture;
887 _heightActiveTexture = heightMapActiveTexture;
891CSCI441::MD5Model::_freeVertexArrays()
893 delete[] _vertexArray;
894 _vertexArray =
nullptr;
896 delete[] _normalArray;
897 _normalArray =
nullptr;
899 delete[] _tangentArray;
900 _tangentArray =
nullptr;
902 delete[] _vertexIndicesArray;
903 _vertexIndicesArray =
nullptr;
905 delete[] _texelArray;
906 _texelArray =
nullptr;
908 glDeleteVertexArrays( 1, &_vao );
911 glDeleteBuffers(2, _vbo );
915 glDeleteVertexArrays( 1, &_skeletonVAO );
918 glDeleteBuffers( 1, &_skeletonVBO );
926 glBindVertexArray(_skeletonVAO );
927 glBindBuffer(GL_ARRAY_BUFFER, _skeletonVBO );
929 constexpr glm::vec3 jointColor = {1.0f, 1.0f, 0.0f };
930 constexpr glm::vec3 boneColor = {1.0f, 0.0f, 1.0f };
933 for(GLint i = 0; i < _numJoints; ++i ) {
934 glBufferSubData(GL_ARRAY_BUFFER, i *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)),
sizeof(glm::vec3), &(_skeleton[i].position) );
935 glBufferSubData(GL_ARRAY_BUFFER, i *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) +
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints * 3,
sizeof(glm::vec3), &jointColor[0]);
940 for(GLint i = 0; i < _numJoints; ++i ) {
941 if( _skeleton[i].parent != CSCI441_INTERNAL::MD5Joint::NULL_JOINT ) {
944 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints + (i * 2) *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)),
945 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)),
946 &(_skeleton[_skeleton[i].parent].position)
950 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints + (i * 2) *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) +
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints * 3,
957 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) * _numJoints + (i * 2) *
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)) +
static_cast<GLsizeiptr
>(
sizeof(glm::vec3)),
958 static_cast<GLsizeiptr
>(
sizeof(glm::vec3)),
959 &(_skeleton[i].position)
963 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,
972 glDrawArrays(GL_POINTS, 0, _numJoints );
976 glDrawArrays(GL_LINES, _numJoints, numBones * 2 );
981CSCI441::MD5Model::_checkAnimValidity(
const GLushort targetAnimationIndex)
const
984 if( _numJoints != _animations[targetAnimationIndex]->getNumberOfJoints() ) {
985 fprintf(stderr,
"[.md5anim | ERROR]: skeleton and animation do not have same number of joints. cannot apply animation %u to skeleton\n", targetAnimationIndex);
988 if (_animations[targetAnimationIndex]->getNumberOfJoints() == 0 ) {
989 fprintf(stderr,
"[.md5anim | ERROR]: animation has zero joints. cannot apply animation %u to skeleton\n\n", targetAnimationIndex);
994 for(GLint i = 0; i < _numJoints; ++i) {
996 if (_baseSkeleton[i].parent != _animations[targetAnimationIndex]->getSkeletonFrameJoint(0, i).parent) {
997 fprintf(stderr,
"[.md5anim | ERROR]: skeleton and animation joints do not have same parent index. cannot apply animation %u to skeleton\n", targetAnimationIndex);
1002 if (strcmp (_baseSkeleton[i].name, _animations[targetAnimationIndex]->getSkeletonFrameJoint(0, i).name) != 0) {
1003 fprintf(stderr,
"[.md5anim | ERROR]: skeleton and animation joints do not have same name. cannot apply animation %u to skeleton\n", targetAnimationIndex);
1008 fprintf(stdout,
"[.md5anim]: skeleton and animation match. animation %u can be applied to skeleton\n", targetAnimationIndex);
1013CSCI441::MD5Model::_buildFrameSkeleton(
1014 const CSCI441_INTERNAL::MD5JointInfo* pJOINT_INFOS,
1015 const CSCI441_INTERNAL::MD5BaseFrameJoint* pBASE_FRAME,
1016 const GLfloat* pANIM_FRAME_DATA,
1017 const CSCI441_INTERNAL::MD5Joint* pSkeletonFrame,
1018 const GLint NUM_JOINTS
1020 if(pJOINT_INFOS ==
nullptr
1021 || pBASE_FRAME ==
nullptr
1022 || pANIM_FRAME_DATA ==
nullptr
1023 || pSkeletonFrame ==
nullptr)
return;
1025 for(GLint i = 0; i < NUM_JOINTS; ++i) {
1026 const CSCI441_INTERNAL::MD5BaseFrameJoint *baseJoint = &pBASE_FRAME[i];
1027 glm::vec3 animatedPosition = baseJoint->position;
1028 glm::quat animatedOrientation = baseJoint->orientation;
1032 if(pJOINT_INFOS[i].flags & 1 ) {
1033 animatedPosition.x = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1038 if(pJOINT_INFOS[i].flags & 2 ) {
1039 animatedPosition.y = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1044 if(pJOINT_INFOS[i].flags & 4 ) {
1045 animatedPosition.z = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1050 if(pJOINT_INFOS[i].flags & 8 ) {
1051 animatedOrientation.x = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1056 if(pJOINT_INFOS[i].flags & 16 ) {
1057 animatedOrientation.y = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1062 if(pJOINT_INFOS[i].flags & 32 ) {
1063 animatedOrientation.z = pANIM_FRAME_DATA[pJOINT_INFOS[i].startIndex + j];
1067 animatedOrientation.w = glm::extractRealComponent(animatedOrientation);
1072 const auto thisJoint =
const_cast<CSCI441_INTERNAL::MD5Joint *
>(&pSkeletonFrame[i]);
1074 const GLint parent = pJOINT_INFOS[i].parent;
1075 thisJoint->parent = parent;
1076 strcpy (thisJoint->name, pJOINT_INFOS[i].name);
1079 if( thisJoint->parent == CSCI441_INTERNAL::MD5Joint::NULL_JOINT ) {
1080 thisJoint->position = animatedPosition;
1081 thisJoint->orientation = animatedOrientation;
1083 const CSCI441_INTERNAL::MD5Joint *parentJoint = &pSkeletonFrame[parent];
1084 glm::vec3 rotatedPosition = glm::rotate(parentJoint->orientation, glm::vec4(animatedPosition, 0.0f));
1087 thisJoint->position = parentJoint->position + rotatedPosition;
1090 thisJoint->orientation = glm::normalize( glm::cross(parentJoint->orientation, animatedOrientation) );
1097 const char *filename,
1098 const GLushort targetAnimationIndex
1100 if (targetAnimationIndex >= _numAnimations) {
1101 fprintf (stderr,
"[.md5anim]: Error: target animation index %u is out of range for currently allocated animations (which is %u)\n", targetAnimationIndex, _numAnimations);
1102 fprintf (stderr,
"[.md5anim]: Hey Developer, if you wish to add an animation to the sequence, use CSCI441::MD5Model::addMD5Anim(const char*)\n");
1107 CSCI441_INTERNAL::MD5JointInfo *jointInfos =
nullptr;
1108 CSCI441_INTERNAL::MD5BaseFrameJoint *baseFrame =
nullptr;
1109 GLfloat *animFrameData =
nullptr;
1111 GLint numAnimatedComponents;
1112 GLint frameIndex, numFrames, numJoints;
1115 printf(
"[.md5anim]: about to read %s into animation %u\n", filename, targetAnimationIndex );
1117 FILE *fp = fopen( filename,
"rb" );
1119 fprintf (stderr,
"[.md5anim]: Error: couldn't open \"%s\"!\n", filename);
1123 while( !feof(fp) ) {
1125 fgets( buff,
sizeof(buff), fp );
1127 if( sscanf(buff,
" MD5Version %d", &version) == 1 ) {
1128 if( version != 10 ) {
1130 fprintf (stderr,
"[.md5anim]: Error: bad animation version\n");
1134 }
else if( sscanf(buff,
" numFrames %d", &numFrames) == 1 ) {
1136 _animations[targetAnimationIndex]->setNumberOfFrames(numFrames);
1137 }
else if( sscanf(buff,
" numJoints %d", &numJoints) == 1 ) {
1138 if (jointInfos !=
nullptr) {
1139 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numJoints already specified\n" );
1141 if( numJoints > 0 ) {
1142 _animations[targetAnimationIndex]->setNumberOfJoints(numJoints);
1145 jointInfos =
new CSCI441_INTERNAL::MD5JointInfo[numJoints];
1146 baseFrame =
new CSCI441_INTERNAL::MD5BaseFrameJoint[numJoints];
1148 }
else if( sscanf(buff,
" frameRate %d", &_animations[targetAnimationIndex]->frameRate) == 1 ) {
1150 }
else if( sscanf(buff,
" numAnimatedComponents %d", &numAnimatedComponents) == 1 ) {
1151 if (animFrameData !=
nullptr) {
1152 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numAnimatedComponents already specified\n" );
1154 if( numAnimatedComponents > 0 ) {
1156 animFrameData =
new GLfloat[numAnimatedComponents];
1158 }
else if( strncmp(buff,
"hierarchy {", 11) == 0 ) {
1159 if (jointInfos ==
nullptr) {
1160 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numJoints not specified prior to hierarchy\n" );
1162 for(i = 0; i < numJoints; ++i) {
1164 fgets( buff,
sizeof(buff), fp );
1167 sscanf(buff,
" %s %d %d %d",
1168 jointInfos[i].name, &jointInfos[i].parent,
1169 &jointInfos[i].flags, &jointInfos[i].startIndex);
1172 }
else if( strncmp(buff,
"bounds {", 8) == 0 ) {
1173 if (_animations[targetAnimationIndex]->getNumberOfFrames() == 0) {
1174 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numFrames not specified prior to bounds\n" );
1176 for(i = 0; i < _animations[targetAnimationIndex]->getNumberOfFrames(); ++i) {
1178 fgets( buff,
sizeof(buff), fp );
1181 sscanf(buff,
" ( %f %f %f ) ( %f %f %f )",
1182 &_animations[targetAnimationIndex]->getBoundingBox(i).min[0], &_animations[targetAnimationIndex]->getBoundingBox(i).min[1], &_animations[targetAnimationIndex]->getBoundingBox(i).min[2],
1183 &_animations[targetAnimationIndex]->getBoundingBox(i).max[0], &_animations[targetAnimationIndex]->getBoundingBox(i).max[1], &_animations[targetAnimationIndex]->getBoundingBox(i).max[2]);
1186 }
else if( strncmp(buff,
"baseframe {", 10) == 0 ) {
1187 if (baseFrame ==
nullptr) {
1188 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numJoints not specified prior to baseframe\n" );
1190 for(i = 0; i < numJoints; ++i) {
1192 fgets( buff,
sizeof(buff), fp );
1195 if( sscanf(buff,
" ( %f %f %f ) ( %f %f %f )",
1196 &baseFrame[i].position[0], &baseFrame[i].position[1], &baseFrame[i].position[2],
1197 &baseFrame[i].orientation[0], &baseFrame[i].orientation[1], &baseFrame[i].orientation[2]) == 6 ) {
1199 baseFrame[i].orientation.w = glm::extractRealComponent(baseFrame[i].orientation);
1203 }
else if(sscanf(buff,
" frame %d", &frameIndex) == 1 ) {
1204 if (animFrameData ==
nullptr) {
1205 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numAnimatedComponents not specified prior to frame\n" );
1206 }
else if (_animations[targetAnimationIndex]->getNumberOfFrames() == 0) {
1207 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numFrames not specified prior to frame\n" );
1208 }
else if (baseFrame ==
nullptr) {
1209 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. baseframe not specified prior to frame\n" );
1210 }
else if (jointInfos ==
nullptr) {
1211 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numJoints not specified prior to frame\n" );
1214 for(i = 0; i < numAnimatedComponents; ++i)
1215 fscanf( fp,
"%f", &animFrameData[i] );
1218 _buildFrameSkeleton(jointInfos, baseFrame, animFrameData,
1219 _animations[targetAnimationIndex]->getSkeletonFrame(frameIndex),
1227 printf(
"[.md5anim]: finished reading %s into animation %u\n", filename, targetAnimationIndex );
1228 printf(
"[.md5anim]: read in %d frames of %d joints with %d animated components\n", _animations[targetAnimationIndex]->getNumberOfFrames(), _animations[targetAnimationIndex]->getNumberOfJoints(), numAnimatedComponents );
1229 printf(
"[.md5anim]: animation's frame rate is %d\n", _animations[targetAnimationIndex]->frameRate );
1232 delete[] animFrameData;
1234 delete[] jointInfos;
1237 _animationInfos[targetAnimationIndex].currFrame = 0;
1238 _animationInfos[targetAnimationIndex].nextFrame = 1;
1240 _animationInfos[targetAnimationIndex].lastTime = 0.0f;
1241 _animationInfos[targetAnimationIndex].maxTime = 1.0f /
static_cast<GLfloat
>(_animations[targetAnimationIndex]->frameRate);
1244 if (_animations[targetAnimationIndex]->getNumberOfJoints() == 0) {
1245 fprintf( stderr,
"[.md5anim]: Error: md5anim file malformed. numJoints never specified\n" );
1247 _skeleton =
new CSCI441_INTERNAL::MD5Joint[_animations[targetAnimationIndex]->getNumberOfJoints()];
1250 if( _checkAnimValidity(targetAnimationIndex) ) {
1261 const char* filename
1264 auto newAnimations =
new CSCI441_INTERNAL::MD5Animation*[_numAnimations + 1];
1265 auto newAnimationInfos =
new CSCI441_INTERNAL::MD5AnimationState[_numAnimations + 1];
1267 for(
int i = 0; i < _numAnimations; ++i ) {
1268 newAnimations[i] = _animations[i];
1269 newAnimationInfos[i] = _animationInfos[i];
1271 newAnimations[_numAnimations] =
new CSCI441_INTERNAL::MD5Animation();
1273 delete[] _animations;
1274 delete[] _animationInfos;
1276 _animations = newAnimations;
1277 _animationInfos = newAnimationInfos;
1282 fprintf( stdout,
"\n[.md5anim]: preparing to read %s into new animation %u\n", filename, (_numAnimations-1) );
1283 const bool readSuccess = readMD5Anim(filename, _numAnimations - 1);
1286 fprintf( stdout,
"[.md5anim]: successfully read %s into new animation %u\n", filename, (_numAnimations-1) );
1288 fprintf( stderr,
"[.md5anim]: Error: could not read %s into new animation %u\n", filename, (_numAnimations-1) );
1291 newAnimations =
new CSCI441_INTERNAL::MD5Animation*[_numAnimations-1];
1292 newAnimationInfos =
new CSCI441_INTERNAL::MD5AnimationState[_numAnimations-1];
1294 for (
int i = 0; i < _numAnimations - 1; ++i) {
1295 newAnimations[i] = _animations[i];
1296 newAnimationInfos[i] = _animationInfos[i];
1298 delete _animations[_numAnimations-1];
1300 delete[] _animations;
1301 delete[] _animationInfos;
1303 _animations = newAnimations;
1304 _animationInfos = newAnimationInfos;
1314 const GLushort targetAnimationIndex
1317 if (targetAnimationIndex < _numAnimations) {
1319 _currentAnimationIndex = targetAnimationIndex;
1322 _animationInfos[targetAnimationIndex].currFrame = 0;
1323 _animationInfos[targetAnimationIndex].nextFrame = 1;
1325 _animationInfos[targetAnimationIndex].lastTime = 0.0f;
1326 _animationInfos[targetAnimationIndex].maxTime = 1.0f /
static_cast<GLfloat
>(_animations[targetAnimationIndex]->frameRate);
1333CSCI441::MD5Model::_freeAnim()
1336 _skeleton =
nullptr;
1338 for (
int i = 0; i < _numAnimations; i++) {
1339 delete _animations[i];
1341 delete[] _animations;
1342 _animations =
nullptr;
1344 delete[] _animationInfos;
1345 _animationInfos =
nullptr;
1348inline void CSCI441::MD5Model::_moveFromSrc(MD5Model &src) {
1349 this->_baseSkeleton = src._baseSkeleton;
1350 src._baseSkeleton =
nullptr;
1352 this->_meshes = src._meshes;
1353 src._meshes =
nullptr;
1355 this->_maxVertices = src._maxVertices;
1356 src._maxVertices = 0;
1358 this->_maxTriangles = src._maxTriangles;
1359 src._maxTriangles = 0;
1361 this->_vertexArray = src._vertexArray;
1362 src._vertexArray =
nullptr;
1364 this->_normalArray = src._normalArray;
1365 src._normalArray =
nullptr;
1367 this->_tangentArray = src._tangentArray;
1368 src._tangentArray =
nullptr;
1370 this->_texelArray = src._texelArray;
1371 src._texelArray =
nullptr;
1373 this->_vertexIndicesArray = src._vertexIndicesArray;
1374 src._vertexIndicesArray =
nullptr;
1376 this->_vao = src._vao;
1379 this->_vbo[0] = src._vbo[0];
1380 this->_vbo[1] = src._vbo[1];
1384 this->_skeletonVAO = src._skeletonVAO;
1385 src._skeletonVAO = 0;
1387 this->_skeletonVBO = src._skeletonVBO;
1388 src._skeletonVBO = 0;
1390 this->_skeleton = src._skeleton;
1391 src._skeleton =
nullptr;
1393 this->_animations = src._animations;
1394 src._animations =
nullptr;
1396 this->_numAnimations = src._numAnimations;
1397 src._numAnimations = 0;
1399 this->_currentAnimationIndex = src._currentAnimationIndex;
1400 src._currentAnimationIndex = 0;
1402 this->_isAnimated = src._isAnimated;
1403 src._isAnimated =
false;
1405 this->_animationInfos = src._animationInfos;
1406 src._animationInfos =
nullptr;
1410CSCI441::MD5Model::_interpolateSkeletons(
const GLfloat interp)
1412 const CSCI441_INTERNAL::MD5Joint *skeletonA = _animations[_currentAnimationIndex]->getSkeletonFrame(_animationInfos[_currentAnimationIndex].currFrame);
1413 const CSCI441_INTERNAL::MD5Joint *skeletonB = _animations[_currentAnimationIndex]->getSkeletonFrame(_animationInfos[_currentAnimationIndex].nextFrame);
1415 for(GLint i = 0; i < _animations[_currentAnimationIndex]->getNumberOfJoints(); ++i) {
1417 _skeleton[i].parent = skeletonA[i].parent;
1420 _skeleton[i].position = glm::mix(skeletonA[i].position, skeletonB[i].position, interp);
1423 _skeleton[i].orientation = glm::slerp(skeletonA[i].orientation, skeletonB[i].orientation, interp);
1432 const GLint maxFrames = _animations[_currentAnimationIndex]->getNumberOfFrames() - 1;
1433 if (maxFrames <= 0)
return;
1435 _animationInfos[_currentAnimationIndex].lastTime += dt;
1438 if( _animationInfos[_currentAnimationIndex].lastTime >= _animationInfos[_currentAnimationIndex].maxTime ) {
1439 _animationInfos[_currentAnimationIndex].currFrame++;
1440 _animationInfos[_currentAnimationIndex].nextFrame++;
1441 _animationInfos[_currentAnimationIndex].lastTime = 0.0;
1443 if( _animationInfos[_currentAnimationIndex].currFrame > maxFrames )
1444 _animationInfos[_currentAnimationIndex].currFrame = 0;
1446 if( _animationInfos[_currentAnimationIndex].nextFrame > maxFrames )
1447 _animationInfos[_currentAnimationIndex].nextFrame = 0;
1451 _interpolateSkeletons( _animationInfos[_currentAnimationIndex].lastTime *
static_cast<GLfloat
>(_animations[_currentAnimationIndex]->frameRate) );
1454inline std::map< std::string, CSCI441_INTERNAL::MD5MaterialShader* > CSCI441::MD5Model::_materials;
1455inline std::map< std::string, GLuint > CSCI441::MD5Model::_textureMap;
1458inline size_t CSCI441::MD5Model::_trim(
char* out,
const size_t len,
const char* str) {
1463 while( isspace(
static_cast<unsigned char>(*str)) ) str++;
1472 const char *end = str + strlen(str) - 1;
1473 while( end > str && isspace(
static_cast<unsigned char>(*end)) ) end--;
1477 const size_t out_size = (end - str) < len - 1 ? (end - str) : len - 1;
1480 memcpy(out, str, out_size);
1481 out[out_size] =
'\0';
1486inline bool CSCI441::MD5Model::_registerShaderTexture(CSCI441_INTERNAL::MD5Texture *texture) {
1487 if (
const auto textureIter = _textureMap.find( texture->filename ); textureIter == _textureMap.end()) {
1489 if( texture->texHandle == 0 ) {
1494 _textureMap.insert( std::pair( texture->filename, texture->texHandle) );
1498 texture->texHandle = textureIter->second;
1505 char buff[512], buff2[512];
1506 GLushort numTextures = 0;
1508 fprintf(stdout,
"\n[.md5mtr]: about to read %s\n", FILENAME );
1510 FILE *fp = fopen(FILENAME,
"rb" );
1512 fprintf (stderr,
"[.md5mtr]: Error: couldn't open \"%s\"!\n", FILENAME);
1516 while( !feof(fp) ) {
1518 fgets( buff,
sizeof(buff), fp );
1520 _trim(buff, 512, buff);
1522 if( sscanf(buff,
"table %s", buff2) == 1 ) {
1524 }
else if ( sscanf(buff,
"//%s", buff2) == 1 ) {
1526 }
else if (strnlen(buff,
sizeof(buff)) == 0) {
1530 auto shader =
new CSCI441_INTERNAL::MD5MaterialShader();
1532 strncpy(shader->name, buff,
sizeof(buff));
1535 unsigned short numBlocks = 0;
1538 while ( fgets( buff,
sizeof(buff), fp ) !=
nullptr) {
1539 _trim(buff, 512, buff);
1541 if (strchr(buff,
'{') !=
nullptr) {
1545 else if (sscanf(buff,
" diffusemap %s", buff2) == 1) {
1547 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::DIFFUSE].filename, buff2,
sizeof(buff2) );
1548 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::DIFFUSE] )) {
1552 else if (sscanf(buff,
" specularmap %s", buff2) == 1) {
1554 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::SPECULAR].filename, buff2,
sizeof(buff2) );
1555 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::SPECULAR] )) {
1560 else if (numBlocks == 1 && sscanf(buff,
" bumpmap %s", buff2) == 1) {
1563 std::vector< char* > tokens;
1564 char* pch = strtok(buff,
" \t(),");
1565 while (pch != NULL) {
1566 tokens.push_back( pch );
1567 pch = strtok(NULL,
" \t(),");
1572 if (tokens.size() == 2) {
1573 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL].filename, tokens[1], strlen(tokens[1]) );
1574 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL] )) {
1581 else if (tokens.size() == 4) {
1582 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT].filename, tokens[2], strlen(tokens[2]) );
1583 shader->displacementScale = strtol(tokens[3], NULL, 10);
1584 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT] )) {
1592 else if (tokens.size() == 6) {
1593 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL].filename, tokens[2], strlen(tokens[2]) );
1594 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::NORMAL] )) {
1597 strncpy( shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT].filename, tokens[4], strlen(tokens[4]) );
1598 shader->displacementScale = strtol(tokens[5], NULL, 10);
1599 if (_registerShaderTexture( &shader->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT] )) {
1604 else if (strchr(buff,
'}') !=
nullptr) {
1607 if (numBlocks == 0) {
1613 _materials.insert( std::pair(shader->name, shader) );
1618 fprintf(stdout,
"[.md5mtr]: finished reading %s\n", FILENAME );
1619 fprintf(stdout,
"[.md5mtr]: read in %lu shaders and %u textures\n\n", _materials.size(), numTextures );
1624 for (
auto &[name, material] : _materials) {
1625 glDeleteTextures(1, &material->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::DIFFUSE].texHandle);
1626 glDeleteTextures(1, &material->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::SPECULAR].texHandle);
1627 glDeleteTextures(1, &material->textures[CSCI441_INTERNAL::MD5MaterialShader::TextureMap::HEIGHT].texHandle);
1628 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:76
bool addMD5Anim(const char *filename)
adds another MD5 Animation sequence to the model's set of animation
Definition: MD5Model.hpp:1260
static void readMD5Material(const char *FILENAME)
loads textures corresponding to MD5 Shaders
Definition: MD5Model.hpp:1504
void draw() const
draws all the meshes that make up the model
Definition: MD5Model.hpp:654
GLushort getNumberOfAnimations() const
returns the number of animations that were successfully loaded against the model
Definition: MD5Model.hpp:193
bool readMD5Model(const char *FILENAME)
parses md5mesh file and allocates corresponding mesh data
Definition: MD5Model.hpp:464
bool readMD5Anim(const char *filename, GLushort targetAnimationIndex=0)
reads in an animation sequence from an external file
Definition: MD5Model.hpp:1096
bool isAnimated() const
returns if the MD5 Model has an accompanying animation
Definition: MD5Model.hpp:133
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:440
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:878
MD5Model()
initializes an empty MD5 Model
Definition: MD5Model.hpp:82
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:813
void animate(GLfloat dt)
advances the model forward in its animation sequence the corresponding amount of time based on frame ...
Definition: MD5Model.hpp:1430
MD5Model & operator=(MD5Model &&src) noexcept
do not allow MD5 models to be moved
Definition: MD5Model.hpp:114
void useTargetAnimationIndex(GLushort targetAnimationIndex)
update current animation to be running through
Definition: MD5Model.hpp:1313
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:108
~MD5Model()
deallocates any used memory on the CPU and GPU
Definition: MD5Model.hpp:430
static void releaseMD5Materials()
deletes textures from GPU that were registered during parsing of *.mtr file
Definition: MD5Model.hpp:1623
void drawSkeleton() const
draws the skeleton joints (as points) and bones (as lines)
Definition: MD5Model.hpp:924
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17