you are right IdeasVacuum the STL models files is simple in its structure, just vertex positions and normals. i have tried the following loader to openglGadget using
glOrtho_(-50,50,-50,50,-50,50)
glDrawArrays_(#GL_TRIANGLES, 0, ArraySize(vertex()))
i first loaded the mesh positions and normals to vertex() array, it will load only the stl if it is ascii and not binary, there are sites in the web which converts binaries to ascii
glOrtho is very nice and will project the 3D scene to the openglgadget sizes, it is making things easy and straightforward. some people may say it is deprecated. whats the point, we are using our DNA code from millions of years ago and it is not deprecated yet
glDrawArrays is also very nice and it is the easiest to graph scenes whether 2d or 3d
the regular expression to parse the 3 floating numbers i have copied from the web , it does not work with scientific notation numbers some times we find it like:
-4.62238e-05 this number is almost equal zero -0.000046223799
this site
https://www.meshconvert.com/ add these notations sometimes . another converter here
https://github.com/cmpolis/convertSTL haven't tried it yet
attached 3 specimens of STL meshes as text, line 17 choose owl.stl or spider.stl or purebasic.stl. to see the spider.stl better use glOrtho_(-10,10,-10,10,-10,10) since glOrtho_(-50,50,-50,50,-50,50) is for big models sizes
http://s000.tinyupload.com/index.php?fi ... 1296197433
Code: Select all
Declare findNumbers(s.s)
Global t
Structure vert
x.f
y.f
z.f
nx.f ;for normals
ny.f
nz.f
EndStructure
stlMesh.s = "owl.stl"
;stlMesh.s = "spider.stl"
;stlMesh.s = "purebasic.stl"
If ReadFile(0, stlMesh) ; if the file could be read, we continue...
While Eof(0) = 0 ; loop as long the 'end of file' isn't reached
s$ = ReadString(0)
If FindString(s$, "vertex")
lines+1
EndIf
Wend
EndIf
CloseFile(0)
;Debug lines
Global.f Dim vertex.vert(lines)
Global.f Dim tmp(2)
Global rot.f = 1
Global t, n
ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "STL reader")
SetWindowColor(0, RGB(200,220,200))
OpenGLGadget(0, 10, 10, WindowWidth(0) , WindowHeight(0) , #PB_OpenGL_Keyboard)
SetWindowTitle(0, "STL reader "+Str(lines)+" vertices")
If ReadFile(0, stlMesh) ; if the file could be read, we continue...
While Eof(0) = 0 ; loop as long the 'end of file' isn't reached
s$ = ReadString(0)
If FindString(s$, "vertex")
findNumbers(s$)
vertex(t)\x = tmp(0)
vertex(t)\y = tmp(1)
vertex(t)\z = tmp(2)
t+1
EndIf
If FindString(s$, "normal")
findNumbers(s$)
vertex(n)\nx = tmp(0)
vertex(n)\ny = tmp(1)
vertex(n)\nz = tmp(2)
n+1
EndIf
Wend
;Debug t
CloseFile(0)
Else
MessageRequester("Information","Couldn't open the file!")
EndIf
glLoadIdentity_()
glOrtho_(-50,50,-50,50,-50,50)
;glOrtho_(-10,10,-10,10,-10,10)
glMatrixMode_(#GL_PROJECTION);
glEnable_(#GL_DEPTH_TEST)
glPolygonMode_(#GL_FRONT_AND_BACK, #GL_LINE )
glShadeModel_(#GL_SMOOTH)
glEnable_(#GL_DEPTH_TEST)
glEnable_(#GL_LIGHT_MODEL_AMBIENT)
glClearColor_ (1.0, 0.6, 0.4, 0.0);
glEnable_(#GL_LIGHTING) ;Enable Lighting
glEnable_(#GL_LIGHT0)
glEnable_(#GL_NORMALIZE)
gluLookAt_( 0, 0.5, 1, ; the camera looking from position 0,0.5,1 to 0,0,0 from above
0, 0, 0,
0, 1, 0 )
;glRotatef_(-90, 0, 1, 0)
Repeat
event = WindowEvent()
glClearColor_(0.9, 0.9, 0.9, 1)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glRotatef_(rot, 0, 1, 0)
glEnableClientState_(#GL_VERTEX_ARRAY)
glEnableClientState_(#GL_NORMAL_ARRAY)
glVertexPointer_(3, #GL_FLOAT, SizeOf(vert), @vertex(0)\x)
glNormalPointer_(#GL_FLOAT, SizeOf(vert), @vertex(0)\nx)
;glDrawArrays_(#GL_POINTS, 0, ArraySize(vertex()))
glDrawArrays_(#GL_TRIANGLES, 0, ArraySize(vertex()))
glDisableClientState_(#GL_NORMAL_ARRAY)
glDisableClientState_(#GL_VERTEX_ARRAY);
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Until Event = #PB_Event_CloseWindow Or quit = 1
Procedure findNumbers(s.s)
;Debug t
If CreateRegularExpression(0, "[+-]?([0-9]*[.])?[0-9]+")
If ExamineRegularExpression(0, s)
For i=0 To 2
If NextRegularExpressionMatch(0)
tmp(c) = ValF(RegularExpressionMatchString(0))
c+1
EndIf
Next
c=0
EndIf
Else
Debug RegularExpressionError()
EndIf
EndProcedure