📄 md5mesh.cpp
字号:
#include "md5mesh.h"#include <zeitgeist/logserver/logserver.h>#include <zeitgeist/fileserver/fileserver.h>#include <kerosin/openglserver/openglserver.h>#include <kerosin/openglserver/openglwrapper.h>using namespace boost;using namespace oxygen;using namespace salt;using namespace std;using namespace zeitgeist;char gCurrentReadLine[1024];bool ReadLine(const shared_ptr<salt::RFile> &fp){ int pos = 0; while (1) { int inchar = fp->Getc(); if (inchar == EOF) { gCurrentReadLine[pos] = 0; return (false); } if (inchar == 13) { fp->Getc(); break; } gCurrentReadLine[pos++] = inchar; } gCurrentReadLine[pos] = 0; return (true);}MD5Mesh::MD5Mesh() :oxygen::BaseNode(), mMeshes(0), mBones(0){}MD5Mesh::~MD5Mesh(){}bool MD5Mesh::Load(const std::string &name){ shared_ptr<FileServer> fileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file")); if (fileServer.get() == NULL) { GetLog()->Error() << "ERROR: Could not locate FileServer" << endl; return false; } shared_ptr<salt::RFile> file(fileServer->Open(name.c_str())); if (file.get() == NULL) { GetLog()->Error() << "ERROR: Could not locate file '" << name << "'" << endl; return false; } // Skip header ReadLine(file); ReadLine(file); // ========== BONES ========== ReadLine(file); if (!memcmp(gCurrentReadLine, "numbones", 8)) { sscanf(gCurrentReadLine, "numbones %d", &mNumBones); mBones = new Bone[mNumBones]; printf("Number of bones: %d\n", mNumBones); for (int i = 0; i < mNumBones; i++) { Bone& bone = mBones[i]; // Start of the bone ReadLine(file); // ========== NAME ========== ReadLine(file); sscanf(gCurrentReadLine, "\tname \"%s", bone.name); bone.name[strlen(bone.name) - 1] = 0; // ========== POSITION ========== ReadLine(file); sscanf(gCurrentReadLine, "\tbindpos %f %f %f", &bone.pos[0], &bone.pos[1], &bone.pos[2]); // ========== ROTATION ========== ReadLine(file); sscanf(gCurrentReadLine, "\tbindmat %f %f %f %f %f %f %f %f %f", &bone.rot[0], &bone.rot[1], &bone.rot[2], &bone.rot[3], &bone.rot[4], &bone.rot[5], &bone.rot[6], &bone.rot[7], &bone.rot[8]); // ========== PARENT ========== ReadLine(file); bone.parent = -1; if (gCurrentReadLine[0] != '}') { char parent[64]; sscanf(gCurrentReadLine, "\tparent \"%s", parent); parent[strlen(parent) - 1] = 0; for (int j = 0; j < i; j++) { if (!strcmp(parent, mBones[j].name)) { bone.parent = j; break; } } ReadLine(file); } // Separating line ReadLine(file); } } // ========== MESHES ========== ReadLine(file); if (!memcmp(gCurrentReadLine, "nummeshes", 9)) { sscanf(gCurrentReadLine, "nummeshes %d", &mNumMeshes); mMeshes = new Mesh[mNumMeshes]; printf("Number of meshes: %d\n", mNumMeshes); ReadLine(file); for (int i = 0; i < mNumMeshes; i++) { int j; //printf("Mesh %d\n", i); Mesh& mesh = mMeshes[i]; // Start of mesh ReadLine(file); // ========== SHADER ========== ReadLine(file); char* ptr = gCurrentReadLine; while (*ptr != '\"') ptr++; strcpy(mesh.texture, ptr + 1); mesh.texture[strlen(mesh.texture) - 1] = 0; ReadLine(file); printf(" Shader: %s\n", mesh.texture); // ========== VERTICES ========== ReadLine(file); sscanf(gCurrentReadLine, "\tnumverts %d", &mesh.nb_vertices); printf(" Number of vertices: %d\n", mesh.nb_vertices); mesh.vertices = new Vertex[mesh.nb_vertices]; for (j = 0; j < mesh.nb_vertices; j++) { int index; Vertex& vertex = mesh.vertices[j]; ReadLine(file); sscanf(gCurrentReadLine, "\tvert %d %f %f %d %d", &index, &vertex.u, &vertex.v, &vertex.weight_index, &vertex.weight_count); vertex.v = 1 - vertex.v; } ReadLine(file); // ========== TRIANGLES ========== ReadLine(file); sscanf(gCurrentReadLine, "\tnumtris %d", &mesh.nb_triangles); printf(" Number of triangles: %d\n", mesh.nb_triangles); mesh.triangles = new Triangle[mesh.nb_triangles]; for (j = 0; j < mesh.nb_triangles; j++) { int index; Triangle& triangle = mesh.triangles[j]; ReadLine(file); sscanf(gCurrentReadLine, "\ttri %d %d %d %d", &index, &triangle.a, &triangle.b, &triangle.c); } ReadLine(file); // ========== WEIGHTS ========== ReadLine(file); sscanf(gCurrentReadLine, "\tnumweights %d", &mesh.nb_weights); printf(" Number of weights: %d\n", mesh.nb_weights); mesh.weights = new Weight[mesh.nb_weights]; for (j = 0; j < mesh.nb_weights; j++) { int index; Weight& weight = mesh.weights[j]; ReadLine(file); sscanf(gCurrentReadLine, "\tweight %d %d %f %f %f %f", &index, &weight.bone, &weight.t, &weight.x, &weight.y, &weight.z); } ReadLine(file); ReadLine(file); } } int i; for (i = 0; i < mNumMeshes; i++) { Mesh& mesh = mMeshes[i]; for (int j = 0; j < mesh.nb_vertices; j++) { Vertex& v = mesh.vertices[j]; v.x = 0; v.y = 0; v.z = 0; for (int k = 0; k < v.weight_count; k++) { Weight& w = mesh.weights[v.weight_index + k]; Bone& b = mBones[w.bone]; v.x += (b.rot[0] * w.x + b.rot[3] * w.y + b.rot[6] * w.z + b.pos[0]) * w.t; v.z += (b.rot[1] * w.x + b.rot[4] * w.y + b.rot[7] * w.z + b.pos[1]) * w.t; v.y += (b.rot[2] * w.x + b.rot[5] * w.y + b.rot[8] * w.z + b.pos[2]) * w.t; } } } CalcBasis(); return true;}bool MD5Mesh::LoadAnimation(const std::string &name){ shared_ptr<FileServer> fileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file")); if (fileServer.get() == NULL) { GetLog()->Error() << "ERROR: Could not locate FileServer" << endl; return false; } shared_ptr<salt::RFile> file(fileServer->Open(name.c_str())); if (file.get() == NULL) { GetLog()->Error() << "ERROR: Could not locate file '" << name << "'" << endl; return false; } // Skip header ReadLine(file); ReadLine(file); ReadLine(file); ReadLine(file); // CHANNELS if (!memcmp(gCurrentReadLine, "numchannels", 11)) { sscanf(gCurrentReadLine, "numchannels %d", &mNumChannels); //printf("Number of Channels: %d\n", mNumChannels); mChannels = new Channel[mNumChannels]; for (int i = 0; i < mNumChannels; i++) { char name[64]; Channel& channel = mChannels[i]; // skip line ReadLine(file); // Start of the channel ReadLine(file); // ========== NAME ========== ReadLine(file); sscanf(gCurrentReadLine, "\tjoint \"%s", name); name[strlen(name) - 1] = 0; //printf("Bone: %s\n", name); // find bone id channel.boneId = -1; for (int j = 0; j < mNumBones; j++) { if (!strcmp(name, mBones[j].name)) { channel.boneId = j; break; } } // ========== ATTRIBUTE ========== ReadLine(file); sscanf(gCurrentReadLine, "\tattribute \"%s", name); name[strlen(name) - 1] = 0; channel.paramId = -1; if (!strcmp(name, "x")) channel.paramId = 0; if (!strcmp(name, "y"))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -