WaveFrontObjectLoader

Advanced game related topics
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

WaveFrontObjectLoader

Post by Guimauve »

Hello everyone,

Someone on the French forum ask me for the WaveFrontObject Loader I have created. So this is the code with a simple example of use.
You can download the sources and example files here : WaveFrontObjectLoader.zip

I have only tested this "Parse" with WaveFrontObject file generated by Rhinoceros 3D 2.0 so file generated form other programs can be problematic.
You should understand it's just a loader there is no command to send information to PB 3D internal commands. If you what them, you add them.

Best regards
Guimauve
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: WaveFrontObjectLoader

Post by Guimauve »

Hello everyone,

A tiny update about Material loading process. The zip file is uploaded at the same address, see first post.

Best regards.
Guimauve
dige
Addict
Addict
Posts: 1420
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: WaveFrontObjectLoader

Post by dige »

Could you please provide a short example, how to use / show the
loaded object? thx.
"Daddy, I'll run faster, then it is not so far..."
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: WaveFrontObjectLoader

Post by Guimauve »

dige wrote:Could you please provide a short example, how to use / show the
loaded object? thx.
This command will transfer the Parsed WaveFrontMesh to PB OGRE MESH

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Transfer the WaveFrontMesh to PB OGRE Mesh <<<<<

Procedure WaveFrontMesh_To_PB_OGRE_MESH(*WaveFrontMeshA.WaveFrontMesh, MeshID)
  
  ; Bounding Sphere Radius Calculation
  
;   ForEach GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)
;     
;     ForEach GetWaveFrontSubMeshVertex(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA))
;       
;       MinusVector3(Radius.Vector3, Origin.Vector3, GetWaveFrontSubMeshVertex(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
;       Current_Radius.f = ModulusVector3(Radius)
;       
;       If Current_Radius > Bounding_Sphere_Radius.f 
;         Bounding_Sphere_Radius = Current_Radius
;       EndIf
;       
;     Next
;     
;   Next

  MeshHandle = CreateMesh(MeshID) ; , Bounding_Sphere_Radius)
  
  If MeshID = #PB_Any
    MeshID = MeshHandle 
  EndIf
  
  ; Geometry Transfer
  
  ForEach GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)
    
    AddSubMesh()
    
    FirstWaveFrontSubMeshNormalElement(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA))
    FirstWaveFrontSubMeshUVMapElement(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA))
    
    ForEach GetWaveFrontSubMeshVertex(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA))
      
      x.f = GetVector3i(GetWaveFrontSubMeshVertex(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
      y.f = GetVector3j(GetWaveFrontSubMeshVertex(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
      z.f = GetVector3k(GetWaveFrontSubMeshVertex(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
      
      i.f = GetVector3i(GetWaveFrontSubMeshNormal(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
      j.f = GetVector3j(GetWaveFrontSubMeshNormal(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
      k.f = GetVector3k(GetWaveFrontSubMeshNormal(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
      
      u.f = GetVector2i(GetWaveFrontSubMeshUVMap(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
      v.f = GetVector2j(GetWaveFrontSubMeshUVMap(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
      
      AddMeshVertex(x, y, z)
      MeshVertexNormal(i, j, k)
      MeshVertexTextureCoordinate(u, v)
      
      NextWaveFrontSubMeshNormalElement(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA))
      NextWaveFrontSubMeshUVMapElement(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA))
      
    Next

    ForEach GetWaveFrontSubMeshIndices(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA))
      
      Vertex1 = GetIndice3P1(GetWaveFrontSubMeshIndices(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
      Vertex2 = GetIndice3P2(GetWaveFrontSubMeshIndices(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))
      Vertex3 = GetIndice3P3(GetWaveFrontSubMeshIndices(GetWaveFrontMeshWaveFrontSubMesh(*WaveFrontMeshA)))

      AddMeshFace(Vertex1, Vertex2, Vertex3)
      
    Next

  Next
  
  ProcedureReturn MeshID
EndProcedure
The command to transfer the Material Information are not very difficult to create when you know how to manage Materials and Textures. But after you have see the command about how to transfer geometry, I think you should be able to create a procedure to do this.

Best regards
Guimauve
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: WaveFrontObjectLoader

Post by djes »

Maybe I could adapt my Lightwave object loader... But we already have a good plugin, so... :?:
dige
Addict
Addict
Posts: 1420
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: WaveFrontObjectLoader

Post by dige »

Thx, Guimauve. I've tried it in this way ("WaveObjToOgreMesh.pb" includes the procedure to convert to mesh)
But I've got the error message:

Code: Select all

CreateEntity(0, MeshID(MeshID), MaterialID(0))
Error: Mesh failed

Code: Select all

IncludeFile "Vector2.pb"
IncludeFile "Vector3.pb"
IncludeFile "Indice3.pb"
IncludeFile "Color4f.pb"
IncludeFile "Material.pb"

IncludeFile "WaveFrontSubMesh.pb"
IncludeFile "WaveFrontMesh.pb"
IncludeFile "WaveObjToOgreMesh.pb"

ParseWaveFrontObjectMesh(WFOMesh.WaveFrontMesh, "Data/Triedre.obj")
DebugWaveFrontMesh(WFOMesh)

IncludePath #PB_Compiler_Home +  "Examples\3D\"
SetCurrentDirectory(#PB_Compiler_Home +  "Examples\3D\")

IncludeFile "Screen3DRequester.pb"

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    MeshID = WaveFrontMesh_To_PB_OGRE_MESH( @WFOMesh, #PB_Any)
    
    Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
    LoadTexture(0, "clouds.jpg")
    
    CreateMaterial(0, TextureID(0))
    
    
    CreateEntity(0, MeshID(MeshID), MaterialID(0))
    SetEntityMaterial(0, MaterialID(0))
    
    DisableMaterialLighting(0, 1)
    
    MaterialAmbientColor(0, RGB(100, 100, 100))
    MaterialSpecularColor(0, RGB(255, 255, 255))
    ScrollMaterial(0, 0.15, 0, 1)
    
    CreateLight(0, RGB(0,0,255), 100.0, 0, 0)
    LightSpecularColor(0, RGB(255, 0, 0))
    
    CreateCamera(0, 0, 0, 100, 100)
    CameraLocate(0, 0, 40, 150)
    CameraBackColor(0, RGB(0, 0, 128))
    
    Repeat
      Screen3DEvents()
      
      RotateEntity(0, 0, 1, 0, #PB_Relative)
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
    
    
  EndIf
EndIf
"Daddy, I'll run faster, then it is not so far..."
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: WaveFrontObjectLoader

Post by Guimauve »

To be honest, I have created this parser for my own 3D engine. Porting it to PB internal command should not be very difficult, but the help file say :

Something strange over here, when I check the MeshID value it's seem to be OK but the MeshID() return zero. So the problem are inside the PureBasic Internal command.

I use PB 4.61 Beta 1 x64 under Linux Mint 12 x64 + Gnome-Shell. What about your system (PureBasic and Windows) ?

Best regards
Guimauve
dige
Addict
Addict
Posts: 1420
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: WaveFrontObjectLoader

Post by dige »

PB4.61Beta1 x86, Win7 x64
"Daddy, I'll run faster, then it is not so far..."
Post Reply