Check depth buffer before texture mapping
This commit is contained in:
parent
ba68f22bee
commit
bedc30038b
|
@ -84,6 +84,21 @@ void Render(EngineBuffer &buffer, EngineMemory &memory)
|
||||||
&& (barycenter[1] >= 0.0f)
|
&& (barycenter[1] >= 0.0f)
|
||||||
&& (barycenter[2] >= 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
|
// Interpolate U and V of the texture for this point
|
||||||
Texture &texture = textures.data[faces.data[f].materialIndex];
|
Texture &texture = textures.data[faces.data[f].materialIndex];
|
||||||
UV &uv0 = uvs.data[face.uvIndex[0]];
|
UV &uv0 = uvs.data[face.uvIndex[0]];
|
||||||
|
@ -163,23 +178,11 @@ void Render(EngineBuffer &buffer, EngineMemory &memory)
|
||||||
color.r *= shading.r;
|
color.r *= shading.r;
|
||||||
|
|
||||||
|
|
||||||
// Interpolate 1/z for the z-buffer
|
// Alpha blend the pixel
|
||||||
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])
|
|
||||||
{
|
|
||||||
ColorU32 *pixel = (ColorU32*)&buffer.buffer[y*buffer.width+x];
|
ColorU32 *pixel = (ColorU32*)&buffer.buffer[y*buffer.width+x];
|
||||||
|
|
||||||
float alpha = color.a / 255.0f;
|
float alpha = color.a / 255.0f;
|
||||||
float alphaDiff = 1.0f - alpha;
|
float alphaDiff = 1.0f - alpha;
|
||||||
|
|
||||||
// Blend new color with the existing color
|
|
||||||
ColorU32 blended =
|
ColorU32 blended =
|
||||||
{
|
{
|
||||||
(uint8_t)((alpha * color.b) + (alphaDiff * pixel->b)),
|
(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))
|
(uint8_t)((alpha * color.a) + (alphaDiff * pixel->a))
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Draw
|
||||||
DrawPixel(buffer.buffer, buffer.width, blended.u32, x, y);
|
DrawPixel(buffer.buffer, buffer.width, blended.u32, x, y);
|
||||||
|
|
||||||
memory.zbuffer[y][x] = zInv;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue