summaryrefslogtreecommitdiff
path: root/src/chunk.h
blob: 12fa111142426e8dde4125f34e6a082254381bd8 (plain) (blame)
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
73
74
75
76
#pragma once
#include "block.h"
#include "cglm/types.h"
#include "shader.h"
#include "texture.h"
#include "world.h"
#include <stdint.h>
#include <junk/vector.h>
#define CHUNK_WIDTH 16
#define CHUNK_LENGTH 16
#define CHUNK_HEIGHT 128

enum biome {
    JUNK_BIOME_PLAINS,
};


//NOTE: Forward declare world here. There is a circular dependency between 
// chunk and world, and the order in which compiler sees them causes issues.
// World.c compiles -> includes world.h -> first line is include chunk.h -> struct world
// is used here, which messes it up. Forward declare to avoid errors
struct world;
/**
 * A chunk is the "basic" rendering unit used - it will allocate buffers for all blocks in a chunk, hide covered faces of blocks, generate a chunk mesh and dispatch that buffer data to the GPU
 *
 */
struct chunk {
    struct block* blocks[CHUNK_WIDTH][CHUNK_LENGTH][CHUNK_HEIGHT];
    GLuint _vao;
    GLuint _vbo;
    GLuint _ebo;
    int vertex_count;
    mat4 model;
    enum biome biome;
    vec2 coord;
    int loaded;
    int dirty;
};

/**
 * Generate a chunk at coords for the given world. Memory allocation for chunk is
 * handled by the function.
 *
 */
int chunk_gen(struct world* wld, vec2 coord, struct chunk** chunk);
/**
 * Load a chunk to the given coordinates. Essentially, a chunk only knows of
 * it's local coordinate system. We want to load this particular chunk to a
 * location in WORLD coordinates, which is what coord is. This vec2 will be
 * used to translate the blocks that constitute the chunk
 *
 * The chunk will allocate a VAO/VBO/EBO buffer to render the chunk mesh. This GPU data is usually not updated in the loop, unless a chunk_update is called
 * @param chunk Chunk to load
 * @param coord coordinates in world space
 */
void chunk_load(struct chunk* chunk, int coord[2]);
/**
 * Chunk updates are performed on already loaded chunks. It will redraw the
 * chunk mesh as needed based on block updates and whatnot.
 * @param chunk Chunk to load
 * @param coord coordinates in world space
 */
void chunk_update(struct chunk* chunk);
/**
 * Unload a chunk. Delete GPU and memory data, not the chunk data itself
 *
 * @param chunk Chunk to load
 */
void chunk_unload(struct chunk* chunk);
/*
 * This dispatches calls to OpenGL to draw the chunk.
 * @param chunk Chunk to draw
 * @param shader Shader to pass to block_draw
 * @param texture Textures that block_draw will use
 */
void chunk_draw(struct chunk* chunk, struct shader* shader, struct texture* texture);