3DS Model Importer By Nino1234
Originally posted at The Home Brew Forums.
Here's a 3ds model importer I've been making for the past little while. I wanted to make it perfect before posting it so its takin me a little while. The main pain was reading in binary mode with blitz but I've sused it out now.
I'm quite chuffed with this as im sick of each modeller having a different asc format. So this importer is a bit more friendly with different platforms. So far I've tried wings and milkshape both of which work fine.
It should be fairly easy to add different chunk sectors to it to get info like meshes,lights & textures. Speaking of which it looks like wings now supports textures, although I've not figured this out yet.
Hope someone finds it of use.
Const max_vertices=10000,max_faces=10000
Global no_objs=2
Type objects
Field object_name$
Field no_vertices
Field no_faces
Field vertex_x#[max_vertices]
Field vertex_y#[max_vertices]
Field vertex_z#[max_vertices]
Field face_a[max_faces]
Field face_b[max_faces]
Field face_c[max_faces]
Field v#[max_vertices]
Field u#[max_vertices]
End Type
Dim model.objects(no_objs)
For x=1 To no_objs
model(x)=New objects
Next
load3DS(model(1),"ninosmodell.3DS")
load3DS(model(2),"spaceship.3DS")
For yt=1 To no_objs
Print "name_of_object: "+model(yt)\object_name$
Print "Number of vertices: "+model(yt)\no_vertices
Print "Number of faces: "+model(yt)\no_faces
Print "press esc!"
While Not KeyHit(1) Wend
For x=0 To model(yt)\no_vertices-1
Print "vertex x: "+model(yt)\vertex_x#[x]
Print "vertex y: "+model(yt)\vertex_y#[x]
Print "vertex z: "+model(yt)\vertex_z#[x]
Print "u: "+model(yt)\u#[x]
Print "v: "+model(yt)\v#[x]
Next
Print "press esc!"
While Not KeyHit(1) Wend
For x=0 To model(yt)\no_faces-1
Print "face_a: "+model(yt)\face_a[x]
Print "face_b: "+model(yt)\face_b[x]
Print "face_c: "+model(yt)\face_c[x]
Next
Print "press esc!"
While Not KeyHit(1) Wend
Next
Function Load3DS(obj.objects,p_filename$)
file=ReadFile(p_filename$)
While (Not Eof(file))
chunk_id = ReadShort(file)
chunk_length = ReadInt(file)
Select chunk_id
Case $4D4D
Case $3D3D
Case $4000
Repeat
char=ReadByte(file)
If char<>"0" then obj\object_name$=obj\object_name$+Chr(char)
endif
Until(char="0")
Case $4100
Case $4110
qty = ReadShort(file)
obj\no_vertices = qty
For it=0 To qty-1
obj\vertex_x#[it]=ReadFloat(file)
obj\vertex_y#[it]=ReadFloat(file)
obj\vertex_z#[it]=ReadFloat(file)
Next
Case $4120
qty = ReadShort(file)
obj\no_faces = qty
For it=0 To qty-1
obj\face_a[it]=ReadShort(file)
obj\face_b[it]=ReadShort(file)
obj\face_c[it]=ReadShort(file)
face_flags=ReadShort(file)
Next
Case $4140
qty = ReadShort(file)
For it=0 To qty-1
obj\u#[it]=ReadFloat(file)
obj\v#[it]=ReadFloat(file)
Next
Default
For x=1 To chunk_length-6
ReadByte(file)
Next
End Select
Wend
CloseFile(file)
End Function



