summaryrefslogtreecommitdiff
path: root/game/vehicles/railVehicleClass.cpp
blob: 34c13592634af441795d27a292506bcd0c0b8ce9 (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
#include "railVehicleClass.h"
#include "gfx/gl/sceneShader.h"
#include "gfx/gl/shadowMapper.h"
#include "gfx/gl/vertexArrayObject.h"
#include <array>
#include <glm/glm.hpp>
#include <lib/resource.h>
#include <location.h>
#include <maths.h>
#include <memory>

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<LocationVertex, &LocationVertex::body, &LocationVertex::bodyPos>(instances.bufferName(), 1);
	bogies.front()
			->configureVAO(instancesBogiesVAO.front())
			.addAttribs<LocationVertex, &LocationVertex::front, &LocationVertex::frontPos>(instances.bufferName(), 1);
	bogies.back()
			->configureVAO(instancesBogiesVAO.back())
			.addAttribs<LocationVertex, &LocationVertex::back, &LocationVertex::backPos>(instances.bufferName(), 1);
	static_assert(sizeof(LocationVertex) == 144UL);
}

void
RailVehicleClass::render(const SceneShader & shader) const
{
	if (const auto count = static_cast<GLsizei>(instances.size())) {
		if (texture) {
			texture->bind();
		}
		shader.basicInst.use();
		bodyMesh->DrawInstanced(instanceVAO, count);
		bogies.front()->DrawInstanced(instancesBogiesVAO.front(), count);
		bogies.back()->DrawInstanced(instancesBogiesVAO.back(), count);
	}
}

void
RailVehicleClass::shadows(const ShadowMapper & mapper) const
{
	if (const auto count = static_cast<GLsizei>(instances.size())) {
		mapper.dynamicPointInst.use();
		bodyMesh->DrawInstanced(instanceVAO, count);
		bogies.front()->DrawInstanced(instancesBogiesVAO.front(), count);
		bogies.back()->DrawInstanced(instancesBogiesVAO.back(), count);
	}
}