Garry's Mod Wiki

surface.DrawPoly

surface.DrawPoly( table vertices )

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.

You must reset the drawing color and texture before calling the function to ensure consistent results. See examples below.

<rendercontext hook="false" type="2D"/>

Arguments

1 table vertices
A table containing integer vertices. See the Structures/PolygonVertex.

The vertices must be in clockwise order.

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 } } hook.Add("HUDPaint", "PolygonTest", function() surface.SetDrawColor( 255, 0, 0, 255 ) draw.NoTexture() surface.DrawPoly( triangle ) end )
Output:

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 hook.Add("HUDPaint", "PolygonCircleTest", function() 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 )

Page Links


Special Pages


Wikis

?

Render Time: 40ms

Session 0
DB GetPage 5
Generate Html 6
SaveChanges 13
Render Body 0
Render Sidebar 12