@@ -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
8895void 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}
0 commit comments