1
0
Fork 0

Check depth buffer before texture mapping

This commit is contained in:
Austin Morlan 2018-09-20 18:19:26 -07:00
parent ba68f22bee
commit bedc30038b
Signed by: austin
GPG Key ID: FD6B27654AF5E348
1 changed files with 87 additions and 84 deletions

View File

@ -84,6 +84,21 @@ void Render(EngineBuffer &buffer, EngineMemory &memory)
&& (barycenter[1] >= 0.0f)
&& (barycenter[2] >= 0.0f))
{
// Interpolate 1/z for the z-buffer
float zInv =
1.0f /
((barycenter[0] * p0.w)
+ (barycenter[1] * p1.w)
+ (barycenter[2] * p2.w));
// Compute pixel color if it's closer than what's in the z-buffer
if (zInv > memory.zbuffer[y][x])
{
// Update the depth buffer
memory.zbuffer[y][x] = zInv;
// Interpolate U and V of the texture for this point
Texture &texture = textures.data[faces.data[f].materialIndex];
UV &uv0 = uvs.data[face.uvIndex[0]];
@ -163,23 +178,11 @@ void Render(EngineBuffer &buffer, EngineMemory &memory)
color.r *= shading.r;
// Interpolate 1/z for the z-buffer
float zInv =
1.0f /
((barycenter[0] * p0.w)
+ (barycenter[1] * p1.w)
+ (barycenter[2] * p2.w));
// Draw the pixel if it's closer than what's in the z-buffer
if (zInv > memory.zbuffer[y][x])
{
// Alpha blend the pixel
ColorU32 *pixel = (ColorU32*)&buffer.buffer[y*buffer.width+x];
float alpha = color.a / 255.0f;
float alphaDiff = 1.0f - alpha;
// Blend new color with the existing color
ColorU32 blended =
{
(uint8_t)((alpha * color.b) + (alphaDiff * pixel->b)),
@ -188,9 +191,9 @@ void Render(EngineBuffer &buffer, EngineMemory &memory)
(uint8_t)((alpha * color.a) + (alphaDiff * pixel->a))
};
// Draw
DrawPixel(buffer.buffer, buffer.width, blended.u32, x, y);
memory.zbuffer[y][x] = zInv;
}
}
}