![]() |
LibGame
v0.4.0
The LG Game Engine - Copyright (C) 2024-2025 ETMSoftware
|
Functions | |
LG_Terrain * | lg_terrain_new (uint16_t width, uint16_t height, float norm_max_height, int noise_type, int seed, float frequency, LG_Texture *tex) |
LG_Terrain * | lg_terrain_new_from_heightmap (float *heightmap, uint16_t width, uint16_t height, float norm_max_height, LG_Texture *tex) |
float | lg_terrain_get_elevation (LG_Terrain *terrain, float x, float y, uint16_t w, uint16_t h) |
void | lg_terrain_free (LG_Terrain *terrain) |
float * | lg_heightmap_generate (uint16_t width, uint16_t height, int noise_type, int seed, float frequency) |
void | lg_heightmaps_add (float *heightmap1, float *heightmap2, uint16_t w, uint16_t h, float k) |
void | lg_heightmap_flatten_border (float *heightmap, uint16_t w, uint16_t h, uint16_t k, int border) |
void | lg_heightmap_apply_func (float *heightmap, uint16_t w, uint16_t h, float(*func)(uint16_t, uint16_t, float)) |
LG_Mesh * | lg_terrain_to_mesh (LG_Terrain *terrain) |
Vertex_rgba * | lg_horiz_grid (uint16_t grid_width, int *n_vertices, float scaling, LG_Color_u c) |
const Vertex * | lg_horiz_grid0 () |
const unsigned short * | lg_horiz_grid0_indices () |
size_t | lg_sizeof_horiz_grid0 () |
size_t | lg_sizeof_horiz_grid0_indices () |
=== Procedural terrain generation ===
Create heightmaps and terrains with Perlin noise.
We use these data structs to compute:
NOTE:
LG_Terrain* lg_terrain_new | ( | uint16_t | width, |
uint16_t | height, | ||
float | norm_max_height, | ||
int | noise_type, | ||
int | seed, | ||
float | frequency, | ||
LG_Texture * | tex | ||
) |
Create a horizontally-centered, normalized LG_Terrain VBO and IBO with noise
If noise_type == PERLIN_NOISE
As we use the excellent FastNoiseLite lib, you can/should check out https://github.com/Auburn/FastNoiseLite/wiki/Documentation
If noise_type == RANDOM_NOISE, seed and frequency are irrelevant
Height is ambiguous here. So:
Returned LG_Terrain must be freed when done
width | Width of the heightmap grid (num of units along one row), must be < HEIGHTMAP_MAX_W |
height | Height of the heightmap grid (num of units along one column), must be < HEIGHTMAP_MAX_H |
norm_max_height | Max height (normalized, should be in range [0.0, 1.0]) |
noise_type | Noise type (lg_noise_type enum) |
seed | Seed, default (if ignored) = 1337 |
frequency | Frequency, default (if ignored) = 0.01 |
tex | A LG_Texture, may be NULL |
LG_Terrain* lg_terrain_new_from_heightmap | ( | float * | heightmap, |
uint16_t | width, | ||
uint16_t | height, | ||
float | norm_max_height, | ||
LG_Texture * | tex | ||
) |
Create a horizontally-centered, normalized LG_Terrain VBO and IBO from a (Vertex_uv_n *) heightmap
Returned LG_Terrain must be freed when done
heightmap | A (float *) heightmap |
width | Width of the heightmap grid (num of units along one row), must be < HEIGHTMAP_MAX_W |
height | Height of the heightmap grid (num of units along one column), must be < HEIGHTMAP_MAX_H |
norm_max_height | Max height (normalized, should be in range [0.0, 1.0]) |
tex | A LG_Texture, may be NULL |
float lg_terrain_get_elevation | ( | LG_Terrain * | terrain, |
float | x, | ||
float | y, | ||
uint16_t | w, | ||
uint16_t | h | ||
) |
Get the terrain elevation at x, y
x and y must be in range [-1.0, 1.0]
w and h are the width and height of the heightmap grid used to generate the terrain
x | |
y | |
width | Width of the heightmap grid (num of units along one row), must be < HEIGHTMAP_MAX_W |
height | Height of the heightmap grid (num of units along one column), must be < HEIGHTMAP_MAX_H |
void lg_terrain_free | ( | LG_Terrain * | terrain | ) |
Free LG_Terrain's vbo_data, ibo_data, heightmap, and instance
terrain | A LG_Terrain instance |
float* lg_heightmap_generate | ( | uint16_t | width, |
uint16_t | height, | ||
int | noise_type, | ||
int | seed, | ||
float | frequency | ||
) |
Generate a (non-centered) width x height heightmap (2D float array) filled with noise values in range [0.0, 1.0]
If noise_type == PERLIN_NOISE
As we use the excellent FastNoiseLite lib, you can/should check out https://github.com/Auburn/FastNoiseLite/wiki/Documentation
If noise_type == RANDOM_NOISE, seed and frequency are irrelevant
Returned array must be freed when done
width | Grid width (num of units along one row), must be < HEIGHTMAP_MAX_W |
height | Grid height (num of units along one column), must be < HEIGHTMAP_MAX_H |
noise_type | Noise type (see lg_noise_type enum in lg_terrain.h) |
seed | Seed, default = 1337 |
frequency | Frequency, default = 0.01 |
void lg_heightmaps_add | ( | float * | heightmap1, |
float * | heightmap2, | ||
uint16_t | w, | ||
uint16_t | h, | ||
float | k | ||
) |
Add height values of heightmap2 to height values of heightmpap1, then multiply by k
Quite useful to experiment and create more "stunning" heightmaps
Heightmaps must have same dimensions
heightmap1 | |
heightmap2 | |
w | Width of both heightmaps |
h | Height of both heightmaps |
k |
void lg_heightmap_flatten_border | ( | float * | heightmap, |
uint16_t | w, | ||
uint16_t | h, | ||
uint16_t | k, | ||
int | border | ||
) |
'Flatten' heightmap border(s) - useful to join 2 heighmaps
Linear flattening so far, should be Gaussian
border = NORTH_BORDER | SOUTH_BORDER | WEST_BORDER | EAST_BORDER mask
heightmap | |
w | Width |
h | Height |
k | horiz_margin = w / k, vert_margin = h / k, should be in range [2, 6] |
border | Mask of which border(s) should be flattened |
void lg_heightmap_apply_func | ( | float * | heightmap, |
uint16_t | w, | ||
uint16_t | h, | ||
float(*)(uint16_t, uint16_t, float) | func | ||
) |
Apply a user-defined func to a heightmap, doing: heightmap[x + y * w] = func(x, y, heightmap[x + y * w])
Quite useful for elevation redistribution
For example, you can get more valley-like Perlin noise with:
heightmap | |
w | Width |
h | Height |
func | Func pointer |
LG_Mesh* lg_terrain_to_mesh | ( | LG_Terrain * | terrain | ) |
Create a new LG_Mesh instance from a LG_Terrain instance
lg_terrain_new() creates a horizontally-centered, normalized LG_Terrain VBO and IBO so the mesh will also be horizontally-centered and normalized
You can free the LG_Terrain instance afterwards, as new vbo_data and ibo_data are dynamically generated (with malloc3())
terrain | Pointer to LG_Terrain instance |
Vertex_rgba* lg_horiz_grid | ( | uint16_t | grid_width, |
int * | n_vertices, | ||
float | scaling, | ||
LG_Color_u | c | ||
) |
Horiz centered square grid
Draw with glDrawArrays(GL_LINES, 0, n_vertices)
Must be freed when done
Example use:
grid_width | Grid width, ie number of units along one row or one column, should be even - if odd, we substract one |
n_vertices | Pointer to int with num of array elements |
scaling | Scaling k |
c | Color |
const Vertex* lg_horiz_grid0 | ( | ) |
DEPRECATED
Horiz centered 8 x 8 grid
Vertices = x, y, z
const unsigned short* lg_horiz_grid0_indices | ( | ) |
DEPRECATED
size_t lg_sizeof_horiz_grid0 | ( | ) |
DEPRECATED
size_t lg_sizeof_horiz_grid0_indices | ( | ) |
DEPRECATED