Simple mesh file format conversion (.stl to .mesh)

Everything related to 3D programming
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Simple mesh file format conversion (.stl to .mesh)

Post by Psychophanta »

Just title say it all :)
Convert the .stl mesh format to the format used by Ogre3D (.mesh)
Of course only the containded data in the .stl is converted, i.e., no color, no tangent,... just vertex positions, faces, and normals.

Code: Select all

;Convertir el formato de malla .stl al formato usado por Ogre3D (.mesh)
;Of course only the containded data in the .stl is converted, i.e., no color, no tangent,... just vertex positions, faces, and normals.

; El formato de fichero .stl es como sigue:
; UINT8[80] – Header
; UINT32 – Number of triangles
; foreach triangle
; REAL32[3] – Normal vector
; REAL32[3] – Vertex 1
; REAL32[3] – Vertex 2
; REAL32[3] – Vertex 3
; UINT16 – Attribute byte count
; end

#ventana=0
#malla=0
#entidad=0
#luz=0
#camara=0
#material=0
#color=$99DDEE
#fichero=1
fichero$=OpenFileRequester("elegir fichero .stl","D:\AL\nu\cubo.stl","STL (*.stl)|*.stl|All files (*.*)|*.*",0)
If ReadFile(#fichero,fichero$,#PB_File_SharedRead|#PB_Ascii)=0:End:EndIf
InitEngine3D(#PB_Engine3D_NoLog,#PB_Compiler_Home+"Compilers\Engine3d.dll")
InitSprite():InitKeyboard():InitMouse():InitSound()
OpenWindow(#ventana,0,0,800,600,"convertir .stl a .mesh",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#ventana),0,0,800,600,1,0,0,#PB_Screen_SmartSynchronization)
CreateCamera(#camara,0,0,100,100):MoveCamera(#camara,0,0,10)
CreateLight(#luz,$eeeeee,10,10,0,#PB_Light_Spot)
CreateMaterial(#Material,0,#color)
MaterialCullingMode(#material,#PB_Material_NoCulling)

Structure triangulo
  normal.Vector3[3]
  vertice1.Vector3[3]
  vertice2.Vector3[3]
  vertice3.Vector3[3]
  atrib.u
EndStructure

FileSeek(#fichero,80):ntriangulos.i=ReadLong(#fichero); <- número de triangulos en total (en dirección 80)
trianguloi.triangulo
CreateMesh(#malla,#PB_Mesh_TriangleList,#PB_Mesh_Static)
For i.i=1 To ntriangulos
  trianguloi\normal\x=ReadFloat(#fichero)
  trianguloi\normal\y=ReadFloat(#fichero)
  trianguloi\normal\z=ReadFloat(#fichero)
  
  trianguloi\vertice1\x=ReadFloat(#fichero)
  trianguloi\vertice1\y=ReadFloat(#fichero)
  trianguloi\vertice1\z=ReadFloat(#fichero)
  
  trianguloi\vertice2\x=ReadFloat(#fichero)
  trianguloi\vertice2\y=ReadFloat(#fichero)
  trianguloi\vertice2\z=ReadFloat(#fichero)
  
  trianguloi\vertice3\x=ReadFloat(#fichero)
  trianguloi\vertice3\y=ReadFloat(#fichero)
  trianguloi\vertice3\z=ReadFloat(#fichero)
  
  trianguloi\atrib=ReadUnicodeCharacter(#fichero); <- custom attribute
  
  MeshVertexPosition(trianguloi\vertice1\x,trianguloi\vertice1\y,trianguloi\vertice1\z)
  MeshVertexNormal(trianguloi\normal\x,trianguloi\normal\y,trianguloi\normal\z)
  MeshVertexColor(#color)
  MeshVertexPosition(trianguloi\vertice2\x,trianguloi\vertice2\y,trianguloi\vertice2\z)
  MeshVertexNormal(trianguloi\normal\x,trianguloi\normal\y,trianguloi\normal\z)
  MeshVertexColor(#color)
  MeshVertexPosition(trianguloi\vertice3\x,trianguloi\vertice3\y,trianguloi\vertice3\z)
  MeshVertexNormal(trianguloi\normal\x,trianguloi\normal\y,trianguloi\normal\z)
  MeshVertexColor(#color)
  ; o bien
;   MeshVertex(trianguloi\vertice1\x,trianguloi\vertice1\y,trianguloi\vertice1\z,0.5,0.5,#color,trianguloi\normal\x,trianguloi\normal\y,trianguloi\normal\z)
;   MeshVertex(trianguloi\vertice2\x,trianguloi\vertice2\y,trianguloi\vertice2\z,0.5,0.5,#color,trianguloi\normal\x,trianguloi\normal\y,trianguloi\normal\z)
;   MeshVertex(trianguloi\vertice3\x,trianguloi\vertice3\y,trianguloi\vertice3\z,0.5,0.5,#color,trianguloi\normal\x,trianguloi\normal\y,trianguloi\normal\z)
  
  MeshFace(i*3-3,i*3-2,i*3-1)
Next
CloseFile(#fichero)
FinishMesh(1)
NormalizeMesh(#malla)
UpdateMeshBoundingBox(#malla)
SetMeshMaterial(#malla,MaterialID(#material),0)
CreateEntity(#entidad,MeshID(#malla),#PB_Material_None)
;CameraRenderMode(#camara,#PB_Camera_Wireframe)
Repeat
  Repeat:evento.i=WindowEvent():Until evento=#PB_Event_None
  ExamineMouse():ExamineKeyboard()
  mdx.f=MouseDeltaX()/10:mdy.f=MouseDeltaY()/10:mdz.f=MouseWheel()*10
  If KeyboardPushed(#PB_Key_LeftControl):MoveCamera(#camara,0,0,mdy/10,#PB_Relative)
  Else:RotateEntity(#entidad,mdy,mdx,-mdz,#PB_Relative)
  EndIf
  TimeSinceLastFrame.i=RenderWorld()
  FlipBuffers():Delay(16)
Until KeyboardPushed(#PB_Key_Escape)
FreeEntity(#entidad)
ReleaseMouse(1)
fichero$=SaveFileRequester("Save .mesh ??",fichero$+".mesh","Ogre mesh (*.mesh)|*.stl|All files (*.*)|*.*",0)
If fichero$:SaveMesh(#malla,fichero$):EndIf
FreeMesh(#malla)
CloseScreen()
CloseWindow(#ventana)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
miso
Enthusiast
Enthusiast
Posts: 406
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Simple mesh file format conversion (.stl to .mesh)

Post by miso »

Thank you for sharing it, Psychophanta.
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Simple mesh file format conversion (.stl to .mesh)

Post by Caronte3D »

Gracias Psychophanta! :wink:

It would be nice if we had an easy way to load models with materials and animations from editors like Blenders, this would be great to increase the interest of 3D in PB.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Simple mesh file format conversion (.stl to .mesh)

Post by Psychophanta »

@miso
welcome!
@Caronte3D
Agree!
Well, at the moment, using that code you can export .stl from blender, and then import and manage it with PB. That's why i made it, because it is useful to me, even only is vertexes and faces, is enough for some tasks.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Fred
Administrator
Administrator
Posts: 18150
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Simple mesh file format conversion (.stl to .mesh)

Post by Fred »

Just install blender 4.1 (not the lastest version) and the Blender Ogre Export plugin. I successfully exported mesh with their material (didn't tried animation)
miso
Enthusiast
Enthusiast
Posts: 406
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Simple mesh file format conversion (.stl to .mesh)

Post by miso »

Mesh support is enough for me. (and i'm happy with any extra format that is loaded and created using code like in this post) The only thing I really need is manual mesh creation, and it just became top-notch. When manual bone creation has been implemented, I was literally crying tears of joy.
I'm also OK with the fixed pipeline, but after the team had put so much work into shaders, now seems to be capable to do great things. (Thank you PB Team and Pf_Shadoko for all these!)

There are some puzzle pieces regarding shaders I could not find yet. Those might be for the future, or might be there already undocumented. I could not find how to get and pass internal bone data to the shader for example.
At current stage, the only remaining thing is some adjustments to the physics engine. It's usable, but some (only a few remaining) functions misbehave, some is not documented well enough.

Rendering 3d data to the screen is one thing, but a game also needs good collision handling to work with. I'm sure there are many skilled PB users that can simply create an octree, put triangles in it and start to check them against each other, but that's what bullet is designed for.
As a final thought, I'm very happy with the current state and direction of PB 3d.
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Simple mesh file format conversion (.stl to .mesh)

Post by Caronte3D »

Fred wrote: Sun Jan 19, 2025 3:13 pm Just install blender 4.1 (not the lastest version) and the Blender Ogre Export plugin. I successfully exported mesh with their material (didn't tried animation)
Last time I tried this way (a few months ago), the animation didn't work (neither shape nor skeleton keys).
The animation is a must to do almost every 3D project.
Maybe I'll try it again when I finish my current project.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Simple mesh file format conversion (.stl to .mesh)

Post by DarkDragon »

Caronte3D wrote: Sun Jan 19, 2025 8:35 pm
Fred wrote: Sun Jan 19, 2025 3:13 pm Just install blender 4.1 (not the lastest version) and the Blender Ogre Export plugin. I successfully exported mesh with their material (didn't tried animation)
Last time I tried this way (a few months ago), the animation didn't work (neither shape nor skeleton keys).
The animation is a must to do almost every 3D project.
Maybe I'll try it again when I finish my current project.
Last time I did it worked (some years ago), but what kind of animation did you pick? Did you use the NLA editor? Only these get exported.
bye,
Daniel
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Simple mesh file format conversion (.stl to .mesh)

Post by Caronte3D »

I don't remember... but I will try again when not busy.

A mini tuto or guide step to step from the gurus here, would be nice to get started :D
miso
Enthusiast
Enthusiast
Posts: 406
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Simple mesh file format conversion (.stl to .mesh)

Post by miso »

Try Blender 2.71 with Ogre mesh exporter plugin. (It was some time ago, but I remember well, that was the version I used that worked)
Last edited by miso on Mon Jan 20, 2025 12:47 pm, edited 1 time in total.
SMaag
Enthusiast
Enthusiast
Posts: 299
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Simple mesh file format conversion (.stl to .mesh)

Post by SMaag »

just do not reinventing the wheel. I did a STL Modul 2 years ago.
Herre you can download the demo with an air plane in STL. "CAD_STL_DEM_V001.7z"

https://github.com/Maagic7/PureBasicFra ... main/Demos

Start the program and select the Airplane.stl file

here the position of th Module_STL source.
https://github.com/Maagic7/PureBasicFra ... ule_STL.pb
miso
Enthusiast
Enthusiast
Posts: 406
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Simple mesh file format conversion (.stl to .mesh)

Post by miso »

Thanks SMaag, looks nice and clean.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 551
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple mesh file format conversion (.stl to .mesh)

Post by minimy »

Muchas gracias Psychophanta!!
Very good code! Thanks for share!
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply