From 7746b2c6fd4d6315556cae539f16e5426c04dfd6 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Tue, 6 Aug 2019 07:15:07 +0200 Subject: [PATCH] - add light array --- .../polyrenderer/drawers/poly_triangle.cpp | 13 --- .../polyrenderer/drawers/poly_triangle.h | 4 +- .../polyrenderer/drawers/screen_triangle.cpp | 81 ++++++++++++++++++- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/src/rendering/polyrenderer/drawers/poly_triangle.cpp b/src/rendering/polyrenderer/drawers/poly_triangle.cpp index 6a2aff9b3..650784485 100644 --- a/src/rendering/polyrenderer/drawers/poly_triangle.cpp +++ b/src/rendering/polyrenderer/drawers/poly_triangle.cpp @@ -340,19 +340,6 @@ void PolyTriangleThreadData::PushStreamData(const StreamData &data, const PolyPu } } drawargs.SetLights(polyLights, numLights); - -#if 0 - FColormap cm; - cm.Clear(); - if (constants.uLightLevel >= 0.0f) - { - drawargs.SetLight(GetColorTable(cm), (int)(constants.uLightLevel * 255.0f), mainVertexShader.Viewpoint->mGlobVis * 32.0f, false); - } - else - { - drawargs.SetLight(GetColorTable(cm), 255, mainVertexShader.Viewpoint->mGlobVis * 32.0f, true); - } -#endif } void PolyTriangleThreadData::PushMatrices(const VSMatrix &modelMatrix, const VSMatrix &normalModelMatrix, const VSMatrix &textureMatrix) diff --git a/src/rendering/polyrenderer/drawers/poly_triangle.h b/src/rendering/polyrenderer/drawers/poly_triangle.h index 4bf2e4a1c..619cf6c16 100644 --- a/src/rendering/polyrenderer/drawers/poly_triangle.h +++ b/src/rendering/polyrenderer/drawers/poly_triangle.h @@ -207,10 +207,8 @@ public: float WorldY[MAXWIDTH]; float WorldZ[MAXWIDTH]; uint32_t FragColor[MAXWIDTH]; -#if 0 uint16_t lightarray[MAXWIDTH]; - uint32_t dynlights[MAXWIDTH]; -#endif + //uint32_t dynlights[MAXWIDTH]; } scanline; static PolyTriangleThreadData *Get(DrawerThread *thread); diff --git a/src/rendering/polyrenderer/drawers/screen_triangle.cpp b/src/rendering/polyrenderer/drawers/screen_triangle.cpp index 83dccffc5..ddbb5ce2c 100644 --- a/src/rendering/polyrenderer/drawers/screen_triangle.cpp +++ b/src/rendering/polyrenderer/drawers/screen_triangle.cpp @@ -54,6 +54,69 @@ static void WriteW(int y, int x0, int x1, const TriDrawTriangleArgs* args, PolyT } } +static void WriteLightArray(int y, int x0, int x1, const TriDrawTriangleArgs* args, PolyTriangleThreadData* thread) +{ + float startX = x0 + (0.5f - args->v1->x); + float startY = y + (0.5f - args->v1->y); + float posW = args->v1->w + args->gradientX.W * startX + args->gradientY.W * startY; + float stepW = args->gradientX.W; + + float globVis = thread->mainVertexShader.Viewpoint->mGlobVis; + + uint32_t light = (int)(thread->PushConstants->uLightLevel * 255.0f); + fixed_t shade = (fixed_t)((2.0f - (light + 12.0f) / 128.0f) * (float)FRACUNIT); + fixed_t lightpos = (fixed_t)(globVis * posW * (float)FRACUNIT); + fixed_t lightstep = (fixed_t)(globVis * stepW * (float)FRACUNIT); + + fixed_t maxvis = 24 * FRACUNIT / 32; + fixed_t maxlight = 31 * FRACUNIT / 32; + + uint16_t *lightarray = thread->scanline.lightarray; + + fixed_t lightend = lightpos + lightstep * (x1 - x0); + if (lightpos < maxvis && shade >= lightpos && shade - lightpos <= maxlight && + lightend < maxvis && shade >= lightend && shade - lightend <= maxlight) + { + //if (BitsPerPixel == 32) + { + lightpos += FRACUNIT - shade; + for (int x = x0; x < x1; x++) + { + lightarray[x] = lightpos >> 8; + lightpos += lightstep; + } + } + /*else + { + lightpos = shade - lightpos; + for (int x = x0; x < x1; x++) + { + lightarray[x] = (lightpos >> 3) & 0xffffff00; + lightpos -= lightstep; + } + }*/ + } + else + { + //if (BitsPerPixel == 32) + { + for (int x = x0; x < x1; x++) + { + lightarray[x] = (FRACUNIT - clamp(shade - MIN(maxvis, lightpos), 0, maxlight)) >> 8; + lightpos += lightstep; + } + } + /*else + { + for (int x = x0; x < x1; x++) + { + lightarray[x] = (clamp(shade - MIN(maxvis, lightpos), 0, maxlight) >> 3) & 0xffffff00; + lightpos += lightstep; + } + }*/ + } +} + static void WriteVarying(float pos, float step, int x0, int x1, const float* w, float* varying) { for (int x = x0; x < x1; x++) @@ -450,13 +513,29 @@ static void RunShader(int x0, int x1, PolyTriangleThreadData* thread) if (constants->uLightLevel >= 0.0f) { - // To do: apply diminishing light and fog + uint16_t* lightarray = thread->scanline.lightarray; + for (int x = x0; x < x1; x++) + { + uint32_t fg = fragcolor[x]; + int lightshade = lightarray[x]; + fragcolor[x] = MAKEARGB( + APART(fg), + (RPART(fg) * lightshade) >> 8, + (GPART(fg) * lightshade) >> 8, + (BPART(fg) * lightshade) >> 8); + } + + // To do: apply fog } } static void DrawSpan(int y, int x0, int x1, const TriDrawTriangleArgs* args, PolyTriangleThreadData* thread) { WriteVaryings(y, x0, x1, args, thread); + + if (thread->PushConstants->uLightLevel >= 0.0f) + WriteLightArray(y, x0, x1, args, thread); + RunShader(x0, x1, thread); if (thread->drawargs.WriteColor())