Advertisement

loading obj files help

Started by April 06, 2005 06:32 PM
18 comments, last by RobTheBloke 19 years, 4 months ago
Quote: Original post by badbrad29
i wasn't planning on saving the files as another format, but i just store them as vertexes and faces. i was just going to use them as they are. i need to do something different because this method is a pain in the ass. i have to edit the files before i can load them. i think i'm going to go with something closer to robs method so it will skip the unneeded lines, but i'm not sure. anyway does anyone know how to get the texture coordinates. i can't save textures with the files so i'm probably going to add an extra line to my obj file that has the adress of the texture and get the coordinates within the program, but how do i do this? i'm pretty new so if im asking an obvious question i'm sorry.


Dude your way is working as IM using it.
To read all obj files use you can skip bits in the readstr loop by adding:

 if((lineBuffer[0] == '#') || (lineBuffer[0] == '\n') || (lineBuffer[0] == 'm') || (lineBuffer[0] == 'g') || (lineBuffer[0] == 's') || (lineBuffer[0] == 'u'))       fgets(lineBuffer,512,objFile);

it leaves these to work with 'v', 'vn', 'vt', 'f' else bad format

dealing with vt and vn are the same as with v

if(lineBuffer[0] == 'v') {		if(lineBuffer[1]=='t'){ 			fgets(lineBuffer,512,objFile);						// Reads In The Next Line Of Text			sscanf(lineBuffer, "%f%f", &ru, &rv);			uvs[uvCount].u = ru, 			uvs[uvCount].v = rv, 			++uvCount;		}       else		   if(lineBuffer[1]=='n'){			fgets(lineBuffer,512,objFile);						// Reads In The Next Line Of Text			sscanf(lineBuffer, "%f%f%f", &rx, &ry, &rz);			normals[normalCount].x = rx, 			normals[normalCount].y = ry, 			normals[normalCount].z = rz,			++normalCount;		   }       else{			fgets(lineBuffer,512,objFile);						// Reads In The Next Line Of Text			sscanf(lineBuffer, "%f%f%f", &rx, &ry, &rz);			vertices[vertexCount].x = rx, 			vertices[vertexCount].y = ry, 			vertices[vertexCount].z = rz;			++vertexCount;			}	   }



Faces will be the tricky bit..Ive got to do those next


Bear in mind that its probably bad code as IM a beginner too

-LW






-------------------LordsWarrior-------------------
Hi,

Ive managed to get my obj models loading and all but im wondering how do i implement groupings? Eventually id want to manipulate my model's limbs and i think groups is the only way this can be done. Am i on the right track here?

Thanks in advance.
Advertisement
Quote: Ive managed to get my obj models loading and all but im wondering how do i implement groupings? Eventually id want to manipulate my model's limbs and i think groups is the only way this can be done. Am i on the right track here?


'ish' though you might want to consider a better format. 'g' flags in the file group faces together into seperate objects. I tend to store them within a structure such as :

struct Group {   int start_face;   int end_face;   std:: string name;};


When drawing you should in theory be able to move seperate groups about by translating or rotation them. There is however a small problem. The vertex data within an obj file is in world space - to animate a hierarchical character you really want the objects in local space relative to the bone you are transforming the geometry by. In short, since the obj file does not contain bones, you can only really move objects around in a fairly basic way. For a fully animated character, this probably isn't sufficient.

If you are using Maya, then consider using the Fbx file format (the devkit is available from the alias site - probably no point in trying to load the file yourself, probably better to write a converter to spit it a file format for your game).

If you fancy a simpler file format, then both the RTG and 3DS formats support bones and animations (The maya bonus tools contains a 3ds exporter which is available for download from the alias site).

Quote: Original post by RobTheBloke
Quote: Original post by LordsWarrior
TO ROb the bloke

I wondered about this .

I see that you scan the file first to count the vertices and face..then make the arrays to hold them with the correct length.

I know a number of programs that take 2 passes at reading in an obj so I guess this is why......


Well, in actual fact, i don't do things like that at all :|

I only bother counting the number of elements if i'm writing C-code, since re-allocating memory in C is a pain. Normally i just use C++ std::vectors to hold the data and just push_back() for each new element. I also don't bother storing faces, instead all faces are triangulated as they are read in, and pushed to the back of a triangles array.

i've got a step-by-step C code version of an objloader on my website (final version uses VBO's, tangent+bi-normal calculation plus a few other goodies). I doubt the code is the most readable ever, but errr....

link

I'll hunt down a C++ version and post that up.


Every loader after the 6th version doesn't work. It crashs if i load a file...
What might be the prob???
Quote: Every loader after the 6th version doesn't work. It crashs if i load a file...
What might be the prob???


err. Dunno. mail me the file and i can have a look. I'd guess that a surface within the file is lacking a uv coords, or normals compared to the rest. I guess that may cause a small problem when converting the data. Alternatively, give this ago. If that doesn't work, i guess it must be the vertex array conversion.



It is the example file girl.obj
Advertisement
all obj-files I load with ObjLoader7.exe, ObjLoader8.exe, ObjLoader9.exe crash!!!
Quote: Original post by Anonymous Poster
all obj-files I load with ObjLoader7.exe, ObjLoader8.exe, ObjLoader9.exe crash!!!


does your graphics card support VBO's?
mmh no, how can i get the libary work without VBO's?
Quote: Original post by Anonymous Poster
mmh no, how can i get the libary work without VBO's?


use version 6, or use the newer one i just posted, http://robthebloke.org/libObj.zip

The newer one correctly handles the mtl files, material groups and seperate meshes. It also provides methods to split the model into seperate surfaces, or into vertex arrays.

This topic is closed to new replies.

Advertisement