summaryrefslogtreecommitdiff
path: root/gfx/gl/shadowMapper.h
blob: 3aa224e1d804621274aa0ae9873e3feaf4d3549c (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
73
74
75
76
77
78
79
80
#pragma once

#include "config/types.h"
#include "gfx/gl/shadowStenciller.h"
#include "lib/glArrays.h"
#include "program.h"
#include <gfx/models/texture.h>
#include <glm/vec2.hpp>
#include <span>
#include <vector>

class SceneProvider;
class Camera;

class ShadowMapper {
public:
	explicit ShadowMapper(const TextureAbsCoord & size);

	static constexpr std::size_t SHADOW_BANDS {4};

	using Definitions = std::vector<glm::mat4x4>;
	using Sizes = std::vector<RelativePosition3D>;

	[[nodiscard]] Definitions update(const SceneProvider &, const Direction3D & direction, const Camera &) const;

	class ShadowProgram : public Program {
	public:
		explicit ShadowProgram(const Shader & vs);
		explicit ShadowProgram(const Shader & vs, const Shader & gs, const Shader & fs);

		void setView(const std::span<const glm::mat4x4>, const std::span<const RelativePosition3D>,
				const GlobalPosition3D) const;
		void use() const;

	private:
		RequiredUniformLocation viewProjectionLoc {*this, "viewProjection"};
		RequiredUniformLocation viewProjectionsLoc {*this, "viewProjections"};
		UniformLocation sizesLoc {*this, "sizes"};
		RequiredUniformLocation viewPointLoc {*this, "viewPoint"};
	};

	class DynamicPoint : public ShadowProgram {
	public:
		DynamicPoint();
		void use(const Location &) const;
		void setModel(const Location &) const;

	private:
		RequiredUniformLocation modelLoc {*this, "model"};
		RequiredUniformLocation modelPosLoc {*this, "modelPos"};
	};

	class StencilShadowProgram : public ShadowProgram {
	public:
		StencilShadowProgram();
		void use(const RelativePosition3D & centre, const float size) const;

	private:
		RequiredUniformLocation centreLoc {*this, "centre"};
		RequiredUniformLocation sizeLoc {*this, "size"};
	};

	ShadowProgram landmess, dynamicPointInst, dynamicPointInstWithTextures;
	DynamicPoint dynamicPoint;
	StencilShadowProgram stencilShadowProgram;

	// NOLINTNEXTLINE(hicpp-explicit-conversions)
	operator GLuint() const
	{
		return depthMap;
	}

private:
	[[nodiscard]] static std::vector<std::array<RelativePosition3D, 4>> getBandViewExtents(
			const Camera &, const glm::mat4 & lightView);
	glFrameBuffer depthMapFBO;
	glTexture depthMap;
	TextureAbsCoord size;
	mutable ShadowStenciller shadowStenciller;
};