surface.DrawPoly

From GMod Wiki

Jump to: navigation, search
Function
Syntax surface.DrawPoly( Table vertex data structure )
Where is this used?
Description:
Draws a textured polygon by drawing each vertex specified within the table, then filling in the area.
Returns: nil
Part of Library: surface
Realm: NewerClient.png
BBCode Link: [b][url=http://wiki.garrysmod.com/?title=Surface.DrawPoly]Surface.DrawPoly [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]


Examples

DescriptionThis will draw a right triangle on the center of the HUD.
Used onNewerClient.png
Code
 
 triangletex = surface.GetTextureID( PICK A TEXTURE PATH ) -- this is our triangles texture, try to find your own
 centerx = ScrW()/2 --this is the middle x cordinate
 centery = ScrH()/2 --this is the middle y cordinate
 trianglevertex = {{ },{ },{ }} --create the two dimensional table
 
 --First Vertex
trianglevertex[1]["x"] = centerx
trianglevertex[1]["y"] = centery
trianglevertex[1]["u"] = 0 //Top Left
trianglevertex[1]["v"] = 0
 
 --Second Vertex
trianglevertex[2]["x"] = centerx + 50
trianglevertex[2]["y"] = centery
trianglevertex[2]["u"] = 1 //Top Right
trianglevertex[2]["v"] = 0
 
 --Third Vertex
trianglevertex[3]["x"] = centerx
trianglevertex[3]["y"] = centery + 50
trianglevertex[3]["u"] = 0 //Bottom Left
trianglevertex[3]["v"] = 1
 
local function HUDPaint()
 
    surface.SetTexture( triangletex ) --set the texture of the triangle
    surface.SetDrawColor( 255, 255, 255, 255 ) --set the additive color
    surface.DrawPoly( trianglevertex ) --draw the triangle with our triangle table
 
end
hook.Add("HUDPaint","Draw Triangle", HUDPaint)
 
OutputN/A


DescriptionThis will generate a polygon ready to draw.
Used onNewerClient.png
Code
 
local sin,cos,rad = math.sin,math.cos,math.rad; --Only needed when you constantly calculate a new polygon, it slightly increases the speed.
function GeneratePoly(x,y,radius,quality)
    local circle = {};
    local tmp = 0;
    for i=1,quality do
        tmp = rad(i*360)/quality
        circle[i] = {x = x + cos(tmp)*radius,y = y + sin(tmp)*radius};
    end
    return circle;
end
 
OutputN/A


DescriptionThis will generate a polygon ready to draw, with uv mapping.
Used onNewerClient.png
Code
 
local sin,cos,rad = math.sin,math.cos,math.rad
local function GeneratePoly(x,y,radius,quality)
    local circle = {};
    local tmp = 0;
	local s,c;
    for i=1,quality do
        tmp = rad(i*360)/quality;
		s = sin(tmp);
		c = cos(tmp);
        circle[i] = {x = x + c*radius,y = y + s*radius,u = (c+1)/2,v = (s+1)/2};
    end
    return circle;
end
 
OutputN/A


Additional Notes

The U and V components are used to control how the texture is rendered on the polygon. EG Setting U,V to 0,0 will have the texture top left corner drawn at that vertex.

For most situations you should use the UV coordinates (0,0) (0,1) (1,1) (1,0) for top-left, top-right, bottom right and bottom left coordinates, respectively.

Using values larger than 1 will repeat the texture, values less than 1 for parts of the texture.

If you are not using a texture you can leave them blank

table ={
    [vertex number] = { you may have any number of vertices
        x= x position of vertex
        y= y position of vertex
        u= horizontal texture cordinate
        v= vertical texture cordinate
    }
}
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox