summaryrefslogtreecommitdiff
path: root/game/terrain.h
blob: eaec01d77ff2a42420007ed513df0cab2f2dd107 (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
#pragma once

#include "chronology.h"
#include "config/types.h"
#include "game/worldobject.h"
#include "geoData.h"
#include "gfx/models/texture.h"
#include "gfx/renderable.h"

class SceneShader;

class Terrain : public GeoData, public WorldObject, public Renderable {
public:
	template<typename... P> explicit Terrain(P &&... params) : GeoData {std::forward<P>(params)...}
	{
		generateMeshes();
	}

	void render(const SceneShader & shader, const Frustum &) const override;
	void shadows(const ShadowMapper &, const Frustum &) const override;

	void tick(TickDuration) override;

	struct Vertex {
		GlobalPosition3D pos;
		Normal3D normal;
	};

	void generateMeshes();

private:
	void afterChange() override;

	struct SurfaceArrayBuffer {
		glVertexArray vertexArray;
		glBuffer indicesBuffer;
		GLsizei count;
		AxisAlignedBoundingBox<GlobalDistance> aabb;
	};

	struct SurfaceKey {
		const Surface * surface;
		GlobalPosition2D basePosition;
		bool operator<(const SurfaceKey &) const;
	};

	glBuffer verticesBuffer;
	std::map<SurfaceKey, SurfaceArrayBuffer> meshes;
	Texture::Ptr grass = std::make_shared<Texture>("grass.png");
};