Thursday, January 12, 2017

How can I generate a terrain heightmap from the perlin algorithm?


How can I generate a terrain heightmap from the perlin algorithm? I am trying to make a terrain generator (like World Machine). This is the source code I have for the perlin. The only thing that I can't figure out is how to use the perlin algorithm to generate a heightmap image.



Answer



A terrain heightmap texture is a regular grid of point samples, which indicate the height at that particular location on the terrain. When you want to create such a heightmap from a function f(vec2), you evaluate the function with the position of each of the grid points of the terrain to get the value at those points, and let that be the color value in the texture.


If the function expects the coordinate in a different space (other scale, offset, range) than the pixel coordinates of your texture, you need to map the pixel coordinates into the coordinate range you wish to invoke the function with.


The output values might have the wrong scale or offset, so you might need to compensate for those by offsetting and scaling the returned value to fit inside the range of values your texture component expects (0 through 255 for a typical 8-bit component):


foreach (y in [0..height))
foreach (x in [0..width))

vec2 p = transform_pixel_position_to_parameter(x, y);
tex(x,y) = height_offset + height_scale * f(p);

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...