From 0caeb1c1acb4d926925987241b4a1338eb02fc93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sun, 16 Sep 2018 17:21:34 +0200 Subject: [PATCH] Optimize texture coords --- raycastlib.h | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index 1416ca4..bbc754a 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -1000,18 +1000,33 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray) Unit wallLength = pref##Z2Screen - pref##Z1Screen - 1;\ wallLength = wallLength != 0 ? wallLength : 1;\ Unit wallPosition = absVal(pref##Z1Screen - pref##PosY) inc (-1);\ - for (i = pref##PosY inc 1; i comp##= limit; inc##inc i)\ - {\ - p.position.y = i;\ - p.hit = hit;\ - if (COMPUTE_WALL_TEXCOORDS == 1)\ - {\ - p.texCoords.x = hit.textureCoord;\ - p.texCoords.y = (wallPosition * UNITS_PER_SQUARE) / wallLength;\ + Unit coordStep = UNITS_PER_SQUARE / wallLength;\ + p.texCoords.y = wallPosition * coordStep;\ + if (coordStep < MIN_TEXTURE_STEP) /* two versions of the loop */ \ + for (i = pref##PosY inc 1; i comp##= limit; inc##inc i)\ + { /* more expensive texture coord computing */\ + p.position.y = i;\ + p.hit = hit;\ + if (COMPUTE_WALL_TEXCOORDS == 1)\ + {\ + p.texCoords.x = hit.textureCoord;\ + p.texCoords.y = (wallPosition * UNITS_PER_SQUARE)/wallLength;\ + }\ + wallPosition++;\ + _pixelFunction(&p);\ + }\ + else\ + for (i = pref##PosY inc 1; i comp##= limit; inc##inc i)\ + { /* cheaper texture coord computing */\ + p.position.y = i;\ + p.hit = hit;\ + if (COMPUTE_WALL_TEXCOORDS == 1)\ + {\ + p.texCoords.x = hit.textureCoord;\ + p.texCoords.y += coordStep;\ + }\ + _pixelFunction(&p);\ }\ - wallPosition++;\ - _pixelFunction(&p);\ - }\ if (pref##PosY comp limit)\ pref##PosY = limit;\ pref##Z1World = pref##Z2World; /* for the next iteration */\