Skip to content

Commit c5606a9

Browse files
committed
- fix Driver 1 polygon types
1 parent 0746207 commit c5606a9

2 files changed

Lines changed: 79 additions & 38 deletions

File tree

DriverLevelTool/driver_routines/models.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ void CDriverLevelModels::OnCarModelFreed(CarModelData_t* data)
312312
//--------------------------------------------------------------------------------
313313

314314
// From Car Poly code of D2
315-
int PolySizes[56] = {
315+
const int PolySizes[56] = {
316316
8,12,16,24,20,20,28,32,8,12,16,16,
317317
0,0,0,0,12,12,12,16,20,20,24,24,
318318
0,0,0,0,0,0,0,0,8,12,16,24,
@@ -360,6 +360,8 @@ int decode_poly(const char* polyList, dpoly_t* out, int forceType /*= -1*/)
360360

361361
*(uint*)&out->color = 0;
362362

363+
// TODO: D1 and D2 to have different decoding routines
364+
363365
switch (ptype)
364366
{
365367
case 1:
@@ -383,11 +385,12 @@ int decode_poly(const char* polyList, dpoly_t* out, int forceType /*= -1*/)
383385
{
384386
// F4
385387
*(uint*)out->vindices = *(uint*)&polyList[1];
388+
*(uint*)out->uv = *(uint*)&polyList[4];
386389
*(uint*)&out->color = *(uint*)&polyList[8];
387390

388391
// FIXME: read colours
389392

390-
out->flags = FACE_RGB; // RGB?
393+
out->flags = FACE_RGB | FACE_IS_QUAD; // RGB?
391394

392395
break;
393396
}
@@ -413,9 +416,9 @@ int decode_poly(const char* polyList, dpoly_t* out, int forceType /*= -1*/)
413416
break;
414417
}
415418
case 5:
419+
case 7:
416420
case 9:
417421
case 11:
418-
case 17:
419422
case 21:
420423
{
421424
POLYFT4* pft4 = (POLYFT4*)polyList;
@@ -438,6 +441,19 @@ int decode_poly(const char* polyList, dpoly_t* out, int forceType /*= -1*/)
438441

439442
break;
440443
}
444+
case 17:
445+
{
446+
// F4
447+
out->page = polyList[1];
448+
out->detail = polyList[2];
449+
450+
*(uint*)out->vindices = *(uint*)&polyList[4];
451+
*(uint*)&out->color = *(uint*)&polyList[8];
452+
453+
out->flags = FACE_IS_QUAD | FACE_RGB; // RGB?
454+
455+
break;
456+
}
441457
case 22:
442458
{
443459
POLYGT3* pgt3 = (POLYGT3*)polyList;
@@ -456,7 +472,6 @@ int decode_poly(const char* polyList, dpoly_t* out, int forceType /*= -1*/)
456472
out->flags = FACE_VERT_NORMAL | FACE_TEXTURED;
457473
break;
458474
}
459-
case 7:
460475
case 23:
461476
{
462477
POLYGT4* pgt4 = (POLYGT4*)polyList;

DriverLevelTool/viewer/rendermodel.cpp

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ genBatch_t* FindBatch(Array<genBatch_t*>& batches, int tpageId)
164164
void CRenderModel::GenerateBuffers()
165165
{
166166
Array<genBatch_t*> batches;
167-
Array<GrVertex> vertices;
167+
Array<GrVertex> vertices;
168168
Array<vertexTuple_t> verticesMap;
169-
169+
170170
MODEL* model = m_sourceModel->model;
171171
MODEL* vertex_ref = model;
172172

@@ -176,7 +176,7 @@ void CRenderModel::GenerateBuffers()
176176
}
177177

178178
genBatch_t* batch = nullptr;
179-
179+
180180
int modelSize = m_sourceModel->size;
181181
int face_ofs = 0;
182182
dpoly_t dec_face;
@@ -195,7 +195,13 @@ void CRenderModel::GenerateBuffers()
195195
break;
196196
}
197197

198-
int poly_size = decode_poly(facedata, &dec_face);
198+
int forcePolyType = -1;
199+
200+
// [A] HACK: is sky? force POLYFT4. This fixes VEGAS skies
201+
if (m_sourceModel->index < 4)
202+
forcePolyType = 21;
203+
204+
int poly_size = decode_poly(facedata, &dec_face, forcePolyType);
199205

200206
// check poly size
201207
if (poly_size == 0)
@@ -208,6 +214,7 @@ void CRenderModel::GenerateBuffers()
208214

209215
int numPolyVerts = (dec_face.flags & FACE_IS_QUAD) ? 4 : 3;
210216
bool bad_face = false;
217+
bool bad_normals = false;
211218

212219
// perform vertex checks
213220
for (int v = 0; v < numPolyVerts; v++)
@@ -223,7 +230,7 @@ void CRenderModel::GenerateBuffers()
223230
{
224231
if (dec_face.nindices[v] >= vertex_ref->num_point_normals)
225232
{
226-
bad_face = true;
233+
bad_normals = true;
227234
break;
228235
}
229236
}
@@ -232,7 +239,7 @@ void CRenderModel::GenerateBuffers()
232239
if (bad_face)
233240
{
234241
MsgError("poly id=%d type=%d ofs=%d has invalid indices (or format is unknown)\n", i, *facedata & 31, model->poly_block + face_ofs);
235-
242+
236243
continue;
237244
}
238245

@@ -241,18 +248,18 @@ void CRenderModel::GenerateBuffers()
241248

242249
if (tpageId == 255)
243250
tpageId = -1;
244-
245-
if(batch && batch->tpage != tpageId)
251+
252+
if (batch && batch->tpage != tpageId)
246253
batch = FindBatch(batches, tpageId);
247-
254+
248255
if (!batch)
249256
{
250257
batch = new genBatch_t;
251258
batch->tpage = tpageId;
252-
259+
253260
batches.append(batch);
254261
}
255-
262+
256263
// Gouraud-shaded poly smoothing
257264
bool smooth = (dec_face.flags & FACE_VERT_NORMAL);
258265

@@ -265,16 +272,19 @@ void CRenderModel::GenerateBuffers()
265272
#define VERT_IDX v//numPolyVerts - 1 - v
266273

267274
int vflags = dec_face.flags & ~(FACE_IS_QUAD | FACE_RGB);
268-
275+
269276
// try searching for vertex
270277
int index = FindGrVertexIndex(verticesMap,
271278
vflags,
272-
dec_face.vindices[VERT_IDX],
273-
dec_face.nindices[VERT_IDX],
279+
dec_face.vindices[VERT_IDX],
280+
dec_face.nindices[VERT_IDX],
274281
*(ushort*)dec_face.uv[VERT_IDX]);
275282

283+
//if (vertexModCb)
284+
// index = -1;
285+
276286
// add new vertex
277-
if(index == -1)
287+
if (index == -1)
278288
{
279289
GrVertex newVert;
280290
vertexTuple_t vertMap;
@@ -283,33 +293,45 @@ void CRenderModel::GenerateBuffers()
283293
vertMap.normalIndex = -1;
284294
vertMap.vertexIndex = dec_face.vindices[VERT_IDX];
285295
vertMap.uvs = *(ushort*)dec_face.uv[VERT_IDX];
286-
296+
287297
// get the vertex
288298
SVECTOR* vert = vertex_ref->pVertex(dec_face.vindices[VERT_IDX]);
289299
Vector3D fVert = Vector3D(vert->x * RENDER_SCALING, vert->y * -RENDER_SCALING, vert->z * RENDER_SCALING);
290-
300+
291301
(*(Vector3D*)&newVert.vx) = fVert;
292302

303+
// set color
304+
newVert.cr = newVert.cg = newVert.cb = newVert.ca = 1.0f;
305+
306+
// add bounding box stuff
293307
AddExtentVertex(m_extMin, m_extMax, fVert);
294308

295-
if (smooth)
309+
if (smooth && !bad_normals)
296310
{
297311
vertMap.normalIndex = dec_face.nindices[VERT_IDX];
298-
312+
299313
SVECTOR* norm = vertex_ref->pPointNormal(vertMap.normalIndex);
300-
*(Vector3D*)&newVert.nx = Vector3D(norm->x * RENDER_SCALING, norm->y * -RENDER_SCALING, norm->z * RENDER_SCALING);
314+
*(Vector3D*)&newVert.nx = -Vector3D(norm->x * RENDER_SCALING, norm->y * -RENDER_SCALING, norm->z * RENDER_SCALING);
301315
}
302-
316+
303317
if (dec_face.flags & FACE_TEXTURED)
304318
{
305319
UV_INFO uv = *(UV_INFO*)dec_face.uv[VERT_IDX];
306320

307321
// map to 0..1
308-
newVert.tc_u = ((float)uv.u + 0.5f) / 256.0f;
309-
newVert.tc_v = ((float)uv.v + 0.5f) / 256.0f;
322+
newVert.tc_u = ((float)uv.u + 0.5f) / TEXPAGE_SIZE_Y;
323+
newVert.tc_v = ((float)uv.v + 0.5f) / TEXPAGE_SIZE_Y;
324+
}
325+
326+
if (dec_face.flags & FACE_RGB)
327+
{
328+
newVert.cr = dec_face.color.r / 255;
329+
newVert.cg = dec_face.color.g / 255;
330+
newVert.cb = dec_face.color.b / 255;
310331
}
311332

312333
index = vertMap.grVertexIndex = vertices.size();
334+
313335
vertices.append(newVert);
314336

315337
// add vertex and a map
@@ -318,20 +340,20 @@ void CRenderModel::GenerateBuffers()
318340
// vertices and verticesMap should be equal
319341
assert(verticesMap.size() == vertices.size());
320342
}
321-
343+
322344
// add index
323345
faceIndices[v] = index;
324346
}
325347

326348
// if not gouraud shaded we just compute face normal
327349
// FIXME: make it like game does?
328-
if(!smooth)
350+
if (!smooth || bad_normals)
329351
{
330352
// it takes only triangle
331353
Vector3D v0 = *(Vector3D*)&vertices[faceIndices[0]].vx;
332354
Vector3D v1 = *(Vector3D*)&vertices[faceIndices[1]].vx;
333355
Vector3D v2 = *(Vector3D*)&vertices[faceIndices[2]].vx;
334-
356+
335357
Vector3D normal = normalize(cross(v2 - v1, v0 - v1));
336358

337359
// set to each vertex
@@ -340,7 +362,7 @@ void CRenderModel::GenerateBuffers()
340362
}
341363

342364
// triangulate quads
343-
if(numPolyVerts == 4)
365+
if (numPolyVerts == 4)
344366
{
345367
batch->indices.append(faceIndices[0]);
346368
batch->indices.append(faceIndices[1]);
@@ -361,14 +383,14 @@ void CRenderModel::GenerateBuffers()
361383
Array<int> indices;
362384

363385
// merge batches
364-
for(usize i = 0; i < batches.size(); i++)
365-
{
386+
for (usize i = 0; i < batches.size(); i++)
387+
{
366388
//int startVertex = vertices.numElem();
367389
int startIndex = indices.size();
368-
390+
369391
//vertices.append(batches[i]->vertices);
370392

371-
for(usize j = 0; j < batches[i]->indices.size(); j++)
393+
for (usize j = 0; j < batches[i]->indices.size(); j++)
372394
indices.append(batches[i]->indices[j]);
373395

374396
modelBatch_t batch;
@@ -377,13 +399,17 @@ void CRenderModel::GenerateBuffers()
377399
batch.tpage = batches[i]->tpage;
378400

379401
m_batches.append(batch);
380-
402+
381403
delete batches[i];
382404
}
383-
405+
406+
// if has existing one - regenerate
407+
if (m_vao)
408+
GR_DestroyVAO(m_vao);
409+
384410
m_vao = GR_CreateVAO(vertices.size(), indices.size(), (GrVertex*)vertices, (int*)indices, 0);
385411

386-
if(!m_vao)
412+
if (!m_vao)
387413
{
388414
MsgError("Cannot create Model VAO!\n");
389415
}

0 commit comments

Comments
 (0)