Loading 3ds Model (MultiObjects)

Started by
2 comments, last by luckyyyyyy 11 years, 6 months ago
EDIT: SOLUTION ONE POST FURTHER!!!!
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:

[size=2]| +--EDIT_OBJECT (0x4000)

[size=2] | | |

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

[size=2] | | | |

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

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

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

[size=2]


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

a

[font="Courier New"][/font]

[font="'Courier New"]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.x, sizeof(float), 1, l_file);

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

fread(&this->vertex.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.a, sizeof(unsigned short), 1, l_file);

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

fread(&this->polygon.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.u, sizeof(float), 1, l_file);

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

}

break;

default:

fseek(l_file, l_chunk_length-6, SEEK_CUR);

}

}

fclose(l_file);







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

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


glBindTexture(GL_TEXTURE_2D, this->texture->get_texture());


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


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


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

Regards

Markus

[/font]

I open sourced my C++/iOS OpenGL 2D RPG engine :-)



See my blog: (Tutorials and GameDev)


[size=2]http://howtomakeitin....wordpress.com/

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




case 0x4000: // Edit_Object
i = 0;
do
{
fread(&l_char, 1, 1, l_file);
this->object[offset].name=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.x, sizeof(float), 1, l_file);
fread(&this->object[offset].vertex.y, sizeof(float), 1, l_file);
fread(&this->object[offset].vertex.z, sizeof(float), 1, l_file);
}

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



and for drawing:




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.a].u,
this->object[j].mapcoord[this->object[j].polygon.a].v);
glVertex3f(this->object[j].vertex[this->object[j].polygon.a].x,
this->object[j].vertex[this->object[j].polygon.a].y,
this->object[j].vertex[this->object[j].polygon.a].z);

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

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

}
}
glEnd();



and this is how the class looks like:



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();
};


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

regards
Markus

I open sourced my C++/iOS OpenGL 2D RPG engine :-)



See my blog: (Tutorials and GameDev)


[size=2]http://howtomakeitin....wordpress.com/

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

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






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

This topic is closed to new replies.

Advertisement