CreateQuad ( [width#][,depth#][,x_segments][,z_segments][,two_sided][,parent] )

Parameters

width# (optional) - size along X; 2 (default)

depth# (optional) - size along Z; 2 (default)

x_segments (optional) - columns across X, from 1 to 255; 1 (default)

z_segments (optional) - rows along Z, from 1 to 255; 1 (default)

two_sided (optional) - True adds a separately lit underside as surface 2; False (default)

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

Description

Creates a flat rectangle facing up, optionally divided into a grid, and returns its handle.

The quad lies flat in X and Z at a height of 0, centred on the origin, with its front facing +Y. Use it for water, floors, screens, cloth and flags, and anything else you want to ripple or deform. Stand it up with RotateEntity: a pitch of -90 turns its front towards -Z, where a default camera looks from.

Its vertices come in a fixed order, so a program can animate them directly. They run row by row from the far-left corner (-X, +Z): the vertex for a given row and column is row*(x_segments+1)+column, where column 0 is at -X and row 0 at +Z. The texture coordinates follow the same grid: U runs from 0 to 1 across X, and V from 0 at the far edge to 1 at the near one. Move the vertices with VertexCoords, then call UpdateNormals to relight the quad, as the example does.

Like any mesh, a quad is one-sided and disappears from behind. With two_sided True, the underside is added as surface 2, with its own vertices in the same order and normals facing down, so it lights correctly from below and can be painted differently with PaintSurface. UpdateNormals works out each surface separately, so it keeps both sides right.

Each side can have up to 255 x 255 segments, which is 65,536 vertices, the most one surface holds. A quad is not a closed solid, so it is not suitable for the CSG commands. Automatic EntityPhysics treats a static quad as a triangle mesh and a moving one as a thin box.

Arguments are checked in release builds too: a size that is not a positive number, or a segment count outside 1 to 255, stops the program with a runtime error naming the argument.

See also: CreateBox, CreatePlane, VertexCoords, UpdateNormals, CreateMesh.

Example

; CreateQuad Example
; ------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

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

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

; A flagpole that turns slowly, so you see both sides of the flag
pole=CreatePivot()
mast=CreateCylinder(12,True,pole)
ScaleEntity mast,0.05,1.8,0.05
PositionEntity mast,-1.55,0.2,0

; Red for the front of the flag, blue for its underside
front=CreateBrush(220,40,40)
back=CreateBrush(40,90,220)

detail=8
two_sided=True
flag=0

While Not KeyDown(1)

    ; [ / ] change the number of segments, Space toggles the underside
    If KeyHit(26) And detail>1 Then
        detail=detail-1
        If flag Then FreeEntity flag
        flag=0
    EndIf
    If KeyHit(27) And detail<20 Then
        detail=detail+1
        If flag Then FreeEntity flag
        flag=0
    EndIf
    If KeyHit(57) Then
        two_sided=Not two_sided
        If flag Then FreeEntity flag
        flag=0
    EndIf

    If flag=0 Then
        ; A 3 x 2 m quad, stood upright on the pole with its +Y side facing us
        x_segments=detail*3
        z_segments=detail*2
        flag=CreateQuad(3,2,x_segments,z_segments,two_sided,pole)
        RotateEntity flag,-90,0,0
        PositionEntity flag,0,1,0
        PaintSurface GetSurface(flag,1),front
        If two_sided Then PaintSurface GetSurface(flag,2),back
    EndIf

    ; Ripple every vertex; both sides use the same documented order
    t#=MilliSecs()*0.3
    For s=1 To CountSurfaces(flag)
        surface=GetSurface(flag,s)
        For row=0 To z_segments
            For column=0 To x_segments
                ; Rows run from the far edge (+Z), columns from the left (-X)
                index=row*(x_segments+1)+column
                x#=-1.5+column*3.0/x_segments
                z#=1-row*2.0/z_segments
                ; The free edge flaps more than the edge on the pole
                y#=0.2*Sin(t-column*20)*column/x_segments
                VertexCoords surface,index,x,y,z
            Next
        Next
    Next
    UpdateNormals flag

    TurnEntity pole,0,0.4,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   [ / ] : segments   Space: underside   Esc: exit"
    Text 0,20,"CreateQuad(3,2,"+x_segments+","+z_segments+","+two_sided+",pole)"
    If two_sided Then
        Text 0,40,"Two-sided: the underside is surface 2, painted blue"
    Else
        Text 0,40,"One-sided: the flag vanishes when its back is turned"
    EndIf

    Flip

Wend

End

Index