Skip to content

Commit e875f6c

Browse files
committed
- fix batch generating and rendering errors
1 parent f6feb20 commit e875f6c

4 files changed

Lines changed: 118 additions & 44 deletions

File tree

DriverLevelTool/viewer/gl_renderer.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "gl_renderer.h"
1+
#include "gl_renderer.h"
22
#include <SDL.h>
33
#include "core/cmdlib.h"
44

@@ -25,6 +25,7 @@ int g_swapInterval = 1;
2525

2626
int g_CurrentBlendMode = BM_NONE;
2727
int g_CurrentDepthMode = 0;
28+
int g_CurrentCullMode = CULL_NONE;
2829
GrVAO* g_CurrentVAO = nullptr;
2930
TextureID g_lastBoundTexture = -1;
3031
ShaderID g_CurrentShader = -1;
@@ -54,6 +55,12 @@ static const GLenum glPrimitiveType[] = {
5455
GL_POINTS,
5556
};
5657

58+
static const GLenum glCullModes[] = {
59+
GL_NONE,
60+
GL_CW,
61+
GL_CCW,
62+
};
63+
5764
void GR_GetWVPUniforms(GLuint program)
5865
{
5966
u_MatrixUniforms[MATRIX_VIEW] = glGetUniformLocation(program, "u_View");
@@ -342,7 +349,7 @@ void GR_UpdateMatrixUniforms()
342349
g_matrices[MATRIX_WORLDVIEWPROJECTION] = identity4() * g_matrices[MATRIX_PROJECTION] * (g_matrices[MATRIX_VIEW] * g_matrices[MATRIX_WORLD]);
343350

344351
for(int i = 0; i < MATRIX_MODES; i++)
345-
glUniformMatrix4fv(u_MatrixUniforms[i], 1, GL_FALSE, g_matrices[i]);
352+
glUniformMatrix4fv(u_MatrixUniforms[i], 1, GL_TRUE, g_matrices[i]);
346353
}
347354

348355
//----------------------------------------------------------------
@@ -365,8 +372,8 @@ TextureID GR_CreateRGBATexture(int width, int height, ubyte* data)
365372
glGenTextures(1, &newTexture);
366373

367374
glBindTexture(GL_TEXTURE_2D, newTexture);
368-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
369-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
375+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
376+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
370377
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
371378

372379
glFinish();
@@ -408,6 +415,16 @@ void GR_ClearDepth(float depth)
408415
glClear(GL_STENCIL_BUFFER_BIT);
409416
}
410417

418+
void GR_BeginScene()
419+
{
420+
GR_SetViewPort(0, 0, g_windowWidth, g_windowHeight);
421+
}
422+
423+
void GR_EndScene()
424+
{
425+
426+
}
427+
411428
void GR_SwapWindow()
412429
{
413430
SDL_GL_SwapWindow(g_window);
@@ -481,6 +498,24 @@ void GR_SetBlendMode(GR_BlendMode blendMode)
481498
g_CurrentBlendMode = blendMode;
482499
}
483500

501+
void GR_SetCullMode(GR_CullMode cullMode)
502+
{
503+
if (g_CurrentCullMode == cullMode)
504+
return;
505+
506+
if(cullMode == CULL_NONE)
507+
{
508+
glDisable(GL_CULL_FACE);
509+
}
510+
else
511+
{
512+
glEnable(GL_CULL_FACE);
513+
glFrontFace(glCullModes[cullMode]);
514+
}
515+
516+
g_CurrentCullMode = cullMode;
517+
}
518+
484519
//--------------------------------------------------------------------------
485520

486521
GrVAO* GR_CreateVAO(int numVertices, GrVertex* verts /*= nullptr*/, int dynamic /*= 0*/)
@@ -565,5 +600,5 @@ void GR_DrawNonIndexed(GR_PrimitiveType primitivesType, int firstVertex, int num
565600

566601
void GR_DrawIndexed(GR_PrimitiveType primitivesType, int firstIndex, int numIndices)
567602
{
568-
glDrawElements(glPrimitiveType[primitivesType], numIndices, GL_UNSIGNED_INT, (void*)(intptr_t)firstIndex);
603+
glDrawElements(glPrimitiveType[primitivesType], numIndices, GL_UNSIGNED_INT, (void*)(intptr_t)(firstIndex*sizeof(int)));
569604
}

DriverLevelTool/viewer/gl_renderer.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@ enum GR_ShaderAttrib
2929

3030
enum GR_BlendMode
3131
{
32-
BM_NONE,
32+
BM_NONE = 0,
3333
BM_AVERAGE,
3434
BM_ADD,
3535
BM_SUBTRACT,
3636
BM_ADD_QUATER_SOURCE
3737
};
3838

39+
enum GR_CullMode
40+
{
41+
CULL_NONE = 0,
42+
CULL_FRONT,
43+
CULL_BACK,
44+
};
45+
3946
enum GR_MatrixMode
4047
{
4148
MATRIX_VIEW = 0,
@@ -69,6 +76,9 @@ ShaderID GR_CompileShader(const char* source);
6976

7077
//--------------------------------------------------
7178

79+
void GR_BeginScene();
80+
void GR_EndScene();
81+
7282
void GR_ClearColor(int r, int g, int b);
7383
void GR_ClearDepth(float depth);
7484

@@ -94,12 +104,13 @@ void GR_SetMatrix(GR_MatrixMode mode, const Matrix4x4& matrix);
94104

95105
void GR_SetPolygonOffset(float ofs);
96106
void GR_SetDepth(int enable);
107+
void GR_SetCullMode(GR_CullMode cullMode);
97108

98109
void GR_UpdateMatrixUniforms();
99110

100111
//--------------------------------------------------
101112

102-
void GR_DrawNonIndexed(GR_PrimitiveType primitivesType, int firstVertex, int numVertices);
103-
void GR_DrawIndexed(GR_PrimitiveType primitivesType, int firstIndex, int numIndices);
113+
void GR_DrawNonIndexed(GR_PrimitiveType primitivesType, int firstVertex, int numVertices);
114+
void GR_DrawIndexed(GR_PrimitiveType primitivesType, int firstIndex, int numIndices);
104115

105116
#endif

DriverLevelTool/viewer/rendermodel.cpp

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,16 @@ int FindGrVertexIndex(const DkList<vertexTuple_t>& whereFind, int flags, int ver
7575
return -1;
7676
}
7777

78-
modelBatch_t* FindBatch(DkList<modelBatch_t*>& batches, int tpageId)
78+
struct genBatch_t
7979
{
80-
for(int i = 0; i < batches.numElem(); i++)
80+
DkList<int> indices;
81+
82+
int tpage;
83+
};
84+
85+
genBatch_t* FindBatch(DkList<genBatch_t*>& batches, int tpageId)
86+
{
87+
for (int i = 0; i < batches.numElem(); i++)
8188
{
8289
if (batches[i]->tpage == tpageId)
8390
return batches[i];
@@ -87,10 +94,9 @@ modelBatch_t* FindBatch(DkList<modelBatch_t*>& batches, int tpageId)
8794

8895
void CRenderModel::GenerateBuffers()
8996
{
90-
DkList<vertexTuple_t> verticesMap;
91-
97+
DkList<genBatch_t*> batches;
9298
DkList<GrVertex> vertices;
93-
DkList<int> indices;
99+
DkList<vertexTuple_t> verticesMap;
94100

95101
MODEL* model = m_sourceModel->model;
96102
MODEL* vertex_ref = model;
@@ -108,13 +114,13 @@ void CRenderModel::GenerateBuffers()
108114
vertex_ref = ref->model;
109115
}
110116

111-
modelBatch_t* batch = nullptr;
117+
genBatch_t* batch = nullptr;
112118

113119
int modelSize = m_sourceModel->size;
114120
int face_ofs = 0;
115121
dpoly_t dec_face;
116122

117-
// go throught all polygons
123+
// go through all polygons
118124
for (int i = 0; i < model->num_polys; i++)
119125
{
120126
char* facedata = model->pPolyAt(face_ofs);
@@ -133,7 +139,9 @@ void CRenderModel::GenerateBuffers()
133139
{
134140
MsgError("poly id=%d type=%d ofs=%d zero size!\n", i, *facedata & 31, model->poly_block + face_ofs);
135141
break;
136-
}
142+
}
143+
144+
face_ofs += poly_size;
137145

138146
int numPolyVerts = (dec_face.flags & FACE_IS_QUAD) ? 4 : 3;
139147
bool bad_face = false;
@@ -161,22 +169,22 @@ void CRenderModel::GenerateBuffers()
161169
if (bad_face)
162170
{
163171
MsgError("poly id=%d type=%d ofs=%d has invalid indices (or format is unknown)\n", i, *facedata & 31, model->poly_block + face_ofs);
164-
face_ofs += poly_size;
172+
165173
continue;
166174
}
167175

168176
// find or create new batch
169177
int tpageId = (dec_face.flags & FACE_TEXTURED) ? dec_face.page : -1;
170178

171-
batch = FindBatch(m_batches, tpageId);
179+
if(batch && batch->tpage != tpageId)
180+
batch = FindBatch(batches, tpageId);
181+
172182
if (!batch)
173183
{
174-
batch = new modelBatch_t;
175-
batch->startIndex = indices.numElem();
176-
batch->numIndices = 0;
184+
batch = new genBatch_t;
177185
batch->tpage = tpageId;
178186

179-
m_batches.append(batch);
187+
batches.append(batch);
180188
}
181189

182190
// Gouraud-shaded poly smoothing
@@ -193,7 +201,7 @@ void CRenderModel::GenerateBuffers()
193201
int vflags = dec_face.flags & ~(FACE_IS_QUAD | FACE_RGB);
194202

195203
// try searching for vertex
196-
int index = FindGrVertexIndex(verticesMap,
204+
int index = FindGrVertexIndex(verticesMap,
197205
vflags,
198206
dec_face.vindices[VERT_IDX],
199207
dec_face.nindices[VERT_IDX],
@@ -207,6 +215,7 @@ void CRenderModel::GenerateBuffers()
207215

208216
vertMap.flags = vflags;
209217
vertMap.normalIndex = -1;
218+
vertMap.vertexIndex = dec_face.vindices[VERT_IDX];
210219

211220
// get the vertex
212221
SVECTOR* vert = vertex_ref->pVertex(dec_face.vindices[VERT_IDX]);
@@ -230,11 +239,10 @@ void CRenderModel::GenerateBuffers()
230239
newVert.tc_v = float(uv.v) / 256.0f;
231240
}
232241

233-
vertMap.grVertexIndex = vertices.append(newVert);
234-
vertMap.vertexIndex = dec_face.vindices[VERT_IDX];
242+
index = vertMap.grVertexIndex = vertices.append(newVert);
235243

236244
// add vertex and a map
237-
index = verticesMap.append(vertMap);
245+
verticesMap.append(vertMap);
238246

239247
// vertices and verticesMap should be equal
240248
_ASSERT(verticesMap.numElem() == vertices.numElem());
@@ -257,30 +265,50 @@ void CRenderModel::GenerateBuffers()
257265

258266
// set to each vertex
259267
for (int v = 0; v < numPolyVerts; v++)
260-
*(Vector3D*)&vertices[faceIndices[0]].nx = normal;
268+
*(Vector3D*)&vertices[faceIndices[v]].nx = normal;
261269
}
262270

263271
// triangulate quads
264272
if(numPolyVerts == 4)
265273
{
266-
indices.append(faceIndices[0]);
267-
indices.append(faceIndices[1]);
268-
indices.append(faceIndices[2]);
269-
270-
indices.append(faceIndices[0]);
271-
indices.append(faceIndices[2]);
272-
indices.append(faceIndices[3]);
273-
batch->numIndices += 6;
274+
batch->indices.append(faceIndices[0]);
275+
batch->indices.append(faceIndices[1]);
276+
batch->indices.append(faceIndices[2]);
277+
278+
batch->indices.append(faceIndices[2]);
279+
batch->indices.append(faceIndices[3]);
280+
batch->indices.append(faceIndices[0]);
274281
}
275282
else
276283
{
277-
indices.append(faceIndices[0]);
278-
indices.append(faceIndices[1]);
279-
indices.append(faceIndices[2]);
280-
batch->numIndices += 6;
284+
batch->indices.append(faceIndices[0]);
285+
batch->indices.append(faceIndices[1]);
286+
batch->indices.append(faceIndices[2]);
281287
}
288+
}
282289

283-
face_ofs += poly_size;
290+
//DkList<GrVertex> vertices;
291+
DkList<int> indices;
292+
293+
// merge batches
294+
for(int i = 0; i < batches.numElem(); i++)
295+
{
296+
//int startVertex = vertices.numElem();
297+
int startIndex = indices.numElem();
298+
299+
//vertices.append(batches[i]->vertices);
300+
301+
for(int j = 0; j < batches[i]->indices.numElem(); j++)
302+
indices.append(batches[i]->indices[j]);
303+
304+
modelBatch_t batch;
305+
batch.startIndex = startIndex;
306+
batch.numIndices = batches[i]->indices.numElem();
307+
batch.tpage = batches[i]->tpage;
308+
309+
m_batches.append(batch);
310+
311+
delete batches[i];
284312
}
285313

286314
m_vao = GR_CreateVAO(vertices.numElem(), indices.numElem(), vertices.ptr(), indices.ptr(), 0);
@@ -300,9 +328,9 @@ void CRenderModel::Draw()
300328

301329
for(int i = 0; i < m_batches.numElem(); i++)
302330
{
303-
modelBatch_t* batch = m_batches[i];
331+
modelBatch_t& batch = m_batches[i];
304332
GR_SetShader(g_modelShader);
305-
GR_SetTexture(g_hwTexturePages[batch->tpage][0]);
306-
GR_DrawIndexed(PRIM_TRIANGLES, batch->startIndex, batch->numIndices);
333+
GR_SetTexture(g_hwTexturePages[batch.tpage][0]);
334+
GR_DrawIndexed(PRIM_TRIANGLES, batch.startIndex, batch.numIndices);
307335
}
308336
}

DriverLevelTool/viewer/rendermodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CRenderModel
3131
RegionModels_t* m_regModelData{ nullptr };
3232
ModelRef_t* m_sourceModel { nullptr };
3333
GrVAO* m_vao { nullptr };
34-
DkList<modelBatch_t*> m_batches;
34+
DkList<modelBatch_t> m_batches;
3535
int m_numVerts;
3636
};
3737

0 commit comments

Comments
 (0)