From 39fa8bcb147918e5f306398481c28d6c3939a51e Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Thu, 16 Feb 2023 18:50:59 +0100 Subject: [PATCH] Apply uv offsets only on non-repeating textures, to avoid overflow errors on textures near the texture page edges. Fixes wonky textures on maps like E1M1 and E1M8. --- tesselate.cpp | 8 ++++---- texture.cpp | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tesselate.cpp b/tesselate.cpp index af74bea..854e5a8 100644 --- a/tesselate.cpp +++ b/tesselate.cpp @@ -42,8 +42,8 @@ std::vector Tesselator::tesselateFace(const face_t* face) // Transform the vertex to texture space and calculate the texture UV bounds Vec3 st = textureTrsf.TransformPoint(vertexPoint); - if (st.x > maxS) maxS = ceil(st.x); if (st.x < minS) minS = floor(st.x); - if (st.y > maxT) maxT = ceil(st.y); if (st.y < minT) minT = floor(st.y); + if (st.x > maxS) maxS = st.x; if (st.x < minS) minS = st.x; + if (st.y > maxT) maxT = st.y; if (st.y < minT) minT = st.y; contour.vertex[edgeListIdx] = gpc_vertex{ st.x, st.y }; } @@ -68,9 +68,9 @@ std::vector Tesselator::tesselateFace(const face_t* face) minT = floor(minT / cellHeight) * cellHeight; } - for (double y = minT; y < maxT; y += cellHeight) + for (double y = floor(minT); y < ceil(maxT); y += cellHeight) { - for (double x = minS; x < maxS; x += cellWidth) + for (double x = floor(minS); x < ceil(maxS); x += cellWidth) { // Create a square polygon that covers the entire cell gpc_polygon cell = { 0 }; diff --git a/texture.cpp b/texture.cpp index cd2d694..5b0e405 100644 --- a/texture.cpp +++ b/texture.cpp @@ -351,9 +351,14 @@ bool process_textures(const world_t* world, TextureList& outTextures) tex.uoffs = (u_char)((x % 64) << (2 - outTim.format)); tex.voffs = (u_char)(y & 0xFF); if (texture_isRepeatable(miptex)) - tex.ps1tex.twin = getTexWindow(tex.uoffs, tex.voffs, tex.w, tex.h); // TODO: figure out the right offsets that are multiples of w and h; NOTE: if uoffs has to >> 1, then w probably has to as well + { + tex.ps1tex.twin = getTexWindow(tex.uoffs, tex.voffs, tex.w, tex.h); + tex.uoffs = tex.voffs = 0; // Apply offset only through the texture window, otherwise we can get overflowing UV values + } else + { tex.ps1tex.twin = getTexWindow(0, 0, 0, 0); + } } }