summaryrefslogtreecommitdiff
path: root/src/systems/terrain.h
blob: 5a9613247e58d39d4a301610b0dceae7a887be08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * @file terrain.h
 * @brief
 */

#pragma once

/*
Future:
 - Chunked terrain
 - Dynamic LOD
*/

#include "defines.h"
#include "maths_types.h"
#include "ral_types.h"
#include "render.h"
#include "str.h"

typedef struct Heightmap {
  Str8 filepath;
  u32x2 pixel_dimensions;
  void* image_data;
  u32 num_channels;
  bool is_uploaded;
} Heightmap;

typedef struct Terrain_Storage {
  // arena terrain_allocator;
  u32x2 grid_dimensions;
  f32 grid_scale;
  u32 num_vertices;
  Heightmap heightmap;  // NULL = no heightmap
  GPU_Renderpass* hmap_renderpass;
  GPU_Pipeline* hmap_pipeline;
  TextureHandle texture;

  bool hmap_loaded;
  BufferHandle vertex_buffer;
  BufferHandle index_buffer;
  u32 indices_count;
} Terrain_Storage;

// --- Public API
PUB bool Terrain_Init(Terrain_Storage* storage);
PUB void Terrain_Shutdown(Terrain_Storage* storage);
PUB void Terrain_Draw(
    Terrain_Storage* storage);  // NOTE: For now it renders directly to main framebuffer

/** @brief Sets the active heightmap to be rendered and collided against. */
PUB void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, f32 grid_scale,
                               bool free_on_upload);
PUB Heightmap Heightmap_FromImage(Str8 filepath);
PUB Heightmap Heightmap_FromPerlin(/* TODO: perlin noise generation parameters */);

PUB bool Terrain_IsActive();  // checks whether we have a loaded heightmap and it's being rendered

/** @brief Get the height (the Y component) for a vertex at a particular coordinate in the heightmap
 */
PUB f32 Heightmap_HeightXZ(const Heightmap* hmap, u32 x, u32 z);

/** @brief Calculate the normal vector of a vertex at a particular coordinate in the heightmap */
PUB Vec3 Heightmap_NormalXZ(const Heightmap* hmap, f32 x, f32 z);

// /** @brief Generate the `geometry_data` for a heightmap ready to be uploaded to the GPU */
// Geometry geo_heightmap(arena* a, Heightmap heightmap);

typedef struct TerrainUniforms {
  TextureHandle tex_slot_1;
} TerrainUniforms;

ShaderDataLayout TerrainUniforms_GetLayout(void* data);