Sign in to follow this  
medevilenemy

Model Loading and Drawing

Recommended Posts

Could anyone please tell me how to load and draw a milkshape3d model in OpenGL without requiring a billion different files? I havent had much success searching for tutorials.

Share this post


Link to post
Share on other sites
The code to it actually seems to spread over many files, and it's rather difficult to trace it all. What i'm seeking is a step by step tutorial or [I]simple[/I] code to load and draw a model.

If there is no such thing for Milkshape3d, Simmilar things for MD2 models would be helpful too.

Thanks!

Share this post


Link to post
Share on other sites
The code to load the mode I'm seeing there is quite neatly in milkshapemodel.h/cpp. Unless you just use the same internal format for models you won't be able to just copy/paste any of it anyway. Every loader will have to load it into _your_ model format. The rest is texture loading, which doesn't have much to do with model loading and hence really doesn't belong into the same file and the app itself.

If you didn't decide how to internally store your models or don't have a texture loader or the rest set up to draw something then all these things should be higher on your todo list than loading model data.

But if it makes you feel better, you can just copy/paste the code from all six files into a single messy cpp file ,-)

However, loading a model will always be a more or less complex issue, because most format will have lots of data you might not care about. That's a nice property of 3ds files, though you need to know how it's structured. Reading one is pretty simple in concept if you look at it like a tree or xml document.


while not end of file
read chunk header (id, size)
switch id
"node" chunk: do nothing
"leaf" or "data" chunk: read data according to type
default (unknown or "dont care" chunk): skip size bytes and ignore


Tons of tutorials, though some are annoyingly having a function for each kind of chunk and subchunk and making it almost impossible to follow the code. Might be bad practice to do everything in one huge switch statement, but at least you will quickly find what you're looking for.

And then you will soon decide that making your own format that's closer to your internal representation would be much easier to load and start writing either exporters for your modelling software or a converter for existing formats.

Anyway, you won't get around looking at tutorials as a guide and not code to copy 1:1. All the code you care about should be in milkshapemodel.cpp and in a best case scenario it shouldn't be more than complementing a description of the format anyway.

Share this post


Link to post
Share on other sites
ok... after some research and experimentation... i'd decided to go with .obj format models. Now I just have to find a working loader/drawer (that i can somehow get to draw at starting coordinates I specify)

Any suggestions? (sorry for the past couple posts)

Share this post


Link to post
Share on other sites
UPDATE:

I've managed to implement a functioning OBJ loader/renderer and I've managed to map textures to the models. My only problem is that the sides of the models (depth -- going "down") show only stripes down the center. I dont know the cause.

Here's My Rendering/Texture Mapping Code:

void DrawObj(ObjModel dat, float x, float y, float z) // Draw OBJ model 'dat' at X, Y, Z
{
int i;
int j;
for(i=0; i<data.NumTriangle; i++)
{
glBegin(GL_TRIANGLES);
for(j=0; j<3; j++)
{
if(dat.NumTexCoord > 0)
glTexCoord2f(dat.TexCoordArray[dat.TriangleArray[i].TexCoord[j]].U, dat.TexCoordArray[dat.TriangleArray[i].TexCoord[j]].V);
glNormal3d(dat.VertexArray[dat.TriangleArray[i].Vertex[j]].X+x, dat.VertexArray[dat.TriangleArray[i].Vertex[j]].Y+y, dat.VertexArray[dat.TriangleArray[i].Vertex[j]].Z+z);
glVertex3d(dat.VertexArray[dat.TriangleArray[i].Vertex[j]].X+x, dat.VertexArray[dat.TriangleArray[i].Vertex[j]].Y+y, dat.VertexArray[dat.TriangleArray[i].Vertex[j]].Z+z);
}
glEnd();
}
}

Sorry for the poor formatting... I dont know how to insert code properly.

EDIT: 3:55 EST
I've tried it with another shape of model (one with a bump on the front) and it seems to try to texture that bump seperately... any suggestions?

Share this post


Link to post
Share on other sites
hi

if(dat.NumTexCoord > 0) you could check this at the top of your function since it does not depend on your loop variables

what do you mean with stripes down the center

triangles with a vertex = (0,0,0)? if thats a case probably a loading bug
can you show us a screenshot of the bug?

also try to use vertex arrays if possible

Share this post


Link to post
Share on other sites
I'll try to explain:

Think of the model as a cube...
The side facing you "front" is properly textured.
The sides (left, right, top, bottom) both have a stripe running down the center.

This might be a result of the way i'm texturing them, but i dont know.

EDIT: Oh, I managed to fix the problem with the "bump" i added. The problem was that the texture coordinates werent set properly... I have a couple of screenshots... but i dont know how to post them here.

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