using models in OpenGL (CAUTION! NEWBIE INSIDE!)

Started by
12 comments, last by MrSandman666 23 years, 5 months ago
Thanx man!
Now I finally understand it.

I started diggin into the material you have on your homepage and to say the least: I''m confused! This is all so complicated that I have the feeling that I have to take another approach or find a reeeeeaaally simple tutorial. I mean, your tutorial is prbably great for someone who has some experience doing stuff like that, but for me this is the first time I have to actually write my own engine for my game. Until know I always used some 3rd party engine to get my graphics onto the screen. I have actually no idea what to do with all the data given by Milkshape and - I''m sorry - your tutorial doesn''t help me much. After all it''s basically only some source code with a few comments. I need someone to take my hand, walk me through this step by step and explain every single bit in detail.
It''s a pitty that NeHe doesn''t have a tutorial on Model Loading. The rest of his tutorials are great!
I really am pretty desperate. I need to get this done and I don''t kno how to approach it.
How did you learn it?

"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
Advertisement
How did I learn about model loaders? I wrote one myself. That simple meshloader uses I format that I dreamt up.

3d programs usually export vertices, face and material data. The first part of the parser reads the vertices into an array.
Eg:
0.0 0.0 0.0 // element [ 0 ]
1.0 0.0 0.0 // element [ 1 ]
0.0 1.0 0.0 // element [ 2 ]
0.0 0.0 0.0 // element [ 3 ]


The face data in the file are indices to the vertices in the array. So one particular triangle might look like:
0 1 2
That would reference the coordinates at positions 0, 1 and 2 in the vertex array - thus making a triangle.

I thought this was fairly easy to pick up from what I''d written - I guess I''ll go back to it and rewrite it a bit when I have the time.

Thanks for the feedback,
Paul.

Paul Grovespauls opengl page
Well, I guess I just have to dig my way through the Milkshape Ascii file specs and come up with a new loader from scratch.
But I still got a few questions:

1. Why didn''t you take the normals from Milkshape but generate your own ones?

2. Did any one make it to read the binary version of the Milkshape file format? If so: how? I don''t undertand their concept. Are there tutorials on this? Example loaders?

Any further help woudl be veeery welcome!


"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
1.
Because I have no idea what Mete (the author) was taking when he designed Milkshapes smoothing group mechanism. And I had the code lying around from my Lightwave object parser.

2.
There aren''t any ''formal'' specs. for the binary version of the files. You get a text file with a bunch of C structs in it - if you try to use those in your program it doesn''t seem to work

Its easy to load binary files if you know their structure. Lets take the ASCII simple meshloader file and make it into a binary format. To make it easier to read we could rearrange the file structure so that the object stats are all together. So at the start of your binary file you''d have two unsigned integers that tell us the number of vertices in the object and the number of (triangular) faces it has.

We''ll collect this together in a type called tObjectInfo. Below are the types that we need to define and some variables of the types we need to store our object data.


/* Type definitions */
typedef struct {
unsigned int num_vertices;
unsigned int num_faces;
} tObjectInfo;

typedef float tVertex[3];
typedef unsigned int tIndex[3];

/* Variable declarations */
tObjectInfo object_stats;
tVertex *object_verts;
tIndex *object_indices;


So you''d open the file with fopen (or whatever you use) set in binary mode, then:

fread ( object_stats, ( sizeof ( tObjectInfo )), 1, file );

That reads data that is the size of a tObjectInfo into a record variable "object_stats". The "1" is the number of times to perform the same read. The "file" is the open file pointer.

You''d use these stats to create arrays of vertices and indices:

object_verts = ( tVertex* ) malloc ( sizeof ( tVertex * object_stats.num_vertices ));

object_indices = ( tIndex* ) malloc ( sizeof ( tIndex * object_stats.num_indices ));


In the code above you''re casting the result of a malloc call to the arrays of either tVertex or tIndex. The "sizeof" part makes sure we allocate the correct number of bytes. It''s good pratice to check whether these return NULL or not. If using C++ you''d call the new operator instead of using malloc.

Next, your file would have a bunch of floats that represented the vertices, so you''d read these in like:

fread ( &object_verts, ( sizeof ( tVertex )), object_stats.num_vertices, file );

And then the indices:

fread ( &object_indices, ( sizeof ( tIndex )), object_stats.num_indices, file );

So as you can see at this level, reading binary files is easy. You just have to know the structure. In reality most 3d binary file formats are made up of ''chunks''. Each chunk has an Id header and its size in there to. But as long as you know the format of each chunk then reading them isn''t a problem.

The bonus of having chunks with their size in the header is that if you want to ignore the data in the chunk you can just fseek past it with the current chunk''s size.

Phew! Wrote a lot there - still it helps with the touch typing -- You can tell I''m bored can''t you
Paul Grovespauls opengl page

This topic is closed to new replies.

Advertisement