Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: ninogenio on April 26, 2007

Title: 3ds model ripper
Post by: ninogenio 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.
Title: Re: 3ds model ripper
Post by: Tetra on April 26, 2007
Nice 1 nino  :cheers: very much k+  :)
Title: Re: 3ds model ripper
Post by: ninogenio on April 26, 2007
cheers mate! its nothing much really but it should make life a bit easyr.
Title: Re: 3ds model ripper
Post by: Emil_halim 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 (http://www.spacesimulator.net/tut4_3dsloader.html)
Title: Re: 3ds model ripper
Post by: ninogenio 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.
Title: Re: 3ds model ripper
Post by: Clyde on April 26, 2007
Thats a very cool tool Nino, welldone on it's creation. :)
Title: Re: 3ds model ripper
Post by: Shockwave on April 26, 2007
That has to be worth some good karma.

Have some of the good stuff Nino!
Title: Re: 3ds model ripper
Post by: Stonemonkey on April 26, 2007
handy stuff nino, k++
Title: Re: 3ds model ripper
Post by: ninogenio 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 ;)
Title: Re: 3ds model ripper
Post by: Emil_halim 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.
Title: Re: 3ds model ripper
Post by: ninogenio 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.
Title: Re: 3ds model ripper
Post by: Emil_halim 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.     
Title: Re: 3ds model ripper
Post by: Emil_halim 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. 
Title: Re: 3ds model ripper
Post by: ninogenio 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: .
Title: Re: 3ds model ripper
Post by: rain_storm 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
Title: Re: 3ds model ripper
Post by: Emil_halim 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.
Title: Re: 3ds model ripper
Post by: BadMrBox on May 06, 2007
This looks really interesting.