Skip to content

Commit

Permalink
lighting: move lightmap data in lightdata.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlj committed Dec 31, 2023
1 parent 0681e26 commit 503062e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 40 deletions.
32 changes: 32 additions & 0 deletions lib/ivis_opengl/pielighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@
#include <functional>
#include "culling.h"
#include "src/profiling.h"
#include "lib/framework/debug.h"


PIELIGHT& LightMap::operator()(int32_t x, int32_t y)
{
// Clamp x and y values to actual ones
// Give one tile worth of leeway before asserting, for units/transporters coming in from off-map.
ASSERT(x >= -1, "mapTile: x value is too small (%d,%d) in %dx%d", x, y, mapWidth, mapHeight);
ASSERT(y >= -1, "mapTile: y value is too small (%d,%d) in %dx%d", x, y, mapWidth, mapHeight);
x = MAX(x, 0);
y = MAX(y, 0);
ASSERT(x < mapWidth + 1, "mapTile: x value is too big (%d,%d) in %dx%d", x, y, mapWidth, mapHeight);
ASSERT(y < mapHeight + 1, "mapTile: y value is too big (%d,%d) in %dx%d", x, y, mapWidth, mapHeight);
x = MIN(x, mapWidth - 1);
y = MIN(y, mapHeight - 1);

return data[x + (y * mapWidth)];
}

PIELIGHT& LightMap::operator()(Vector2i const& v)
{
return operator()(v.x, v.y);
}

void LightMap::reset(size_t width, size_t height)
{
mapWidth = static_cast<int32_t>(width);
mapHeight = static_cast<int32_t>(height);
data = std::make_unique<PIELIGHT[]>(width * height);
ASSERT(data != nullptr, "Out of memory");
}


static LightingData scene;

Expand Down
14 changes: 14 additions & 0 deletions lib/ivis_opengl/pielighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,23 @@ struct LIGHT
PIELIGHT colour;
};


struct LightMap
{
PIELIGHT& operator()(int32_t x, int32_t y);
PIELIGHT& operator()(Vector2i const& v);
void reset(size_t width, size_t height);
private:
std::unique_ptr<PIELIGHT[]> data = nullptr;
int32_t mapWidth;
int32_t mapHeight;
};

struct LightingData
{
std::vector<LIGHT> lights;
LightMap lightmap;

};

LightingData& getCurrentLightingData();
Expand Down
2 changes: 1 addition & 1 deletion src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,7 @@ void dealWithLMB()
tileIsExplored(psTile) ? "Explored" : "Unexplored",
mouseTileX, mouseTileY, world_coord(mouseTileX), world_coord(mouseTileY),
(int)psTile->limitedContinent, (int)psTile->hoverContinent, psTile->level, (int)psTile->illumination,
(int)psTile->ambientOcclusion, mapTileColor(mouseTileX, mouseTileY).rgba,
(int)psTile->ambientOcclusion, getCurrentLightingData().lightmap(mouseTileX, mouseTileY).rgba,
aux & AUXBITS_DANGER ? "danger" : "", aux & AUXBITS_THREAT ? "threat" : "",
(int)psTile->watchers[selectedPlayer], (int)psTile->sensors[selectedPlayer], (int)psTile->jammers[selectedPlayer],
TileNumber_tile(psTile->texture), (TILE_HAS_DECAL(psTile)) ? "y" : "n",
Expand Down
2 changes: 1 addition & 1 deletion src/display3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ static void drawTiles(iView *player)

pos.y = map_TileHeight(playerXTile + j, playerZTile + i);
auto color = pal_SetBrightness((currTerrainShaderType == TerrainShaderType::SINGLE_PASS) ? 0 : static_cast<UBYTE>(psTile->level));
mapTileColor(playerXTile + j, playerZTile + i) = color;
getCurrentLightingData().lightmap(playerXTile + j, playerZTile + i) = color;
}
tileScreenInfo[idx][jdx].z = pie_RotateProjectWithPerspective(&pos, tileCalcPerspectiveViewMatrix, &screen);
tileScreenInfo[idx][jdx].x = screen.x;
Expand Down
4 changes: 2 additions & 2 deletions src/lighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ static void calcTileIllum(UDWORD tileX, UDWORD tileY)

static void colourTile(SDWORD xIndex, SDWORD yIndex, PIELIGHT light_colour, double fraction)
{
PIELIGHT colour = mapTileColor(xIndex, yIndex);
PIELIGHT colour = getCurrentLightingData().lightmap(xIndex, yIndex);
colour.byte.r = static_cast<uint8_t>(MIN(255, colour.byte.r + light_colour.byte.r * fraction));
colour.byte.g = static_cast<uint8_t>(MIN(255, colour.byte.g + light_colour.byte.g * fraction));
colour.byte.b = static_cast<uint8_t>(MIN(255, colour.byte.b + light_colour.byte.b * fraction));
mapTileColor(xIndex, yIndex) = colour;
getCurrentLightingData().lightmap(xIndex, yIndex) = colour;
}

void rendering1999::LightingManager::ComputeFrameData(const LightingData& data, const glm::mat4& worldViewProjectionMatrix)
Expand Down
4 changes: 2 additions & 2 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "fpath.h"
#include "levels.h"
#include "lib/framework/wzapp.h"
#include "lib/ivis_opengl/pielighting.h"

#define GAME_TICKS_FOR_DANGER (GAME_TICKS_PER_SEC * 2)

Expand Down Expand Up @@ -111,7 +112,6 @@ struct GATEWAY_SAVE
/* The size and contents of the map */
SDWORD mapWidth = 0, mapHeight = 0;
std::unique_ptr<MAPTILE[]> psMapTiles;
Component< PIELIGHT> mapTileColor;
std::unique_ptr<uint8_t[]> psBlockMap[AUX_MAX];
std::unique_ptr<uint8_t[]> psAuxMap[MAX_PLAYERS + AUX_MAX]; // yes, we waste one element... eyes wide open... makes API nicer

Expand Down Expand Up @@ -1013,7 +1013,7 @@ bool mapLoadFromWzMapData(std::shared_ptr<WzMap::MapData> loadedMap)

/* Allocate the memory for the map */
psMapTiles = std::make_unique<MAPTILE[]>(static_cast<size_t>(width) * height);
mapTileColor.reset(width, height);
getCurrentLightingData().lightmap.reset(width, height);
ASSERT(psMapTiles != nullptr, "Out of memory");

mapWidth = width;
Expand Down
33 changes: 0 additions & 33 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,42 +81,9 @@ struct MAPTILE
/* The size and contents of the map */
extern SDWORD mapWidth, mapHeight;

template<typename T>
struct Component
{
auto& operator()(int32_t x, int32_t y)
{
// Clamp x and y values to actual ones
// Give one tile worth of leeway before asserting, for units/transporters coming in from off-map.
ASSERT(x >= -1, "mapTile: x value is too small (%d,%d) in %dx%d", x, y, mapWidth, mapHeight);
ASSERT(y >= -1, "mapTile: y value is too small (%d,%d) in %dx%d", x, y, mapWidth, mapHeight);
x = MAX(x, 0);
y = MAX(y, 0);
ASSERT(x < mapWidth + 1, "mapTile: x value is too big (%d,%d) in %dx%d", x, y, mapWidth, mapHeight);
ASSERT(y < mapHeight + 1, "mapTile: y value is too big (%d,%d) in %dx%d", x, y, mapWidth, mapHeight);
x = MIN(x, mapWidth - 1);
y = MIN(y, mapHeight - 1);

return data[x + (y * mapWidth)];
}

auto& operator()(Vector2i const& v)
{
return operator()(v.x, v.y);
}

void reset(size_t width, size_t height)
{
data = std::make_unique<T[]>(width * height);
ASSERT(data != nullptr, "Out of memory");
}
private:
std::unique_ptr<T[]> data; // component like in Entity Component System
};


extern std::unique_ptr<MAPTILE[]> psMapTiles;
extern Component< PIELIGHT> mapTileColor;
extern float waterLevel;
extern char *tilesetDir;
extern MAP_TILESET currentMapTileset;
Expand Down
2 changes: 1 addition & 1 deletion src/terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@ static void updateLightMap()
for (int i = 0; i < mapWidth; ++i)
{
MAPTILE *psTile = mapTile(i, j);
PIELIGHT colour = mapTileColor(i, j);
PIELIGHT colour = getCurrentLightingData().lightmap(i, j);
UBYTE level = static_cast<UBYTE>(psTile->level);

if (psTile->tileInfoBits & BITS_GATEWAY && showGateways)
Expand Down

0 comments on commit 503062e

Please sign in to comment.