PDA

View Full Version : Tiny 3DS loader



virtual
08-09-2010, 11:55 AM
hi
a tiny 3ds warpper draw 1 object wih 1 texture
in fact this is my translation of a c++ code has been found in www.codesampler.com



Unit Mini3ds;

interface

uses windows,opengl;


const
MAX_GEO = 10000;
type

// Vertex
Vertex = record
x,
y,
z : single;
end;

// texture
mapcoord = record
u,
v : single;
end;

// index
polygon = record
a,
b,
c : integer;
end;

// 3d object structure
_object = record
name : array[0..21] of char;
numVerts : integer;
numPolys : integer;
v : array [0..MAX_GEO] of vertex;
p : array [0..MAX_GEO] of polygon;
m : array [0..MAX_GEO] of mapcoord;
end;

// 3ds object
T3DS = object
public
procedure Load(_fn : string);
procedure Draw();
procedure free();
public
obj : _object;
end;


implementation

{ T3DS }

procedure T3DS.Load(_fn: string);
var
_file : file of byte;
i : integer;
chunkID : short;
chunkLength : integer;
tmp : char;
useless : short;
from : integer;
_fsize : integer;
label begincase;
begin

AssignFile(_file,_fn);
Reset(_file);
_fsize := filesize(_file);

while ( filepos(_file) < _fsize) do
begin
begincase:
BlockRead(_file,chunkID,2);
BlockRead(_file,chunkLength,4);

case chunkID of

$4d4d : begin end;
$3d3d : begin end;
$4000 :
for i:=0 to 20-1 do
begin
Blockread (_file,tmp, 1);
obj.name[i]:=tmp;
if pointer(tmp) = NIL then
goto begincase;
end;

$3f80 :begin end;
$4100 :begin end;
$4110 :
begin
BlockRead(_file,obj.numVerts,2);
for i:=0 to obj.numVerts - 1 do
begin
BlockRead (_file, obj.v[i].x,4);
BlockRead (_file, obj.v[i].y,4);
BlockRead (_file, obj.v[i].z,4);
end;
end;

$4120 :
begin
BlockRead (_file,obj.numPolys, 2);
for i:=0 to obj.numPolys - 1 do
begin
BlockRead (_file,obj.p[i].a, 2);
BlockRead (_file,obj.p[i].b, 2);
BlockRead (_file,obj.p[i].c, 2);
BlockRead (_file,useless, 2);
end;
end;

$4140 :
begin
BlockRead (_file , useless, 2);
for i:=0 to obj.numVerts - 1 do
begin
BlockRead (_file , obj.m[i].u , 4);
BlockRead (_file , obj.m[i].v , 4);
end;
end;
else
begin
from := filePos(_file);
seek(_file,from + chunkLength-6);
end;
end;
end;
closefile(_file);
end;

procedure T3DS.Draw();
var i : integer;
begin

glBegin(GL_TRIANGLES);
for i:=0 to obj.numPolys - 1 do
begin
glTexCoord2f( obj.m[ obj.p[i].a ].u,obj.m[ obj.p[i].a ].v);
glVertex3f( obj.v[ obj.p[i].a ].x,
obj.v[ obj.p[i].a ].y,
obj.v[ obj.p[i].a ].z);

glTexCoord2f( obj.m[ obj.p[i].b ].u,obj.m[ obj.p[i].b ].v);
glVertex3f( obj.v[ obj.p[i].b ].x,
obj.v[ obj.p[i].b ].y,
obj.v[ obj.p[i].b ].z);

glTexCoord2f( obj.m[ obj.p[i].c ].u,obj.m[ obj.p[i].c ].v);
glVertex3f( obj.v[ obj.p[i].c ].x,
obj.v[ obj.p[i].c ].y,
obj.v[ obj.p[i].c ].z);
end;

glEnd();

end;

procedure T3DS.free;
begin
//
end.


virtual

WILL
10-09-2010, 12:20 AM
Cool. Is this something that you will be continually updating?

virtual
10-09-2010, 02:31 PM
this way we don't need to update

User137
10-09-2010, 03:27 PM
I was hoping it'd have bone structure i could learn from :P Useful regardless.

noeska
11-09-2010, 05:19 PM
Oh another 3ds loader for pascal and opengl :-)

Have a look at mine too: http://www.noeska.com/dogl/glmodel.aspx . That also does bone animation for milkshape ascii files. But afaik mine might have lost its tinyness...

WILL
14-09-2010, 06:40 PM
Wow, I just realized just how tiny this little loader really is. Now you called it a wrapper, but in reality it's just a mini library for use with OpenGL, right? From the source code above it doesn't seem to need an extra API file to work.

Brainer
14-09-2010, 08:08 PM
Don't take it as a criticism, but I'd really love to see the code more Delphi-like ("objects" were deprecated a long time ago). Plus, just like User137 said, animations would be most welcome, too. Also, some normal calculation code can fit in too, if you ask me.

Enough bitchin, nice piece of work, virtual. :) Keep up the great work, hope you can expand the code.

virtual
15-09-2010, 09:50 AM
glad that you like it

i think this is the most minimal 3ds loader , i used it to draw low res objects. ( i use another loader for other purposes)
another thing can benefit is to write your own simple 3d file format based on the previous data structure

Ingemar
30-06-2011, 02:59 PM
Can you point me to a 3ds file for which this loader works? I have tried it on some, but they seem to include multiple sets of vertices, which overwrite each other in this loader. I suppose these are complex shapes, unsupported by this code, right?

Ingemar
30-06-2011, 03:19 PM
Can you point me to a 3ds file for which this loader works? I have tried it on some, but they seem to include multiple sets of vertices, which overwrite each other in this loader. I suppose these are complex shapes, unsupported by this code, right?

I found one answer in my own notes (after trying the C++ original of this code):

"62standing-woman.3ds"

I don't know where I got that, but that one works - but most others I have tried fail. That limits the usage quite a bit.