; CreateSurface Example
; ---------------------

; A mesh is only a container: its triangles actually live in surfaces.
; Each surface has ONE material (brush), so a two-colour object needs
; two surfaces. Here we build a warning sign from two diamonds.

Graphics3D 640,480
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,0,-5

sign=CreateMesh()

; Brushes give each surface its own colour
red_brush=CreateBrush()
BrushColor red_brush,255,60,60
yellow_brush=CreateBrush()
BrushColor yellow_brush,255,220,0

; Surface 1: outer diamond, created with the red brush
outer=CreateSurface(sign,red_brush)
v0=AddVertex(outer,0,1.8,0)
v1=AddVertex(outer,1.8,0,0)
v2=AddVertex(outer,0,-1.8,0)
v3=AddVertex(outer,-1.8,0,0)
AddTriangle outer,v0,v1,v2
AddTriangle outer,v0,v2,v3

; Surface 2: inner diamond, created with the yellow brush,
; nudged slightly towards the camera so it draws on top
inner=CreateSurface(sign,yellow_brush)
v0=AddVertex(inner,0,1.2,-0.01)
v1=AddVertex(inner,1.2,0,-0.01)
v2=AddVertex(inner,0,-1.2,-0.01)
v3=AddVertex(inner,-1.2,0,-0.01)
AddTriangle inner,v0,v1,v2
AddTriangle inner,v0,v2,v3

; Fullbright (1) + two-sided (16) so the sign shows while spinning
EntityFX sign,17

While Not KeyDown(1)

    TurnEntity sign,0,1,0

    ; Arrow keys move the camera
    If KeyDown(200) Then MoveEntity camera,0,0,0.1
    If KeyDown(208) Then MoveEntity camera,0,0,-0.1
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    RenderWorld

    Text 0,0,"Arrow keys: move camera   Esc: exit"
    Text 0,20,"CreateSurface: this mesh has "+CountSurfaces(sign)+" surfaces - one per colour"

    Flip

Wend

End
