Skip to main content
GameDev.net gamedev.net
🔒 Locked

3d file (.obj) loader for symbian

Started by Miranda May 5, 2005 at 7:04 PM 3 replies 3k views
Original Post
Miranda
Miranda
Hi guys, I'm having some trouble creating a 3d file loader for symbian. Basically I want to be able to read in the text and float data from a .obj file into my symbian program as vertex, normal amd face arrays. I have done this in C++ before, but I am struggling with Symbian's ReadStreams and >> operator. This is the code I am trying to port to Symbian:

bool geometry::buildFromFile(string filename) {
	vertexN *vert;
	face *fac;
	vertex *normal;
	string _type = "";
	string tempStr = "";
	int normalPtr=0;

	bool endLine = false;
	const int lineSize = 50;

	ifstream meshFile(filename.c_str());

	if (!meshFile) {
		cout << "geometry file failed" << filename << endl;
		return false;
	} else {
		//while still data in file
		while(meshFile) {
			meshFile >> _type; 
			//load verticies
			if (_type == "v") {
				vert = new vertexN;
				meshFile >> vert->x >> vert->y >> vert->z;
				this->vertList.push_back(vert);
				this->numVertices++;
			} else if (_type == "vn") {				
				normal = new vertex;
				meshFile >> normal->x >> normal->y >> normal->z;			
				normalList.push_back(normal);
			} else if (_type == "f") {
				fac = new face;

				for(int i=0;i<3;i++){
					meshFile >> tempStr;
					// read in vertex & normal
					fac->verts=(int)atof(tempStr.substr(0,tempStr.find("/",0)).c_str())-1;
					normalPtr = (int)atof(tempStr.substr(tempStr.rfind("/",tempStr.length()-1)+1).c_str())-1;

					// assign face normal 
					fac->normal=normalPtr;
				}

				fac->numEdges = 3;
				this->numFaces++;
				this->faceList.push_back(fac);
			} 
			
		}
		addMaterials(filename);
	}
	return true;
}



EDIT: hmm it seems everytime a >> appears in my code it comes out as & g t; & g t; I hope it is still readable... Do you you think this is a good way to read in a 3d file? Will it be hard to get it into the required arrays for glDrawElements? Any help / advice would be greatly appreciated! Thanks, Miranda
serg3d
serg3d
Quote:
Original post by Miranda
Do you you think this is a good way to read in a 3d file ?
Miranda


No. Parsing obj on the device is a waste IMO. If I would need a lot of files I'd do my own format, and convert to it all data on PC. For now I'm using only several small models, and read only textures. For vertices I just generate cpp files with const global arrays.
GreenToad
GreenToad
If the ReadStreams is messing you up, you can always try symbian's implementation of the standard C file loading, like fopen, fread, etc. I've been able to get it to work in the emulator, but haven't tried it on the phone.

I too agree with serg3d. Unless you are using a ton of models, just convert them into .cpp files and compile them in with the executable. In my 3d games targeted for cellphones, I just converted the models from the .x format to .cpp files (I also converted .bmp files into big char arrays). It just makes things easier (and you get much faster load times).
Miranda
Miranda
Thanks for the replys.

I did originally want to use a .h file with the arrays in it, as I only need one model. But I couldn't find an easy way to convert a .obj file with thousands of lines in the annoying obj format into an array without having to give myself rsi taking out all the bits I don't need (ie most of the face data).

Do you have any suggestion to convert and obj file into arrays?

Thanks muchly,
Miranda
serg3d
serg3d
I'm using .X format. It's easy to convert by cloning fvf mesh, locking vertex/index buffers and printing them to cpp file. .obj is not so convinient, but you can do similar - parse obj jile, build vertex and index buffers and print, or convert .obj to .x and work from the .x. Extracting from .x is easy, especially if you have only one texture.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.