surface/DrawPoly

(Difference between revisions)

surface.DrawPoly( )

Description

Draws a textured polygon (secretly a triangle fan) with a maximum of 256 vertices. Only works properly with convex polygons. You may try to render concave polygons, but there is no guarantee that things wont get messed up.

Unlike most surface library functions, non-integer coordinates are not rounded.

 WARNING You must reset the drawing color and texture before calling the function to ensure consistent results. See examples below.
 NOTE This is a rendering function that requires a 2D rendering context.This means that it will only work in hooks with a 2D rendering context.

Arguments

table vertices

A table containing integer vertices. See the PolygonVertex structure.

The vertices must be in clockwise order.

Examples

Example

Draws a red triangle in the top left corner of the screen.

```local triangle = {
{ x = 100, y = 200 },
{ x = 150, y = 100 },
{ x = 200, y = 200 }
}

surface.SetDrawColor( 255, 0, 0, 255 )
draw.NoTexture()
surface.DrawPoly( triangle )

end )```

Output:

Examples

Example

A helper function to draw a circle using surface.DrawPoly.

```function draw.Circle( x, y, radius, seg )
local cir = {}

table.insert( cir, { x = x, y = y, u = 0.5, v = 0.5 } )
for i = 0, seg do
local a = math.rad( ( i / seg ) * -360 )
table.insert( cir, { x = x + math.sin( a ) * radius, y = y + math.cos( a ) * radius, u = math.sin( a ) / 2 + 0.5, v = math.cos( a ) / 2 + 0.5 } )
end

local a = math.rad( 0 ) -- This is needed for non absolute segment counts
table.insert( cir, { x = x + math.sin( a ) * radius, y = y + math.cos( a ) * radius, u = math.sin( a ) / 2 + 0.5, v = math.cos( a ) / 2 + 0.5 } )

surface.DrawPoly( cir )
end

surface.SetDrawColor( 0, 0, 0, 200)
draw.NoTexture()
draw.Circle( ScrW() / 2, ScrH() / 2, 200, math.sin( CurTime() ) * 20 + 25 )

--Usage:
--draw.Circle( x, y, radius, segments )

end )```