Fix triangle drawer clipping bug

This commit is contained in:
Magnus Norddahl 2016-11-08 23:08:25 +01:00
parent 9413ea6edf
commit 7d3e8d1414
4 changed files with 190 additions and 88 deletions

View File

@ -36,6 +36,10 @@
#include "r_data/colormaps.h" #include "r_data/colormaps.h"
#include "r_poly_triangle.h" #include "r_poly_triangle.h"
#ifndef NO_SSE
#include <immintrin.h>
#endif
void PolyTriangleDrawer::draw(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, FTexture *texture) void PolyTriangleDrawer::draw(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, FTexture *texture)
{ {
if (r_swtruecolor) if (r_swtruecolor)
@ -124,10 +128,8 @@ void PolyTriangleDrawer::draw_shaded_triangle(const TriVertex *vert, bool ccw, S
{ {
// Cull, clip and generate additional vertices as needed // Cull, clip and generate additional vertices as needed
TriVertex clippedvert[6]; TriVertex clippedvert[6];
int numclipvert = 0; int numclipvert;
clipedge(vert[0], vert[1], clippedvert, numclipvert); clipedge(vert, clippedvert, numclipvert);
clipedge(vert[1], vert[2], clippedvert, numclipvert);
clipedge(vert[2], vert[0], clippedvert, numclipvert);
// Map to 2D viewport: // Map to 2D viewport:
for (int j = 0; j < numclipvert; j++) for (int j = 0; j < numclipvert; j++)
@ -170,62 +172,113 @@ void PolyTriangleDrawer::draw_shaded_triangle(const TriVertex *vert, bool ccw, S
bool PolyTriangleDrawer::cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2) bool PolyTriangleDrawer::cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2)
{ {
float d1 = clipdistance1 * (1.0f - t1) + clipdistance2 * t1; if (clipdistance1 < 0.0f && clipdistance2 < 0.0f)
float d2 = clipdistance1 * (1.0f - t2) + clipdistance2 * t2;
if (d1 < 0.0f && d2 < 0.0f)
return true; return true;
if (d1 < 0.0f) if (clipdistance1 < 0.0f)
t1 = MAX(-clipdistance1 / (clipdistance2 - clipdistance1), t1); t1 = MAX(-clipdistance1 / (clipdistance2 - clipdistance1), 0.0f);
else
t1 = 0.0f;
if (d2 < 0.0f) if (clipdistance2 < 0.0f)
t2 = MIN(1.0f + clipdistance2 / (clipdistance1 - clipdistance2), t2); t2 = MIN(1.0f + clipdistance2 / (clipdistance1 - clipdistance2), 1.0f);
else
t2 = 1.0f;
return false; return false;
} }
void PolyTriangleDrawer::clipedge(const TriVertex &v1, const TriVertex &v2, TriVertex *clippedvert, int &numclipvert) void PolyTriangleDrawer::clipedge(const TriVertex *verts, TriVertex *clippedvert, int &numclipvert)
{ {
// Clip and cull so that the following is true for all vertices: // Clip and cull so that the following is true for all vertices:
// -v.w <= v.x <= v.w // -v.w <= v.x <= v.w
// -v.w <= v.y <= v.w // -v.w <= v.y <= v.w
// -v.w <= v.z <= v.w // -v.w <= v.z <= v.w
float t1 = 0.0f, t2 = 1.0f; // use barycentric weights while clipping vertices
bool culled = float weights[6 * 3 * 2];
cullhalfspace(v1.x + v1.w, v2.x + v2.w, t1, t2) || for (int i = 0; i < 3; i++)
cullhalfspace(v1.w - v1.x, v2.w - v2.x, t1, t2) ||
cullhalfspace(v1.y + v1.w, v2.y + v2.w, t1, t2) ||
cullhalfspace(v1.w - v1.y, v2.w - v2.y, t1, t2) ||
cullhalfspace(v1.z + v1.w, v2.z + v2.w, t1, t2) ||
cullhalfspace(v1.w - v1.z, v2.w - v2.z, t1, t2);
if (culled)
return;
if (t1 == 0.0f)
{ {
clippedvert[numclipvert++] = v1; weights[i * 3 + 0] = 0.0f;
} weights[i * 3 + 1] = 0.0f;
else weights[i * 3 + 2] = 0.0f;
{ weights[i * 3 + i] = 1.0f;
auto &v = clippedvert[numclipvert++];
v.x = v1.x * (1.0f - t1) + v2.x * t1;
v.y = v1.y * (1.0f - t1) + v2.y * t1;
v.z = v1.z * (1.0f - t1) + v2.z * t1;
v.w = v1.w * (1.0f - t1) + v2.w * t1;
for (int i = 0; i < TriVertex::NumVarying; i++)
v.varying[i] = v1.varying[i] * (1.0f - t1) + v2.varying[i] * t1;
} }
if (t2 != 1.0f) // halfspace clip distances
float clipdistance[6 * 3];
for (int i = 0; i < 3; i++)
{ {
auto &v = clippedvert[numclipvert++]; const auto &v = verts[i];
v.x = v1.x * (1.0f - t2) + v2.x * t2; clipdistance[i * 6 + 0] = v.x + v.w;
v.y = v1.y * (1.0f - t2) + v2.y * t2; clipdistance[i * 6 + 1] = v.w - v.x;
v.z = v1.z * (1.0f - t2) + v2.z * t2; clipdistance[i * 6 + 2] = v.y + v.w;
v.w = v1.w * (1.0f - t2) + v2.w * t2; clipdistance[i * 6 + 3] = v.w - v.y;
for (int i = 0; i < TriVertex::NumVarying; i++) clipdistance[i * 6 + 4] = v.z + v.w;
v.varying[i] = v1.varying[i] * (1.0f - t2) + v2.varying[i] * t2; clipdistance[i * 6 + 5] = v.w - v.z;
}
// Clip against each halfspace
float *input = weights;
float *output = weights + 6 * 3;
int inputverts = 3;
int outputverts = 0;
for (int p = 0; p < 6; p++)
{
// Clip each edge
outputverts = 0;
for (int i = 0; i < inputverts; i++)
{
int j = (i + 1) % inputverts;
float clipdistance1 =
clipdistance[0 * 6 + p] * input[i * 3 + 0] +
clipdistance[1 * 6 + p] * input[i * 3 + 1] +
clipdistance[2 * 6 + p] * input[i * 3 + 2];
float clipdistance2 =
clipdistance[0 * 6 + p] * input[j * 3 + 0] +
clipdistance[1 * 6 + p] * input[j * 3 + 1] +
clipdistance[2 * 6 + p] * input[j * 3 + 2];
float t1, t2;
if (!cullhalfspace(clipdistance1, clipdistance2, t1, t2))
{
// add t1 vertex
for (int k = 0; k < 3; k++)
output[outputverts * 3 + k] = input[i * 3 + k] * (1.0f - t1) + input[j * 3 + k] * t1;
outputverts++;
if (t2 != 1.0f && t2 > t1)
{
// add t2 vertex
for (int k = 0; k < 3; k++)
output[outputverts * 3 + k] = input[i * 3 + k] * (1.0f - t2) + input[j * 3 + k] * t2;
outputverts++;
}
}
}
std::swap(input, output);
std::swap(inputverts, outputverts);
if (inputverts == 0)
break;
}
// Convert barycentric weights to actual vertices
numclipvert = inputverts;
for (int i = 0; i < numclipvert; i++)
{
auto &v = clippedvert[i];
memset(&v, 0, sizeof(TriVertex));
for (int w = 0; w < 3; w++)
{
float weight = input[i * 3 + w];
v.x += verts[w].x * weight;
v.y += verts[w].y * weight;
v.z += verts[w].z * weight;
v.w += verts[w].w * weight;
for (int iv = 0; iv < TriVertex::NumVarying; iv++)
v.varying[iv] += verts[w].varying[iv] * weight;
}
} }
} }

View File

@ -39,7 +39,7 @@ private:
static void draw_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor, DrawerThread *thread, void(*drawfunc)(const ScreenPolyTriangleDrawerArgs *, DrawerThread *)); static void draw_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor, DrawerThread *thread, void(*drawfunc)(const ScreenPolyTriangleDrawerArgs *, DrawerThread *));
static void draw_shaded_triangle(const TriVertex *vertices, bool ccw, ScreenPolyTriangleDrawerArgs *args, DrawerThread *thread, void(*drawfunc)(const ScreenPolyTriangleDrawerArgs *, DrawerThread *)); static void draw_shaded_triangle(const TriVertex *vertices, bool ccw, ScreenPolyTriangleDrawerArgs *args, DrawerThread *thread, void(*drawfunc)(const ScreenPolyTriangleDrawerArgs *, DrawerThread *));
static bool cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2); static bool cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2);
static void clipedge(const TriVertex &v1, const TriVertex &v2, TriVertex *clippedvert, int &numclipvert); static void clipedge(const TriVertex *verts, TriVertex *clippedvert, int &numclipvert);
static void queue_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor); static void queue_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor);

View File

@ -139,10 +139,8 @@ void TriangleDrawer::draw_shaded_triangle(const TriVertex *vert, bool ccw, Scree
{ {
// Cull, clip and generate additional vertices as needed // Cull, clip and generate additional vertices as needed
TriVertex clippedvert[6]; TriVertex clippedvert[6];
int numclipvert = 0; int numclipvert;
clipedge(vert[0], vert[1], clippedvert, numclipvert); clipedge(vert, clippedvert, numclipvert);
clipedge(vert[1], vert[2], clippedvert, numclipvert);
clipedge(vert[2], vert[0], clippedvert, numclipvert);
// Map to 2D viewport: // Map to 2D viewport:
for (int j = 0; j < numclipvert; j++) for (int j = 0; j < numclipvert; j++)
@ -185,62 +183,113 @@ void TriangleDrawer::draw_shaded_triangle(const TriVertex *vert, bool ccw, Scree
bool TriangleDrawer::cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2) bool TriangleDrawer::cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2)
{ {
float d1 = clipdistance1 * (1.0f - t1) + clipdistance2 * t1; if (clipdistance1 < 0.0f && clipdistance2 < 0.0f)
float d2 = clipdistance1 * (1.0f - t2) + clipdistance2 * t2;
if (d1 < 0.0f && d2 < 0.0f)
return true; return true;
if (d1 < 0.0f) if (clipdistance1 < 0.0f)
t1 = MAX(-clipdistance1 / (clipdistance2 - clipdistance1), t1); t1 = MAX(-clipdistance1 / (clipdistance2 - clipdistance1), 0.0f);
else
t1 = 0.0f;
if (d2 < 0.0f) if (clipdistance2 < 0.0f)
t2 = MIN(1.0f + clipdistance2 / (clipdistance1 - clipdistance2), t2); t2 = MIN(1.0f + clipdistance2 / (clipdistance1 - clipdistance2), 1.0f);
else
t2 = 1.0f;
return false; return false;
} }
void TriangleDrawer::clipedge(const TriVertex &v1, const TriVertex &v2, TriVertex *clippedvert, int &numclipvert) void TriangleDrawer::clipedge(const TriVertex *verts, TriVertex *clippedvert, int &numclipvert)
{ {
// Clip and cull so that the following is true for all vertices: // Clip and cull so that the following is true for all vertices:
// -v.w <= v.x <= v.w // -v.w <= v.x <= v.w
// -v.w <= v.y <= v.w // -v.w <= v.y <= v.w
// -v.w <= v.z <= v.w // -v.w <= v.z <= v.w
float t1 = 0.0f, t2 = 1.0f; // use barycentric weights while clipping vertices
bool culled = float weights[6 * 3 * 2];
cullhalfspace(v1.x + v1.w, v2.x + v2.w, t1, t2) || for (int i = 0; i < 3; i++)
cullhalfspace(v1.w - v1.x, v2.w - v2.x, t1, t2) ||
cullhalfspace(v1.y + v1.w, v2.y + v2.w, t1, t2) ||
cullhalfspace(v1.w - v1.y, v2.w - v2.y, t1, t2) ||
cullhalfspace(v1.z + v1.w, v2.z + v2.w, t1, t2) ||
cullhalfspace(v1.w - v1.z, v2.w - v2.z, t1, t2);
if (culled)
return;
if (t1 == 0.0f)
{ {
clippedvert[numclipvert++] = v1; weights[i * 3 + 0] = 0.0f;
} weights[i * 3 + 1] = 0.0f;
else weights[i * 3 + 2] = 0.0f;
{ weights[i * 3 + i] = 1.0f;
auto &v = clippedvert[numclipvert++];
v.x = v1.x * (1.0f - t1) + v2.x * t1;
v.y = v1.y * (1.0f - t1) + v2.y * t1;
v.z = v1.z * (1.0f - t1) + v2.z * t1;
v.w = v1.w * (1.0f - t1) + v2.w * t1;
for (int i = 0; i < TriVertex::NumVarying; i++)
v.varying[i] = v1.varying[i] * (1.0f - t1) + v2.varying[i] * t1;
} }
if (t2 != 1.0f) // halfspace clip distances
float clipdistance[6 * 3];
for (int i = 0; i < 3; i++)
{ {
auto &v = clippedvert[numclipvert++]; const auto &v = verts[i];
v.x = v1.x * (1.0f - t2) + v2.x * t2; clipdistance[i * 6 + 0] = v.x + v.w;
v.y = v1.y * (1.0f - t2) + v2.y * t2; clipdistance[i * 6 + 1] = v.w - v.x;
v.z = v1.z * (1.0f - t2) + v2.z * t2; clipdistance[i * 6 + 2] = v.y + v.w;
v.w = v1.w * (1.0f - t2) + v2.w * t2; clipdistance[i * 6 + 3] = v.w - v.y;
for (int i = 0; i < TriVertex::NumVarying; i++) clipdistance[i * 6 + 4] = v.z + v.w;
v.varying[i] = v1.varying[i] * (1.0f - t2) + v2.varying[i] * t2; clipdistance[i * 6 + 5] = v.w - v.z;
}
// Clip against each halfspace
float *input = weights;
float *output = weights + 6 * 3;
int inputverts = 3;
int outputverts = 0;
for (int p = 0; p < 6; p++)
{
// Clip each edge
outputverts = 0;
for (int i = 0; i < inputverts; i++)
{
int j = (i + 1) % inputverts;
float clipdistance1 =
clipdistance[0 * 6 + p] * input[i * 3 + 0] +
clipdistance[1 * 6 + p] * input[i * 3 + 1] +
clipdistance[2 * 6 + p] * input[i * 3 + 2];
float clipdistance2 =
clipdistance[0 * 6 + p] * input[j * 3 + 0] +
clipdistance[1 * 6 + p] * input[j * 3 + 1] +
clipdistance[2 * 6 + p] * input[j * 3 + 2];
float t1, t2;
if (!cullhalfspace(clipdistance1, clipdistance2, t1, t2))
{
// add t1 vertex
for (int k = 0; k < 3; k++)
output[outputverts * 3 + k] = input[i * 3 + k] * (1.0f - t1) + input[j * 3 + k] * t1;
outputverts++;
if (t2 != 1.0f && t2 > t1)
{
// add t2 vertex
for (int k = 0; k < 3; k++)
output[outputverts * 3 + k] = input[i * 3 + k] * (1.0f - t2) + input[j * 3 + k] * t2;
outputverts++;
}
}
}
std::swap(input, output);
std::swap(inputverts, outputverts);
if (inputverts == 0)
break;
}
// Convert barycentric weights to actual vertices
numclipvert = inputverts;
for (int i = 0; i < numclipvert; i++)
{
auto &v = clippedvert[i];
memset(&v, 0, sizeof(TriVertex));
for (int w = 0; w < 3; w++)
{
float weight = input[i * 3 + w];
v.x += verts[w].x * weight;
v.y += verts[w].y * weight;
v.z += verts[w].z * weight;
v.w += verts[w].w * weight;
for (int iv = 0; iv < TriVertex::NumVarying; iv++)
v.varying[iv] += verts[w].varying[iv] * weight;
}
} }
} }

View File

@ -102,7 +102,7 @@ private:
static void draw_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor, DrawerThread *thread, void(*drawfunc)(const ScreenTriangleDrawerArgs *, DrawerThread *)); static void draw_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor, DrawerThread *thread, void(*drawfunc)(const ScreenTriangleDrawerArgs *, DrawerThread *));
static void draw_shaded_triangle(const TriVertex *vertices, bool ccw, ScreenTriangleDrawerArgs *args, DrawerThread *thread, void(*drawfunc)(const ScreenTriangleDrawerArgs *, DrawerThread *)); static void draw_shaded_triangle(const TriVertex *vertices, bool ccw, ScreenTriangleDrawerArgs *args, DrawerThread *thread, void(*drawfunc)(const ScreenTriangleDrawerArgs *, DrawerThread *));
static bool cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2); static bool cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2);
static void clipedge(const TriVertex &v1, const TriVertex &v2, TriVertex *clippedvert, int &numclipvert); static void clipedge(const TriVertex *verts, TriVertex *clippedvert, int &numclipvert);
static void queue_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor); static void queue_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor);