1 /*
2 * This code is released under the GNU General Public License.  See COPYING for
3 * details.  Copyright 2003 John Spray: spray_john@users.sourceforge.net
4 */
5 
6 #include <stdlib.h>
7 #include <string>
8 using namespace std;
9 #include <physfs.h>
10 
11 #include "Mesh.h"
12 
Mesh()13 Mesh::Mesh()
14 {
15 	geom=NULL;
16 	filename[0]=0;
17 	skinname[0]=0;
18 	vert_count=0;
19 	gl_array_id=-1;
20 	visual=NULL;
21 }
22 
~Mesh()23 Mesh::~Mesh()
24 {
25 	if(geom)
26 		free(geom);
27 }
28 
Draw()29 void Mesh::Draw()
30 {
31 	if(!visual){
32 					printf("Mesh::Draw: visual==NULL!\n");
33 					return;
34 	}
35 
36 	visual->UseTexture(skinname);
37 	glInterleavedArrays(GL_T2F_C4F_N3F_V3F,0,geom);
38 	glDrawArrays(GL_TRIANGLES,0,vert_count);
39 }
40 
Load(char * meshfile)41 int Mesh::Load(char* meshfile)
42 {
43 	PHYSFS_file* filehandle=NULL;
44 	char *wholefile;
45 	int filelength;
46 	int vertindex;
47 
48 	string sourcetext;
49 	string lhs;
50 	string rhs;
51 	string curline;
52 
53 	//open the file
54 	filehandle=PHYSFS_openRead(meshfile);
55 
56 	if(!filehandle){
57 		printf("Mesh::Load: Can't open file %s\n",meshfile);
58 		printf("Mesh::Load: PHYSFS says: %s\n",PHYSFS_getLastError());
59 		return 1;
60 	}
61 
62 	//how long is it (bytes)?
63 	filelength=PHYSFS_fileLength(filehandle);
64 
65 	//allocate storage for whole file
66 	wholefile=(char*)malloc(filelength+1);
67 
68 	//dump out of file, into char*
69 	filelength=PHYSFS_read(filehandle,wholefile,1,filelength);
70 	wholefile[filelength]='\0';
71 
72 	//transfer that into a C++ string
73 	sourcetext=wholefile;
74 
75 	//we're done with the C string
76 	free((void*)wholefile);
77 
78 	//and we're done with the file
79 	if(!PHYSFS_close(filehandle))
80 		printf("Mesh::Load: PHYSFS_close failed with error %s\n",PHYSFS_getLastError());
81 
82 
83 
84 	//Get the header line
85 	curline=sourcetext.substr(0,sourcetext.find("\n",0));
86 	sourcetext.erase(0,sourcetext.find("\n",0)+1);
87 	//Do something with it?
88 
89 	//Count the vertex lines
90 	vert_count=0;
91 	for(unsigned int i=0;i<sourcetext.size();i++)
92 					if(sourcetext.c_str()[i]=='V'&&sourcetext.c_str()[i-1]=='\n')
93 									vert_count++;
94 
95 	//malloc for geom vert_count*GL_T2F_C4F_N3F_V3F
96 	geom=(float*)malloc ( vert_count*(2+4+3+3)*sizeof(GLfloat) );
97 	for(int i=0;i<vert_count*12;i++)
98 					geom[i]=666.0f;
99 
100 	vertindex=0;
101 	while(sourcetext.find("\n",0)!=string::npos){
102 		//grab up to the next newline
103 		curline=sourcetext.substr(0,sourcetext.find("\n",0));
104 		//scrub up to and including the newline
105 		sourcetext.erase(0,sourcetext.find("\n",0)+1);
106 
107 		//empty lines, comment lines
108 		if(curline.size() == 0 || curline.substr(0,1)=="#")
109 			continue;
110 
111 		if(curline.find("V",0)==0){
112 			//Read vertex line
113 			if(vertindex+1>vert_count)
114 				printf("Mesh::Load: found too many vertices!\n");
115 			sscanf(curline.c_str(),"V%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f;",
116 						&geom[vertindex*12+0],
117 						&geom[vertindex*12+1],
118 						&geom[vertindex*12+2],
119 						&geom[vertindex*12+3],
120 						&geom[vertindex*12+4],
121 						&geom[vertindex*12+5],
122 						&geom[vertindex*12+6],
123 						&geom[vertindex*12+7],
124 						&geom[vertindex*12+8],
125 						&geom[vertindex*12+9],
126 						&geom[vertindex*12+10],
127 						&geom[vertindex*12+11]
128 						);
129 			vertindex++;
130 		}
131 		else if(curline.find("T",0)==0){
132 			sscanf(curline.c_str(),"T%s ;",skinname);
133 		}
134 	}
135 
136 	strcpy(filename,meshfile);
137 
138 	visual->LoadTexture(skinname);
139 
140 	return 0;
141 }
142 
143