diff --git a/src/pxl8_gfx.c b/src/pxl8_gfx.c index b553d9b..a8a01ab 100644 --- a/src/pxl8_gfx.c +++ b/src/pxl8_gfx.c @@ -1058,8 +1058,9 @@ static inline void pxl8_fill_scanline_textured_hicolor( gfx->frame_pixel_count++; f32 u = u0, v = v0; if (!gfx->affine_textures && fabsf(w0) > 1e-6f) { - u /= w0; - v /= w0; + f32 perspective_w = 1.0f / w0; + u *= perspective_w; + v *= perspective_w; } u32 color = pxl8_sample_texture(gfx, texture_id, u, v); if (color & 0xFF000000) { @@ -1089,8 +1090,9 @@ static inline void pxl8_fill_scanline_textured_hicolor( gfx->frame_pixel_count++; f32 tex_u = u, tex_v = v; if (!gfx->affine_textures && fabsf(w) > 1e-6f) { - tex_u /= w; - tex_v /= w; + f32 perspective_w = 1.0f / w; + tex_u *= perspective_w; + tex_v *= perspective_w; } u32 color = pxl8_sample_texture(gfx, texture_id, tex_u, tex_v); if (color & 0xFF000000) { @@ -1131,8 +1133,9 @@ static inline void pxl8_fill_scanline_textured_indexed( gfx->frame_pixel_count++; f32 u = u0, v = v0; if (!gfx->affine_textures && fabsf(w0) > 1e-6f) { - u /= w0; - v /= w0; + f32 perspective_w = 1.0f / w0; + u *= perspective_w; + v *= perspective_w; } u32 color = pxl8_sample_texture(gfx, texture_id, u, v); if (color != 0) { @@ -1162,8 +1165,9 @@ static inline void pxl8_fill_scanline_textured_indexed( gfx->frame_pixel_count++; f32 tex_u = u, tex_v = v; if (!gfx->affine_textures && fabsf(w) > 1e-6f) { - tex_u /= w; - tex_v /= w; + f32 perspective_w = 1.0f / w; + tex_u *= perspective_w; + tex_v *= perspective_w; } u32 color = pxl8_sample_texture(gfx, texture_id, tex_u, tex_v); if (color != 0) { @@ -1418,21 +1422,21 @@ void pxl8_3d_draw_triangle_textured( f32 z_tmp; pxl8_project_to_screen(gfx, cv0, &tv[0].x, &tv[0].y, &z_tmp); tv[0].z = z_tmp; - tv[0].u = gfx->affine_textures ? u0 : u0 * cv0.w; - tv[0].v = gfx->affine_textures ? v0f : v0f * cv0.w; - tv[0].w = cv0.w; + tv[0].u = gfx->affine_textures ? u0 : u0 / cv0.w; + tv[0].v = gfx->affine_textures ? v0f : v0f / cv0.w; + tv[0].w = gfx->affine_textures ? 1.0f : 1.0f / cv0.w; pxl8_project_to_screen(gfx, cv1, &tv[1].x, &tv[1].y, &z_tmp); tv[1].z = z_tmp; - tv[1].u = gfx->affine_textures ? u1 : u1 * cv1.w; - tv[1].v = gfx->affine_textures ? v1f : v1f * cv1.w; - tv[1].w = cv1.w; + tv[1].u = gfx->affine_textures ? u1 : u1 / cv1.w; + tv[1].v = gfx->affine_textures ? v1f : v1f / cv1.w; + tv[1].w = gfx->affine_textures ? 1.0f : 1.0f / cv1.w; pxl8_project_to_screen(gfx, cv2, &tv[2].x, &tv[2].y, &z_tmp); tv[2].z = z_tmp; - tv[2].u = gfx->affine_textures ? u2 : u2 * cv2.w; - tv[2].v = gfx->affine_textures ? v2f : v2f * cv2.w; - tv[2].w = cv2.w; + tv[2].u = gfx->affine_textures ? u2 : u2 / cv2.w; + tv[2].v = gfx->affine_textures ? v2f : v2f / cv2.w; + tv[2].w = gfx->affine_textures ? 1.0f : 1.0f / cv2.w; if (gfx->backface_culling) { i32 cross =