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 vert_scaling_k, 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 vert_scaling_k, LG_Texture *tex) |
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) |
LG_Mesh * | lg_terrain_to_mesh (LG_Terrain *terrain) |
Vertex_rgba * | lg_horiz_grid (int 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:
LG_Terrain* lg_terrain_new | ( | uint16_t | width, |
uint16_t | height, | ||
float | vert_scaling_k, | ||
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
If noise_type == RANDOM_NOISE, seed and frequency are irrelevant
Height is ambiguous here. So:
Returned LG_Terrain must be freed afterwards
width | Width of the heightmap grid, must be < HEIGHTMAP_MAX_W |
height | Height of the heightmap grid, must be < HEIGHTMAP_MAX_H |
vert_scaling_k | Heightmap vertical scaling k, should be in range [0.0, 1.0] - actually clamped to 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 | vert_scaling_k, | ||
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 afterwards
heightmap | A (Vertex_uv_n *) heightmap |
width | Width of the heightmap grid, must be < HEIGHTMAP_MAX_W |
height | Height of the heightmap grid, must be < HEIGHTMAP_MAX_H |
vert_scaling_k | Heightmap vertical scaling k, should be in range [0.0, 1.0] - actually clamped to 1.0 |
tex | A LG_Texture, may be NULL |
void lg_terrain_free | ( | LG_Terrain * | terrain | ) |
Free LG_Terrain's vbo_data, ibo_data, 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
If noise_type == RANDOM_NOISE, seed and frequency are irrelevant
TODO: add octave param
Returned array must be freed afterwards
width | Grid width, must be < HEIGHTMAP_MAX_W |
height | Grid height, 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
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 |
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 | ( | int | 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 afterwards
Example use:
grid_width | If grid_width is 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