ModelMD2::ModelMD2() : m_skins(NULL), m_texCoords(NULL), m_triangles(NULL), m_frames(NULL), m_actions(NULL), m_textureId(-1), m_frameIdx(0), m_actionIdx(0), m_actionCount(0) { #ifdef DEBUG printf("ModelMD2 constructor...\n"); #endif setMeshCount(1); }
bool ModelMS3D::loadModel(const char *filename, GLboolean is_absPath) { FILE *file; int i, j; file = fopen(Utils::getFileName(filename, is_absPath), "rb"); if (!file) { printf("Open MS3D model:%s failure!\n", filename); #if defined(WIN32) || defined(_WIN32_WCE) TCHAR errorStr[512]; wsprintf(errorStr, TEXT("Open MS3d model:[%s] error!"), Utils::getFileName(filename)); MessageBox(0, errorStr, TEXT("MS3D"), MB_OK); #endif return false; } /* initialize model and read header */ fread (&m_header, sizeof (ms3d_header_t), 1, file); #ifdef DEBUG printf("id:%s\n", m_header.id); printf("version:%d\n", m_header.version); #if defined(WIN32) || defined(_WIN32_WCE) TCHAR infoStr[512]; wsprintf(infoStr, TEXT("id:%s\nversion: %d"), m_header.id, m_header.version); MessageBox(0, infoStr, TEXT("MS3D"), MB_OK); #endif #endif if (!strcmp((char *)m_header.id, "MS3D000000") || m_header.version != 4) { printf("Invalid MS3D model or unsupport version, id:[%s], version[%d]...\n", m_header.id, m_header.version); fclose(file); return false; } //read vertices fread(&m_verticesCount, sizeof(GLushort), 1, file); #ifdef DEBUG printf("m_verticesCount:%d\n", m_verticesCount); #if defined(WIN32) || defined(_WIN32_WCE) wsprintf(infoStr, TEXT("m_verticesCount: %d"), m_verticesCount); MessageBox(0, infoStr, TEXT("MS3D"), MB_OK); #endif #endif m_vertices = new ms3d_vertex_t[m_verticesCount]; fread(m_vertices, sizeof(ms3d_vertex_t), m_verticesCount, file); //read triangles fread(&m_trianglesCount, sizeof(GLushort), 1, file); #ifdef DEBUG printf("m_trianglesCount:%d\n", m_trianglesCount); #if defined(WIN32) || defined(_WIN32_WCE) wsprintf(infoStr, TEXT("m_trianglesCount: %d"), m_trianglesCount); MessageBox(0, infoStr, TEXT("MS3D"), MB_OK); #endif #endif m_triangles = new ms3d_triangle_t[m_trianglesCount]; fread(m_triangles, sizeof(ms3d_triangle_t), m_trianglesCount, file); //read groups fread(&m_groupsCount, sizeof(GLushort), 1, file); #ifdef DEBUG printf("m_groupsCount:%d\n", m_groupsCount); #if defined(WIN32) || defined(_WIN32_WCE) wsprintf(infoStr, TEXT("m_groupsCount: %d"), m_groupsCount); MessageBox(0, infoStr, TEXT("MS3D"), MB_OK); #endif #endif //set model mesh count to group count to init model meshs. setMeshCount(m_groupsCount); m_groups = new ms3d_group_t[m_groupsCount]; for (i = 0; i < m_groupsCount; i++) { fread(&m_groups[i].flags, sizeof(GLubyte), 1, file); fread(&m_groups[i].name, MS3D_NAME_SIZE, 1, file); fread(&m_groups[i].numTriangles, sizeof(GLushort), 1, file); //after read triangles count, read triangle indices m_groups[i].triangleIndices = new GLushort[m_groups[i].numTriangles]; fread(&m_groups[i].triangleIndices[0], sizeof(GLushort), m_groups[i].numTriangles, file); //read material index fread(&m_groups[i].materialIndex, sizeof(char), 1, file); //set mesh triangle count setTriangleNums(m_groups[i].numTriangles, i); #ifdef DEBUG printf("m_groups[%d] name:%s\n", i, m_groups[i].name); printf("m_groups[%d] numTriangles:%d\n", i, m_groups[i].numTriangles); printf("m_groups[%d] materialIndex:%d\n", i, m_groups[i].materialIndex); #endif } //read materials fread(&m_materialsCount, sizeof(GLushort), 1, file); #ifdef DEBUG printf("m_materialsCount:%d\n", m_materialsCount); #endif if (m_materialsCount > 0) { m_materials = new ms3d_material_t[m_materialsCount]; fread(m_materials, sizeof(ms3d_material_t), m_materialsCount, file); #ifdef DEBUG for (i = 0; i < m_materialsCount; i++) { printf("m_materials[%d].name:%s\n", i, m_materials[i].name); printf("m_materials[%d].texture:%s\n", i, m_materials[i].texture); } #endif } //read animation fps, current time, total frames fread(&m_animationFPS, sizeof(float), 1, file); fread(&m_currentTime, sizeof(float), 1, file); fread(&m_totalFrames, sizeof(int), 1, file); //set frame count & frame index to 0 m_frameCount = m_totalFrames - 1; // totleFrames start with "1", not "0" m_frameIdx = 0; #ifdef DEBUG printf("m_animationFPS:%.4f\n", m_animationFPS); printf("m_currentTime:%.4f\n", m_currentTime); printf("m_totalFrames:%d\n", m_totalFrames); #endif //read joints fread(&m_jointsCount, sizeof(GLushort), 1, file); #ifdef DEBUG printf("m_jointsCount:%d\n", m_jointsCount); #if defined(WIN32) || defined(_WIN32_WCE) wsprintf(infoStr, TEXT("m_jointsCount: %d"), m_jointsCount); MessageBox(0, infoStr, TEXT("MS3D"), MB_OK); #endif #endif if (m_jointsCount > 0) { m_joints = new ms3d_joint_t[m_jointsCount]; for (i = 0; i < m_jointsCount; i++) { fread(&m_joints[i].header, sizeof(ms3d_joint_header_t), 1, file); //read frame rots m_joints[i].keyFramesRot = new ms3d_keyframe_rot_t[m_joints[i].header.numKeyFramesRot]; fread(&m_joints[i].keyFramesRot[0], sizeof(ms3d_keyframe_rot_t), m_joints[i].header.numKeyFramesRot, file); //read frame trans m_joints[i].keyFramesTrans = new ms3d_keyframe_pos_t[m_joints[i].header.numKeyFramesTrans]; fread(&m_joints[i].keyFramesTrans[0], sizeof(ms3d_keyframe_pos_t), m_joints[i].header.numKeyFramesTrans, file); //get parent index by parentName m_joints[i].parentJointIndex = -1; if (strlen(m_joints[i].header.parentName) > 0) { for (j = 0; j < i; j++) { if (strcmp(m_joints[i].header.parentName, m_joints[j].header.name) == 0) { m_joints[i].parentJointIndex = j; //find the parent index by name, break! break; } } } //init matrices m_joints[i].absMatrix = new Matrix(); m_joints[i].relMatrix = new Matrix(); m_joints[i].finMatrix = new Matrix(); #ifdef DEBUG printf("================================================\n"); printf("m_joints[%d] name:%s\n", i, m_joints[i].header.name); printf("m_joints[%d] parentName:%s\n", i, m_joints[i].header.parentName); printf("m_joints[%d] numKeyFramesRot:%d\n", i, m_joints[i].header.numKeyFramesRot); printf("m_joints[%d] numKeyFramesTrans:%d\n", i, m_joints[i].header.numKeyFramesTrans); printf("m_joints[%d] parentJointIndex:%d\n", i, m_joints[i].parentJointIndex); #endif } } //ignore other sections, such as sub version, comments, etc... fclose(file); setupJoints(); return true; }
void ModelAM::loadModel (const char* filename) { FILE *file; int r, i, j; int version = 0; int mesh_num = 0; int vertex_num = 0; int uv_num = 0; int triangle_num = 0; int temp = 0; int material_num = 0; float specular_level, glossiness; char mesh_name[32]; float box; float *vertices = NULL; float *uvs = NULL; short *indices = NULL; printf ("loadModel %s.\n", filename); file = fopen (filename, "r"); if (file == NULL) return; r = fscanf (file, "SimpleMeshFormat %d\n", &version); printf ("verson = %d.\n", version); r = fscanf (file, "%d\n", &mesh_num); printf ("mesh_num = %d.\n", mesh_num); setMeshCount (mesh_num); for (i = 0; i < mesh_num; i++) { r = fscanf (file, "%s\n", mesh_name); printf ("mesh_name = %s.\n", mesh_name); r = fscanf (file, "%d\n", &vertex_num); printf ("vertex_num = %d.\n", vertex_num); vertices = (float*)malloc (vertex_num * 3 * sizeof (float)); if (!vertices) { //Fixme: free sth if needed. return; } for (j = 0; j < vertex_num; j++) { r = fscanf (file, "%g %g %g\n", &vertices[j*3], &vertices[j*3+1], &vertices[j*3+2]); } setVertices (vertices, vertex_num * 3 * sizeof (float), i); FREEANDNULL (vertices); r = fscanf (file, "%d\n", &uv_num); uvs = (float*)malloc (uv_num * 2 * sizeof (float)); if (!uvs) { //Fixme: free sth if needed. return; } for (j = 0; j < uv_num; j++) { r = fscanf (file, "%g %g\n", &uvs[j*2], &uvs[j*2+1]); } setUvs(uvs, uv_num * 2 * sizeof (float), i); FREEANDNULL (uvs); r = fscanf (file, "%d\n", &triangle_num); printf ("triangle_num = %d.\n", triangle_num); setTriangleNums(triangle_num, i); indices = (short *)malloc (triangle_num * 3 * sizeof (short)); if (!indices) { //Fixme: free sth if needed. return; } for (j = 0; j < triangle_num; j++) { r = fscanf (file, "%d %hd %hd %hd %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g\n", &temp, //nSmoothGroup &indices[j*3], &indices[j*3+1], &indices[j*3+2], &box, &box, &box, &box, &box, &box, &box, &box, &box, &box, &box, &box, &box, &box, &box); } setIndices (indices, triangle_num * 3 * sizeof (short), i); FREEANDNULL (indices); // setMaterialAmbient (0.9, 0.6, 0.6, 0); // setMaterialDiffuse (0.9, 0.6, 0.6, 0); float ambient [3]; float diffuse [3]; float specular [3]; float emission [3]; float shininess; r = fscanf (file, "%d\n", &material_num); //_M3D_PRINTF ("material_num = %d.\n", material_num); //FIXME: handle only one material //Basicly, diffuse decide mesh color in white light r = fscanf (file, "%g %g %g %g %g %g %g %g %g %g %g %g %g %g\n", &specular_level, &glossiness, &ambient[0], &ambient[1], &ambient[2], &diffuse[0], &diffuse[1], &diffuse[2], &specular[0], &specular[1], &specular[2], &emission[0], &emission[1], &emission[2]); if (specular_level > 0) shininess = glossiness; else shininess = -1; /* _M3D_PRINTF ("loadAmModel: %g %g %g %g %g %g %g %g %g %g\n", shininess, ambient[0], ambient[1], ambient[2], diffuse[0], diffuse[1], diffuse[2], specular[0], specular[1], specular[2]); } */ setMaterialAmbient (ambient[0], ambient[1], ambient[2], i); setMaterialDiffuse (diffuse[0], diffuse[1], diffuse[2], i); setMaterialEmission (emission[0], emission[1], emission[2], i); if (shininess > 0) { setMaterialShininess (shininess, i); setMaterialSpecular (specular[0], specular[1], specular[2], i); } } fclose (file); return; }