Here's the first lod function I wrote for vivid. It works using a radial claw. Any verts within the claw of another vert is removed, and replaced with the original vert. This is repeated for every vert.
Then any triangles with more than 1 clashing vert(I.e repeat) are removed from the mesh. Result is it works on any
kind of mesh.
VBank=a bank containing your verts. I.e byte0=vertex0 x,byte4 =vertex0 y.
and the tri banks are simply indices to each vert.
The cool thing is you can re-use the original vert pool using this method.
Uv mapping like jhocking said though can get a little distorted with too much lod.
Function glGenLod(sdat.sdat,level=0,rad#=0.08,mathes=2)
Local Vbank
oBank=sdat\cVerts
nTri=CreateBank( BankSize(sdat\cTris))
;CopyBank(oBank,0,vBank,0,BankSize(sdat\cverts))
Local vertSt[50000] ;Should have used banks for this.
Local vertTo[50000]
vertC=sdat\vc
; DebugLog "Was "+vertC+" vertices"
For v=0 To VertC-1
If vertSt[v]=False
x#=PeekFloat(obank,vo)
y#=PeekFloat(oBank,vo+4)
z#=PeekFloat(obank,vo+8)
;DebugLog x+" >"+y+" z>"+z
so=0
; DebugLog "==============Checking Vert>"+v+"----"
For ov=0 To vertC-1
tx#=PeekFloat(oBank,so)-x
ty#=PeekFloat(oBank,so+4)-y
tz#=PeekFloat(oBank,so+8)-z
If Sqr(tx*tx+ty*ty+tz*tz)<rad ;see if it's within clipping range.
If ov<>v ;make sure its not the same vert. quicker to check here.
If vertSt[ov]=False
vertSt[ov]=True
vertTo[ov]=v
;opti=opti+1
; DebugLog "------------>"+ov
oc=oc+1
; DebugLog x+" >"+y+" z>"+z
; DebugLog "2:"+PeekFloat(obank,so)+" >"+PeekFloat(obank,so+4)+" >"+PeekFloat(obank,so+8)
EndIf
Else
; DebugLog "nope"
; DebugLog "----NOT MATCH-------->"+ov
; oc=oc+1
; DebugLog x+" >"+y+" z>"+z
; DebugLog "2:"+PeekFloat(obank,so)+" >"+PeekFloat(obank,so+4)+" >"+PeekFloat(obank,so+8)
;s;o=so+12
EndIf
Else
;DebugLog "----NOT MATCH-------->"+ov
; oc=oc+1
; DebugLog x+" >"+y+" z>"+z
; DebugLog "2:"+PeekFloat(obank,so)+" >"+PeekFloat(obank,so+4)+" >"+PeekFloat(obank,so+8)
EndIf
so=so+12
Next
EndIf
vo=vo+12
Next
;Now generate new TriList
ctris=sdat\ctris
nTri=CreateBank(BankSize(sdat\cTris))
Local vI[5]
For t=0 To sdat\tc-1
For v=0 To 2
vid=PeekInt(ctris,(t*12)+(v*4))
If vid>49999 Stop
If vertSt[vid]=True
nVert=vertTo[vid]
Else
nVert=vid
EndIf
vI[v]=nVert
PokeInt(nTri,texO+(v*4),nVert)
Next
did=1
If vi[0]=vi[1] Or vi[0]=vi[2] Or vi[1]=vi[2]
; If vi[1]=vi[2]
did=0
;EndIf
EndIf
If did=1
texO=texO+12
tC=tC+1
EndIf
Next
;sdat\vBufId[level]=vBank
; sdat\lTris[level]=nTri
glGenBuffersObj(1,vbank)
glBindBufferObj( GL_ELEMENT_ARRAY_BUFFER_ARB,PeekInt(vbank,0))
If sdat\glOptimise=0 sdat\glOptimise=glStatic
glBufferDataObj(GL_ELEMENT_ARRAY_BUFFER_ARB,BankSize(sdat\cTris),Ntri,sdat\glOptimise)
sdat\tBufId[level]=PeekInt(vbank,0)
sdat\lTc[level]=tc
FreeBank vbank
;DebugLog "Now "+Str(vertC-opti)+" vertices"
;DebugLog "saved "+oc+" verts
; DebugLog "old Tris>"+sdat\tc
; DebugLog "new Tri count>"+tc
End Function