/// <summary>
/// BuildSurfaceDisplayList
/// </summary>
static void BuildSurfaceDisplayList(msurface_t fa)
{
// reconstruct the polygon
medge_t[] pedges = _CurrentModel.edges;
int lnumverts = fa.numedges;
//
// draw texture
//
glpoly_t poly = new glpoly_t();
poly.AllocVerts(lnumverts);
poly.next = fa.polys;
poly.flags = fa.flags;
fa.polys = poly;
ushort[] r_pedge_v;
Vector3 vec;
for (int i = 0; i < lnumverts; i++)
{
int lindex = _CurrentModel.surfedges[fa.firstedge + i];
if (lindex > 0)
{
r_pedge_v = pedges[lindex].v;
vec = _CurrentVertBase[r_pedge_v[0]].position;
}
else
{
r_pedge_v = pedges[-lindex].v;
vec = _CurrentVertBase[r_pedge_v[1]].position;
}
float s = Mathlib.DotProduct(ref vec, ref fa.texinfo.vecs[0]) + fa.texinfo.vecs[0].W;
s /= fa.texinfo.texture.width;
float t = Mathlib.DotProduct(ref vec, ref fa.texinfo.vecs[1]) + fa.texinfo.vecs[1].W;
t /= fa.texinfo.texture.height;
poly.verts[i][0] = vec.X;
poly.verts[i][1] = vec.Y;
poly.verts[i][2] = vec.Z;
poly.verts[i][3] = s;
poly.verts[i][4] = t;
//
// lightmap texture coordinates
//
s = Mathlib.DotProduct(ref vec, ref fa.texinfo.vecs[0]) + fa.texinfo.vecs[0].W;
s -= fa.texturemins[0];
s += fa.light_s * 16;
s += 8;
s /= BLOCK_WIDTH * 16;
t = Mathlib.DotProduct(ref vec, ref fa.texinfo.vecs[1]) + fa.texinfo.vecs[1].W;
t -= fa.texturemins[1];
t += fa.light_t * 16;
t += 8;
t /= BLOCK_HEIGHT * 16;
poly.verts[i][5] = s;
poly.verts[i][6] = t;
}
//
// remove co-linear points - Ed
//
if (_glKeepTJunctions.Value == 0 && (fa.flags & Surf.SURF_UNDERWATER) == 0)
{
for (int i = 0; i < lnumverts; ++i)
{
if (IsCollinear(poly.verts[(i + lnumverts - 1) % lnumverts],
poly.verts[i],
poly.verts[(i + 1) % lnumverts]))
{
int j;
for (j = i + 1; j < lnumverts; ++j)
{
//int k;
for (int k = 0; k < Mod.VERTEXSIZE; ++k)
poly.verts[j - 1][k] = poly.verts[j][k];
}
--lnumverts;
++_ColinElim;
// retry next vertex next time, which is now current vertex
--i;
}
}
}
poly.numverts = lnumverts;
}