Jump to content
  • Advertisement
Sign in to follow this  

Writing a model loader

This topic is 4125 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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


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



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



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
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
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
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!