-
Notifications
You must be signed in to change notification settings - Fork 1
/
rdl.cpp
245 lines (210 loc) · 6.88 KB
/
rdl.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//===----------------------------------------------------------------------===//
//
// The Descent map loader
//
// NAME : Rdl
// PURPOSE : Providing a decoder and wrapper for the Descent .RDL format.
// COPYRIGHT : (c) 2011 Sean Donnellan. All Rights Reserved.
// AUTHORS : Sean Donnellan ([email protected])
// DESCRIPTION : A decoder for the RDL file format that is used by Parallax
// Software in the computer game, Descent.
//
// The file format is as follows:
//
// - Magic number which is 4 bytes and corresponding to the
// string "LVLP"
//
// - TODO: Document the rest of the format.
//
//===----------------------------------------------------------------------===//
#include "rdl.hpp"
#include "arrayreader.hpp"
#include "cube.hpp"
#include <assert.h>
#include <string.h>
#include <stdio.h>
// TODO: Refactor this out.
inline double fixedToFloating(int32_t value)
{
return value / 65536.0;
}
inline double fixedToFloating(int16_t value)
{
return value / 4096.0;
}
inline void printBitmask(uint8_t bitmask)
{
for (uint8_t j = 0; j<8; ++j, bitmask = bitmask>> 1)
{
putchar((bitmask & 1) ? '1' : '0');
}
putchar('\n');
}
// The 4-byte MAGIC number at the start of the file format used to identifiy the
// file as being a Descent HOG file.
static uint8_t magicRdl[4] = { 'L', 'V', 'L', 'P' };
struct RdlHeader
{
uint8_t signature[4];
uint32_t version;
uint32_t mineDataOffset;
uint32_t objectsOffset;
uint32_t fileSize;
};
struct RdlTexture
{
uint16_t primaryTextureNumber;
// This isn't present if the high bit of primaryTextureNumber is a 0.
uint16_t secondaryTextureNumber;
};
// struct RdlMineData
//{
// uint16_t vertexCount;
// uint16_t cubeCount;
// Vertex verticies[vertexCount]; // Vertex = { int32_t, int32_t, int32_t};
// Cube cubes[cubeCount];
//};
#ifdef _MSC_VER
static_assert(sizeof(RdlHeader) == 20,
"The RdlHeader structure is incorrectly packed");
#endif
RdlReader::RdlReader(const std::vector<uint8_t>& Data)
: myData(Data),
myHeader(reinterpret_cast<const struct RdlHeader* const>(&Data.front()))
{
// printf("Version: %d\n", myHeader->version);
// printf("Mine data offset: %d\n", myHeader->mineDataOffset);
// printf("Object offset: %d\n", myHeader->objectsOffset);
}
bool RdlReader::IsValid() const
{
if (myData.size() < sizeof(magicRdl)) return false;
if (memcmp(magicRdl, &myData.front(), sizeof(magicRdl)) != 0) return false;
return myHeader->fileSize == myData.size();
}
std::vector<Vertex> RdlReader::Vertices() const
{
// First step, determine how many vertices there are.
size_t index = myHeader->mineDataOffset + 1 /* version byte */;
const uint16_t vertexCount = (myData[index + 1] << 8) + myData[index + 0];
std::vector<Vertex> vertices(vertexCount);
index += 4; // For reading the vertex count and skipping the cube count.
int32_t buffer[3];
for (int i = 0; i < vertexCount; index += 12, ++i)
{
// 32-bit fixed point number, in 16:16 format
memcpy(&buffer, &myData[index], 3 * sizeof(int32_t));
vertices[i].x = fixedToFloating(buffer[0]);
vertices[i].y = fixedToFloating(buffer[1]);
vertices[i].z = fixedToFloating(buffer[2]);
}
assert(index == CubeOffset());
return vertices;
}
std::vector<Cube> RdlReader::Cubes() const
{
ArrayReader reader(myData.data(), myData.size());
reader.Seek(myHeader->mineDataOffset + 1 /* version */);
// First step, determine how many vertices and cubes there are.
const uint16_t vertexCount = reader.ReadUInt16();
const uint16_t cubeCount = reader.ReadUInt16();
// Skip over the vertex data and go to the cubes.
reader.Seek(CubeOffset());
std::vector<Cube> cubes(cubeCount);
for (int i = 0; i < cubeCount; ++i)
{
Cube& cube = cubes[i];
const uint8_t neighbourBitmask = reader.ReadByte();
const bool isEnergyCenter = (neighbourBitmask & (1 << 6)) != 0;
// Read neighbour information.
for (uint8_t j = 0; j < 6; ++j)
{
if (neighbourBitmask & (1 << j))
{
cube.neighbors[j] = reader.ReadInt16();
}
else
{
cube.neighbors[j] = -1;
}
}
// Read the indices of the eight vertices that make up this cube.
for (uint8_t j = 0; j < 8; ++j)
{
cube.vertices[j] = reader.ReadUInt16();
assert(cube.vertices[j] < vertexCount);
}
// Optionally there are four-bytes that define the energy centre.
// This probably needs to be given a better name.
if (isEnergyCenter)
{
struct EnergyCenter
{
uint8_t special;
int8_t energyCenterNumber;
int16_t value;
};
// TODO: Store this information in the Cube.
EnergyCenter energyCentre;
energyCentre.special = reader.ReadByte();
energyCentre.energyCenterNumber = reader.ReadByte();
energyCentre.value = reader.ReadInt16();
}
const int16_t rawLighting = reader.ReadInt16();
cube.lighting = rawLighting / (24 * 327.68);
// Wall bit masks where a 1 means it is a wall or door.
const uint8_t wallMask = reader.ReadByte();
for (uint8_t wallIndex = 0; wallIndex < 6; ++wallIndex)
{
if (wallMask & (1 << wallIndex))
{
cube.walls[wallIndex] = reader.ReadByte();
}
else
{
cube.walls[wallIndex] = 255;
}
}
// Read texturing information for the sides.
for (size_t j = 0, count = 6; j < count; ++j)
{
const bool hasTexture =
(cube.neighbors[j] == -1) || (cube.walls[j] != 255);
if (!hasTexture)
{
cube.textures[j].primaryTextureNumber = 0;
cube.textures[j].secondaryTextureNumber = 0;
continue;
}
cube.textures[j].primaryTextureNumber = reader.ReadUInt16();
if ((cube.textures[j].primaryTextureNumber >> 15) & 1)
{
cube.textures[j].secondaryTextureNumber = reader.ReadUInt16();
// printf("Side: %d: Texture: %d and %d\n", j + 1,
// cube.textures[j].primaryTextureNumber & ~(1 << 15),
// cube.textures[j].secondaryTextureNumber & 0xFFF);
}
else
{
// printf("Side: %d: Texture: %d\n", j + 1,
// cube.textures[j].primaryTextureNumber);
}
for (int k = 0; k < 4; k++)
{
// UVLs (3 numbers), each is 16-bits (so 2*3 bytes).
const int16_t u = reader.ReadInt16();
const int16_t v = reader.ReadInt16();
const uint16_t l = reader.ReadUInt16();
}
}
}
return cubes;
}
size_t RdlReader::CubeOffset() const
{
const size_t index = myHeader->mineDataOffset + 1 /* version */;
const uint16_t vertexCount = (myData[index + 1] << 8) + myData[index + 0];
// The 1 is the size of the version number, the 4 is for the four bytes that
// are the vertex and cube counts then lastly we skip over all the vertices.
return myHeader->mineDataOffset + 1 + 4 + 12 * vertexCount;
}