- 2D code works in GL legacy mode.

Not tested yet with shaders.
This commit is contained in:
Christoph Oelckers 2018-03-29 11:58:28 +02:00
parent 0ff1426b23
commit 077df87704
2 changed files with 33 additions and 41 deletions

View File

@ -440,10 +440,15 @@ unsigned char *FGLRenderer::GetTextureBuffer(FTexture *tex, int &w, int &h)
//=========================================================================== //===========================================================================
#define TDiO ((F2DDrawer::TwoDVertex*)NULL) #define TDiO ((F2DDrawer::TwoDVertex*)NULL)
class F2DVertexBuffer : public FVertexBuffer class F2DVertexBuffer : public FSimpleVertexBuffer
{ {
uint32_t ibo_id; uint32_t ibo_id;
// Make sure we can build upon FSimpleVertexBuffer.
static_assert(&VSiO->x == &TDiO->x, "x not aligned");
static_assert(&VSiO->u == &TDiO->u, "y not aligned");
static_assert(&VSiO->color == &TDiO->color0, "color not aligned");
public: public:
F2DVertexBuffer() F2DVertexBuffer()
@ -464,24 +469,12 @@ public:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexcount * sizeof(indices[0]), indices, GL_STREAM_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexcount * sizeof(indices[0]), indices, GL_STREAM_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
void BindVBO() override void BindVBO() override
{ {
// set up the vertex buffer for drawing the 2D elements. FSimpleVertexBuffer::BindVBO();
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id);
glVertexAttribPointer(VATTR_VERTEX, 3, GL_FLOAT, false, sizeof(F2DDrawer::TwoDVertex), &TDiO->x);
glVertexAttribPointer(VATTR_TEXCOORD, 2, GL_FLOAT, false, sizeof(F2DDrawer::TwoDVertex), &TDiO->u);
glVertexAttribPointer(VATTR_COLOR, 4, GL_UNSIGNED_BYTE, true, sizeof(F2DDrawer::TwoDVertex), &TDiO->color0);
glEnableVertexAttribArray(VATTR_VERTEX);
glEnableVertexAttribArray(VATTR_TEXCOORD);
glEnableVertexAttribArray(VATTR_COLOR);
glDisableVertexAttribArray(VATTR_VERTEX2);
glDisableVertexAttribArray(VATTR_NORMAL);
} }
}; };
@ -532,11 +525,12 @@ void FGLRenderer::Draw2D(F2DDrawer *drawer)
{ {
glEnable(GL_SCISSOR_TEST); glEnable(GL_SCISSOR_TEST);
// scissor test doesn't use the current viewport for the coordinates, so use real screen coordinates // scissor test doesn't use the current viewport for the coordinates, so use real screen coordinates
glScissor( // Note that the origin here is the lower left corner!
GLRenderer->ScreenToWindowX(cmd.mScissor[0]), auto sciX = ScreenToWindowX(cmd.mScissor[0]);
GLRenderer->ScreenToWindowY(cmd.mScissor[1]), auto sciY = ScreenToWindowY(cmd.mScissor[3]);
GLRenderer->ScreenToWindowX(cmd.mScissor[2] - cmd.mScissor[0]), auto sciW = ScreenToWindowX(cmd.mScissor[2]) - sciX;
GLRenderer->ScreenToWindowY(cmd.mScissor[3] - cmd.mScissor[1])); auto sciH = ScreenToWindowY(cmd.mScissor[1]) - sciY;
glScissor(sciX, sciY, sciW, sciH);
} }
else glDisable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
@ -574,7 +568,7 @@ void FGLRenderer::Draw2D(F2DDrawer *drawer)
auto mat = FMaterial::ValidateTexture(cmd.mTexture, false); auto mat = FMaterial::ValidateTexture(cmd.mTexture, false);
if (mat == nullptr) continue; if (mat == nullptr) continue;
int gltrans = GLTranslationPalette::GetInternalTranslation(cmd.mTranslation); int gltrans = GLTranslationPalette::GetInternalTranslation(cmd.mTranslation);
gl_RenderState.SetMaterial(mat, cmd.mFlags & F2DDrawer::DTF_Wrap ? CLAMP_NONE : CLAMP_XY_NOMIP, gltrans, -1, false); gl_RenderState.SetMaterial(mat, cmd.mFlags & F2DDrawer::DTF_Wrap ? CLAMP_NONE : CLAMP_XY_NOMIP, -gltrans, -1, false);
gl_RenderState.EnableTexture(true); gl_RenderState.EnableTexture(true);
} }
else else
@ -582,8 +576,7 @@ void FGLRenderer::Draw2D(F2DDrawer *drawer)
gl_RenderState.EnableTexture(false); gl_RenderState.EnableTexture(false);
} }
gl_RenderState.Apply(); gl_RenderState.Apply();
glDrawArrays(GL_TRIANGLE_STRIP, cmd.mVertIndex, 4); glDrawElements(GL_TRIANGLES, cmd.mIndexCount, GL_UNSIGNED_INT, (const void *)(cmd.mIndexIndex * sizeof(unsigned int)));
//glDrawElements(GL_TRIANGLES, cmd.mIndexCount, GL_UNSIGNED_INT, (const void *)(cmd.mIndexIndex * sizeof(unsigned int)));
break; break;
case F2DDrawer::DrawTypeLines: case F2DDrawer::DrawTypeLines:
@ -601,9 +594,11 @@ void FGLRenderer::Draw2D(F2DDrawer *drawer)
} }
} }
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);
gl_RenderState.SetVertexBuffer(nullptr);
gl_RenderState.SetVertexBuffer(GLRenderer->mVBO);
gl_RenderState.EnableTexture(true); gl_RenderState.EnableTexture(true);
gl_RenderState.SetTextureMode(TM_MODULATE); gl_RenderState.SetTextureMode(TM_MODULATE);
gl_RenderState.ResetColor(); gl_RenderState.ResetColor();
gl_RenderState.Apply();
delete vb; delete vb;
} }

View File

@ -46,6 +46,7 @@ int F2DDrawer::AddCommand(const RenderCommand *data)
{ {
// Merge with the last command. // Merge with the last command.
mData.Last().mIndexCount += data->mIndexCount; mData.Last().mIndexCount += data->mIndexCount;
mData.Last().mVertCount += data->mVertCount;
return mData.Size(); return mData.Size();
} }
else else
@ -223,22 +224,23 @@ bool F2DDrawer::SetStyle(FTexture *tex, DrawParms &parms, PalEntry &vertexcolor,
void F2DDrawer::SetColorOverlay(PalEntry color, float alpha, PalEntry &vertexcolor, PalEntry &overlaycolor) void F2DDrawer::SetColorOverlay(PalEntry color, float alpha, PalEntry &vertexcolor, PalEntry &overlaycolor)
{ {
if (APART(color) != 0) if (color.a != 0 && (color & 0xffffff) != 0)
{ {
// overlay color uses premultiplied alpha. The alpha channel is what the main color gets multiplied with in the blending. // overlay color uses premultiplied alpha.
int a = APART(color) * 256 / 255; int a = color.a * 256 / 255;
overlaycolor.r = (color.r * a) >> 8; overlaycolor.r = (color.r * a) >> 8;
overlaycolor.g = (color.g * a) >> 8; overlaycolor.g = (color.g * a) >> 8;
overlaycolor.b = (color.b * a) >> 8; overlaycolor.b = (color.b * a) >> 8;
overlaycolor.a = 255 - color.a; overlaycolor.a = 0; // The overlay gets added on top of the texture data so to preserve the pixel's alpha this must be 0.
vertexcolor = PalEntry(int(alpha * 255), color.a, color.a, color.a);
} }
else else
{ {
overlaycolor = 0; overlaycolor = 0;
vertexcolor = PalEntry(int(alpha * 255), 255, 255, 255);
} }
// Vertex intensity is the inverse of the overlay so that the shader can do a simple addition to combine them.
uint8_t light = 255 - color.a;
vertexcolor = PalEntry(int(alpha * 255), light, light, light);
// The real color gets multiplied into vertexcolor later. // The real color gets multiplied into vertexcolor later.
} }
@ -257,7 +259,6 @@ void F2DDrawer::AddTexture(FTexture *img, DrawParms &parms)
double w = parms.destwidth; double w = parms.destwidth;
double h = parms.destheight; double h = parms.destheight;
double u1, v1, u2, v2; double u1, v1, u2, v2;
int light = 255;
PalEntry vertexcolor; PalEntry vertexcolor;
RenderCommand dg; RenderCommand dg;
@ -266,13 +267,6 @@ void F2DDrawer::AddTexture(FTexture *img, DrawParms &parms)
dg.mVertCount = 4; dg.mVertCount = 4;
dg.mTexture = img; dg.mTexture = img;
if (parms.colorOverlay && (parms.colorOverlay & 0xffffff) == 0)
{
// handle black overlays as reduced light.
light = 255 - APART(parms.colorOverlay);
parms.colorOverlay = 0;
}
dg.mTranslation = 0; dg.mTranslation = 0;
SetStyle(img, parms, vertexcolor, dg); SetStyle(img, parms, vertexcolor, dg);
@ -303,7 +297,7 @@ void F2DDrawer::AddTexture(FTexture *img, DrawParms &parms)
u2 = float(u2 - (parms.texwidth - wi) / parms.texwidth); u2 = float(u2 - (parms.texwidth - wi) / parms.texwidth);
} }
if (x < (double)parms.lclip || y < (double)parms.uclip || x + w >(double)parms.lclip || y + h >(double)parms.dclip) if (x < (double)parms.lclip || y < (double)parms.uclip || x + w >(double)parms.rclip || y + h >(double)parms.dclip)
{ {
dg.mScissor[0] = parms.lclip; dg.mScissor[0] = parms.lclip;
dg.mScissor[1] = parms.uclip; dg.mScissor[1] = parms.uclip;
@ -415,6 +409,7 @@ void F2DDrawer::AddFlatFill(int left, int top, int right, int bottom, FTexture *
dg.mTexture = src; dg.mTexture = src;
dg.mVertCount = 4; dg.mVertCount = 4;
dg.mTexture = src; dg.mTexture = src;
dg.mFlags = DTF_Wrap;
// scaling is not used here. // scaling is not used here.
if (!local_origin) if (!local_origin)
@ -484,6 +479,7 @@ void F2DDrawer::AddLine(int x1, int y1, int x2, int y2, int palcolor, uint32_t c
RenderCommand dg; RenderCommand dg;
dg.mType = DrawTypeLines; dg.mType = DrawTypeLines;
dg.mRenderStyle = LegacyRenderStyles[STYLE_Translucent];
dg.mVertCount = 2; dg.mVertCount = 2;
dg.mVertIndex = (int)mVertices.Reserve(2); dg.mVertIndex = (int)mVertices.Reserve(2);
mVertices[dg.mVertIndex].Set(x1, y1, 0, 0, 0, p); mVertices[dg.mVertIndex].Set(x1, y1, 0, 0, 0, p);
@ -505,6 +501,7 @@ void F2DDrawer::AddPixel(int x1, int y1, int palcolor, uint32_t color)
RenderCommand dg; RenderCommand dg;
dg.mType = DrawTypePoints; dg.mType = DrawTypePoints;
dg.mRenderStyle = LegacyRenderStyles[STYLE_Translucent];
dg.mVertCount = 1; dg.mVertCount = 1;
dg.mVertIndex = (int)mVertices.Reserve(1); dg.mVertIndex = (int)mVertices.Reserve(1);
mVertices[dg.mVertIndex].Set(x1, y1, 0, 0, 0, p); mVertices[dg.mVertIndex].Set(x1, y1, 0, 0, 0, p);