Page 1 of 2

When using love.math.noise, either the program stops responding or I get some kind of expected results

Posted: Fri Nov 26, 2021 10:22 pm
by rando
Hello, I'm trying to generate some terrain to make a game similar to Risk. To generate the terrain, I'm using love.math.noise.

Code: Select all

for x = 0, 800 do
	for y = 0, 600 do
		value[x] = value[x] or {}
		xCoord = x / 800 - 0.5
		yCoord = y / 600 - 0.5
		Noise = love.math.noise(xCoord, yCoord)
		value[x][y] = Noise
	end
end
But this code most of the time ends with a blank window that I have to close because it isn't responding. When it doesn't do that, it gets to the drawing code:

Code: Select all

for i = 0, 799 do
	for j = 0, 599 do
		love.graphics.setColor(value[i][j], value[i][j], value[i][j])
		love.graphics.points(i, j)
	end
end
The results are mostly what I'd expect, but there are weird vertical lines that really stick out.
Screenshot (85).png
Screenshot (85).png (105.27 KiB) Viewed 6808 times
I have no idea what's going on. Can someone help me please?

Re: When using love.math.noise, either the program stops responding or I get some kind of expected results

Posted: Sat Nov 27, 2021 2:59 am
by BrotSagtMist
Running this very code:ImageWhatever problem you have there is not hidden in these lines.

Re: When using love.math.noise, either the program stops responding or I get some kind of expected results

Posted: Sat Nov 27, 2021 8:40 am
by grump
Drawing half a million points is probably the worst way to do this.

Code: Select all

local imgd = love.image.newImageData(800, 600)
imgd:mapPixel(function(x, y)
	local n = love.math.noise(x / 800, y / 600)
	return n, n, n, 1
end)

local img = love.graphics.newImage(imgd)

function love.draw()
	love.graphics.draw(img)
end

Re: When using love.math.noise, either the program stops responding or I get some kind of expected results

Posted: Sat Nov 27, 2021 10:25 pm
by pgimeno
What grump said, but besides, if you place the point in the middle between two pixels, your GL driver is free to pick either side, and it decides that it's fun to pick sometimes the left pixel, sometimes the right pixel. You need to add 0.5 to the pixel coordinates so that they fall in the centre of the pixel. BrotSagtMist's drivers seem not to be so whimsical.

Re: When using love.math.noise, either the program stops responding or I get some kind of expected results

Posted: Thu Dec 02, 2021 1:59 pm
by milon
pgimeno wrote: Sat Nov 27, 2021 10:25 pm What grump said, but besides, if you place the point in the middle between two pixels, your GL driver is free to pick either side, and it decides that it's fun to pick sometimes the left pixel, sometimes the right pixel. You need to add 0.5 to the pixel coordinates so that they fall in the centre of the pixel. BrotSagtMist's drivers seem not to be so whimsical.
Either that, or else draw rectangles that are 1 px in width and height.
But grump's version is better. :) (And now I'm off to review mapPixel, haha!)

Re: When using love.math.noise, either the program stops responding or I get some kind of expected results

Posted: Sat Dec 04, 2021 10:36 pm
by rando
I got this fixed on another website. The problem was 100% lag, because of drawing all those points every frame. What I needed to do was render the data onto a buffer and draw that buffer instead of each individual point every frame. One thing I experienced is that canvases didn't work well, and I'm not sure why that was, but imageData works really well.

Re: When using love.math.noise, either the program stops responding or I get some kind of expected results

Posted: Sat Dec 04, 2021 10:57 pm
by grump
rando wrote: Sat Dec 04, 2021 10:36 pm What I needed to do was render the data onto a buffer and draw that buffer instead of each individual point every frame.
Well that's a pretty good solution, ngl.

Re: When using love.math.noise, either the program stops responding or I get some kind of expected results

Posted: Sun Dec 05, 2021 1:45 pm
by zorg
Canvases are still gpu-side; ImageData isn't. The former was equivalent to what you were already doing, drawing tons of stuff onto a buffer, except the buffer wasn't the game's window, but a canvas; more or less the same thing.

Re: When using love.math.noise, either the program stops responding or I get some kind of expected results

Posted: Tue Dec 07, 2021 4:01 pm
by milon
rando wrote: Sat Dec 04, 2021 10:36 pm ... One thing I experienced is that canvases didn't work well, and I'm not sure why that was, but imageData works really well.
zorg wrote: Sun Dec 05, 2021 1:45 pm Canvases are still gpu-side; ImageData isn't. ...
I know this is kind of off-topic now, sorry about that. But it seems really strange that ImageData (CPU-side) would perform better than canvases (GPU-side). Is that just a strange corner case, or is there a bigger picture that I'm missing?

Re: When using love.math.noise, either the program stops responding or I get some kind of expected results

Posted: Tue Dec 07, 2021 5:01 pm
by grump
ImageData: lives in RAM, allows easy and fast pixel access. Is just a blob of data and can't be drawn to screen, must be copied into a texture on the GPU first.

Canvas: lives in GPU's VRAM, no easy or fast pixel access, can be rendered to and drawn to screen, does not require copying of pixel data.

You can draw a million polygons to a Canvas to simulate that one mapPixel function, but it's not as efficient. GPUs are just not in the single-pixel business. A fragment shader would be possible, but also much more work.