Skip to content

Commit

Permalink
Map Renderer: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Apr 16, 2013
1 parent 5142d7d commit 117cf23
Showing 1 changed file with 98 additions and 106 deletions.
204 changes: 98 additions & 106 deletions doomsday/client/src/render/rend_main.cpp
Expand Up @@ -2480,50 +2480,108 @@ static void Rend_WriteBspLeafSkyFixStripGeometry(BspLeaf *leaf, HEdge *startNode
R_FreeRendTexCoords(coords);
}

uint Rend_NumFanVerticesForBspLeaf(BspLeaf *leaf)
{
if(!leaf) return 0;
// Are we using a hedge vertex as the fan base?
return leaf->hedgeCount() + (leaf->fanBase()? 0 : 2);
}

/**
* @param leaf BspLeaf to write geometry for.
* @param skyFix @ref skyCapFlags
* Prepare the trifan rvertex_t buffer specified according to the edges of this
* BSP leaf. If a fan base HEdge has been chosen it will be used as the center of
* the trifan, else the mid point of this leaf will be used instead.
*
* @param leaf BspLeaf instance.
* @param antiClockwise @c true= wind vertices in anticlockwise order (else clockwise).
* @param height Z map space height coordinate to be set for each vertex.
* @param verts Built vertices are written here.
* @param vertsSize Number of built vertices is written here. Can be @c NULL.
*
* @return Number of built vertices (same as written to @a vertsSize).
*/
static void Rend_WriteBspLeafSkyFixGeometry(BspLeaf *leaf, int skyFix)
static uint Rend_BuildBspLeafPlaneGeometry(BspLeaf *leaf, boolean antiClockwise,
coord_t height, rvertex_t **verts, uint *vertsSize)
{
bool const antiClockwise = false;
if(!leaf || !verts) return 0;

HEdge *fanBase = leaf->fanBase();
HEdge *baseNode = fanBase? fanBase : leaf->firstHEdge();

uint totalVerts = leaf->hedgeCount() + (!fanBase? 2 : 0);
*verts = R_AllocRendVertices(totalVerts);

uint n = 0;
if(!fanBase)
{
V2f_Copyd((*verts)[n].pos, leaf->center());
(*verts)[n].pos[VZ] = float( height );
n++;
}

if(!leaf || !leaf->hedgeCount() || !leaf->hasSector()) return;
if(!(skyFix & (SKYCAP_LOWER|SKYCAP_UPPER))) return;
// Add the vertices for each hedge.
HEdge *node = baseNode;
do
{
V2f_Copyd((*verts)[n].pos, node->v1Origin());
(*verts)[n].pos[VZ] = float( height );
n++;
} while((node = antiClockwise? &node->prev() : &node->next()) != baseNode);

// The last vertex is always equal to the first.
if(!fanBase)
{
V2f_Copyd((*verts)[n].pos, leaf->firstHEdge()->v1Origin());
(*verts)[n].pos[VZ] = float( height );
}

if(vertsSize) *vertsSize = totalVerts;
return totalVerts;
}

/// @param skyFix @ref skyCapFlags.
static void writeLeafSkyMaskStrips(int skyFix)
{
BspLeaf *bspLeaf = currentBspLeaf;
DENG_ASSERT(!isNullLeaf(bspLeaf));

if(!(skyFix & (SKYCAP_LOWER|SKYCAP_UPPER)))
return;

bool const antiClockwise = false;

// We may need to break the loop into multiple strips.
HEdge *startNode = 0;
coord_t startZBottom = 0;
coord_t startZTop = 0;
// We may need to break the half-edge loop into multiple strips.
HEdge *startNode = 0;
coord_t startZBottom = 0;
coord_t startZTop = 0;
Material *startMaterial = 0;

HEdge *base = leaf->firstHEdge();
HEdge *base = bspLeaf->firstHEdge();
HEdge *node = base;
forever
{
HEdge *hedge = (antiClockwise? &node->prev() : node);
bool endStrip = false;
bool beginNewStrip = false;
bool endStrip = false, beginNewStrip = false;

// Is a fix or two necessary for this hedge?
// Is a fix or two necessary for this edge?
if(chooseHEdgeSkyFixes(hedge, skyFix))
{
coord_t zBottom, zTop;
Material *skyMaterial = 0;

skyFixZCoords(hedge, skyFix, &zBottom, &zTop);

Material *skyMaterial = 0;
if(devRendSkyMode)
{
skyMaterial = hedge->sector().planeSurface(skyFix == SKYCAP_UPPER? Plane::Ceiling : Plane::Floor).materialPtr();
int relPlane = skyFix == SKYCAP_UPPER? Plane::Ceiling : Plane::Floor;
skyMaterial = hedge->sector().planeSurface(relPlane).materialPtr();
}

if(zBottom >= zTop)
{
// End the current strip.
endStrip = true;
}
else if(startNode && (!FEQUAL(zBottom, startZBottom) || !FEQUAL(zTop, startZTop) ||
else if(startNode && (!de::fequal(zBottom, startZBottom) || !de::fequal(zTop, startZTop) ||
(devRendSkyMode && skyMaterial != startMaterial)))
{
// End the current strip and start another.
Expand All @@ -2548,7 +2606,7 @@ static void Rend_WriteBspLeafSkyFixGeometry(BspLeaf *leaf, int skyFix)
if(endStrip && startNode)
{
// We have complete strip; build and write it.
Rend_WriteBspLeafSkyFixStripGeometry(leaf, startNode, node, antiClockwise,
Rend_WriteBspLeafSkyFixStripGeometry(bspLeaf, startNode, node, antiClockwise,
skyFix, startMaterial);

// End the current strip.
Expand All @@ -2568,110 +2626,44 @@ static void Rend_WriteBspLeafSkyFixGeometry(BspLeaf *leaf, int skyFix)
// Have we an unwritten strip? - build it.
if(startNode)
{
Rend_WriteBspLeafSkyFixStripGeometry(leaf, startNode, base, antiClockwise,
Rend_WriteBspLeafSkyFixStripGeometry(bspLeaf, startNode, base, antiClockwise,
skyFix, startMaterial);
}
}

uint Rend_NumFanVerticesForBspLeaf(BspLeaf *leaf)
{
if(!leaf) return 0;
// Are we using a hedge vertex as the fan base?
return leaf->hedgeCount() + (leaf->fanBase()? 0 : 2);
}

/**
* Prepare the trifan rvertex_t buffer specified according to the edges of this
* BSP leaf. If a fan base HEdge has been chosen it will be used as the center of
* the trifan, else the mid point of this leaf will be used instead.
*
* @param leaf BspLeaf instance.
* @param antiClockwise @c true= wind vertices in anticlockwise order (else clockwise).
* @param height Z map space height coordinate to be set for each vertex.
* @param verts Built vertices are written here.
* @param vertsSize Number of built vertices is written here. Can be @c NULL.
*
* @return Number of built vertices (same as written to @a vertsSize).
*/
static uint Rend_BuildBspLeafPlaneGeometry(BspLeaf *leaf, boolean antiClockwise,
coord_t height, rvertex_t **verts, uint *vertsSize)
{
if(!leaf || !verts) return 0;

HEdge *fanBase = leaf->fanBase();
HEdge *baseNode = fanBase? fanBase : leaf->firstHEdge();

uint totalVerts = leaf->hedgeCount() + (!fanBase? 2 : 0);
*verts = R_AllocRendVertices(totalVerts);

uint n = 0;
if(!fanBase)
{
V2f_Copyd((*verts)[n].pos, leaf->center());
(*verts)[n].pos[VZ] = float( height );
n++;
}

// Add the vertices for each hedge.
HEdge *node = baseNode;
do
{
V2f_Copyd((*verts)[n].pos, node->v1Origin());
(*verts)[n].pos[VZ] = float( height );
n++;
} while((node = antiClockwise? &node->prev() : &node->next()) != baseNode);

// The last vertex is always equal to the first.
if(!fanBase)
{
V2f_Copyd((*verts)[n].pos, leaf->firstHEdge()->v1Origin());
(*verts)[n].pos[VZ] = float( height );
}

if(vertsSize) *vertsSize = totalVerts;
return totalVerts;
}

/// @param skyFix @ref skyCapFlags.
static void Rend_RenderSkyFix(int skyFix)
{
BspLeaf *leaf = currentBspLeaf;
if(!leaf || !skyFix) return;

Rend_WriteBspLeafSkyFixGeometry(leaf, skyFix/*, &verts, &numVerts*/);
}

/// @param skyCap @ref skyCapFlags.
static void Rend_RenderSkyCap(int skyCap)
static void writeLeafSkyMaskCap(int skyCap)
{
BspLeaf *bspLeaf = currentBspLeaf;
DENG_ASSERT(!isNullLeaf(bspLeaf));

// Caps are unnecessary in sky debug mode (will be drawn as regular planes).
if(devRendSkyMode) return;

BspLeaf *leaf = currentBspLeaf;
if(!leaf || !skyCap) return;
if(!skyCap) return;

rvertex_t *verts;
uint numVerts;
Rend_BuildBspLeafPlaneGeometry(leaf, !!(skyCap & SKYCAP_UPPER), R_SkyCapZ(leaf, skyCap),
&verts, &numVerts);
Rend_BuildBspLeafPlaneGeometry(bspLeaf, (skyCap & SKYCAP_UPPER) != 0,
R_SkyCapZ(bspLeaf, skyCap), &verts, &numVerts);

RL_AddPoly(PT_FAN, RPF_DEFAULT | RPF_SKYMASK, numVerts, verts, NULL);
R_FreeRendVertices(verts);
}

/// @param skyCap @ref skyCapFlags
static void writeLeafSkyMask(int skyCap)
static void writeLeafSkyMask(int skyCap = SKYCAP_LOWER|SKYCAP_UPPER)
{
BspLeaf *leaf = currentBspLeaf;
BspLeaf *bspLeaf = currentBspLeaf;
DENG_ASSERT(!isNullLeaf(bspLeaf));

// Any work to do?
if(!leaf || !leaf->hedgeCount()) return;
if(!leaf->hasSector() || !R_SectorContainsSkySurfaces(leaf->sectorPtr())) return;
if(!R_SectorContainsSkySurfaces(bspLeaf->sectorPtr()))
return;

// Sky caps are only necessary in sectors with sky-masked planes.
if((skyCap & SKYCAP_LOWER) && !leaf->sector().floorSurface().hasSkyMaskedMaterial())
if((skyCap & SKYCAP_LOWER) && !bspLeaf->sector().floorSurface().hasSkyMaskedMaterial())
skyCap &= ~SKYCAP_LOWER;
if((skyCap & SKYCAP_UPPER) && !leaf->sector().ceilingSurface().hasSkyMaskedMaterial())
if((skyCap & SKYCAP_UPPER) && !bspLeaf->sector().ceilingSurface().hasSkyMaskedMaterial())
skyCap &= ~SKYCAP_UPPER;

if(!skyCap) return;
Expand All @@ -2685,15 +2677,15 @@ static void writeLeafSkyMask(int skyCap)
// Lower?
if(skyCap & SKYCAP_LOWER)
{
Rend_RenderSkyFix(SKYCAP_LOWER);
Rend_RenderSkyCap(SKYCAP_LOWER);
writeLeafSkyMaskStrips(SKYCAP_LOWER);
writeLeafSkyMaskCap(SKYCAP_LOWER);
}

// Upper?
if(skyCap & SKYCAP_UPPER)
{
Rend_RenderSkyFix(SKYCAP_UPPER);
Rend_RenderSkyCap(SKYCAP_UPPER);
writeLeafSkyMaskStrips(SKYCAP_UPPER);
writeLeafSkyMaskCap(SKYCAP_UPPER);
}
}

Expand Down Expand Up @@ -2946,7 +2938,7 @@ static void drawCurrentBspLeaf()
*/
R_AddSprites(bspLeaf);

writeLeafSkyMask(SKYCAP_LOWER|SKYCAP_UPPER);
writeLeafSkyMask();
writeLeafWallSections();
writeLeafPolyobjs();
writeLeafPlanes();
Expand Down Expand Up @@ -2986,7 +2978,7 @@ static void traverseBspAndDrawLeafs(MapElement *bspElement)
return;

// Is this leaf visible?
if(!firstBspLeaf && C_CheckBspLeaf(bspLeaf))
if(!firstBspLeaf && !C_CheckBspLeaf(bspLeaf))
return;

// This is now the current leaf.
Expand Down

0 comments on commit 117cf23

Please sign in to comment.