From 2249a689b020a4e0d2860e58eafde84010e8f3f4 Mon Sep 17 00:00:00 2001
From: Ketmar Dark <ketmar@ketmar.no-ip.org>
Date: Tue, 15 Aug 2017 09:07:33 +0300
Subject: [PATCH] light: use proper scissoring instead of clearing the whole
 stencil buffer for each light

---
 src/game/g_game.pas | 26 ++++++++++++++------------
 src/game/g_map.pas  |  2 +-
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/src/game/g_game.pas b/src/game/g_game.pas
index c56bc48..6937983 100644
--- a/src/game/g_game.pas
+++ b/src/game/g_game.pas
@@ -2736,12 +2736,6 @@ begin
   g_Map_DrawPanels(PANEL_ACID1);
   g_Map_DrawPanels(PANEL_ACID2);
   g_Map_DrawPanels(PANEL_WATER);
-  g_Map_DrawPanels(PANEL_FORE);
-  if g_debug_HealthBar then
-  begin
-    g_Monsters_DrawHealth();
-    g_Player_DrawHealth();
-  end;
 
   if gwin_has_stencil and (g_dynLightCount > 0) then
   begin
@@ -2749,8 +2743,8 @@ begin
     glStencilMask($FFFFFFFF);
     glStencilFunc(GL_ALWAYS, 0, $FFFFFFFF);
     glEnable(GL_STENCIL_TEST);
-    //!glEnable(GL_SCISSOR_TEST);
-    //glClear(GL_STENCIL_BUFFER_BIT);
+    glEnable(GL_SCISSOR_TEST);
+    glClear(GL_STENCIL_BUFFER_BIT);
     glStencilFunc(GL_EQUAL, 0, $ff);
 
     for lln := 0 to g_dynLightCount-1 do
@@ -2758,11 +2752,11 @@ begin
       lx := g_dynLights[lln].x;
       ly := g_dynLights[lln].y;
       lrad := g_dynLights[lln].radius;
-      if lrad < 2 then continue;
+      if lrad < 3 then continue;
       // set scissor to optimize drawing
-      //!glScissor((lx-cameraOfsX)-lrad+2, v_height-(ly-cameraOfsY)-lrad-1+2, lrad*2-4, lrad*2-4);
+      glScissor((lx-sX)-lrad+2, gPlayerScreenSize.Y-(ly-sY)-lrad-1+2, lrad*2-4, lrad*2-4);
       // clear stencil buffer
-      glClear(GL_STENCIL_BUFFER_BIT); //!!!
+      //glClear(GL_STENCIL_BUFFER_BIT); //!!!
       glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
       // draw extruded panels
       glDisable(GL_TEXTURE_2D);
@@ -2787,11 +2781,19 @@ begin
         glTexCoord2f(0.0, 1.0); glVertex2i(lx-lrad, ly+lrad); // bottom-left
       glEnd();
     end;
+
     // done
     glDisable(GL_STENCIL_TEST);
     glDisable(GL_BLEND);
     glDisable(GL_SCISSOR_TEST);
-    //!glScissor((lx-cameraOfsX)-radius+2, v_height-(ly-cameraOfsY)-radius-1+2, radius*2-4, radius*2-4);
+    glScissor(0, 0, sWidth, sHeight);
+  end;
+
+  g_Map_DrawPanels(PANEL_FORE);
+  if g_debug_HealthBar then
+  begin
+    g_Monsters_DrawHealth();
+    g_Player_DrawHealth();
   end;
 
   if p.FSpectator then
diff --git a/src/game/g_map.pas b/src/game/g_map.pas
index e080bca..aceb7e3 100644
--- a/src/game/g_map.pas
+++ b/src/game/g_map.pas
@@ -1811,7 +1811,7 @@ procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius:
 
 begin
   drawPanels(gWalls);
-  drawPanels(gRenderForegrounds);
+  //drawPanels(gRenderForegrounds);
 end;
 
 procedure g_Map_DrawBack(dx, dy: Integer);
-- 
2.29.2