Simple mesh file format conversion (.stl to .mesh)
Posted: Sun Jan 19, 2025 12:02 pm
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.

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)