Page 1 of 1

Weird error on testing mesh code from wiki

Posted: Sun May 29, 2016 7:11 pm
by palmettos
I'm running this code (simplified, but taken from the wiki article on meshes: https://love2d.org/wiki/love.graphics.newMesh [see example 'Creates a circle and draws it more efficiently than love.graphics.circle.']) on version 0.9.0:

Code: Select all

function love.load()
	local vertices = {{0, 0}}
	local segments = 40
	for i=0, segments do
		local angle = (i/segments)*math.pi*2
		local x = math.cos(angle)
		local y = math.sin(angle)
		table.insert(vertices, {x, y})
	end
	img = love.graphics.newImage('pig.png')
	mesh = love.graphics.newMesh(vertices, img, 'fan')
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
and I'm receiving this error:
main.lua:14: bad argument #8 to 'newMesh' (number expected, got nil)

Traceback

[C]: in function 'newMesh'
main.lua:14: in function 'load'
[C]: in function 'xpcall'
The wiki article on meshes is confusing regarding which function template is available in 0.9.0. My only guess is that I'm sending the wrong arguments to the constructor. Can anyone see what is wrong with this code?

Re: Weird error on testing mesh code from wiki

Posted: Sun May 29, 2016 8:17 pm
by palmettos
I fixed it. The problem is that the example I pulled code from doesn't set the u and v texture coordinates in the vertex tables, as the other examples do. I assumed they defaulted to some value, but they do not. They need to be set explicitly. The example is wrong in the context of 0.9.0.

Here is the correct code:

Code: Select all

function love.load()
	local vertices = {{0, 0, 0.5, 0.5}}
	local segments = 40
	for i=0, segments do
		local angle = (i/segments)*math.pi*2
		local x = math.cos(angle)
		local y = math.sin(angle)
		table.insert(vertices, {x, y, (x+1)*0.5, (y+1)*0.5})
	end
	img = love.graphics.newImage('pig.png')
	mesh = love.graphics.newMesh(vertices, img, 'fan')
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