Difference between revisions of "love.graphics.newMesh"

m (Creates and draws a textured circle with a red tint at the center.: Renamed 'theta' variable to 'angle')
m (Creates and draws a textured circle with a red tint at the center.)
Line 67: Line 67:
 
<source lang="lua">
 
<source lang="lua">
 
function CreateTexturedCircle(image, segments)
 
function CreateTexturedCircle(image, segments)
local segments = segments or 40
+
segments = segments or 40
 
local vertices = {}
 
local vertices = {}
 
 

Revision as of 03:07, 17 January 2014

Available since LÖVE 0.9.0
This function is not supported in earlier versions.

Creates a new Mesh.

O.png This function can be slow if it is called repeatedly, such as from love.update or love.draw. If you need to use a specific resource often, create it once and store it somewhere it can be reused!  



Function

Synopsis

mesh = love.graphics.newMesh( vertices, image, mode )

Arguments

table vertices
The table filled with vertex information tables for each vertex as follows:
number [1]
The position of the vertex on the x-axis.
number [2]
The position of the vertex on the y-axis.
number [3]
The u texture coordinate. Texture coordinates are normally in the range of [0, 1], but can be greater or less (see WrapMode.)
number [4]
The v texture coordinate. Texture coordinates are normally in the range of [0, 1], but can be greater or less (see WrapMode.)
number [5] (255)
The red color component.
number [6] (255)
The green color component.
number [7] (255)
The blue color component.
number [8] (255)
The alpha color component.
Image image (nil)
The image to use when drawing the Mesh. May be nil to use no image.
MeshDrawMode mode ("fan")
How the vertices are used when drawing. The default mode "fan" is sufficient for simple convex polygons.

Returns

Mesh mesh
The new mesh.

Examples

Creates and draws a Mesh identical to a normal drawn image but with different colors at each corner

function love.load()
	image = love.graphics.newImage("pig.png")
	
	local vertices = {
		{
			-- top-left corner (red-tinted)
			0, 0, -- position of the vertex
			0, 0, -- texture coordinate at the vertex position
			255, 0, 0, -- color of the vertex
		},
		{
			-- top-right corner (green-tinted)
			image:getWidth(), 0,
			1, 0, -- texture coordinates are in the range of [0, 1]
			0, 255, 0
		},
		{
			-- bottom-right corner (blue-tinted)
			image:getWidth(), image:getHeight(),
			1, 1,
			0, 0, 255
		},
		{
			-- bottom-left corner (yellow-tinted)
			0, image:getHeight(),
			0, 1,
			255, 255, 0
		},
	}
	
	-- the Mesh DrawMode "fan" works well for 4-vertex Meshes.
	mesh = love.graphics.newMesh(vertices, image, "fan")
end

function love.draw()
	love.graphics.draw(mesh, 0, 0)
end

Creates and draws a textured circle with a red tint at the center.

function CreateTexturedCircle(image, segments)
	segments = segments or 40
	local vertices = {}
	
	-- The first vertex is at the center, and has a red tint. We're centering the circle around the origin (0, 0).
	table.insert(vertices, {0, 0, 0.5, 0.5, 255, 0, 0})
	
	-- Create the vertices at the edge of the circle.
	for i=0, segments do
		local angle = (i / segments) * math.pi * 2

		-- Unit-circle.
		local x = math.cos(angle)
		local y = math.sin(angle)
		
		-- Our position is in the range of [-1, 1] but we want the texture coordinate to be in the range of [0, 1].
		local u = (x + 1) * 0.5
		local v = (y + 1) * 0.5
		
		-- The per-vertex color defaults to white.
		table.insert(vertices, {x, y, u, v})
	end
	
	-- The "fan" draw mode is perfect for our circle.
	return love.graphics.newMesh(vertices, image, "fan")
end

function love.load()
	image = love.graphics.newImage("pig.png")
	mesh = CreateTexturedCircle(image)
end

function love.draw()
	local radius = 100
	local mx, my = love.mouse.getPosition()
	
	-- We created a unit-circle, so we can use the scale parameter for the radius directly.
	love.graphics.draw(mesh, mx, my, 0, radius, radius)
end

See Also


Other Languages