Skip to content

Commit

Permalink
Possible approach for tile map rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-ushakov committed Jun 6, 2024
1 parent 56721f9 commit b5f28dc
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 50 deletions.
45 changes: 33 additions & 12 deletions Source/Render/tilemap/TileMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ cTileMap::cTileMap(cScene* pScene,TerraInterface* terra_) : cUnkObj(KIND_TILEMAP
TexturePoolSize = 512;

tilesize.set(0,0,0);
pTileMapRender=NULL;

ShadowDrawNode=pScene->CreateCamera();
LightDrawNode=new cCameraPlanarLight(pScene);
Expand All @@ -199,7 +198,11 @@ cTileMap::~cTileMap()
gb_RenderDevice->DeleteTilemap(this);
if(Tile) { delete [] Tile; Tile=nullptr; }
MTDONE(lock_update_rect);
xassert(pTileMapRender == nullptr);

for (auto& p : pTileMapRender)
{
xassert(p == nullptr);
}
}

int cTileMap::CheckLightMapType()
Expand Down Expand Up @@ -313,18 +316,27 @@ void cTileMap::PreDraw(cCamera *DrawNode)

DrawNode->Attach(SCENENODE_OBJECT_TILEMAP,this);

cTileMapRender* render = GetTilemapRender();
if (render) {
render->PreDraw(DrawNode);
}
for (auto& render : pTileMapRender)
{
if (render) {
render->PreDraw(DrawNode);
}
}

for (int y=0; y < GetTileNumber().y; y++) {
for (int x = 0; x < GetTileNumber().x; x++) {
auto& Tile = GetTile(x, y);
Tile.ClearAttribute(ATTRTILE_UPDATELOD);
}
}
}

void cTileMap::Draw(cCamera *DrawNode)
{
if(!Option_ShowType[SHOW_TILEMAP])
return;

cTileMapRender* render = GetTilemapRender();
cTileMapRender* render = GetTilemapRender(RenderType::DIRECT);
if (!render) return;

if(DrawNode->GetAttribute(ATTRCAMERA_SHADOW))
Expand All @@ -334,14 +346,23 @@ void cTileMap::Draw(cCamera *DrawNode)
else if(DrawNode->GetAttribute(ATTRCAMERA_SHADOWMAP))
{
if(Option_ShadowType==SHADOW_MAP_SELF) {
render->DrawBump(DrawNode, ALPHA_TEST, TILEMAP_ALL, true);
cTileMapRender* shadowRender = GetTilemapRender(RenderType::SHADOW);
if (shadowRender)
{
shadowRender->DrawBump(DrawNode, ALPHA_TEST, TILEMAP_ALL, true);
}
}
}
else if(DrawNode->GetAttribute(ATTRCAMERA_REFLECTION))
{ // рисовать отражение
gb_RenderDevice->SetRenderState(RS_ALPHA_TEST_MODE, ALPHATEST_GT_254/*GetRefSurface()*/);
render->DrawBump(DrawNode, ALPHA_TEST, TILEMAP_NOZEROPLAST, false);
gb_RenderDevice->SetRenderState(RS_ALPHA_TEST_MODE, ALPHATEST_GT_0);
{
// рисовать отражение
cTileMapRender* reflectionRender = GetTilemapRender(RenderType::REFLECTION);
if (reflectionRender)
{
gb_RenderDevice->SetRenderState(RS_ALPHA_TEST_MODE, ALPHATEST_GT_254/*GetRefSurface()*/);
reflectionRender->DrawBump(DrawNode, ALPHA_TEST, TILEMAP_NOZEROPLAST, false);
gb_RenderDevice->SetRenderState(RS_ALPHA_TEST_MODE, ALPHATEST_GT_0);
}
}else
{
if(GetAttribute(ATTRUNKOBJ_REFLECTION)) {
Expand Down
37 changes: 27 additions & 10 deletions Source/Render/tilemap/TileMap.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef PERIMETER_TILEMAP_H
#define PERIMETER_TILEMAP_H

#include <array>

class cScene;

typedef std::vector<Vect2s> Vect2sVect;
Expand Down Expand Up @@ -30,12 +32,7 @@ struct sTile : public sAttribute
zmin=255;zmax=0;
}

inline int GetDraw() { return GetAttribute(ATTRTILE_DRAWLOD); }
inline int GetUpdate() { return GetAttribute(ATTRTILE_UPDATELOD); }
inline void SetDraw() { SetAttribute(ATTRTILE_DRAWLOD); }

inline void ClearDraw() { ClearAttribute(ATTRTILE_DRAWLOD); }
inline void ClearUpdate() { ClearAttribute(ATTRTILE_UPDATELOD); }
};

typedef std::vector<std::vector<Vect2s>* > CurrentRegion;
Expand All @@ -45,6 +42,18 @@ class cTileMapRender;
class Column;
class cTileMap : public cUnkObj
{
public:
enum class RenderType
{
REFLECTION, SHADOW, DIRECT
};
static inline const std::array<RenderType, 3> RenderTypes{
RenderType::REFLECTION,
RenderType::SHADOW,
RenderType::DIRECT
};

private:
friend class cScene;

sTile* Tile;
Expand All @@ -54,7 +63,7 @@ class cTileMap : public cUnkObj

Vect3d tilesize;

cTileMapRender* pTileMapRender = nullptr;
std::array<cTileMapRender*, RenderTypes.size()> pTileMapRender{};

cCamera* ShadowDrawNode;
cCamera* LightDrawNode;
Expand Down Expand Up @@ -119,11 +128,19 @@ class cTileMap : public cUnkObj
zeroplast_color[player]=color;
}

void SetTilemapRender(cTileMapRender* p) {
VISASSERT(p == nullptr || pTileMapRender == nullptr);
pTileMapRender=p;
void SetTilemapRender(RenderType type, cTileMapRender* p)
{
const auto index = static_cast<size_t>(type);
VISASSERT(index < pTileMapRender.size());
VISASSERT(p == nullptr || pTileMapRender[index] == nullptr);
pTileMapRender[index] = p;
};
cTileMapRender* GetTilemapRender(){return pTileMapRender;}
cTileMapRender* GetTilemapRender(RenderType type)
{
const auto index = static_cast<size_t>(type);
VISASSERT(index < pTileMapRender.size());
return pTileMapRender[index];
}

TerraInterface* GetTerra(){return terra;}

Expand Down
14 changes: 7 additions & 7 deletions Source/Render/tilemap/TileMapBumpTile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ float sBumpTile::SetVertexZ(TerraInterface* terra,int x,int y)
return zi;
}

sBumpTile::sBumpTile(cTileMap* tilemap, cTilemapTexturePool* pool, int lod, int xpos, int ypos)
sBumpTile::sBumpTile(cTileMap* tilemap, cTileMapRender* render, cTilemapTexturePool* pool, int lod, int xpos, int ypos)
{
this->tilemap = tilemap;
cTileMapRender* render = tilemap->GetTilemapRender();
this->render = render;

tile_pos.set(xpos,ypos);
texPool = pool;
texPage = texPool->allocPage();
Expand All @@ -45,7 +46,7 @@ sBumpTile::sBumpTile(cTileMap* tilemap, cTilemapTexturePool* pool, int lod, int

sBumpTile::~sBumpTile()
{
tilemap->GetTilemapRender()->GetVertexPool()->DeletePage(vtx);
render->GetVertexPool()->DeletePage(vtx);
texPool->freePage(texPage);

DeleteIndex();
Expand All @@ -55,7 +56,7 @@ void sBumpTile::DeleteIndex()
{
for (auto& i : index) {
if (i.index.page >= 0) {
tilemap->GetTilemapRender()->GetIndexPool()->DeletePage(i.index);
render->GetIndexPool()->DeletePage(i.index);
}
}
index.clear();
Expand All @@ -68,7 +69,7 @@ uint8_t* sBumpTile::LockTex(int& Pitch)

uint8_t *sBumpTile::LockVB()
{
return static_cast<uint8_t*>(tilemap->GetTilemapRender()->GetVertexPool()->LockPage(vtx));
return static_cast<uint8_t*>(render->GetVertexPool()->LockPage(vtx));
}

void sBumpTile::UnlockTex()
Expand All @@ -78,7 +79,7 @@ void sBumpTile::UnlockTex()

void sBumpTile::UnlockVB()
{
tilemap->GetTilemapRender()->GetVertexPool()->UnlockPage(vtx);
render->GetVertexPool()->UnlockPage(vtx);
}

inline int IUCLAMP(int val,int clamp)
Expand Down Expand Up @@ -127,7 +128,6 @@ void sBumpTile::CalcPoint()
Column** columns = tilemap->GetColumn();
Vect2i pos=tile_pos;

cTileMapRender* render = tilemap->GetTilemapRender();
render->IncUpdate(this);

int tilenumber = tilemap->GetZeroplastNumber();
Expand Down
3 changes: 2 additions & 1 deletion Source/Render/tilemap/TileMapBumpTile.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct sBumpTile
int age, LOD;

class cTileMap *tilemap;
cTileMapRender* render;
class cTilemapTexturePool* texPool;
int texPage = 0;

Expand All @@ -70,7 +71,7 @@ struct sBumpTile
protected:
float vStart, vStep, uStart, uStep;
public:
sBumpTile(cTileMap* TileMap, cTilemapTexturePool* pool, int lod, int xpos, int ypos);
sBumpTile(cTileMap* TileMap, cTileMapRender* render, cTilemapTexturePool* pool, int lod, int xpos, int ypos);
~sBumpTile();
uint8_t* LockTex(int& Pitch);
uint8_t* LockVB();
Expand Down
51 changes: 31 additions & 20 deletions Source/Render/tilemap/TileMapRender.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <array>
#include "StdAfxRD.h"
#include "PoolManager.h"
#include "TileMap.h"
Expand All @@ -12,20 +13,27 @@

int cInterfaceRenderDevice::CreateTilemap(cTileMap *TileMap)
{
cTileMapRender* p = new cTileMapRender(TileMap);
TileMap->SetTilemapRender(p);
p->RestoreTilemapPool();
for (auto type : cTileMap::RenderTypes)
{
cTileMapRender* p = new cTileMapRender(TileMap);
TileMap->SetTilemapRender(type, p);
p->RestoreTilemapPool();
}

return 0;
}

int cInterfaceRenderDevice::DeleteTilemap(cTileMap *TileMap)
{
cTileMapRender* p = TileMap->GetTilemapRender();
for (auto type : cTileMap::RenderTypes)
{
cTileMapRender* p = TileMap->GetTilemapRender(type);

if (p) {
p->ClearTilemapPool();
TileMap->SetTilemapRender(nullptr);
delete p;
if (p) {
p->ClearTilemapPool();
TileMap->SetTilemapRender(type, nullptr);
delete p;
}
}

return true;
Expand All @@ -41,6 +49,8 @@ cTileMapRender::cTileMapRender(cTileMap *pTileMap)
for(int i=0;i<dxy;i++)
vis_lod[i]=-1;

renderTiles.resize(dxy);

update_stat=NULL;
// update_stat=new char[dxy*TILEMAP_LOD];
update_in_frame=false;
Expand All @@ -64,9 +74,8 @@ void cTileMapRender::ClearTilemapPool()
{
for (int y=0; y < tilemap->GetTileNumber().y; y++) {
for (int x = 0; x < tilemap->GetTileNumber().x; x++) {
sTile& Tile = tilemap->GetTile(x, y);
int& bumpTileID = Tile.bumpTileID;
bumpTileID = -1;
auto& Tile = GetRenderTile(x, y);
Tile.bumpTileID = -1;
}
}

Expand Down Expand Up @@ -153,7 +162,7 @@ void cTileMapRender::PreDraw(cCamera* DrawNode)
for(int y=0; y < tilemap->GetTileNumber().y; y++)
for(int x=0; x < tilemap->GetTileNumber().x; x++)
{
sTile &Tile = tilemap->GetTile(x, y);
sRenderTile &Tile = GetRenderTile(x, y);
int &bumpTileID = Tile.bumpTileID;
if(!Tile.GetAttribute(ATTRTILE_DRAWLOD))
{
Expand All @@ -162,6 +171,10 @@ void cTileMapRender::PreDraw(cCamera* DrawNode)
}

Tile.ClearAttribute(ATTRTILE_DRAWLOD);

if (tilemap->GetTile(x, y).GetAttribute(ATTRTILE_UPDATELOD)) {
Tile.SetAttribute(ATTRTILE_UPDATELOD);
}
}

if(update_stat)
Expand Down Expand Up @@ -218,7 +231,7 @@ int cTileMapRender::bumpTileAlloc(int lod,int xpos,int ypos)
int w = tilemap->GetTileSize().x >> bumpTexScale[lod];
int h = tilemap->GetTileSize().y >> bumpTexScale[lod];
cTilemapTexturePool* pool = FindFreeTexturePool(w, h);
sBumpTile* tile = new sBumpTile(tilemap, pool, lod, xpos, ypos);
sBumpTile* tile = new sBumpTile(tilemap, this, pool, lod, xpos, ypos);
int i;
for (i = 0; i < bumpTiles.size(); i++) {
if (!bumpTiles[i]) {
Expand Down Expand Up @@ -310,7 +323,6 @@ void cTileMapRender::DrawBump(cCamera* DrawNode,eBlendMode MatMode,TILEMAP_DRAW
cCamera* pShadowMapCamera=DrawNode->FindCildCamera(ATTRCAMERA_SHADOWMAP);
int reflection = DrawNode->GetAttribute(ATTRCAMERA_REFLECTION);
cCamera* pNormalCamera=DrawNode->GetRoot();
cTileMapRender* render=tilemap->GetTilemapRender();
bool use_shadow_map=false;

//TODO remove this once D3D9 specifics are removed
Expand Down Expand Up @@ -464,7 +476,7 @@ void cTileMapRender::DrawBump(cCamera* DrawNode,eBlendMode MatMode,TILEMAP_DRAW
/**/
{
// process visible tile
sTile &Tile = tilemap->GetTile(k, n);
sRenderTile &Tile = GetRenderTile(k, n);
int &bumpTileID = Tile.bumpTileID;

// calc LOD считается всегда по отгошению к прямой камере для
Expand All @@ -477,8 +489,7 @@ void cTileMapRender::DrawBump(cCamera* DrawNode,eBlendMode MatMode,TILEMAP_DRAW
vis_lod[k+n*dk]=iLod;

// create/update render tile
if (render->bumpTileValid(bumpTileID)
&& render->bumpTiles[bumpTileID]->LOD != iLod && !shadow)
if (bumpTileValid(bumpTileID) && bumpTiles[bumpTileID]->LOD != iLod && !shadow)
{
// LOD changed, free old tile and allocate new
bumpTileFree(bumpTileID);
Expand Down Expand Up @@ -520,7 +531,7 @@ void cTileMapRender::DrawBump(cCamera* DrawNode,eBlendMode MatMode,TILEMAP_DRAW
for (n = 0; n < dn; n++)
for (k = 0; k < dk; k++)
{
sTile &Tile = tilemap->GetTile(k, n);
sRenderTile &Tile = GetRenderTile(k, n);
int bumpTileID = Tile.bumpTileID;
if(bumpTileID<0)continue;
sBumpTile *bumpTile = bumpTiles[bumpTileID];
Expand Down Expand Up @@ -607,8 +618,8 @@ void cTileMapRender::DrawBump(cCamera* DrawNode,eBlendMode MatMode,TILEMAP_DRAW
int nTiles = 0;
VertexBuffer* lastVB = nullptr;
#endif
VertexPoolManager* vtxPoolMan = render->GetVertexPool();
IndexPoolManager* idxPoolMan = render->GetIndexPool();
VertexPoolManager* vtxPoolMan = GetVertexPool();
IndexPoolManager* idxPoolMan = GetIndexPool();
for (cTilemapTexturePool* curpool : bumpTexPools) {
if (curpool->tileRenderList.empty()) {
continue;
Expand Down
14 changes: 14 additions & 0 deletions Source/Render/tilemap/TileMapRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@ class cTileMapRender
char* update_stat;
bool update_in_frame;

struct sRenderTile final : public sAttribute
{
int bumpTileID = -1;

inline int GetDraw() { return GetAttribute(ATTRTILE_DRAWLOD); }
inline int GetUpdate() { return GetAttribute(ATTRTILE_UPDATELOD); }
inline void SetDraw() { SetAttribute(ATTRTILE_DRAWLOD); }

inline void ClearDraw() { ClearAttribute(ATTRTILE_DRAWLOD); }
inline void ClearUpdate() { ClearAttribute(ATTRTILE_UPDATELOD); }
};
std::vector<sRenderTile> renderTiles;

void SaveUpdateStat();

VectDelta* delta_buffer;
std::vector<std::vector<sPolygon>> index_buffer;

cTilemapTexturePool* FindFreeTexturePool(int tex_width, int tex_height);
sRenderTile& GetRenderTile(int i, int j) { return renderTiles[i + j*tilemap->GetTileNumber().x]; }
public:
void IncUpdate(sBumpTile* pbump);

Expand Down

0 comments on commit b5f28dc

Please sign in to comment.