diff options
author | Arisotura <thetotalworm@gmail.com> | 2020-08-19 13:02:54 +0200 |
---|---|---|
committer | Arisotura <thetotalworm@gmail.com> | 2020-08-19 13:02:54 +0200 |
commit | de19ce6250dcdcaae8a5a67d2e88694b771eafad (patch) | |
tree | aaf3c940d9f8e61904e5088dba5ce55b13e4c864 /src | |
parent | a32d997e1c341085da4ccfe5ae4c812f64681f10 (diff) |
3D/GL: make polygon generation code cleaner, add quicker codepath for triangles (also laying ground for some evil experiment)
also fix stupid bug with line polygons
Diffstat (limited to 'src')
-rw-r--r-- | src/GPU3D_OpenGL.cpp | 126 |
1 files changed, 63 insertions, 63 deletions
diff --git a/src/GPU3D_OpenGL.cpp b/src/GPU3D_OpenGL.cpp index 8a06874..b47f0b5 100644 --- a/src/GPU3D_OpenGL.cpp +++ b/src/GPU3D_OpenGL.cpp @@ -527,6 +527,46 @@ void SetupPolygon(RendererPolygon* rp, Polygon* polygon) } } +u32* SetupVertex(Polygon* poly, int vid, Vertex* vtx, u32 vtxattr, u32* vptr) +{ + u32 z = poly->FinalZ[vid]; + u32 w = poly->FinalW[vid]; + + u32 alpha = (poly->Attr >> 16) & 0x1F; + + // Z should always fit within 16 bits, so it's okay to do this + u32 zshift = 0; + while (z > 0xFFFF) { z >>= 1; zshift++; } + + u32 x, y; + if (ScaleFactor > 1) + { + x = (vtx->HiresPosition[0] * ScaleFactor) >> 4; + y = (vtx->HiresPosition[1] * ScaleFactor) >> 4; + } + else + { + x = vtx->FinalPosition[0]; + y = vtx->FinalPosition[1]; + } + + *vptr++ = x | (y << 16); + *vptr++ = z | (w << 16); + + *vptr++ = (vtx->FinalColor[0] >> 1) | + ((vtx->FinalColor[1] >> 1) << 8) | + ((vtx->FinalColor[2] >> 1) << 16) | + (alpha << 24); + + *vptr++ = (u16)vtx->TexCoords[0] | ((u16)vtx->TexCoords[1] << 16); + + *vptr++ = vtxattr | (zshift << 16); + *vptr++ = poly->TexParam; + *vptr++ = poly->TexPalette; + + return vptr; +} + void BuildPolygons(RendererPolygon* polygons, int npolys) { u32* vptr = &VertexBuffer[0]; @@ -564,43 +604,16 @@ void BuildPolygons(RendererPolygon* polygons, int npolys) { Vertex* vtx = poly->Vertices[j]; - u32 z = poly->FinalZ[j]; - u32 w = poly->FinalW[j]; - - // Z should always fit within 16 bits, so it's okay to do this - u32 zshift = 0; - while (z > 0xFFFF) { z >>= 1; zshift++; } - - u32 x, y; - if (ScaleFactor > 1) - { - x = (vtx->HiresPosition[0] * ScaleFactor) >> 4; - y = (vtx->HiresPosition[1] * ScaleFactor) >> 4; - } - else - { - x = vtx->FinalPosition[0]; - y = vtx->FinalPosition[1]; - } - if (j > 0) { - if (lastx == x && lasty == y) continue; + if (lastx == vtx->FinalPosition[0] && + lasty == vtx->FinalPosition[1]) continue; } - *vptr++ = x | (y << 16); - *vptr++ = z | (w << 16); - - *vptr++ = (vtx->FinalColor[0] >> 1) | - ((vtx->FinalColor[1] >> 1) << 8) | - ((vtx->FinalColor[2] >> 1) << 16) | - (alpha << 24); - - *vptr++ = (u16)vtx->TexCoords[0] | ((u16)vtx->TexCoords[1] << 16); + lastx = vtx->FinalPosition[0]; + lasty = vtx->FinalPosition[1]; - *vptr++ = vtxattr | (zshift << 16); - *vptr++ = poly->TexParam; - *vptr++ = poly->TexPalette; + vptr = SetupVertex(poly, j, vtx, vtxattr, vptr); *iptr++ = vidx; rp->NumIndices++; @@ -610,46 +623,33 @@ void BuildPolygons(RendererPolygon* polygons, int npolys) if (nout >= 2) break; } } - else + else if (poly->NumVertices == 3) // regular triangle { rp->PrimType = GL_TRIANGLES; - for (int j = 0; j < poly->NumVertices; j++) + for (int j = 0; j < 3; j++) { Vertex* vtx = poly->Vertices[j]; - u32 z = poly->FinalZ[j]; - u32 w = poly->FinalW[j]; - - // Z should always fit within 16 bits, so it's okay to do this - u32 zshift = 0; - while (z > 0xFFFF) { z >>= 1; zshift++; } - - u32 x, y; - if (ScaleFactor > 1) - { - x = (vtx->HiresPosition[0] * ScaleFactor) >> 4; - y = (vtx->HiresPosition[1] * ScaleFactor) >> 4; - } - else - { - x = vtx->FinalPosition[0]; - y = vtx->FinalPosition[1]; - } - - *vptr++ = x | (y << 16); - *vptr++ = z | (w << 16); + vptr = SetupVertex(poly, j, vtx, vtxattr, vptr); + vidx++; + } - *vptr++ = (vtx->FinalColor[0] >> 1) | - ((vtx->FinalColor[1] >> 1) << 8) | - ((vtx->FinalColor[2] >> 1) << 16) | - (alpha << 24); + // build a triangle + *iptr++ = vidx_first; + *iptr++ = vidx - 2; + *iptr++ = vidx - 1; + rp->NumIndices += 3; + } + else + { + rp->PrimType = GL_TRIANGLES; - *vptr++ = (u16)vtx->TexCoords[0] | ((u16)vtx->TexCoords[1] << 16); + for (int j = 0; j < poly->NumVertices; j++) + { + Vertex* vtx = poly->Vertices[j]; - *vptr++ = vtxattr | (zshift << 16); - *vptr++ = poly->TexParam; - *vptr++ = poly->TexPalette; + vptr = SetupVertex(poly, j, vtx, vtxattr, vptr); if (j >= 2) { |