summaryrefslogtreecommitdiff
path: root/game/vehicles/railVehicleClass.cpp
blob: 66d7ba3e86a818ea1251edd0fe57f8d17301a848 (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
#include "railVehicleClass.h"
#include "gfx/gl/sceneShader.h"
#include "gfx/gl/shadowMapper.h"
#include "gfx/gl/vertexArrayObject.hpp"
#include "gfx/models/mesh.h"
#include "gfx/models/texture.h"
#include <algorithm>
#include <array>
#include <cache.h>
#include <cmath>
#include <cstddef>
#include <filesystem>
#include <glm/glm.hpp>
#include <iterator>
#include <lib/resource.h>
#include <location.hpp>
#include <map>
#include <maths.h>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

bool
RailVehicleClass::persist(Persistence::PersistenceStore & store)
{
	return STORE_TYPE && STORE_MEMBER(length) && STORE_MEMBER(wheelBase) && STORE_MEMBER(maxSpeed)
			&& STORE_NAME_HELPER("bogie", bogies, Asset::MeshArrayConstruct)
			&& STORE_HELPER(bodyMesh, Asset::MeshConstruct) && Asset::persist(store);
}

void
RailVehicleClass::postLoad()
{
	texture = getTexture();
	bodyMesh->configureVAO(instanceVAO).addAttribs<glm::mat4>(instancesBody.bufferName(), 1);
	bogies.front()
			->configureVAO(instancesBogiesVAO.front())
			.addAttribs<glm::mat4>(instancesBogies.front().bufferName(), 1);
	bogies.back()
			->configureVAO(instancesBogiesVAO.back())
			.addAttribs<glm::mat4>(instancesBogies.back().bufferName(), 1);
}

void
RailVehicleClass::render(const SceneShader & shader) const
{
	if (const auto count = instancesBody.count()) {
		if (texture) {
			texture->bind();
		}
		shader.basicInst.use();
		glBindVertexArray(instanceVAO);
		glDrawElementsInstanced(
				bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast<GLsizei>(count));
		glBindVertexArray(instancesBogiesVAO.front());
		glDrawElementsInstanced(bogies.front()->type(), bogies.front()->count(), GL_UNSIGNED_INT, nullptr,
				static_cast<GLsizei>(instancesBogies.front().count()));
		glBindVertexArray(instancesBogiesVAO.back());
		glDrawElementsInstanced(bogies.back()->type(), bogies.back()->count(), GL_UNSIGNED_INT, nullptr,
				static_cast<GLsizei>(instancesBogies.back().count()));
		glBindVertexArray(0);
	}
}

void
RailVehicleClass::shadows(const ShadowMapper & mapper) const
{
	if (const auto count = instancesBody.count()) {
		mapper.dynamicPointInst.use();
		glBindVertexArray(instanceVAO);
		glDrawElementsInstanced(
				bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast<GLsizei>(count));
		glBindVertexArray(instancesBogiesVAO.front());
		glDrawElementsInstanced(bogies.front()->type(), bogies.front()->count(), GL_UNSIGNED_INT, nullptr,
				static_cast<GLsizei>(instancesBogies.front().count()));
		glBindVertexArray(instancesBogiesVAO.back());
		glDrawElementsInstanced(bogies.back()->type(), bogies.back()->count(), GL_UNSIGNED_INT, nullptr,
				static_cast<GLsizei>(instancesBogies.back().count()));
		glBindVertexArray(0);
	}
}