00001 00002 00008 /* parseObject.cpp 00009 - used to parse the cloneware.avatar format into internal data format 00010 00011 ??0400 StonedBones now not a dummy anymore - reads from file 00012 28042000 BreezyGuy added additional comments 00013 ??042000 Xandi initial version 00014 */ 00015 #include <stdio.h> 00016 #include <string.h> 00017 #include <stdlib.h> 00018 00019 #include <string> 00020 00021 #include <iostream> 00022 #include <fstream> 00023 00024 00025 string readFile ( char* filename ) { 00026 ifstream f(filename); 00027 00028 char* str = new char[12500]; 00029 00030 float tempF; 00031 int tempI; 00032 char tempC; 00033 int pos; 00034 00035 strcpy ( str, filename); 00036 00037 pos = 64; // position in output string,after header, where data starts 00038 00039 f >> tempI; // read number of vertices 00040 00041 00042 memcpy ( str+pos, &tempI, sizeof(int) ); // update output string with data 00043 pos = pos + sizeof(int); // update position variable 00044 00045 int i,ii,jj; 00046 float vertex[3]; 00047 ii = tempI; 00048 for ( i = 0; i < ii; i++ ) { // read in coordinates for vertices 00049 f >> vertex[0]; // read X coordinate 00050 f >> vertex[1]; // read Y coordinate 00051 f >> vertex[2]; // read Z coordinate 00052 00053 // update output string with numbner of points and update position variable 00054 tempI = i+1; 00055 memcpy ( str+pos, &tempI, sizeof(int) ); 00056 pos = pos + sizeof(int); 00057 00058 // update output string with x coordinate and update position variable 00059 tempF = vertex[0]; 00060 memcpy ( str+pos, &tempF, sizeof(float) ); 00061 pos = pos + sizeof(float); 00062 00063 // update output string with y coordinate and update position variable 00064 tempF = vertex[1]; 00065 memcpy ( str+pos, &tempF, sizeof(float) ); 00066 pos = pos + sizeof(float); 00067 00068 // update output string with z coordinate and update position variable 00069 tempF = vertex[2]; 00070 memcpy ( str+pos, &tempF, sizeof(float) ); 00071 pos = pos + sizeof(float); 00072 00073 #ifdef DEBUG 00074 cerr << "Vertex " << tempI << ": " << vertex[0] << ", " << vertex[1] << ", " << vertex[2] << "\n"; 00075 #endif 00076 } 00077 00078 00079 // update output string with number of polygons and update position variable 00080 f >> tempI; // read number of polygons 00081 memcpy ( str+pos, &tempI, sizeof(int) ); 00082 pos = pos + sizeof(int); 00083 00084 ii = tempI; 00085 for ( i = 1; i <= ii; i++) { 00086 00087 // update output string with polygons number and update position variable 00088 f >> tempI; // read in polygon number 00089 memcpy ( str+pos, &tempI, sizeof(int) ); 00090 pos = pos + sizeof(int); 00091 00092 // update output string with polygons type and update position variable 00093 tempC = 1; // define polygon type 00094 memcpy ( str+pos, &tempC, sizeof(char) ); 00095 pos = pos + sizeof(char); 00096 00097 // update output string with polygons color and update position variable 00098 f >> tempF; // first color part 00099 memcpy ( str+pos, &tempF, sizeof(float) ); 00100 pos = pos + sizeof(float); 00101 00102 f >> tempF; // second color part 00103 memcpy ( str+pos, &tempF, sizeof(float) ); 00104 pos = pos + sizeof(float); 00105 00106 f >> tempF; // third color part 00107 memcpy ( str+pos, &tempF, sizeof(float) ); 00108 pos = pos + sizeof(float); 00109 00110 f >> tempF; // fourth color part 00111 memcpy ( str+pos, &tempF, sizeof(float) ); 00112 pos = pos + sizeof(float); 00113 00114 // update output string with number of vertice in the present polygons 00115 // and updates position variable 00116 f >> tempI; // read number of vertices in polygon 00117 memcpy ( str+pos, &tempI, sizeof(int) ); 00118 pos = pos + sizeof(int); 00119 00120 #ifdef DEBUG 00121 cerr << "Poly " << i << ": "; 00122 #endif 00123 00124 // update output string with vertice indexes in present polygon 00125 // and updates position variable 00126 jj = tempI; 00127 for (int j = 1; j <= jj; j++) { 00128 00129 f >> tempI; 00130 memcpy ( str+pos, &tempI, sizeof(int) ); 00131 pos = pos + sizeof(int); 00132 #ifdef DEBUG 00133 cerr << tempI << " "; 00134 #endif 00135 }; 00136 #ifdef DEBUG 00137 cerr << "\n"; 00138 #endif 00139 }; 00140 00141 00142 #ifdef DEBUG 00143 fprintf(stderr,"[parser] String is %d long\n", pos ); 00144 #endif 00145 00146 return string ( str, pos ); 00147 } 00148