summaryrefslogtreecommitdiff
path: root/themes/src/summer/water_model.h
blob: a99f91db017cb1e875847e29793b3c55a3e70ec4 (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
#ifndef WATER_MODEL_H
#define WATER_MODEL_H

#include "../mathlib.h"
#include "../shader.h"
#include "../types.h"

struct WebglContext;

/// A water mesh is a 3D plane in the simulation world.
///
/// The mesh reflects the sun above it and refracts whatever is
/// below its surface. It owns its own shader, geometry buffers,
/// and perspective camera, so it is fully standalone.
///
/// The mesh can be rendered and updated.
class WaterModel {
public:
    WaterModel(Vector3 position, Vector3 size);
    ~WaterModel();

    /// Compile the water shader, build the grid mesh, and set up the camera.
    void load(WebglContext* context);
    void update(f32 dtSeconds);
    void render();
    void unload();

    /// Direction pointing toward the sun (normalized internally) and its color,
    /// used to fake the surface reflection.
    void setSun(Vector3 direction, Vector4 color);

private:
    Vector3 position;
    Vector3 size;

    WebglContext* context = nullptr;
    Shader shader = 0;
    u32 vao = 0;
    u32 vbo = 0;
    u32 ebo = 0;
    i32 numIndices = 0;
    f32 elapsedTime = 0.f;

    Mat4x4 projection;
    Mat4x4 view;
    Mat4x4 model;

    Vector3 cameraPos;
    Vector3 sunDir;
    Vector3 sunColor;

    static const i32 RES = 200;

    struct {
        i32 position = -1;
    } attributes;

    struct {
        i32 projection = -1;
        i32 view = -1;
        i32 model = -1;
        i32 time = -1;
        i32 amplitude = -1;
        i32 cameraPos = -1;
        i32 sunDir = -1;
        i32 sunColor = -1;
    } uniforms;
};

#endif // WATER_MODEL_H