I wrote this little model optimizer when I was getting annoyed with models having polys in places never seen. Could also be used for a really wierd subtraction algo that doesn't split polys. Anyway, I think it works, but for whatever reason the stuff I wrote to redisplay the model minus several polys just leaves me with no model. anyway, The idea is that a camera or object rotates around the object checking if it can see each triangle. if it can see one, it marks it down as visible. At the end of rotating, it creates a new mesh and puts the visible triangles and vertices into it. sorry, no comments in the code as I just wrote it tonight and am to lazy.
Wish I knew how to do code boxes!
Graphics3D 640,480
campiv=CreatePivot()
Global cam=CreateCamera()
li=CreateLight()
model=LoadMesh("models\plltload.3ds")
EntityPickMode model,2,True
EntityAlpha model,.5
mid3dhandle model
If MeshHeight(model)>MeshWidth(model) Then
If MeshDepth(model)>MeshHeight(model) Then l=MeshDepth(model) Else l=MeshHeight(model)
Else
If MeshDepth(model)>MeshWidth(model) Then l=MeshDepth(model) Else l=MeshWidth(model)
EndIf
PositionEntity cam,0,0,-l*2
EntityParent cam,campiv
CameraRange cam,.1,l*4
FlipMesh model
Global sum#
sc=CountSurfaces(model)
For s=1 To sc
ct=CountTriangles(GetSurface(model,s))
sum#=sum#+ct
If ct>max Then max=ct
Next
Dim tri(sc,max)
Global pointer=CreatePivot()
Global camp=CreatePivot()
Global c
For y=0 To 35
For p=0 To 35
RotateEntity campiv,p*10,y*10,0
PositionEntity camp,EntityX(cam,1),EntityY(cam,1),EntityZ(cam,1)
UpdateWorld
RenderWorld
Flip
optimize model
Next
Next
m2=CreateMesh()
m2s=CreateSurface(m2)
For s=1 To sc
surf=GetSurface(model,s)
For t=0 To ct
If tri(s,t)=1 Then
coun#=coun#+1
v0=TriangleVertex(surf,t,0)
v1=TriangleVertex(surf,t,1)
v2=TriangleVertex(surf,t,2)
x0#=VertexX(surf,v0)
x1#=VertexX(surf,v1)
x2#=VertexX(surf,v2)
y0#=VertexY(surf,v0)
y1#=VertexY(surf,v1)
y2#=VertexY(surf,v2)
z0#=VertexZ(surf,v0)
z1#=VertexZ(surf,v1)
z2#=VertexZ(surf,v2)
v0a=AddVertex(surf,x0#,y0#,z0#)
VertexColor surf,v0a,255,0,0
v1a=AddVertex(surf,x1#,y1#,z1#)
VertexColor surf,v1a,255,0,0
v2a=AddVertex(surf,x2#,y2#,z2#)
VertexColor surf,v2a,255,0,0
temp=AddTriangle(surf,v0a,v1a,v2a)
EndIf
Next
Next
If coun=0 Then DebugLog "0%" Else DebugLog ((1-coun#/TrisRendered())*100)+"% reduced"
EntityAlpha m2,1
EntityColor m2,255,0,0
UpdateNormals m2
mid3dhandle m2
PositionEntity m2,0,0,0
FreeEntity model
While Not KeyHit(1)
UpdateWorld
RenderWorld
Flip
Wend
Function optimize(model)
sc=CountSurfaces(model)
For s=1 To sc
surf=GetSurface(model,s)
ct=CountTriangles(surf)
For t=0 To ct
If tri(s,t)=0 Then
v0=TriangleVertex(surf,t,0)
v1=TriangleVertex(surf,t,1)
v2=TriangleVertex(surf,t,2)
x0#=VertexX(surf,v0)
x1#=VertexX(surf,v1)
x2#=VertexX(surf,v2)
y0#=VertexY(surf,v0)
y1#=VertexY(surf,v1)
y2#=VertexY(surf,v2)
z0#=VertexZ(surf,v0)
z1#=VertexZ(surf,v1)
z2#=VertexZ(surf,v2)
PositionEntity pointer,x0#,y0#,z0#
If EntityVisible(camp,pointer) And tri(s,t)=0 Then
VertexColor surf,v0,255,0,0
VertexColor surf,v1,255,0,0
VertexColor surf,v2,255,0,0
tri(s,t)=1
c=c+1
EndIf
PositionEntity pointer,x1#,y1#,z1#
If EntityVisible(camp,pointer) And tri(s,t)=0 Then
VertexColor surf,v0,255,0,0
VertexColor surf,v1,255,0,0
VertexColor surf,v2,255,0,0
tri(s,t)=1
c=c+1
EndIf
PositionEntity pointer,x2#,y2#,z2#
If EntityVisible(camp,pointer) And tri(s,t)=0 Then
VertexColor surf,v0,255,0,0
VertexColor surf,v1,255,0,0
VertexColor surf,v2,255,0,0
tri(s,t)=1
c=c+1
EndIf
EndIf
;DebugLog c+" out of "+sum
Next
Next
End Function
Function Mid3dHandle(mesh)
ux#=-100000
uy#=-100000
uz#=-100000
lx#=100000
ly#=100000
lz#=100000
cs=CountSurfaces(mesh)
For s=1 To cs
surf=GetSurface(mesh,s)
cv=CountVertices(surf)-1
For v=0 To cv
vx#=VertexX#(surf,v)
vy#=VertexY#(surf,v)
vz#=VertexZ#(surf,v)
If vx#<lx# Then lx#=vx#
If vx#>ux# Then ux#=vx#
If vy#<ly# Then ly#=vy#
If vy#>uy# Then uy#=vy#
If vz#<lz# Then lz#=vz#
If vz#>uz# Then uz#=vz#
Next
Next
ax#=(ux#+lx#)/2
ay#=(uy#+ly#)/2
az#=(uz#+lz#)/2
PositionMesh mesh,-ax#,-ay#,-az#
End Function
Also, Mid3dHandle is a function I recently made and intend to put in the code archives, although have not yet.
|