Blitz3D+ Command Reference

AddTriangle ( surface,v0,v1,v2 )

Parameters

surface - surface handle
v0 - index number of the triangle's first vertex
v1 - index number of the triangle's second vertex
v2 - index number of the triangle's third vertex

Description

Adds a triangle to a surface and returns the triangle's index number, starting from 0.

The v0, v1 and v2 parameters are vertex index numbers returned by AddVertex on the same surface.

Triangles are one-sided, and the order of the three corners - the winding - decides which side you see. Picture the three points as a dot-to-dot: if v0, v1, v2 run clockwise from where the camera is looking, the triangle is visible; if they run anti-clockwise you are looking at its invisible back. A blank screen when you first build a mesh almost always means the winding is backwards - swap any two indices and it will appear.

One-sided triangles are a speed win: the renderer skips every face pointing away from the camera, such as the far side of a ball. When you genuinely need both sides, add a second triangle with two indices swapped, flip a whole copied mesh with FlipMesh, or set EntityFX flag 16 to disable backface culling for the entity.

Adding triangles does not update vertex normals - call UpdateNormals when the mesh is finished so lighting behaves.

See also: AddVertex, CreateSurface, FlipMesh, TriangleVertex, UpdateNormals, EntityFX.

Example

; 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

Index