Author Topic: 3ds model ripper  (Read 7345 times)

0 Members and 3 Guests are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
3ds model ripper
« on: April 26, 2007 »
heres a 3ds model ripper ive coded up quickly for my own purposes i thought it might be usefull to you guys so im posting it.

Code: [Select]
Dim Shared As String ModelPath
ModelPath = "Sphere.3ds"
Const OutPutPath = "Sphere.Bi"

Type Vertices
   
     X As Double
     Y As Double
     Z As Double
     U As Double
     V As Double
     
End Type

Type Faces
   
     A As Integer
     B As Integer
     C As Integer
     
End Type

Type Entity3ds
   
    ModelName As STRING
    NoVertices As INTEGER
    NoFaces As INTEGER
     
    Vertex As Vertices PTR
    Face As Faces PTR
   
End Type



Declare     Function    Load3ds             ( BYVAL FileName As STRING ) As Entity3ds PTR
Declare     Sub         ConvertToDataFile   ( Entity As Entity3ds PTR , BYVAL OutPath As STRING )
Declare     Sub         Delete              ( Entity As Entity3ds PTR )
   
 
 
Dim Shared As Entity3ds PTR TestEntity

TestEntity = Load3ds( ModelPath )
ConvertToDataFile( TestEntity , OutPutPath )
Delete( TestEntity )


Function Load3ds( BYVAL FileName As STRING ) As Entity3ds PTR
   
        dim Entity As Entity3ds ptr
         
        Entity = Callocate(Len(Entity3ds))
         
         dim vertex_x as single
         dim vertex_y as single
         dim vertex_z as single
         dim face_p1 as ushort
         dim face_p2 as ushort
         dim face_p3 as ushort
         dim vertex_u as single
         dim vertex_v as single
         dim chunk_id as ushort
         dim chunk_len as integer
         dim char as byte
         dim qty as ushort
         dim face_flags as ushort
         dim filepointer as ubyte
         
         dim as integer file = freefile
         
         open filename for binary as #file
                 
         While (1)
             
               if eof(1) then
                         close #file
                         return Entity
               endif
               
               get #file, , chunk_id
               get #file, , chunk_len
               
               Select case chunk_id
                      Case &h4D4D
                      Case &h3D3D
          Case &h4000
                         
                                do
                                     get #file, ,char
                                     If Char<>0 Then Entity->ModelName = Entity->ModelName+Chr(Char)
                                loop Until(char=0)
                               
          Case &h4100
              Case &h4110
                         
                get #file, ,qty
                                entity->NoVertices = qty
                                entity->vertex = callocate( Entity->NoVertices * len(vertices) )
                                For i=0 To qty-1
                                   
                                        get #file, ,vertex_x
                                        get #file, ,vertex_y
                                        get #file, ,vertex_z
                                        Entity->vertex[i]->x = vertex_x
                                        Entity->vertex[i]->y = vertex_y
                    Entity->vertex[i]->z = vertex_z
                                       
                                Next
                               
          Case &h4120
                         
                                get #file, ,qty
                                Entity->NoFaces = qty
                                Entity->face = callocate( Entity->NoFaces * len(faces) )
                                For i=0 To qty-1
                                   
                                        get #file, ,face_p1
                                        get #file, ,face_p2
                                        get #file, ,face_p3
                                       
                    Entity->face[i]->a = face_p1
                    Entity->face[i]->b = face_p2
                    Entity->face[i]->c = face_p3
                                        get #file, ,face_flags
                                       
                Next
                               
          Case &h4140
                         
                                get #file, ,qty
                For i=0 To qty-1
                                   
                                        get #file, ,vertex_u
                                        get #file, ,vertex_v
                                       
                                       Entity->vertex[i]->u = vertex_u
                                       Entity->vertex[i]->v = vertex_v
                                       
                        Next
                               
                        case  else
                         
                              for x = 1 to chunk_len - 6
                                    get #file, ,filepointer
                              next
               End Select

         Wend
         
         return 0
         
End Function



Sub ConvertToDataFile( Entity As Entity3ds PTR , BYVAL OutPath As STRING )
   
        File = FreeFile
        Open OutPutPath For Binary As #File
       
        Put #File , , Entity->ModelName+":"+Chr$(13)+Chr$(10)
        Put #File , , Chr$(13)+Chr$(10)+"'REM NoVerttices , NoFaces"+Chr$(13)+Chr$(10)
        Put #File , , "Data "+Str(Entity->NoVertices)+" , "+Str(Entity->NoFaces)+Chr$(13)+Chr$(10)
        Put #File , , Chr$(13)+Chr$(10)+"'REM Vertices"+Chr$(13)+Chr$(10)
       
        For x = 0 To Entity->NoVertices-1
                Put #File , , "Data "+Str(Entity->Vertex[ X ]->X)+" , "+Str(Entity->Vertex[ X ]->Y)+" , "+Str(Entity->Vertex[ X ]->Z)+Chr$(13)+Chr$(10)
        Next
        Put #File , , Chr$(13)+Chr$(10)+"'REM UV`s"+Chr$(13)+Chr$(10)
        For x = 0 To Entity->NoVertices-1
                Put #File , , "Data "+Str(Entity->Vertex[ X ]->U)+" , "+Str(Entity->Vertex[ X ]->V)+Chr$(13)+Chr$(10)
        Next
        Put #File , , Chr$(13)+Chr$(10)+"'Face Connections "+Chr$(13)+Chr$(10)
        For x = 0 To Entity->NoFaces-1
                Put #File , , "Data "+Str(Entity->Face[ X ]->A)+" , "+Str(Entity->Face[ X ]->B)+" , "+Str(Entity->Face[ X ]->C)+Chr$(13)+Chr$(10)
        Next
        Close
       
End Sub



Sub Delete( Entity As Entity3ds PTR )
    If (Entity) Then
            If (Entity->Vertex) Then
                    Deallocate(Entity->Vertex)
            EndIf
            If (Entity->Face) Then
                    Deallocate(Entity->Face)
            EndIf
            Deallocate(Entity)
    Endif
End Sub

all it does is rip the vertex face and uv data out of a 3ds file and put it in a .bi file in data statments itll be good for bulding complex models into the exe

heres the exe and a test model.
« Last Edit: April 26, 2007 by ninogenio »
Challenge Trophies Won:

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: 3ds model ripper
« Reply #1 on: April 26, 2007 »
Nice 1 nino  :cheers: very much k+  :)
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: 3ds model ripper
« Reply #2 on: April 26, 2007 »
cheers mate! its nothing much really but it should make life a bit easyr.
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: 3ds model ripper
« Reply #3 on: April 26, 2007 »
Nice work Nino.  :cheers:

you can rip a texture too , it is very simple , and be careful the 3Ds file may contains more than one object , and each object has it's own material.

Edited:

here is a useful link for 3DS loader Nino. i have used it when i made a loader for my BCXDX
http://www.spacesimulator.net/tut4_3dsloader.html
« Last Edit: April 26, 2007 by Emil_halim »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: 3ds model ripper
« Reply #4 on: April 26, 2007 »
yeah thats a good link mate :cheers:.

i really should add meterials and texture loading mabey even lights too. one thing ive been meaning to ask is that dosome people actually store there textures inside the 3ds file as ive had a couple of models that are 1mbin size and the people clame the texture is in the file.
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: 3ds model ripper
« Reply #5 on: April 26, 2007 »
Thats a very cool tool Nino, welldone on it's creation. :)
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: 3ds model ripper
« Reply #6 on: April 26, 2007 »
That has to be worth some good karma.

Have some of the good stuff Nino!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: 3ds model ripper
« Reply #7 on: April 26, 2007 »
handy stuff nino, k++

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: 3ds model ripper
« Reply #8 on: April 26, 2007 »
 :cheers: guys!

actually i think im going to add a lot more stuff to this but ill keep the forum updated on progress ;)
« Last Edit: April 26, 2007 by ninogenio »
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: 3ds model ripper
« Reply #9 on: April 26, 2007 »
dosome people actually store there textures inside the 3ds file as ive had a couple of models that are 1mbin size and the people clame the texture is in the file.

as far as i know , all textuers of 3DS file must exist outside the file and they must be in the same folder of 3DS file.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: 3ds model ripper
« Reply #10 on: April 26, 2007 »
i think there is a way of storing the texture bmp style in the 3ds file not sure where or how though!

i personally wouldnt like to have to load the texture out of the file but im curious if this is the case.
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: 3ds model ripper
« Reply #11 on: April 27, 2007 »

Of course there is a way to merge textures within the 3DS file , but that will not be a standard one, so any one will use your mod 3ds file should load it with your loader.

I think you can put a unique tag for a texture Chunk in your 3DS file then put the length of it then put the actual data of texture.     

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: 3ds model ripper
« Reply #12 on: May 03, 2007 »

@ Nino :

what is the license of your 3DS loader , can I use it or not ?

actually I am very lazy to search in my old BCXDX code to reuse my 3DS loader, you know I gave up using BCX 2 years ago , and your one is more simple than mine , so if you do not mind I want to use it. 

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: 3ds model ripper
« Reply #13 on: May 04, 2007 »
yep by all means use as you like mate and you dont have to credit me or anything like that  :cheers: .
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: 3ds model ripper
« Reply #14 on: May 05, 2007 »
Thats very modest of you nino to share you work so freely :D but I think its okay to accept credit for your work you deserve the recognition and I think most people would be happy to include it

Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: 3ds model ripper
« Reply #15 on: May 05, 2007 »

thanks Nino very much for sharing your code.  :)

and ofcourse as rain_storm said , you deserve the credit and will do it.

Offline BadMrBox

  • ZX 81
  • *
  • Posts: 22
  • Karma: 0
    • View Profile
    • Ragnarok Games
Re: 3ds model ripper
« Reply #16 on: May 06, 2007 »
This looks really interesting.
Bringer of Apocalypse