00001
00007 #include "MilkShape.h"
00008
00009 MS_Mesh::MS_Mesh() {
00010 this->vertices = NULL;
00011 this->triangles = NULL;
00012 this->normals = NULL;
00013 }
00014
00015 MS_Mesh::~MS_Mesh() {
00016 clear();
00017 }
00018
00019 void MS_Mesh::clear() {
00020
00021 if (this->vertices)
00022 free(vertices);
00023
00024 if (this->triangles)
00025 free(triangles);
00026
00027 if (this->normals)
00028 free(normals);
00029
00030 vertices = NULL;
00031 triangles = NULL;
00032 normals = NULL;
00033 }
00034
00035 bool MS_Mesh::loadFromAsciiSegment(FILE *file) {
00036 bool bError = false;
00037 char szLine[256] = {0};
00038 int nFlags, nIndex, j;
00039
00040 if (!fgets(szLine, 256, file)) {
00041 return false;
00042 }
00043
00044 if (sscanf(szLine, "%d", &this->numVertices) != 1) {
00045 return false;
00046 }
00047
00048 this->vertices = (MS_Vec *)malloc(sizeof(MS_Vec) * this->numVertices);
00049
00050 for (j=0; j<this->numVertices; j++) {
00051 if (!fgets(szLine, 256, file)) {
00052 return false;
00053 }
00054
00055 if (sscanf(szLine, "%d %f %f %f %f %f %d",
00056 &nFlags,
00057 &this->vertices[j].x, &this->vertices[j].y, &this->vertices[j].z,
00058 &this->vertices[j].u, &this->vertices[j].v,
00059 &this->vertices[j].bone) != 7) {
00060 return false;
00061 }
00062
00063 this->vertices[j].v = 1.0f - this->vertices[j].v;
00064 }
00065
00066
00067
00068
00069 if (!fgets(szLine, 256, file)) {
00070 return false;
00071 }
00072
00073 if (sscanf(szLine, "%d", &this->numNormals) != 1) {
00074 return false;
00075 }
00076 this->normals = (MS_Normal *)malloc(sizeof(MS_Normal) * this->numNormals);
00077
00078 for (j=0; j<this->numNormals; j++) {
00079 if (!fgets(szLine, 256, file)) {
00080 return false;
00081 }
00082
00083 if (sscanf(szLine, "%f %f %f",
00084 &this->normals[j].x, &this->normals[j].y, &this->normals[j].z) != 3) {
00085 return false;
00086 }
00087 }
00088
00089
00090
00091
00092 if (!fgets(szLine, 256, file)) {
00093 return false;
00094 }
00095
00096 if (sscanf(szLine, "%d", &this->numTriangles) != 1) {
00097 return false;
00098 }
00099
00100 this->triangles = (MS_Tri *)malloc(sizeof(MS_Tri) * this->numTriangles);
00101
00102 for (j=0; j<this->numTriangles; j++) {
00103 if (!fgets(szLine, 256, file)) {
00104 return false;
00105 }
00106
00107 if (sscanf(szLine, "%d %d %d %d %d %d %d %d",
00108 &nFlags,
00109 &this->triangles[j].v[0], &this->triangles[j].v[1], &this->triangles[j].v[2],
00110 &this->triangles[j].n[0], &this->triangles[j].n[1], &this->triangles[j].n[2],
00111 &nIndex) != 8) {
00112 return false;
00113 }
00114
00115 assert(this->triangles[j].v[0] >= 0);
00116 assert(this->triangles[j].v[0] < this->numVertices);
00117 assert(this->triangles[j].v[1] >= 0);
00118 assert(this->triangles[j].v[1] < this->numVertices);
00119 assert(this->triangles[j].v[2] >= 0);
00120 assert(this->triangles[j].v[2] < this->numVertices);
00121 }
00122
00123 return true;
00124 }