; AddTriangle Example ; ------------------- ; AddTriangle connects three existing vertices into a solid triangle. ; Triangles are ONE-SIDED: a triangle is only visible from the side ; where its vertices appear in clockwise order. Space rebuilds this ; triangle with the opposite winding so you can see the difference. Graphics3D 640,480 SetBuffer BackBuffer() camera=CreateCamera() PositionEntity camera,0,0,-4 mesh=CreateMesh() surf=CreateSurface(mesh) ; Show vertex colours without needing a light EntityFX mesh,2 clockwise=1 rebuild=1 While Not KeyDown(1) ; Space flips the winding order and rebuilds the triangle If KeyHit(57) Then clockwise=1-clockwise : rebuild=1 If rebuild Then rebuild=0 ClearSurface surf v0=AddVertex(surf,-1.5,-1,0) v1=AddVertex(surf,0,1.5,0) v2=AddVertex(surf,1.5,-1,0) VertexColor surf,v0,255,80,80 VertexColor surf,v1,80,255,80 VertexColor surf,v2,80,160,255 If clockwise Then ; Clockwise as seen by the camera - the triangle is visible AddTriangle surf,v0,v1,v2 Else ; Anticlockwise - the triangle faces AWAY and disappears AddTriangle surf,v0,v2,v1 EndIf EndIf ; Arrow keys move the camera (walk round the back to see the other side) 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,"Space: flip winding order Arrow keys: move camera Esc: exit" If clockwise Then Text 0,20,"AddTriangle surf,v0,v1,v2 - clockwise, facing the camera" Else Text 0,20,"AddTriangle surf,v0,v2,v1 - anticlockwise, facing away!" EndIf Flip Wend End