DEADSOFTWARE

replaced "pvs" for lights with grid
authorKetmar Dark <ketmar@ketmar.no-ip.org>
Wed, 16 Aug 2017 08:39:23 +0000 (11:39 +0300)
committerKetmar Dark <ketmar@ketmar.no-ip.org>
Wed, 16 Aug 2017 09:27:46 +0000 (12:27 +0300)
src/game/g_game.pas
src/game/g_map.pas

index 96c9856d3e91ead2ac9bff6c59cc86de6c79db8c..206d6e4d8301b561a14fb27b460731e9e78ad84c 100644 (file)
@@ -2632,7 +2632,6 @@ var
   //R: TRect;
   lln: Integer;
   lx, ly, lrad: Integer;
   //R: TRect;
   lln: Integer;
   lx, ly, lrad: Integer;
-  ltminx, ltminy, ltmaxx, ltmaxy: Integer;
 begin
   if (p = nil) or (p.FDummy) then
   begin
 begin
   if (p = nil) or (p.FDummy) then
   begin
@@ -2734,84 +2733,62 @@ begin
   g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_ACID2);
   g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_WATER);
 
   g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_ACID2);
   g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_WATER);
 
+  //TODO: lights should be in separate grid, i think
+  //      but on the other side: grid may be slower for dynlights, as their lifetime is short
   if gwin_has_stencil and (g_dynLightCount > 0) then
   begin
   if gwin_has_stencil and (g_dynLightCount > 0) then
   begin
-    // get light bounds
-    ltminx := $3fffffff;
-    ltminy := $3fffffff;
-    ltmaxx := -$3fffffff;
-    ltmaxy := -$3fffffff;
+    // setup OpenGL parameters
+    glStencilMask($FFFFFFFF);
+    glStencilFunc(GL_ALWAYS, 0, $FFFFFFFF);
+    glEnable(GL_STENCIL_TEST);
+    glEnable(GL_SCISSOR_TEST);
+    glClear(GL_STENCIL_BUFFER_BIT);
+    glStencilFunc(GL_EQUAL, 0, $ff);
 
     for lln := 0 to g_dynLightCount-1 do
     begin
 
     for lln := 0 to g_dynLightCount-1 do
     begin
-      lx := g_dynLights[lln].x-sX;
-      ly := g_dynLights[lln].y-sY;
+      lx := g_dynLights[lln].x;
+      ly := g_dynLights[lln].y;
       lrad := g_dynLights[lln].radius;
       if lrad < 3 then continue;
 
       lrad := g_dynLights[lln].radius;
       if lrad < 3 then continue;
 
-      if lx+lrad < 0 then continue;
-      if ly+lrad < 0 then continue;
-      if lx-lrad >= gPlayerScreenSize.X then continue;
-      if ly-lrad >= gPlayerScreenSize.Y then continue;
-
-      lx := lx+sX;
-      ly := ly+sY;
-
-      if ltminx > lx-lrad then ltminx := lx-lrad;
-      if ltminy > ly-lrad then ltminy := ly-lrad;
-      if ltmaxx < lx+lrad then ltmaxx := lx+lrad;
-      if ltmaxy < ly+lrad then ltmaxy := ly+lrad;
-    end;
-
-    if g_Map_BuildPLP(ltminx, ltminy, ltmaxx, ltmaxy) then
-    begin
-      // setup OpenGL parameters
-      glStencilMask($FFFFFFFF);
-      glStencilFunc(GL_ALWAYS, 0, $FFFFFFFF);
-      glEnable(GL_STENCIL_TEST);
-      glEnable(GL_SCISSOR_TEST);
-      glClear(GL_STENCIL_BUFFER_BIT);
-      glStencilFunc(GL_EQUAL, 0, $ff);
-
-      for lln := 0 to g_dynLightCount-1 do
-      begin
-        lx := g_dynLights[lln].x;
-        ly := g_dynLights[lln].y;
-        lrad := g_dynLights[lln].radius;
-        if lrad < 3 then continue;
-        // set scissor to optimize drawing
-        glScissor((lx-sX)-lrad+2, gPlayerScreenSize.Y-(ly-sY)-lrad-1+2, lrad*2-4, lrad*2-4);
-        // no need to clear stencil buffer, light blitting will do it for us
-        glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
-        // draw extruded panels
-        glDisable(GL_TEXTURE_2D);
-        glDisable(GL_BLEND);
-        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // no need to modify color buffer
-        if (lrad > 4) then g_Map_DrawPanelShadowVolumes(lx, ly, lrad);
-        // render light texture
-        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // modify color buffer
-        glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); // draw light, and clear stencil buffer
-        // blend it
-        glEnable(GL_BLEND);
-        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-        glEnable(GL_TEXTURE_2D);
-        // color and opacity
-        glColor4f(g_dynLights[lln].r, g_dynLights[lln].g, g_dynLights[lln].b, g_dynLights[lln].a);
-        glBindTexture(GL_TEXTURE_2D, g_Texture_Light());
-        glBegin(GL_QUADS);
-          glTexCoord2f(0.0, 0.0); glVertex2i(lx-lrad, ly-lrad); // top-left
-          glTexCoord2f(1.0, 0.0); glVertex2i(lx+lrad, ly-lrad); // top-right
-          glTexCoord2f(1.0, 1.0); glVertex2i(lx+lrad, ly+lrad); // bottom-right
-          glTexCoord2f(0.0, 1.0); glVertex2i(lx-lrad, ly+lrad); // bottom-left
-        glEnd();
-      end;
-
-      // done
-      glDisable(GL_STENCIL_TEST);
+      if lx-sX+lrad < 0 then continue;
+      if ly-sY+lrad < 0 then continue;
+      if lx-sX-lrad >= gPlayerScreenSize.X then continue;
+      if ly-sY-lrad >= gPlayerScreenSize.Y then continue;
+
+      // set scissor to optimize drawing
+      glScissor((lx-sX)-lrad+2, gPlayerScreenSize.Y-(ly-sY)-lrad-1+2, lrad*2-4, lrad*2-4);
+      // no need to clear stencil buffer, light blitting will do it for us
+      glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
+      // draw extruded panels
+      glDisable(GL_TEXTURE_2D);
       glDisable(GL_BLEND);
       glDisable(GL_BLEND);
-      glDisable(GL_SCISSOR_TEST);
-      glScissor(0, 0, sWidth, sHeight);
+      glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // no need to modify color buffer
+      if (lrad > 4) then g_Map_DrawPanelShadowVolumes(lx, ly, lrad);
+      // render light texture
+      glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // modify color buffer
+      glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); // draw light, and clear stencil buffer
+      // blend it
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      glEnable(GL_TEXTURE_2D);
+      // color and opacity
+      glColor4f(g_dynLights[lln].r, g_dynLights[lln].g, g_dynLights[lln].b, g_dynLights[lln].a);
+      glBindTexture(GL_TEXTURE_2D, g_Texture_Light());
+      glBegin(GL_QUADS);
+        glTexCoord2f(0.0, 0.0); glVertex2i(lx-lrad, ly-lrad); // top-left
+        glTexCoord2f(1.0, 0.0); glVertex2i(lx+lrad, ly-lrad); // top-right
+        glTexCoord2f(1.0, 1.0); glVertex2i(lx+lrad, ly+lrad); // bottom-right
+        glTexCoord2f(0.0, 1.0); glVertex2i(lx-lrad, ly+lrad); // bottom-left
+      glEnd();
     end;
     end;
+
+    // done
+    glDisable(GL_STENCIL_TEST);
+    glDisable(GL_BLEND);
+    glDisable(GL_SCISSOR_TEST);
+    glScissor(0, 0, sWidth, sHeight);
   end;
 
   g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_FORE);
   end;
 
   g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_FORE);
index 730812cdd2e16113909fb57d2e888277aef0a807..b2e35c0eb0b3044abc5308cd98354f286651a9c8 100644 (file)
@@ -89,8 +89,6 @@ function g_Map_PanelForPID(PanelID: Integer; var PanelArrayID: Integer): PPanel;
 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
 
 procedure g_Map_SaveState(Var Mem: TBinMemoryWriter);
 procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
 
-// build "possibly lit panels" index, so we can avoid looping over all level panels again and again
-function g_Map_BuildPLP (ltminx, ltminy, ltmaxx, ltmaxy: Integer): Boolean; // returns `false` if no patels lit
 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
 
 const
 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
 
 const
@@ -1912,67 +1910,23 @@ begin
   end;
 end;
 
   end;
 end;
 
-var
-  plpset: array of Integer = nil; // potentially lit panels
-  plpcount: Integer; // to avoid constant reallocations
-
-function g_Map_BuildPLP (ltminx, ltminy, ltmaxx, ltmaxy: Integer): Boolean;
-var
-  idx: Integer;
-  panels: TPanelArray;
-begin
-  panels := gWalls;
-  plpcount := 0;
-  if (ltminx < ltmaxx) and (ltminy < ltmaxy) then
-  begin
-    if panels <> nil then
-    begin
-      for idx := 0 to High(panels) do
-      begin
-        if (panels[idx].Width < 1) or (panels[idx].Height < 1) then continue;
-        if (panels[idx].X+panels[idx].Width <= ltminx) then continue;
-        if (panels[idx].Y+panels[idx].Height <= ltminy) then continue;
-        if (panels[idx].X > ltmaxx) then continue;
-        if (panels[idx].Y > ltmaxy) then continue;
-        if plpcount = length(plpset) then SetLength(plpset, plpcount+32768);
-        plpset[plpcount] := idx;
-        Inc(plpcount);
-      end;
-      //e_WriteLog(Format('%d panels left out of %d', [plpcount, Length(panels)]), MSG_NOTIFY);
-    end;
-  end;
-  result := (plpcount > 0);
-end;
 
 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
 
 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
-
-  (* old
-  procedure drawPanels (var panels: TPanelArray);
+  function qq (obj: TObject; tag: Integer): Boolean;
   var
   var
-    a: Integer;
+    pan: TPanel;
   begin
   begin
-    if panels <> nil then
-    begin
-      for a := 0 to High(panels) do
-      begin
-        panels[a].DrawShadowVolume(lightX, lightY, radius);
-      end;
-    end;
+    result := false; // don't stop, ever
+    if (tag <> PANEL_WALL) then exit; // only walls
+    pan := (obj as TPanel);
+    pan.DrawShadowVolume(lightX, lightY, radius);
   end;
   end;
-  *)
-var
-  idx: Integer;
+
 begin
 begin
-  (*
-  drawPanels(gWalls);
-  //drawPanels(gRenderForegrounds);
-  *)
-  for idx := 0 to plpcount-1 do
-  begin
-    gWalls[plpset[idx]].DrawShadowVolume(lightX, lightY, radius);
-  end;
+  gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, qq);
 end;
 
 end;
 
+
 procedure g_Map_DrawBack(dx, dy: Integer);
 begin
   if gDrawBackGround and (BackID <> DWORD(-1)) then
 procedure g_Map_DrawBack(dx, dy: Integer);
 begin
   if gDrawBackGround and (BackID <> DWORD(-1)) then