use proper index buffers

This commit is contained in:
RSDuck 2020-11-16 15:57:56 +01:00
parent 550241dbad
commit 21dbca9543

View File

@ -74,11 +74,11 @@ typedef struct
Polygon* PolyData;
u32 NumIndices;
u16* Indices;
u32 IndicesOffset;
GLuint PrimType;
u32 NumEdgeIndices;
u16* EdgeIndices;
u32 EdgeIndicesOffset;
u32 RenderKey;
@ -107,7 +107,11 @@ u32 VertexBuffer[10240 * 7];
u32 NumVertices;
GLuint VertexArrayID;
GLuint IndexBufferID;
u16 IndexBuffer[2048 * 40];
u32 NumIndices, NumEdgeIndices;
const u32 EdgeIndicesOffset = 2048 * 30;
GLuint TexMemID;
GLuint TexPalMemID;
@ -320,6 +324,9 @@ bool Init()
glEnableVertexAttribArray(3); // attrib
glVertexAttribIPointer(3, 3, GL_UNSIGNED_INT, 7*4, (void*)(4*4));
glGenBuffers(1, &IndexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(IndexBuffer), NULL, GL_DYNAMIC_DRAW);
glGenFramebuffers(4, &FramebufferID[0]);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferID[0]);
@ -563,15 +570,15 @@ void BuildPolygons(RendererPolygon* polygons, int npolys)
u32* vptr = &VertexBuffer[0];
u32 vidx = 0;
u16* iptr = &IndexBuffer[0];
u16* eiptr = &IndexBuffer[2048*30];
u32 iidx = 0;
u32 eidx = EdgeIndicesOffset;
for (int i = 0; i < npolys; i++)
{
RendererPolygon* rp = &polygons[i];
Polygon* poly = rp->PolyData;
rp->Indices = iptr;
rp->IndicesOffset = iidx;
rp->NumIndices = 0;
u32 vidx_first = vidx;
@ -606,7 +613,7 @@ void BuildPolygons(RendererPolygon* polygons, int npolys)
vptr = SetupVertex(poly, j, vtx, vtxattr, vptr);
*iptr++ = vidx;
IndexBuffer[iidx++] = vidx;
rp->NumIndices++;
vidx++;
@ -627,9 +634,9 @@ void BuildPolygons(RendererPolygon* polygons, int npolys)
}
// build a triangle
*iptr++ = vidx_first;
*iptr++ = vidx - 2;
*iptr++ = vidx - 1;
IndexBuffer[iidx++] = vidx_first;
IndexBuffer[iidx++] = vidx - 2;
IndexBuffer[iidx++] = vidx - 1;
rp->NumIndices += 3;
}
else // quad, pentagon, etc
@ -649,9 +656,9 @@ void BuildPolygons(RendererPolygon* polygons, int npolys)
if (j >= 2)
{
// build a triangle
*iptr++ = vidx_first;
*iptr++ = vidx - 1;
*iptr++ = vidx;
IndexBuffer[iidx++] = vidx_first;
IndexBuffer[iidx++] = vidx - 1;
IndexBuffer[iidx++] = vidx;
rp->NumIndices += 3;
}
@ -743,46 +750,48 @@ void BuildPolygons(RendererPolygon* polygons, int npolys)
if (j >= 1)
{
// build a triangle
*iptr++ = vidx_first;
*iptr++ = vidx - 1;
*iptr++ = vidx;
IndexBuffer[iidx++] = vidx_first;
IndexBuffer[iidx++] = vidx - 1;
IndexBuffer[iidx++] = vidx;
rp->NumIndices += 3;
}
vidx++;
}
*iptr++ = vidx_first;
*iptr++ = vidx - 1;
*iptr++ = vidx_first + 1;
IndexBuffer[iidx++] = vidx_first;
IndexBuffer[iidx++] = vidx - 1;
IndexBuffer[iidx++] = vidx_first + 1;
rp->NumIndices += 3;
}
}
rp->EdgeIndices = eiptr;
rp->EdgeIndicesOffset = eidx;
rp->NumEdgeIndices = 0;
u32 vidx_cur = vidx_first;
for (int j = 1; j < poly->NumVertices; j++)
{
*eiptr++ = vidx_cur;
*eiptr++ = vidx_cur + 1;
IndexBuffer[eidx++] = vidx_cur;
IndexBuffer[eidx++] = vidx_cur + 1;
vidx_cur++;
rp->NumEdgeIndices += 2;
}
*eiptr++ = vidx_cur;
*eiptr++ = vidx_first;
IndexBuffer[eidx++] = vidx_cur;
IndexBuffer[eidx++] = vidx_first;
rp->NumEdgeIndices += 2;
}
NumVertices = vidx;
NumIndices = iidx;
NumEdgeIndices = eidx;
}
void RenderSinglePolygon(int i)
{
RendererPolygon* rp = &PolygonList[i];
glDrawElements(rp->PrimType, rp->NumIndices, GL_UNSIGNED_SHORT, rp->Indices);
glDrawElements(rp->PrimType, rp->NumIndices, GL_UNSIGNED_SHORT, (void*)(uintptr_t)(rp->IndicesOffset * 2));
}
int RenderPolygonBatch(int i)
@ -803,7 +812,7 @@ int RenderPolygonBatch(int i)
numindices += cur_rp->NumIndices;
}
glDrawElements(primtype, numindices, GL_UNSIGNED_SHORT, rp->Indices);
glDrawElements(primtype, numindices, GL_UNSIGNED_SHORT, (void*)(uintptr_t)(rp->IndicesOffset * 2));
return numpolys;
}
@ -823,7 +832,7 @@ int RenderPolygonEdgeBatch(int i)
numindices += cur_rp->NumEdgeIndices;
}
glDrawElements(GL_LINES, numindices, GL_UNSIGNED_SHORT, rp->EdgeIndices);
glDrawElements(GL_LINES, numindices, GL_UNSIGNED_SHORT, (void*)(uintptr_t)(rp->EdgeIndicesOffset * 2));
return numpolys;
}
@ -1320,6 +1329,11 @@ void RenderFrame()
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
glBufferSubData(GL_ARRAY_BUFFER, 0, NumVertices*7*4, VertexBuffer);
// bind to access the index buffer
glBindVertexArray(VertexArrayID);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, NumIndices * 2, IndexBuffer);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, EdgeIndicesOffset * 2, NumEdgeIndices * 2, IndexBuffer + EdgeIndicesOffset);
RenderSceneChunk(0, 192);
}