B3D 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

B3D import for Ogre

Post by mpz »

Hi,

here comes a little example of a B3D import for Ogre. Only one textur layer and no animation.

best regards,
Michael

Code: Select all

;////////////////////////////////////////////////////////////////
;//
;// Project Title: B3D Import
;// File Title: OgreMesh_B3D.pb
;// Created On: 22.3.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_LoadB3D(  Mesh.l , FileName.s )

  Color.l = $FFFFFF  
  If FileName.s
    If ReadFile(0, FileName)
      chunk_id.s = "    "
      ReadData(0, @chunk_id.s, 4)
      If chunk_id.s <> "BB3D"
           ProcedureReturn #False
      EndIf
      chunk_len.l = ReadLong(0)
      Version = ReadLong(0)

      If Version <> 1 ; "Invalid b3d file version"
           ProcedureReturn #False
      EndIf
      
      Texture.D3DMATERIAL

      While Eof(0) = 0           ; sich wiederholende Schleife bis das Ende der Datei ("end of file") erreicht ist
          
          ReadData(0, @chunk_id.s, 4)
          chunk_len.l = ReadLong(0)
          
          ;Debug "Chunk_id = "+chunk_id 
          ;Debug "Chunk_Len = "+Str(chunk_Len) + " / "+Hex(chunk_Len)
          
          Select chunk_id

                  Case "TEXS" ;  Main chunk
                       While chunk_len > 4
                             start = Loc(0)
                             Texturname.s = ReadString(0)
                             flags.l = ReadLong(0)
                             blend.l = ReadLong(0)
                             x_pos.f = ReadFloat(0)
                             y_pos.f = ReadFloat(0)
                             x_scl.f = ReadFloat(0)
                             y_scl.f = ReadFloat(0)
                             rot.f   = ReadFloat(0)
                             chunk_len + start - Loc(0)
                             ;Debug "< Textur = <"+Texturname + "> = "+Str(chunk_len )
                     Wend

                  Case "BRUS"
                      n_texs.l=ReadLong(0)
  		                chunk_len - 12
                      While chunk_len > 0
                          start = Loc(0)
                          tname.s = ReadString(0)
                          Texture\Ambient\r = ReadFloat(0)*255
                          Texture\Ambient\g = ReadFloat(0)*255
                          Texture\Ambient\b = ReadFloat(0)*255

                          alp.f   = ReadFloat(0)
                          shi.f   = ReadFloat(0)
                          blend.l = ReadLong(0)
                          fx.l    = ReadLong(0)
                          For k=0 To n_texs-1
                              tex_id.l=ReadLong(0)
                          Next
                          chunk_len + start - Loc(0)
                          ;Debug "< BRUS = <"+tname+"> = "+Str(chunk_len )
                       Wend
 
		              Case "VRTS"
			                 flags.l=ReadLong(0)
			                 tc_sets.l=ReadLong(0)
			                 tc_size.l=ReadLong(0)
			                 chunk_len - 12
			                 sz.l=12+tc_sets*tc_size*4
			                 If flags & 1 = 1
			                    sz+12
			                 EndIf
			                 If flags & 2 = 2
			                    sz+16
			                 EndIf
                       qty_Vertex.l =chunk_len/sz
                       ;Debug "< Vertex = "+Str(qty_Vertex)

                       *VBuffer = AllocateMemory(SizeOf(MyVertex)*qty_Vertex) 
                       *PtrV.MyVertex = *VBuffer
			                 If qty_Vertex*sz = chunk_len ;b3dChunkSize() 

				                  ;read all verts in chunk
				                  While chunk_len > 0 ; b3dChunkSize()
			                      
			                      start = Loc(0)
                            *PtrV\x = ReadFloat(0) 
                            *PtrV\y = ReadFloat(0) 
                            *PtrV\z = ReadFloat(0)

	 			                    If flags & 1 = 1
 		 			                     *PtrV\nx=ReadFloat(0)
					                     *PtrV\ny=ReadFloat(0)
					                     *PtrV\nz=ReadFloat(0)
		                      	Else
			                         *PtrV\nx = *PtrV\x
                               *PtrV\ny = *PtrV\y
                               *PtrV\nz = *PtrV\z
      	                    EndIf
      	                    
				                    If flags & 2 = 2
						                    r.f = ReadFloat(0) * 255
						                    g.f = ReadFloat(0) * 255
						                    b.f = ReadFloat(0) * 255
						                    a.f = ReadFloat(0) * 255
						                    
						                    *PtrV\Color = (((Int(r) & $FF) << 24) | ((Int(g) & $FF) << 16) | ((Int(b) & $FF) << 8) | (Int(a) & $FF))

                            Else
                                *PtrV\Color = Color                             
				                    EndIf

				                    ;read tex coords...
				                    For j=1 To tc_sets*tc_size
						                    wert.f = ReadFloat(0)
                                If j = 1 : *PtrV\u = wert : EndIf ;Single Textur = u1
                                If j = 2 : *PtrV\v = wert : EndIf ;Single Textur = v1
                                ;If j = 3 : *PtrV\u = wert : EndIf ; j > 2 = multitexture u2,v2,u3... but the problem here is Ogre
				                    Next
				                    *PtrV + SizeOf(MyVertex) 
				                    chunk_len + start - Loc(0)
				                  Wend
			                Else
				                  ProcedureReturn #False ; "***** Illegal number of vertices *****"
			                EndIf
		             Case "TRIS"
			                brush_id.l=ReadLong(0)
			                chunk_len - 4
			                sz.l=12
			                qty_Triangle.l=chunk_len/sz
                      *IBuffer=AllocateMemory(SizeOf(FTriangle)*qty_Triangle) 
                      *PtrF.FTriangle=*IBuffer 
			                If qty_Triangle*sz=chunk_len
				                  While chunk_len > 0
				                      start = Loc(0)
					                    *PtrF\f1=ReadLong(0)
					                    *PtrF\f2=ReadLong(0)
					                    *PtrF\f3=ReadLong(0)
					                    *PtrF + SizeOf(FTriangle) 
					                    chunk_len + start - Loc(0)
					                Wend
			                Else
			                    ProcedureReturn #False ; "***** Illegal number of triangles *****"
			                EndIf
                 Case "MESH"
                     brush_id.l = ReadLong(0)
                     ;Debug "Brush ID " +Str(brush_id)
                 Case "NODE"
                     start = Loc(0)
			               name.s=ReadString(0)
			               x_pos.f=ReadFloat(0)
			               y_pos.f=ReadFloat(0)
			               z_pos.f=ReadFloat(0)
			               x_scl.f=ReadFloat(0)
			               y_scl.f=ReadFloat(0)
			               z_scl.f=ReadFloat(0)
			               w_rot.f=ReadFloat(0)
			               x_rot.f=ReadFloat(0)
			               y_rot.f=ReadFloat(0)
			               z_rot.f=ReadFloat(0)
                     chunk_len + start - Loc(0)
                     ;Debug "< NODE = <"+name+"> = "+Str(chunk_len )
                  Default
                        For x = 1 To chunk_len
                              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)

               Texture\Diffuse\r = Texture\Ambient\r 
               Texture\Diffuse\g = Texture\Ambient\g 
               Texture\Diffuse\b = Texture\Ambient\b 
               Texture\Specular\r = Texture\Ambient\r 
               Texture\Specular\g = Texture\Ambient\g 
               Texture\Specular\b = Texture\Ambient\b 

               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,"B3D 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$ = "B3D Files(*.b3d)|*.b3d"
  directory$ = "C:\Programme\PureBasic\media\"
  FileName.s = OpenFileRequester("Please load 3D File", directory$, Pattern$, 0) 
  If Not FileName.s
     MessageRequester("Information", "Der Requester wurde abgebrochen.", 0)
     End 
  EndIf
  MP_Loadb3d( #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
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Thanks! Keep 'em coming! :D
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Post Reply