Here's another version which retains texture coords if it interests you ..
Graphics3D 640,480,16,1
AmbientLight 128,128,128
;texture
TEX = CreateTexture(64,64)
SetBuffer TextureBuffer( TEX )
Color 250,50,50
Rect 0,0,64,64,0
Rect 1,1,62,62,0
Rect 2,2,60,60,0
SetBuffer BackBuffer()
;plane
PLANE = CreatePlane()
EntityColor PLANE, 50,50,100
;light
Global LIGHT = CreateLight()
RotateEntity LIGHT, 30,-45,0
;camera
Global CAMERA = CreateCamera()
PositionEntity CAMERA, 0, 50, -50
PointEntity CAMERA, PLANE
;level
Global LEVEL = CreateMesh() : CreateSurface( LEVEL )
EntityFX LEVEL, 4
EntityColor LEVEL, 200,100,50
EntityTexture LEVEL, TEX
Dim PT( 46,26 )
;fps
Global FPSlastupdatetime
Global FPSframecount
Global FPSlastcount#
;culling constants
Const C_Front = 1
Const C_Rear = 4
Const C_Left = 8
Const C_Right = 2
Const C_Top = 16
Const C_Bottom = 32
LEVELcreate()
While Not KeyHit(1)
TurnEntity LEVEL, 0, .25, 0
RenderWorld
If KeyHit( 57) LEVELcreate()
Color 255,255,255
Text 0,0, "Polys : "+CountTriangles( GetSurface( LEVEL, 1 ) )
Text 0,10, "Fps : "+FPS()
Flip
Wend
End
;==================================================================================================
;==================================================================================================
;==================================================================================================
Function LEVELcreate()
ClearSurface GetSurface( LEVEL, 1 ), True, True
;reset maze
For x = 1 To 45
For y = 1 To 25
PT( x, y ) = 0
Next
Next
;create outer wall
For x = 1 To 45
PT( x , 1 ) = 1
PT( x , 25 ) = 1
Next
For y = 1 To 25
PT( 1 , y ) = 1
PT( 45 , y ) = 1
Next
;create random points
For l = 1 To 400
PT( Rand(2,44), Rand(2,24) ) = 1
Next
;create mesh
For y = 1 To 25
For x = 1 To 45
If PT( x, y ) = 1
Flag = C_Bottom ;+C_Top
If PT( x-1 , y ) = 1 Flag = Flag + C_Left
If PT( x+1 , y ) = 1 Flag = Flag + C_Right
If PT( x , y-1 ) = 1 Flag = Flag + C_Rear
If PT( x, y+1 ) = 1 Flag = Flag + C_Front
;flag = 0
Tmp = MESHcull( CreateCube(), Flag )
PositionMesh tmp, ( x -23 ) * 2 , 1 , ( y - 13 ) * 2
AddMesh tmp, LEVEL : FreeEntity tmp
EndIf
Next
Next
End Function
;==================================================================================================
;==================================================================================================
;==================================================================================================
Function MESHcull( Mesh , Flags = 0 )
If Flags = 0 Return Mesh
s = GetSurface( Mesh , 1 )
Copy = CreateMesh()
Cs = CreateSurface( Copy )
For t = 0 To CountTriangles( s )-1
;calculate triangle normal
v0 = TriangleVertex( s, t, 0 )
v1 = TriangleVertex( s, t, 1 )
v2 = TriangleVertex( s, t, 2 )
ax# = VertexX( s, v1 ) - VertexX( s, v0 )
ay# = VertexY( s, v1 ) - VertexY( s, v0 )
az# = VertexZ( s, v1 ) - VertexZ( s, v0 )
bx# = VertexX( s, v2 ) - VertexX( s, v1 )
by# = VertexY( s, v2 ) - VertexY( s, v1 )
bz# = VertexZ( s, v2 ) - VertexZ( s, v1 )
Nx# = ( ay * bz ) - ( az * by )
Ny# = ( az * bx ) - ( ax * bz )
Nz# = ( ax * by ) - ( ay * bx )
Ns# = Sqr( Nx * Nx + Ny*Ny + Nz*Nz )
Nx = Nx / Ns
Ny = Ny / Ns
Nz = Nz / Ns
OK = True
For l = 0 To 5
Flag = 2^l
If ( Flags And Flag) = Flag
;determine plane normal
Select Flag
Case C_Front px# = 0 : py# = 0 : pz# = 1
Case C_Rear px# = 0 : py# = 0 : pz# = -1
Case C_Left px# = -1 : py# = 0 : pz# = 0
Case C_Right px# = 1 : py# = 0 : pz# = 0
Case C_Top px# = 0 : py# = 1 : pz# = 0
Case C_Bottom px# = 0 : py# = -1 : pz# = 0
End Select
If Nx = Px And Ny = Py And Nz = Pz OK = False
EndIf
Next
If OK
nv0 = AddVertex( Cs, VertexX( s , v0 ) , VertexY( s, v0 ) , VertexZ( s, v0 ) )
nv1 = AddVertex( Cs, VertexX( s , v1 ) , VertexY( s, v1 ) , VertexZ( s, v1 ) )
nv2 = AddVertex( Cs, VertexX( s , v2 ) , VertexY( s, v2 ) , VertexZ( s, v2 ) )
VertexNormal Cs , nv0 , Nx, Ny, Nz
VertexNormal Cs , nv1 , Nx, Ny, Nz
VertexNormal Cs , nv2 , Nx, Ny, Nz
VertexTexCoords Cs, nv0, VertexU( s, v0 ), VertexV( s, v0 )
VertexTexCoords Cs, nv1, VertexU( s, v1 ), VertexV( s, v1 )
VertexTexCoords Cs, nv2, VertexU( s, v2 ), VertexV( s, v2 )
AddTriangle( Cs , nv0, nv1, nv2 )
EndIf
Next
FreeEntity mesh
Return copy
End Function
;==================================================================================================
;==================================================================================================
;==================================================================================================
Function FPS(frequency=200)
time=MilliSecs()
FPSframecount=FPSframecount+1
elapsed#=time-FPSlastupdatetime
If elapsed>=frequency
FPSlastcount=FPSframecount/elapsed*1000.0
FPSframecount=0
FPSlastupdatetime=time
EndIf
Return FPSlastcount
End Function