Blitz3D+ Command Reference

CreateMesh ( [parent] )

Parameters

parent (optional) - parent entity for the new mesh; 0 for no parent (default)

Description

Creates an empty mesh entity and returns its handle.

A freshly created mesh has no surfaces, vertices or triangles, so it is invisible until you build some geometry into it. The usual pattern is: CreateSurface to add a surface, AddVertex at least three times to place corner points, then AddTriangle to connect them. This is how you build anything the standard primitives can't give you - trails, custom level geometry, debris, procedural props.

Two gotchas while building: triangles are one-sided (the vertex winding decides which side you can see - see AddTriangle), and vertex normals start at 0,0,0, so call UpdateNormals when the shape is finished or the mesh will light strangely.

The optional parent parameter attaches the new mesh to another entity so it moves, turns and scales with it. The relationship is one-way: moving the child never affects the parent. The new mesh starts exactly at the parent's position (its own local coordinates are 0,0,0 relative to the parent).

See also: CreateSurface, AddVertex, AddTriangle, UpdateNormals, LoadMesh, CopyMesh.

Example

; CreateMesh Example
; ------------------

; CreateMesh creates an empty mesh with no geometry at all.
; We then add a surface, add vertices, and connect them with
; triangles to build a ramp entirely by hand.

Graphics3D 640,480
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,1,-6

light=CreateLight()
RotateEntity light,45,30,0

; Create the blank mesh - the documented command
ramp=CreateMesh()

; Surfaces hold the actual triangles (a surface must belong to a mesh)
surf=CreateSurface(ramp)

; Six vertices, one per corner of a wedge shape
v0=AddVertex(surf,-2,0,-1) ; bottom front left
v1=AddVertex(surf,-2,0,1)  ; bottom back left
v2=AddVertex(surf,2,0,1)   ; bottom back right
v3=AddVertex(surf,2,0,-1)  ; bottom front right
v4=AddVertex(surf,-2,2,-1) ; top front left
v5=AddVertex(surf,-2,2,1)  ; top back left

; Triangles are one-sided: list each triangle's vertices in clockwise
; order as seen from OUTSIDE the ramp, or that face will be invisible
AddTriangle surf,v0,v3,v2 ; bottom half 1
AddTriangle surf,v0,v2,v1 ; bottom half 2
AddTriangle surf,v0,v4,v3 ; front face
AddTriangle surf,v1,v2,v5 ; back face
AddTriangle surf,v0,v1,v5 ; left side half 1
AddTriangle surf,v0,v5,v4 ; left side half 2
AddTriangle surf,v2,v4,v5 ; slope half 1
AddTriangle surf,v2,v3,v4 ; slope half 2

; Point the normals outwards so the light shades the ramp correctly
UpdateNormals ramp

EntityColor ramp,90,180,255

wire=0

While Not KeyDown(1)

    ; Space toggles wireframe so you can see the triangle structure
    If KeyHit(57) Then wire=1-wire : WireFrame wire

    ; Keep the ramp slowly turning
    TurnEntity ramp,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,"Space: toggle wireframe   Arrow keys: move camera   Esc: exit"
    Text 0,20,"CreateMesh ramp: 6 vertices, 8 triangles built by hand"

    Flip

Wend

End

Index