Sign in to follow this  
MarkusPfundstein

Loading 3ds Model (MultiObjects)

Recommended Posts

[b]EDIT: SOLUTION ONE POST FURTHER!!!![/b]
Hello everybody..

for my 3d engine i've written a loader for 3ds models based on some tutorial i found on the net called spacesimulator or s.th. like this .-). It works perfectly fine when i import a model which consists out of one mesh.. texture and model will be displayed correctly. But when i load in an object which consists of multiple meshes, lets say a car.. which consists out of the main part and 4 weels, only one wheel gets rendered and the rest of the 4 parts not. i probably think that this is because i need to read out the other meshes as well and displaying them.

the 3ds file looks like this:
[CODE]
[size=2]| +--EDIT_OBJECT (0x4000)[/size]

[size=2] | | |[/size]

[size=2] | | +--OBJ_TRIMESH (0x4100) [/size]

[size=2] | | | |[/size]

[size=2] | | | +--TRI_VERTEXL (0x4110) [/size]

[size=2] | | | +--TRI_VERTEXOPTIONS (0x4111)[/size]

[size=2] | | | +--TRI_MAPPINGCOORS (0x4140) [/size]

[size=2][/CODE][/size]

[font="Courier New"][size="2"]and i read out 4110 to 4140 like this:[/size][/font]

a

[font="Courier New"][size="2"][CODE][/size][/font]

[font="'Courier New"][size="2"]while blabla: switch (chunk_id)

case 0x4100: // Object Trimesh

break;

case 0x4110: // Tri Vertex (Vertices List)

fread(&l_qty, sizeof(unsigned short), 1, l_file);

this->vertices_qty = l_qty;



for (i = 0; i < l_qty; i++)

{

fread(&this->vertex[i].x, sizeof(float), 1, l_file);

fread(&this->vertex[i].y, sizeof(float), 1, l_file);

fread(&this->vertex[i].z, sizeof(float), 1, l_file);

}

break;

case 0x4120:

fread(&l_qty, sizeof(unsigned short), 1, l_file);

this->polygons_qty = l_qty;



for (i = 0; i < l_qty; i++)

{

fread(&this->polygon[i].a, sizeof(unsigned short), 1, l_file);

fread(&this->polygon[i].b, sizeof(unsigned short), 1, l_file);

fread(&this->polygon[i].c, sizeof(unsigned short), 1, l_file);

fread(&l_face_flags, sizeof(unsigned short), 1, l_file);

}

break;

case 0x4140:

fread(&l_qty, sizeof(unsigned short), 1, l_file);

for (i = 0; i < l_qty; i++)

{

fread(&this->mapcoord[i].u, sizeof(float), 1, l_file);

fread(&this->mapcoord[i].v, sizeof(float), 1, l_file);

}

break;

default:

fseek(l_file, l_chunk_length-6, SEEK_CUR);

}

}

fclose(l_file);




[/CODE]


It wonders me that i skip through the different vertices, polygons and tex coordinates but not through the object. where in the 3ds files are they defined then? so where is the point where the next objects begins...

[/size][/font][font="Courier New"][size="2"] Hope someone can help me.. it would really like to understand the concept ..
For completeness: Thats how i draw them:
[/size][/font]
[font="Courier New"][CODE]


[size="2"] glBindTexture(GL_TEXTURE_2D, this->texture->get_texture());[/size]
[size="2"]
[/size]
[size="2"] glBegin(GL_TRIANGLES);[/size]
[size="2"] for (unsigned int i = 0; i < this->polygons_qty; i++)[/size]
[size="2"] {[/size]
[size="2"] glTexCoord2f(this->mapcoord[this->polygon[i].a].u,[/size]
[size="2"] this->mapcoord[this->polygon[i].a].v);[/size]
[size="2"] glVertex3f(this->vertex[this->polygon[i].a].x,[/size]
[size="2"] this->vertex[this->polygon[i].a].y,[/size]
[size="2"] this->vertex[this->polygon[i].a].z);[/size]
[size="2"]
[/size]
[size="2"] glTexCoord2f(this->mapcoord[this->polygon[i].b].u,[/size]
[size="2"] this->mapcoord[this->polygon[i].b].v);[/size]
[size="2"] glVertex3f(this->vertex[this->polygon[i].b].x,[/size]
[size="2"] this->vertex[this->polygon[i].b].y,[/size]
[size="2"] this->vertex[this->polygon[i].b].z);[/size]
[size="2"]
[/size]
[size="2"] glTexCoord2f(this->mapcoord[this->polygon[i].c].u,[/size]
[size="2"] this->mapcoord[this->polygon[i].c].v);[/size]
[size="2"] glVertex3f(this->vertex[this->polygon[i].c].x,[/size]
[size="2"] this->vertex[this->polygon[i].c].y,[/size]
[size="2"] this->vertex[this->polygon[i].c].z);[/size]
[size="2"] }[/size]
[size="2"] glEnd();[/size]
[size="2"][/CODE][/size]
[size="2"]Regards[/size]

[size="2"]Markus[/size]

[/font]

Share this post


Link to post
Share on other sites
Hello.. I just wanted to let you guys know that i solved the problems!!!!

the solution was actually pretty simple.. everytime when the parser goes through 0x4100 -> OBJECT TRIMESH

it begins to read a new Object.. so the chain is

object 1
0x4000 -> 0x4100 -> vertices, polygon, faces

object 2
0x4000 -> 0x4100 -> vertices, polygon, faces

and so on...

now everytime when i am in 0x4000 i increase my object counter and write all the future information in Objects[object_counter]..

Objects is a struct which containts vertices, polygons & tex coordinates..

by drawing i do the same!!

here is part of the code:

[CODE]


case 0x4000: // Edit_Object
i = 0;
do
{
fread(&l_char, 1, 1, l_file);
this->object[offset].name[i]=l_char;
i++;
} while(l_char != '\0' && i < 20);

break;
case 0x4100: // Object Trimesh
offset = this->objects_qty;
this->objects_qty+=1; // by car we go in here 5 times

break;
case 0x4110: // Tri Vertex (Vertices List)
fread(&l_qty, sizeof(unsigned short), 1, l_file);

for (i = 0; i < l_qty; i++)
{
fread(&this->object[offset].vertex[i].x, sizeof(float), 1, l_file);
fread(&this->object[offset].vertex[i].y, sizeof(float), 1, l_file);
fread(&this->object[offset].vertex[i].z, sizeof(float), 1, l_file);
}

this->object[offset].vertices_qty = l_qty;

[/CODE]

and for drawing:

[CODE]


glBegin(GL_TRIANGLES);
for (unsigned int j = 0; j < this->objects_qty; j++)
{
for (unsigned int i = 0; i < this->object[j].polygons_qty; i++)
{
glTexCoord2f(this->object[j].mapcoord[this->object[j].polygon[i].a].u,
this->object[j].mapcoord[this->object[j].polygon[i].a].v);
glVertex3f(this->object[j].vertex[this->object[j].polygon[i].a].x,
this->object[j].vertex[this->object[j].polygon[i].a].y,
this->object[j].vertex[this->object[j].polygon[i].a].z);

glTexCoord2f(this->object[j].mapcoord[this->object[j].polygon[i].b].u,
this->object[j].mapcoord[this->object[j].polygon[i].b].v);
glVertex3f(this->object[j].vertex[this->object[j].polygon[i].b].x,
this->object[j].vertex[this->object[j].polygon[i].b].y,
this->object[j].vertex[this->object[j].polygon[i].b].z);

glTexCoord2f(this->object[j].mapcoord[this->object[j].polygon[i].c].u,
this->object[j].mapcoord[this->object[j].polygon[i].c].v);
glVertex3f(this->object[j].vertex[this->object[j].polygon[i].c].x,
this->object[j].vertex[this->object[j].polygon[i].c].y,
this->object[j].vertex[this->object[j].polygon[i].c].z);

}
}
glEnd();

[/CODE]

and this is how the class looks like:

[CODE]

class Object3ds : public Mesh
{
private:
typedef struct
{
float x;
float y;
float z;
} vertex_type;

typedef struct
{
unsigned short a;
unsigned short b;
unsigned short c;
} polygon_type;

typedef struct
{
float u;
float v;
} mapcoord_type;

float scalex;
float scaley;
float scalez;

struct tObject
{
vertex_type vertex[MAX_VERTICES];
mapcoord_type mapcoord[MAX_VERTICES];
polygon_type polygon[MAX_POLYGONS];
unsigned int vertices_qty;
unsigned int polygons_qty;
char name[20];
};

tObject object[30];
unsigned int objects_qty;


public:
Object3ds(char *filename, char *_texture);
Object3ds(char *filename, char *_texture, float _scalex, float _scaley, float _scalez);

bool Load3DS(char *filename);
void draw();
};
[/CODE]

THANKS TO THE HOLY DEBUGGER OF VISUAL C++ ;-) I spent ours of analyizing this f**** parser structure..

regards
Markus

Share this post


Link to post
Share on other sites
luckyyyyyy    145
I can successfully read and load the objects like ur code. But I am unable to read Color info.. means color of objects that we reading from the file.
actually there are many ways to read 3DS chunks on the internet but in our way how can we read color info ?
U can see my code here..
[CODE]
//OBJ_TRIMESH
//TRI_VERTEXL
//TRI_FACEL1
//TRI_MAPPINGCOORS
case 0xa020: // diffuse color
if(1)
{
case 0×0010: // Color F
fread (&red, sizeof(float), 1, l_file);
fread (&green, sizeof(float), 1, l_file);
fread (&blue, sizeof(float), 1, l_file);
//if (!ReadFloat(fp, red)) return FALSE;
//if (!ReadFloat(fp, green)) return FALSE;
//if (!ReadFloat(fp, blue)) return FALSE;
break;
case 0×0011: // Color 24
fread (&red, sizeof(float), 1, l_file);
fread (&green, sizeof(float), 1, l_file);
fread (&blue, sizeof(float), 1, l_file);
//if (!ReadUByte(fp, tmp)) return FALSE;
//red = (float) tmp / (float) 255.0;
//if (!ReadUByte(fp, tmp)) return FALSE;
//green = (float) tmp / (float) 255.0;
//if (!ReadUByte(fp, tmp)) return FALSE;
//blue = (float) tmp / (float) 255.0;
break;
}
break;
[/CODE]



[quote]

Think of Design with Dimensions
http://real3d.pk
[/quote]

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