/
gloomworldrenderer.cpp
195 lines (167 loc) · 5.78 KB
/
gloomworldrenderer.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/** @file gloomworldrenderer.cpp
*
* @authors Copyright (c) 2020 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "render/gloomworldrenderer.h"
#include "render/rendersystem.h"
#include "render/rend_main.h"
#include "world/p_players.h"
#include "clientapp.h"
#include <doomsday/world/map.h>
#include <doomsday/world/sector.h>
#include <doomsday/world/world.h>
#include <gloom/world.h>
#include <gloom/world/map.h>
#include <gloom/render/icamera.h>
#include <gloom/render/maprender.h>
#include <de/ImageBank>
#include <de/FS>
#include <de/data/json.h>
#include <de/legacy/timer.h>
using namespace de;
DE_PIMPL(GloomWorldRenderer)
, DE_OBSERVES(world::World, PlaneMovement)
{
/**
* Gloom camera for rendering a specific player's view.
*/
struct PlayerCamera : public gloom::ICamera
{
Vec3f pos, up, front;
Mat4f mvMat, projMat;
void update(int console, const Vec3f &metersPerUnit)
{
player_t *player = DD_Player(console);
viewdata_t *vd = &player->viewport();
pos = vd->current.origin.xzy() * metersPerUnit;
up = vd->upVec;
front = vd->frontVec;
mvMat = Mat4f::scale(metersPerUnit) * Rend_GetModelViewMatrix(console) *
Mat4f::scale(Vec3f(1.0f) / metersPerUnit) *
Mat4f::scale(Vec3f(1, 1, -1));
projMat = Rend_GetProjectionMatrix(0.0f, metersPerUnit.x /* clip planes in meters */);
}
Vec3f cameraPosition() const
{
return pos;
}
Vec3f cameraFront() const
{
return front;
}
Vec3f cameraUp() const
{
return up;
}
Mat4f cameraProjection() const
{
return projMat;
}
Mat4f cameraModelView() const
{
return mvMat;
}
};
PlayerCamera playerCamera;
std::unique_ptr<gloom::World> glWorld;
Set<const world::Plane *> planesToUpdate;
List<gloom::ID> sectorLut; // sector number => gloom ID
ImageBank images;
Impl(Public *i) : Base(i)
{
images.add("sky.morning", "/home/sky-morning.jpg");
}
void planeMovementBegan(const world::Plane &plane) override
{
planesToUpdate.insert(&plane);
}
};
GloomWorldRenderer::GloomWorldRenderer()
: d(new Impl(this))
{
world::World::get().audienceForPlaneMovement() += d;
}
void GloomWorldRenderer::glInit()
{
d->glWorld.reset(new gloom::World(ClientApp::shaders(), d->images));
d->glWorld->glInit();
}
void GloomWorldRenderer::glDeinit()
{
d->glWorld->glDeinit();
d->glWorld.reset();
}
void GloomWorldRenderer::setCamera()
{}
void GloomWorldRenderer::loadMap(const String &mapId)
{
d->glWorld->loadMap(mapId);
// Read the lookup tables.
{
const auto & asset = App::asset("map." + mapId);
const Record lut =
parseJSON(String::fromUtf8(FS::locate<const File>(asset.absolutePath("lookupPath"))));
const auto §orIds = lut["sectorIds"].array();
d->sectorLut.resize(sectorIds.size());
auto i = d->sectorLut.begin();
for (const auto *value : sectorIds.elements())
{
*i++ = value->asUInt();
}
}
}
void GloomWorldRenderer::unloadMap()
{
d->sectorLut.clear();
}
void GloomWorldRenderer::advanceTime(TimeSpan elapsed)
{
if (d->glWorld)
{
// Update changed plane heights.
if (!d->sectorLut.isEmpty())
{
const auto &glMap = d->glWorld->map();
for (const auto &plane : d->planesToUpdate)
{
const gloom::ID sectorId = d->sectorLut[plane->sector().indexInArchive()];
const gloom::ID planeId = plane->isSectorFloor() ? glMap.floorPlaneId(sectorId)
: glMap.ceilingPlaneId(sectorId);
d->glWorld->mapRender().setPlaneY(planeId,
plane->heightTarget(),
plane->initialHeightOfMovement(),
plane->movementBeganAt(),
plane->speed() * TICSPERSEC);
// TODO: Pass the target height and speed to glWorld and let the shaders update
// the heights on the GPU. Only update the plane heights buffer when a move begins.
}
}
d->glWorld->update(elapsed);
d->glWorld->setCurrentTime(world::World::get().time());
// TODO: time sync should happen once, and after that `elapsed` should be scaled
// according to tic length.
d->planesToUpdate.clear();
}
}
void GloomWorldRenderer::renderPlayerView(int num)
{
// Gloom assumes counterclockwise front faces.
glFrontFace(GL_CCW);
d->playerCamera.update(num, d->glWorld->map().metersPerUnit().toVec3f());
d->glWorld->render(d->playerCamera);
// Classic renderer assumes clockwise.
glFrontFace(GL_CW);
}