Skip to content

Commit

Permalink
- major cleanup of hw_fakeflat to fix problems with the recent refact…
Browse files Browse the repository at this point in the history
…orings.

This function had major bugs with copying the plane texture info. Not only didn't all cases copy all info, it was also using a function that was altering sector state.
Fixed by making the plane and texture copies inlined functions of sector_t, so that a) one copy equals one function call and b) changing this can be done in one central place instead of dealing with multiple occurences in here.
  • Loading branch information
coelckers committed Aug 19, 2018
1 parent c3cc79f commit 9dbc3f5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 71 deletions.
87 changes: 23 additions & 64 deletions src/hwrenderer/scene/hw_fakeflat.cpp
Expand Up @@ -191,6 +191,7 @@ area_t hw_CheckViewArea(vertex_t *v1, vertex_t *v2, sector_t *frontsector, secto
//==========================================================================
sector_t * hw_FakeFlat(sector_t * sec, sector_t * dest, area_t in_area, bool back)
{
return sec;
if (!sec->GetHeightSec() || sec->heightsec==sec)
{
// check for backsectors with the ceiling lower than the floor. These will create
Expand Down Expand Up @@ -223,21 +224,15 @@ sector_t * hw_FakeFlat(sector_t * sec, sector_t * dest, area_t in_area, bool bac
int diffTex = (sec->heightsec->MoreFlags & SECMF_CLIPFAKEPLANES);
sector_t * s = sec->heightsec;

#if 0
*dest=*sec; // This will invoke the copy operator which isn't really needed here. Memcpy is faster.
#else
memcpy(dest, sec, sizeof(sector_t));
#endif

// Replace floor and ceiling height with control sector's heights.
if (diffTex)
{
if (s->floorplane.CopyPlaneIfValid (&dest->floorplane, &sec->ceilingplane))
if (s->floorplane.CopyPlaneIfValid (&dest->floorplane, &sec->ceilingplane, false))
{
dest->SetTexture(sector_t::floor, s->GetTexture(sector_t::floor), false);
dest->SetPlaneTexZQuick(sector_t::floor, s->GetPlaneTexZ(sector_t::floor));
dest->iboindex[sector_t::floor] = sec->iboindex[sector_t::vbo_fakefloor];
dest->vboheight[sector_t::floor] = s->vboheight[sector_t::floor];
dest->CopySecPlaneInfo(sector_t::floor, s, sector_t::floor, sec, sector_t::vbo_fakefloor);
dest->CopyTextureInfo(sector_t::floor, s, sector_t::floor);
}
else if (s->MoreFlags & SECMF_FAKEFLOORONLY)
{
Expand All @@ -259,49 +254,31 @@ sector_t * hw_FakeFlat(sector_t * sec, sector_t * dest, area_t in_area, bool bac
}
else
{
dest->SetPlaneTexZQuick(sector_t::floor, s->GetPlaneTexZ(sector_t::floor));
dest->floorplane = s->floorplane;

dest->iboindex[sector_t::floor] = sec->iboindex[sector_t::vbo_fakefloor];
dest->vboheight[sector_t::floor] = s->vboheight[sector_t::floor];
dest->CopySecPlaneInfo(sector_t::floor, s, sector_t::floor, sec, sector_t::vbo_fakefloor);
}

if (!(s->MoreFlags&SECMF_FAKEFLOORONLY))
{
if (diffTex)
{
if (s->ceilingplane.CopyPlaneIfValid (&dest->ceilingplane, &sec->floorplane))
// This clips to the actual floor plane and inverts the texture rules.
if (s->ceilingplane.CopyPlaneIfValid (&dest->ceilingplane, &sec->floorplane, false))
{
dest->SetTexture(sector_t::ceiling, s->GetTexture(sector_t::ceiling), false);
dest->SetPlaneTexZQuick(sector_t::ceiling, s->GetPlaneTexZ(sector_t::ceiling));
dest->iboindex[sector_t::ceiling] = sec->iboindex[sector_t::vbo_fakeceiling];
dest->vboheight[sector_t::ceiling] = s->vboheight[sector_t::ceiling];
dest->CopySecPlaneInfo(sector_t::ceiling, s, sector_t::ceiling, sec, sector_t::vbo_fakeceiling);
dest->CopyTextureInfo(sector_t::ceiling, s, sector_t::ceiling);
}
}
else
{
dest->ceilingplane = s->ceilingplane;
dest->SetPlaneTexZQuick(sector_t::ceiling, s->GetPlaneTexZ(sector_t::ceiling));
dest->iboindex[sector_t::ceiling] = sec->iboindex[sector_t::vbo_fakeceiling];
dest->vboheight[sector_t::ceiling] = s->vboheight[sector_t::ceiling];
dest->CopySecPlaneInfo(sector_t::ceiling, s, sector_t::ceiling, sec, sector_t::vbo_fakeceiling);
}
}

if (in_area==area_below)
{
dest->CopyColors(s);
dest->SetPlaneTexZQuick(sector_t::floor, sec->GetPlaneTexZ(sector_t::floor));
dest->SetPlaneTexZQuick(sector_t::ceiling, s->GetPlaneTexZ(sector_t::floor));
dest->floorplane=sec->floorplane;
dest->ceilingplane=s->floorplane;
dest->ceilingplane.FlipVert();

dest->iboindex[sector_t::floor] = sec->iboindex[sector_t::floor];
dest->vboheight[sector_t::floor] = sec->vboheight[sector_t::floor];

dest->iboindex[sector_t::ceiling] = sec->iboindex[sector_t::vbo_fakefloor];
dest->vboheight[sector_t::ceiling] = s->vboheight[sector_t::floor];

dest->CopySecPlaneInfo(sector_t::floor, sec, sector_t::floor, sec, sector_t::floor);
dest->CopySecPlaneInfo(sector_t::ceiling, s, sector_t::floor, sec, sector_t::vbo_fakefloor);
dest->ClearPortal(sector_t::ceiling);

if (!(s->MoreFlags & SECMF_NOFAKELIGHT))
Expand All @@ -311,24 +288,15 @@ sector_t * hw_FakeFlat(sector_t * sec, sector_t * dest, area_t in_area, bool bac

if (!back)
{
dest->SetTexture(sector_t::floor, diffTex ? sec->GetTexture(sector_t::floor) : s->GetTexture(sector_t::floor), false);
dest->planes[sector_t::floor].xform = s->planes[sector_t::floor].xform;

//dest->ceilingplane = s->floorplane;
dest->CopyTextureInfo(sector_t::floor, diffTex? sec : s, sector_t::floor);

if (s->GetTexture(sector_t::ceiling) == skyflatnum)
{
dest->SetTexture(sector_t::ceiling, dest->GetTexture(sector_t::floor), false);
//dest->floorplane = dest->ceilingplane;
//dest->floorplane.FlipVert ();
//dest->floorplane.ChangeHeight (+1);
dest->planes[sector_t::ceiling].xform = dest->planes[sector_t::floor].xform;

dest->CopyTextureInfo(sector_t::ceiling, dest, sector_t::floor);
}
else
{
dest->SetTexture(sector_t::ceiling, diffTex ? s->GetTexture(sector_t::floor) : s->GetTexture(sector_t::ceiling), false);
dest->planes[sector_t::ceiling].xform = s->planes[sector_t::ceiling].xform;
dest->CopyTextureInfo(sector_t::ceiling, s, diffTex? sector_t::floor : sector_t::ceiling);
}

if (!(s->MoreFlags & SECMF_NOFAKELIGHT))
Expand All @@ -343,18 +311,8 @@ sector_t * hw_FakeFlat(sector_t * sec, sector_t * dest, area_t in_area, bool bac
else if (in_area == area_above)
{
dest->CopyColors(s);
dest->SetPlaneTexZQuick(sector_t::ceiling, sec->GetPlaneTexZ(sector_t::ceiling));
dest->SetPlaneTexZQuick(sector_t::floor, s->GetPlaneTexZ(sector_t::ceiling));
dest->ceilingplane = sec->ceilingplane;
dest->floorplane = s->ceilingplane;
dest->floorplane.FlipVert();

dest->iboindex[sector_t::floor] = sec->iboindex[sector_t::vbo_fakeceiling];
dest->vboheight[sector_t::floor] = s->vboheight[sector_t::ceiling];

dest->iboindex[sector_t::ceiling] = sec->iboindex[sector_t::ceiling];
dest->vboheight[sector_t::ceiling] = sec->vboheight[sector_t::ceiling];

dest->CopySecPlaneInfo(sector_t::ceiling, sec, sector_t::ceiling, sec, sector_t::ceiling);
dest->CopySecPlaneInfo(sector_t::floor, s, sector_t::ceiling, sec, sector_t::vbo_fakeceiling);
dest->ClearPortal(sector_t::floor);

if (!(s->MoreFlags & SECMF_NOFAKELIGHT))
Expand All @@ -364,14 +322,15 @@ sector_t * hw_FakeFlat(sector_t * sec, sector_t * dest, area_t in_area, bool bac

if (!back)
{
dest->SetTexture(sector_t::ceiling, diffTex ? sec->GetTexture(sector_t::ceiling) : s->GetTexture(sector_t::ceiling), false);
dest->SetTexture(sector_t::floor, s->GetTexture(sector_t::ceiling), false);
dest->planes[sector_t::ceiling].xform = dest->planes[sector_t::floor].xform = s->planes[sector_t::ceiling].xform;
dest->CopyTextureInfo(sector_t::ceiling, diffTex? sec : s, sector_t::ceiling);

if (s->GetTexture(sector_t::floor) != skyflatnum)
{
dest->SetTexture(sector_t::floor, s->GetTexture(sector_t::floor), false);
dest->planes[sector_t::floor].xform = s->planes[sector_t::floor].xform;
dest->CopyTextureInfo(sector_t::floor, s, sector_t::floor);
}
else
{
dest->CopyTextureInfo(sector_t::floor, s, sector_t::ceiling);
}

if (!(s->MoreFlags & SECMF_NOFAKELIGHT))
Expand Down
4 changes: 2 additions & 2 deletions src/p_sectors.cpp
Expand Up @@ -2276,7 +2276,7 @@ DEFINE_ACTION_FUNCTION(_Sector, NextLowestFloorAt)
//
//===========================================================================

bool secplane_t::CopyPlaneIfValid (secplane_t *dest, const secplane_t *opp) const
bool secplane_t::CopyPlaneIfValid (secplane_t *dest, const secplane_t *opp, bool copyit) const
{
bool copy = false;

Expand All @@ -2298,7 +2298,7 @@ bool secplane_t::CopyPlaneIfValid (secplane_t *dest, const secplane_t *opp) cons
copy = true;
}

if (copy)
if (copy && copyit)
{
*dest = *this;
}
Expand Down
32 changes: 27 additions & 5 deletions src/r_defs.h
Expand Up @@ -437,7 +437,7 @@ struct secplane_t
}
}

bool CopyPlaneIfValid (secplane_t *dest, const secplane_t *opp) const;
bool CopyPlaneIfValid (secplane_t *dest, const secplane_t *opp, bool copy = false) const;
inline double ZatPoint(const AActor *ac) const;

};
Expand Down Expand Up @@ -685,10 +685,10 @@ struct sector_t
struct splane
{
FTransform xform;
int Flags;
int Light;
double alpha;
double TexZ;
int Flags;
int Light;
PalEntry GlowColor;
float GlowHeight;
FTextureID Texture;
Expand All @@ -708,8 +708,8 @@ struct sector_t
void InvalidatePlane(int pos)
{
// negate the buffer index to mark it as invalid.
auto & p = planes[pos].ubIndexPlane;
if (p > 0) p = -p;
//auto & p = planes[pos].ubIndexPlane;
//if (p > 0) p = -p;
}

void SetXOffset(int pos, double o)
Expand Down Expand Up @@ -923,6 +923,22 @@ struct sector_t
CheckOverlap();
}

void CopySecPlaneInfo(int pos, sector_t *src, int srcpos, sector_t *org, int vbopos)
{
SetSecPlane(pos, src->GetSecPlane(srcpos));
if (pos != srcpos) GetSecPlane(pos).FlipVert();
iboindex[pos] = org->iboindex[vbopos];
vboheight[pos] = src->vboheight[srcpos];
SetPlaneTexZQuick(pos, src->GetPlaneTexZ(srcpos));
}

void CopyTextureInfo(int pos, sector_t *src, int srcpos)
{
planes[pos].xform = src->planes[srcpos].xform;
planes[pos].ubIndexMatrix = src->planes[srcpos].ubIndexMatrix;
planes[pos].Texture = src->planes[srcpos].Texture;
}

static inline short ClampLight(int level)
{
return (short)clamp(level, SHRT_MIN, SHRT_MAX);
Expand All @@ -943,6 +959,12 @@ struct sector_t
return lightlevel;
}

void SetSecPlane(int pos, const secplane_t &other)
{
if (pos == floor) floorplane = other;
else ceilingplane = other;
}

secplane_t &GetSecPlane(int pos)
{
return pos == floor? floorplane:ceilingplane;
Expand Down

0 comments on commit 9dbc3f5

Please sign in to comment.