Blitz3D+ Command Reference

TriangleVertex ( surface,index,vertex )

Parameters

surface - surface handle
index - triangle index, in the range 0 to CountTriangles( surface )-1
vertex - which corner of the triangle: 0, 1 or 2

Description

Returns the vertex index used by one corner of a triangle.

This is the key to walking a mesh's structure: loop over the triangles, ask for corners 0, 1 and 2, then feed those vertex indices to VertexX, VertexY and VertexZ to read the actual positions. Games use it for things like building custom collision data, finding the triangle under a pick, or writing simple exporters and analysers.

The corner order is the same winding order the triangle was created with, which decides its visible side.

See also: CountTriangles, AddTriangle, VertexX, VertexCoords, PickedTriangle.

Example

; TriangleVertex Example
; ----------------------

; TriangleVertex is the reverse of AddTriangle: it reads back which
; vertex index forms a given corner (0, 1 or 2) of a triangle.
; Space cycles through the corners; a marker highlights the vertex
; that TriangleVertex reports.

Graphics3D 640,480
SetBuffer BackBuffer()

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

mesh=CreateMesh()
surf=CreateSurface(mesh)

; One triangle with a distinct colour per corner
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
tri=AddTriangle(surf,v0,v1,v2)

; Show vertex colours without needing a light
EntityFX mesh,2

; A marker sphere highlights the reported vertex
marker=CreateSphere()
ScaleEntity marker,0.12,0.12,0.12
EntityColor marker,255,0,255
EntityFX marker,1

corner=0

While Not KeyDown(1)

    ; Space cycles corner 0 -> 1 -> 2
    If KeyHit(57) Then corner=(corner+1) Mod 3

    ; The documented command: which vertex forms this corner?
    v=TriangleVertex(surf,tri,corner)

    ; Park the marker on that vertex using its stored position
    PositionEntity marker,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)

    ; 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,"Space: next corner   Arrow keys: move camera   Esc: exit"
    Text 0,20,"TriangleVertex(surf,"+tri+","+corner+") = vertex "+v

    Flip

Wend

End

Index