summaryrefslogtreecommitdiff
path: root/themes/src/summer/water_model.h
diff options
context:
space:
mode:
Diffstat (limited to 'themes/src/summer/water_model.h')
-rw-r--r--themes/src/summer/water_model.h70
1 files changed, 70 insertions, 0 deletions
diff --git a/themes/src/summer/water_model.h b/themes/src/summer/water_model.h
new file mode 100644
index 0000000..a99f91d
--- /dev/null
+++ b/themes/src/summer/water_model.h
@@ -0,0 +1,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