summaryrefslogtreecommitdiff
path: root/game/scenary/foliage.cpp
blob: 49a4831e91b5cd39fb00ee7161fcbbad284016ab (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "foliage.h"
#include "gfx/frustum.h"
#include "gfx/gl/billboardPainter.h"
#include "gfx/gl/sceneShader.h"
#include "gfx/gl/shadowMapper.h"
#include "gfx/gl/shadowStenciller.h"
#include <location.h>

static_assert(std::is_constructible_v<Foliage>);
constexpr float OBJECT_BILLBOARD_DIVISOR = 64;
constexpr float BILLBOARD_ANGLE_TOLERANCE = 250.F; // Radians per mm size
constexpr float ASSUMED_VIEWPORT = 1440;
constexpr float OVER_SAMPLE_MULTIPLIER = 2; // Use mesh until billboard 1/2 of rendered size

namespace {
	GLsizei
	billboardTextureSizeForObject(RelativeDistance objectSize)
	{
		return static_cast<GLsizei>(std::pow(2, std::ceil(std::log2(objectSize / OBJECT_BILLBOARD_DIVISOR))));
	}
}

std::any
Foliage::createAt(const Location & position) const
{
	return std::make_shared<InstanceVertices<LocationVertex>::InstanceProxy>(
			instances.acquire(position.getRotationTransform(), position.rot.y, position.pos));
}

bool
Foliage::persist(Persistence::PersistenceStore & store)
{
	return STORE_TYPE && STORE_HELPER(bodyMesh, Asset::MeshConstruct) && Asset::persist(store);
}

void
Foliage::postLoad()
{
	texture = getTexture();
	bodyMesh->configureVAO(instanceVAO, 0)
			.addAttribs<LocationVertex, &LocationVertex::rotation, &LocationVertex::position>(
					1, instances.bufferName());
	instancePointVAO.configure().addAttribs<LocationVertex, &LocationVertex::position, &LocationVertex::yaw>(
			0, instances.bufferName());

	const auto & size = bodyMesh->getDimensions().size;
	billboardSize = billboardTextureSizeForObject(size);
	shadowStencil = ShadowStenciller::createStencilTexture(billboardSize, billboardSize);
	billboard = BillboardPainter::createBillBoardTextures(billboardSize, billboardSize);
	useMeshClipDist = (ASSUMED_VIEWPORT * OVER_SAMPLE_MULTIPLIER * size) / static_cast<RelativeDistance>(billboardSize);
}

void
Foliage::updateStencil(const ShadowStenciller & shadowStenciller) const
{
	if (instancePartitions.second.second != instancePartitions.second.first
			&& glm::distance(shadowStenciller.getLightDirection(), shadowStencilDir)
					> BILLBOARD_ANGLE_TOLERANCE / bodyMesh->getDimensions().size) {
		shadowStenciller.renderStencil(shadowStencil, *bodyMesh, texture);
	}
}

void
Foliage::updateBillboard(const BillboardPainter & bbp) const
{
	if (instancePartitions.first != instancePartitions.second.first
			&& std::abs(bbp.getAngle() - billboardAngle) > BILLBOARD_ANGLE_TOLERANCE / bodyMesh->getDimensions().size) {
		bbp.renderBillBoard(billboard, *bodyMesh, texture);
		billboardAngle = bbp.getAngle();
	}
}

void
Foliage::preFrame(const Frustum & frustum, const Frustum & lighting)
{
	if (instances.size() > 0) {
		const auto & dims = bodyMesh->getDimensions();
		instancePartitions = instances.partition(
				[&frustum, &dims](const auto & location) {
					return frustum.contains(location.position + dims.centre, dims.size);
				},
				[&frustum, this](const auto & location) {
					return distance(frustum.getPosition(), location.position) < useMeshClipDist;
				},
				[&lighting, &dims](const auto & location) {
					return lighting.contains(location.position + dims.centre, dims.size);
				});
		//         In view frustum       /            Outside view frustum            /
		// Close to view / Far from view / Casts shadow into view / No shadow in view /
	}
}

void
Foliage::render(const SceneShader & shader, const Frustum &) const
{
	if (instancePartitions.first) {
		glDebugScope _ {instancePointVAO};
		std::ignore = instances.size();
		if (const auto count = instancePartitions.first - instancePartitions.second.first) {
			glDebugScope _ {0, "Billboard"};
			const auto dimensions = bodyMesh->getDimensions();
			shader.billboard.use(dimensions.size, dimensions.centre);
			billboard[0].bind(GL_TEXTURE_2D_ARRAY, GL_TEXTURE0);
			billboard[1].bind(GL_TEXTURE_2D_ARRAY, GL_TEXTURE1);
			billboard[2].bind(GL_TEXTURE_2D_ARRAY, GL_TEXTURE2);
			glBindVertexArray(instancePointVAO);
			glDrawArrays(GL_POINTS, static_cast<GLint>(instancePartitions.second.first), static_cast<GLsizei>(count));
			glBindVertexArray(0);
		}
		if (const auto count = instancePartitions.second.first) {
			glDebugScope _ {0, "Mesh"};
			shader.basicInst.use();
			if (texture) {
				texture->bind();
			}
			bodyMesh->DrawInstanced(instanceVAO, static_cast<GLsizei>(count));
		}
	}
}

void
Foliage::shadows(const ShadowMapper & mapper, const Frustum &) const
{
	if (instancePartitions.second.second) {
		glDebugScope _ {instancePointVAO};
		std::ignore = instances.size();
		if (const auto count = instancePartitions.second.second - instancePartitions.second.first) {
			const auto dimensions = bodyMesh->getDimensions();
			mapper.stencilShadowProgram.use(dimensions.centre, dimensions.size);
			shadowStencil.bind(GL_TEXTURE_2D_ARRAY, GL_TEXTURE0);
			glBindVertexArray(instancePointVAO);
			glDrawArrays(GL_POINTS, static_cast<GLint>(instancePartitions.second.first), static_cast<GLsizei>(count));
			glBindVertexArray(0);
		}
		if (const auto count = instancePartitions.second.first) {
			if (texture) {
				texture->bind(GL_TEXTURE3);
				mapper.dynamicPointInstWithTextures.use();
			}
			else {
				mapper.dynamicPointInst.use();
			}
			bodyMesh->DrawInstanced(instanceVAO, static_cast<GLsizei>(count));
		}
	}
}