3DS import for Ogre

Share your advanced PureBasic knowledge/code with the community.
mpz
Enthusiast
Enthusiast
Posts: 497
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

3DS import for Ogre

Post by mpz »

Hi,

here comes a little example of a 3DS import for Ogre. I am working now on a B3D import for my 3D engine. Perhaps i make a adaption for Ogre too...


best regards,
Michael

Code: Select all

;////////////////////////////////////////////////////////////////
;//
;// Project Title: 3DS Import
;// File Title: OgreMesh_3ds.pb
;// Created On: 16.5.2009
;// Updated On: 
;// Author: Michael Paulwitz
;// OS:Windows
;// 
;// Ein Beispielcode meiner MP 3D Engine angepasst für Ogre 
;// Unterstützt nur einen Texturlayer
;// 
;// A piece of code for my MP 3D Engine, adapted for Ogre
;// support only one textur layer
;// 
;////////////////////////////////////////////////////////////////


Structure MyVertex 
    x.f : y.f : z.f     ; vertices coordinates 
    nx.f : ny.f : nz.f  ; normals coordinates
    Color.l             ; color ARGB
    u.f : v.f           ; texture coordinates 
EndStructure 

Structure FTriangle 
    f1.w 
    f2.w 
    f3.w  
EndStructure 

Structure D3DCOLORVALUE
    r.f
    g.f
    b.f
    a.f
EndStructure

Structure D3DMATERIAL
   Ambient.D3DCOLORVALUE ;/* Ambient color RGB */
   Diffuse.D3DCOLORVALUE ;/* Diffuse color RGBA */
   Specular.D3DCOLORVALUE ;/* Specular 'shininess' */
EndStructure

#Mesh = 0
    
Procedure MP_Load3ds( Mesh.l , FileName.s )
  
  Color.l = $FFFFFF  
  If FileName.s
    If ReadFile(0, FileName)
      Texture.D3DMATERIAL
    
    
      While Eof(0) = 0           ; sich wiederholende Schleife bis das Ende der Datei ("end of file") erreicht ist
          chunk_id  = ReadWord(0) & $FFFF
          chunk_len.l = ReadLong(0)
          Select chunk_id
                  Case $4D4D ;  Main chunk 
               ;   Case $0002 ;   : 3DS-Version
               ;         Debug "< Version = "+Str( ReadLong(0)) 
                  Case $3D3D ;  3D editor chunk
                  Case $AFFF ; Material exist
                ;  Case $A000 ; Material name
                ;        MaterialName.s = ReadString(0)
                ;        Debug "< MaterialName = "+MaterialName                  
                  Case $10 ; * 0010 - RGB Color, float size 4,4,4
                        If Text_Kind = 1
                          Texture\Ambient\r = ReadFloat(0)*255
                          Texture\Ambient\g = ReadFloat(0)*255
                          Texture\Ambient\b = ReadFloat(0)*255
                        ElseIf Text_Kind = 2
                          Texture\Diffuse\r = ReadFloat(0)*255
                          Texture\Diffuse\g = ReadFloat(0)*255
                          Texture\Diffuse\b = ReadFloat(0)*255
                        ElseIf Text_Kind = 3
                          Texture\Specular\r = ReadFloat(0)*255
                          Texture\Specular\g = ReadFloat(0)*255
                          Texture\Specular\b = ReadFloat(0)*255
                        EndIf
                  Case $11 ; * 0011 - RGB Color - 24 bit, 3 x Byte  
                        If Text_Kind = 1
                          Texture\Ambient\r = ReadByte(0)
                          Texture\Ambient\g = ReadByte(0)
                          Texture\Ambient\b = ReadByte(0)
                        ElseIf Text_Kind = 2
                          Texture\Diffuse\r = ReadByte(0)
                          Texture\Diffuse\g = ReadByte(0)
                          Texture\Diffuse\b = ReadByte(0)
                        ElseIf Text_Kind = 3
                          Texture\Specular\r = ReadByte(0)
                          Texture\Specular\g = ReadByte(0)
                          Texture\Specular\b = ReadByte(0)
                        EndIf
                  Case $A010 ; Ambient color
                          ;Debug "< Ambient color"
                          Text_Kind = 1
                  Case $A020 ; Diffuse color
                          ;Debug "< Diffuse color"
                          Text_Kind = 2
                  Case $A030 ; Specular color
                          ;Debug "< Specular color"
                          Text_Kind = 3
                  Case $A040 ; Material shininess percent
                  Case $A041 ; Material shininess strength percent
                  Case $A200 ; Map
                  Case $A300 ; Mapping filename
                        Texturname.s = ReadString(0)
                        ;Debug "< Texturname = "+Texturname
                  Case $4000 ; Object block (with name of your object)
                        ModelName.s = ReadString(0)
                  ;      Debug "< Modelname = "+ModelName
			            Case $4100 ;  Triangular mesh
		   	          Case $4110 ; Your vertices
                        qty_Vertex.w = ReadWord(0)
                        ;Debug "< Vertex = "+Str(qty_Vertex)
                        *VBuffer = AllocateMemory(SizeOf(MyVertex)*qty_Vertex) 
                        *PtrV.MyVertex = *VBuffer
                        For i=0 To qty_Vertex-1
                            *PtrV\x = ReadFloat(0) 
                            *PtrV\y = ReadFloat(0) 
                            *PtrV\z = ReadFloat(0)
                            *PtrV\nx = *PtrV\x
                            *PtrV\ny = *PtrV\y
                            *PtrV\nz = *PtrV\z
                            *PtrV\Color = Color  
                            *PtrV + SizeOf(MyVertex) 
                        Next    
 			            Case $4120 ; Your faces
                        qty_Triangle.w = ReadWord(0)
                        ;Debug "< Triangle = "+Str(qty_Triangle)
                        *IBuffer=AllocateMemory(SizeOf(FTriangle)*qty_Triangle) 
                        *PtrF.FTriangle=*IBuffer 
                        For i=0 To qty_Triangle-1
                            *PtrF\f1 = ReadWord(0)
                            *PtrF\f2 = ReadWord(0)
                            *PtrF\f3 = ReadWord(0)
                            face_flags.w = ReadWord(0)
                      ;      Debug Str(*PtrF\f1)+ " / "+Str(*PtrF\f2)+ " / "+Str(*PtrF\f3)+" / "+Str(face_flags)
                            *PtrF + SizeOf(FTriangle) 
                            
				                Next
    		          Case $4140 ; : Mapping coordinates list
                        qty_Vertex.w = ReadWord(0)
                        ;Debug "< Vertex uv = "+Str(qty_Vertex)
                        *PtrV.MyVertex = *VBuffer

				                For i=0 To qty_Vertex-1
                            *PtrV\u = ReadFloat(0) 
                            *PtrV\v = ReadFloat(0) 
                            *PtrV + SizeOf(MyVertex) 
                        Next
                  Default
                        For x = 1 To chunk_len - 6
                              ReadByte(0)
                        Next
               EndSelect
               Wend

               CreateMesh(Mesh, qty_Vertex)
               SetMeshData(Mesh, #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_Color  | #PB_Mesh_UVCoordinate, *VBuffer, qty_Vertex);3)
               SetMeshData(Mesh, #PB_Mesh_Face, *IBuffer, qty_Triangle)

               Add3DArchive(GetPathPart(FileName.s), #PB_3DArchive_FileSystem) ; Textur in Ordner / Add folder

               If FileSize(GetPathPart(FileName.s)+Texturname) > 0
                  CreateEntity(Mesh, MeshID(Mesh), CreateMaterial(0, LoadTexture(0, Texturname.s)))
                  MaterialAmbientColor(0, RGB(Texture\Ambient\r,Texture\Ambient\g,Texture\Ambient\b))
                  MaterialDiffuseColor(0, RGB(Texture\Diffuse\r,Texture\Diffuse\g,Texture\Diffuse\b))
                  MaterialSpecularColor(0, RGB(Texture\Specular\r,Texture\Specular\g,Texture\Specular\b))
               Else
                  CreateEntity(Mesh, MeshID(0),#PB_Material_None)
               EndIf
               ProcedureReturn #True
      EndIf
      CloseFile(0)
    EndIf

  ProcedureReturn #False

EndProcedure

If InitEngine3D() And InitSprite() And InitKeyboard()
  OpenWindow(0,0,0,640,480,"3DS Import TEST  - Key A and Y/Z bewegt/move Mesh",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
 
  Pattern$ = "3DS Files(*.3ds)|*.3ds"
  directory$ = "C:\Programme\PureBasic\media\"
  FileName.s = OpenFileRequester("Bitte Datei zum Laden auswählen", directory$, Pattern$, 0) 
  If Not FileName.s
     MessageRequester("Information", "Der Requester wurde abgebrochen.", 0)
     End 
  EndIf
  MP_Load3ds( #Mesh , FileName.s )
 
  colorlight = 0
  
  If colorlight = 0
 
     CreateLight(0, RGB(255,255,255), 100.0, 0, 0)   ; White Light

  Else
  
     CreateLight(0, RGB(0,0,255), 100.0, 0, 0)   ; Blue Light
     CreateLight(1, RGB(255,0,0), -100.0, 0, 0)  ; Red Light
     CreateLight(2, RGB(255,255,0), 0.0, 100, 0)   ; Yellow Light
  
  EndIf
    
    CreateCamera(0, 0, 0, 100, 100)
   CameraLocate(0,0,0,40)
 
    Repeat
      ExamineKeyboard()
      Select WindowEvent()
        Case #PB_Event_CloseWindow
          Quit = #True
      EndSelect
 
      If KeyboardPushed(#PB_Key_A) ; Key A = Zoom +
         z-1
         CameraLocate(0,0,0,z)
      EndIf
 
      If KeyboardPushed(#PB_Key_Z) ; Key Z = Zoom +
         z+1
         CameraLocate(0,0,0,z)
      EndIf
  
      If KeyboardPushed(#PB_Key_Y) ; Key Y = Zoom +
         z+1
         CameraLocate(0,0,0,z)
      EndIf
 
      RotateEntity(#Mesh, 1, 1, 1, #PB_Relative)
 
      ClearScreen(RGB(0,0,0))
      RenderWorld()
      FlipBuffers()
 
    Until KeyboardPushed(#PB_Key_Escape) Or Quit
Else
  MessageRequester("Error", "Cant init DirectX 3D Engine",0)
EndIf
 
End
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

Thank you , i work fine here :)
Please correct my english
http://purebasic.developpez.com/
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

1000x thanks for sharing.
Post Reply