Sign in to follow this  
Opwiz

Writing a model loader

Recommended Posts

Opwiz    348
Hello I'm developing a 3d engine and I'm at the point where I want to be able to load some models. I'm not really sure where to begin so I wonder if anyone has tried writing their own model loader. How do I approach this problem? Should I start with creating my own model format? I'm using Blender to create models so is it a good idea to create a loader/exporter for the .blender format? Is there any resources on the web on this? Thanks for your time.

Share this post


Link to post
Share on other sites
Lord_Evil    680
Use the .3ds format for a start. There's plenty of material on the web dealing with that topic.

If you're familiar with that format you are free to go for further formats or write your own format (not necessary unless you need very special data).

Share this post


Link to post
Share on other sites
Daaark    3553
Quote:
Original post by Opwiz
I wonder if anyone has tried writing their own model loader?
Of course.

First, you will need to define some kind of class, or structure to hold your model, wich will need a few sub structures/classes.

You will need something to hold your vertices, normals, texure UVs, and triangles. (later on, you will need other things, like perhaps edge loops if you want to cast shadows)

Vertices and Normals being basically vectors. (x,y,z). texture UVs need 2 floats, (or just use a 2d vector). The triangle class will need to store which 3 vertices, normals, and UV sets belong to each triangle.

So your model class is a collection of vertices, normals, uvs, and triangles all in one. It might look something like this:

class CModel
{

private:

int m_Vertices; //stores the amount of vertices
int m_Normals; //stores the amount of normals
int m_TexCoords; //stores the amount of texcoords
int m_Triangles; //stores the amount of triangles

VERTEXTYPE *m_vtx;
NORMALTYPE *m_nrm;
TEXCOORDTYPE *m_uv;
TRIANGLETYPE *m_tri;

MATERIALTYPE *m_mat;

float* m_va; //for vertex vertex array
float* m_na; //for normal vertex array
float* m_ua; //for uv vertex array

public:

CModel();
~CModel();

bool CreateVertexArrays(void);
bool LoadXXX(char *filename);
void Release(void);
void Render(void);
void RenderArray(void);

};


Obj is an easy format to load, it's own simple ascii text. You can find the file format specs at wotsit.org Called WaveFront OBJ or something similar. A little tip for OBJ files is that the array indices in these files start at 1 and not 0 like you would expect.

OBJ can hold many different shapes, not just triangles. So you will have to break them down into triangles in your loader, or just make sure to do it in your modelling program before you export your data.

Share this post


Link to post
Share on other sites
cignox1    735
This is my own 3ds loader. It is simple and only supports basic features of the format (i.e. no animation yet). If you want to give it a look there are also a few files that cover the format structure and the doc about how to use the code.

BUT I suggest you to choose another format: while I was building the loader I was told that 3ds only support one uv coords per vertex, so if you plain to implement multitexturing you will need to get the additional uv coords somewhere else.
Someone correct me if I'm wrong, of course.

I've hears good things about COLLADA and the lightwave format (IIRC Blender supports the first one).

Share this post


Link to post
Share on other sites
Jason Z    6436
Milkshape is also a good one to start out with:

The modelling program itself is only $20!

Plus you get an SDK to go along with it that shows how to load the file format!
Plus there is a dedicated forum that you can ask questions at!
Plus the format covers bone animations!
Plus the modelling package can import about 100 other formats!

That is a whole lotta value for only $20!

(No, I don't get kickbacks from Milkshape, its just a good value)

Share this post


Link to post
Share on other sites
Opwiz    348
Thanks for the replies! I'll let you know if I make any progress. I'm currently playing around with the XNA framework and the .x model format.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this