string STLfilename ' loaded file
string status ' info about file
float smax ' max scalar size
'
function ModelRender()
======================
indexbase 1
string d 'STRING FOR STL FILE DATA
string er 'READING ERROR MESSAGE
sys le 'LENGTH OF STRING
sys n 'FACET COUNT
sys tf 'TEXT FORMAT FLAG
'
getfile stlfilename,d
if not d then
er="Problem: Please check Filename and location " stlfilename
goto done
end if
le=len d
'TEXT FORMAT
============
/*
solid name
where name is an optional string (though if name is omitted there must still be a space after solid). The file continues with any number of triangles, each represented as follows:
facet normal ni nj nk
outer loop
vertex v1x v1y v1z
vertex v2x v2y v2z
vertex v3x v3y v3z
endloop
endfacet
*/
string wr 'EXTRACTED WORDS
string name 'NAME OF SOLID
sys i=1 'STRING INDEXER
string t 'TEMP HEADER STRING
t=left d,80
i=instr t,"solid "
'if i<>1 then
if i=0 then
goto BinaryFormat
else
tf=1
end if
StepWord d,i
name=GetWord d,i
sys p=strptr(d) -1 'DATA STRING POINTER BASE
float v[3] 'VERTEX OR NORMAL
'
macro ReadVector(v)
'READ NUMBERS DIRECTLY FROM STL STRING
StepWord d,i : v[1]=valat p+sttw
StepWord d,i : v[2]=valat p+sttw
StepWord d,i : v[3]=valat p+sttw
end macro
'
glBegin GL_TRIANGLES
smax=0
do
wr=GetWord d,i
if ascw=0 then exit do
if wr="normal" then
ReadVector v : glNormal3fv v
n++
elseif wr="vertex" then
ReadVector v : glVertex3fv v
if v[1]>smax then smax=v[1]
if v[2]>smax then smax=v[2]
if v[3]>smax then smax=v[3]
end if
end do
glEnd
goto done
'BINARY FORMAT
==============
BinaryFormat:
/*
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
*/
type STLHeader
byte hbytes[80]
dword count
end type
packed
type SurfaceElement
float xn,yn,zn 'NORMAL
float x1,y1,z1 'VERTEX 1
float x2,y2,z2 'VERTEX 2
float x3,y3,z3 'VERTEX 3
word AttribSize 'N BYTES DATA FOLLOWING
end type
if len(d)<sizeof(STLHeader) then
er="binary file too small"
goto done
end if
p=strptr d
stlheader h at (p)
n=h.count
if n*sizeof(SufaceElement)+sizeof(STLHeader)>le then
er="corrupt element count " n
goto done
end if
SurfaceElement s at (p+sizeof(STLHeader))
'OPENGL READ IN TRIANGLES
glBegin GL_TRIANGLES
smax=0
for i=1 to n
glNormal3fv s.xn
glVertex3fv s.x1
glVertex3fv s.x2
glVertex3fv s.x3
if s.x1>smax then smax=s.x1
if s.y1>smax then smax=s.y1
if s.z1>smax then smax=s.z1
if s.x2>smax then smax=s.x2
if s.y2>smax then smax=s.y2
if s.z2>smax then smax=s.z2
if s.x3>smax then smax=s.x3
if s.y3>smax then smax=s.y3
if s.z3>smax then smax=s.z3
@s+=sizeof(s) + s.AttribSize 'STEP
next
glEnd
done:
if er then
status=er
else
if tf then status="Text File : " else status="Binary File : "
status+=str(n)+" Facets : " str(smax,2) " Max dimension"
end if
end function 'ModelRender