From e3274d7fc1cf8af74b245dfc8d559c187f6d144d Mon Sep 17 00:00:00 2001 From: erwincoumans Date: Tue, 27 Oct 2020 13:38:01 -0700 Subject: [PATCH] Update model.cpp fix uv wrapping, thanks to Marek --- examples/TinyRenderer/model.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/examples/TinyRenderer/model.cpp b/examples/TinyRenderer/model.cpp index 6bbaa5167..1f5bebc1b 100644 --- a/examples/TinyRenderer/model.cpp +++ b/examples/TinyRenderer/model.cpp @@ -166,14 +166,23 @@ TGAColor Model::diffuse(Vec2f uvf) // bool repeat = true; // if (repeat) { - uvf[0] = std::modf(uvf[0], &val); - uvf[1] = std::modf(uvf[1], &val); - } - Vec2i uv(uvf[0] * diffusemap_.get_width(), uvf[1] * diffusemap_.get_height()); + uvf[0] = std::modf(uvf[0], &val); + if (uvf[0] < 0) + { + uvf[0] = uvf[0] + 1; + } + uvf[1] = std::modf(uvf[1], &val); + if (uvf[1] < 0) + { + uvf[1] = uvf[1] + 1; + } + } + Vec2i uv(uvf[0] * diffusemap_.get_width(), uvf[1] * diffusemap_.get_height()); return diffusemap_.get(uv[0], uv[1]); } return TGAColor(255, 255, 255, 255); } + Vec3f Model::normal(Vec2f uvf) {