-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsp.c
312 lines (255 loc) · 7.36 KB
/
bsp.c
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gl/gl_3.h"
#include "bsp.h"
#include "texture.h"
#include "vec.h"
#define BSP_VERSION 29
typedef struct {
int32_t offset;
int32_t size;
} BspEntry;
typedef struct {
int32_t version;
BspEntry entities;
BspEntry planes;
BspEntry miptex;
BspEntry vertices;
BspEntry visilist;
BspEntry nodes;
BspEntry texinfo;
BspEntry faces;
BspEntry lightmaps;
BspEntry clipnodes;
BspEntry leaves;
BspEntry lface;
BspEntry edges;
BspEntry ledges;
BspEntry models;
} BspHeader;
typedef struct {
char name[16];
uint32_t width;
uint32_t height;
uint32_t offset1;
uint32_t offset2;
uint32_t offset4;
uint32_t offset8;
} TextureDef;
typedef struct {
int16_t planeId;
int16_t side;
int32_t edgeId;
int16_t edgeNum;
int16_t texinfoId;
uint8_t typeLight;
uint8_t baseLight;
uint8_t light[2];
int32_t lightmap;
} FaceDef;
typedef struct {
uint16_t vert0;
uint16_t vert1;
} EdgeDef;
typedef struct {
vec3Float normal;
float dist;
int32_t type;
} PlaneDef;
typedef struct {
vec3Float vecS;
float distS;
vec3Float vecT;
float distT;
int32_t texId;
int32_t animated;
} TexInfoDef;
typedef struct {
vec3Float position;
vec2Float texCoord;
vec3Float normal;
} Vert;
typedef struct {
GLuint glId;
Texture *texture;
int numVerts;
Vert *verts;
} Face;
struct s_Bsp {
GLuint vaoId;
int32_t numTextures;
Texture **textures;
vec3Float *verts;
TexInfoDef *texInfo;
PlaneDef *planes;
int numFaces;
Face *faces;
};
static PlaneDef *getPlane(Bsp *bsp, int planeId){
return &bsp->planes[planeId];
}
static vec3Float getVert(Bsp *bsp, int vertId){
return bsp->verts[vertId];
}
static TexInfoDef *getTexInfo(Bsp *bsp, int texInfoId){
return &bsp->texInfo[texInfoId];
}
static Texture *getTexture(Bsp *bsp, int texId){
return bsp->textures[texId];
}
static void loadBspPlanes(Bsp *bsp, void *planeData, int32_t planeSize){
// Load plane data
bsp->planes = malloc(planeSize);
memcpy(bsp->planes, planeData, planeSize);
}
static void loadBspVerts(Bsp *bsp, void *vertData, int32_t vertSize){
// Load vertex positions
bsp->verts = malloc(vertSize);
memcpy(bsp->verts, vertData, vertSize);
}
static void loadBspTexInfo(Bsp *bsp, void *texInfoData, int32_t texInfoSize){
// Load Texture mapping information
bsp->texInfo = malloc(texInfoSize);
memcpy(bsp->texInfo, texInfoData, texInfoSize);
}
static void loadBspGeometry(Bsp *bsp, void *faceData, int32_t faceSize, void *edgeData, int32_t edgeSize,
void *ledgeData, int32_t ledgeSize){
bsp->numFaces = faceSize / sizeof(FaceDef);
bsp->faces = malloc(sizeof(Face)*bsp->numFaces);
FaceDef *faces = malloc(faceSize);
memcpy(faces, faceData, faceSize);
EdgeDef *edges = malloc(edgeSize);
memcpy(edges, edgeData, edgeSize);
int32_t *ledges = malloc(ledgeSize);
memcpy(ledges, ledgeData, ledgeSize);
for(int i=0; i < bsp->numFaces; i++){
// Read all verts from edge data into a vert data structure
// that is more suitable for GL rendering
Face *face = &bsp->faces[i];
face->numVerts = faces[i].edgeNum;
face->verts = malloc(sizeof(Vert)*face->numVerts);
TexInfoDef *texInfo = getTexInfo(bsp, faces[i].texinfoId);
face->texture = getTexture(bsp, texInfo->texId);
face->glId = 0;
// If face is a trigger or clip skip loading it's verts
// TODO make somekind of stack or vector to store faces so we don't bother
// allocating useless memory
if(strcmp("trigger", getTexName(face->texture)) == 0 || strcmp("clip", getTexName(face->texture)) == 0){
continue;
}
for(int edge=0; edge < faces[i].edgeNum; edge++){
int32_t edgeIndex = ledges[edge+faces[i].edgeId];
vec3Float verts[2];
// if edgeIndex is negative flip v0 and v1
if(edgeIndex < 0){
edgeIndex = -edgeIndex;
verts[0] = getVert(bsp, edges[edgeIndex].vert1);
verts[1] = getVert(bsp, edges[edgeIndex].vert0);
} else {
verts[0] = getVert(bsp, edges[edgeIndex].vert0);
verts[1] = getVert(bsp, edges[edgeIndex].vert1);
}
face->verts[edge].normal = getPlane(bsp, faces[i].planeId)->normal;
// Negate y and flip y & z so it's oriented properly
face->verts[edge].position.x = verts[0].x;
face->verts[edge].position.y = verts[0].z;
face->verts[edge].position.z = -verts[0].y;
// compute s and t coordinate for texture mapping
face->verts[edge].texCoord.x = (vec3FDot(&verts[0], &texInfo->vecS) + texInfo->distS) / getTexWidth(face->texture);
face->verts[edge].texCoord.y = (vec3FDot(&verts[0], &texInfo->vecT) + texInfo->distT) / getTexHeight(face->texture);
}
// Now that we have loaded all vertex data for this face
// We can generate a vbo for it
glGenBuffers(1, &face->glId);
glBindBuffer(GL_ARRAY_BUFFER, face->glId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vert)*face->numVerts, &face->verts[0], GL_STATIC_DRAW);
}
free(faces);
free(edges);
free(ledges);
}
static void loadBspTextures(Bsp *bsp, void *texData, Palette *plt){
// Read in number of textures
memcpy(&bsp->numTextures, texData, sizeof(int32_t));
bsp->textures = malloc(sizeof(Texture*)*bsp->numTextures);
// For each texture read its offset and then read texture data
int32_t *offsetData = texData + sizeof(int32_t);
for(int i=0; i < bsp->numTextures; i++){
int32_t offset = offsetData[i];
void *texDefData = texData + offset;
TextureDef texDef;
memcpy(&texDef, texDefData, sizeof(TextureDef));
// Load and convert the texture to RGB
// TODO: deal with mipmaps
bsp->textures[i] = readPltTex(texDefData + texDef.offset1, texDef.width, texDef.height, plt, texDef.name);
}
return;
}
Bsp *readBsp(void *bspData, Palette *plt){
BspHeader header;
memcpy(&header, bspData, sizeof(BspHeader));
if(header.version != BSP_VERSION){
printf("Invalid BSP version expected %d found %d\n",
BSP_VERSION, header.version);
return NULL;
}
Bsp *bsp = malloc(sizeof(Bsp));
// Start by loading texture data from BSP
loadBspTextures(bsp, bspData + header.miptex.offset, plt);
// Next we load the vertex data, plane data, and texture mapping data
loadBspVerts(bsp, bspData + header.vertices.offset, header.vertices.size);
loadBspPlanes(bsp, bspData + header.planes.offset, header.planes.size);
loadBspTexInfo(bsp, bspData + header.texinfo.offset, header.texinfo.size);
// Next we can load the rest of geometry from the BSP
loadBspGeometry(bsp, bspData+header.faces.offset, header.faces.size,
bspData+header.edges.offset, header.edges.size,
bspData+header.ledges.offset, header.ledges.size);
// Create vao for face data
glGenVertexArrays(1, &bsp->vaoId);
glBindVertexArray(bsp->vaoId);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
return bsp;
}
void renderAllBspFaces(Bsp *bsp){
glBindVertexArray(bsp->vaoId);
for(int f=0; f < bsp->numFaces; f++){
Face *face = &bsp->faces[f];
if(face->glId == 0){
continue;
}
glBindBuffer(GL_ARRAY_BUFFER, face->glId);
Vert *vert = NULL;
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
sizeof(Vert),
&vert->position);
glVertexAttribPointer(
1,
2,
GL_FLOAT,
GL_FALSE,
sizeof(Vert),
&vert->texCoord);
bindTexture(face->texture);
glDrawArrays(GL_TRIANGLE_FAN, 0, face->numVerts);
}
}
void freeBsp(Bsp *bsp){
for(int i=0; i < bsp->numTextures; i++){
freeTex(bsp->textures[i]);
}
for(int i=0; i < bsp->numFaces; i++){
free(bsp->faces[i].verts);
}
free(bsp->faces);
free(bsp->textures);
free(bsp->verts);
free(bsp->planes);
free(bsp->texInfo);
free(bsp);
}