From 7124e58b5eb8bdee858f158ce4f235328450d16a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Mon, 10 Sep 2018 08:03:09 +0200 Subject: [PATCH] Add texture averaging --- demo2.cpp | 9 ++++++++- general.hpp | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/demo2.cpp b/demo2.cpp index c2c93e7..9fb6bac 100644 --- a/demo2.cpp +++ b/demo2.cpp @@ -19,6 +19,7 @@ Player player; #define SHOT_SPEED 5 * UNITS_PER_SQUARE #define INFO_BAR_START 70 +#define TEXTURE_DISTANCE (UNITS_PER_SQUARE * 6) const unsigned char level[] = { @@ -469,6 +470,7 @@ const unsigned char spritePlasma[] = }; const unsigned char *textures[] = {texture1, texture2, texture3, texture4}; +unsigned char textureAverageColors[4]; Unit floorHeightAt(int16_t x, int16_t y) { @@ -508,7 +510,9 @@ inline void pixelFunc(PixelInfo pixel) if (pixel.isWall) { - c = sampleImage(textures[pixel.hit.type],pixel.hit.textureCoord,pixel.textureCoordY); + c = pixel.depth > TEXTURE_DISTANCE ? + textureAverageColors[pixel.hit.type] : + sampleImage(textures[pixel.hit.type],pixel.hit.textureCoord,pixel.textureCoordY); if (previousColumn == pixel.position.x) { @@ -562,6 +566,9 @@ int main() { initGeneral(); + for (uint8_t i = 0; i < 4; ++i) + textureAverageColors[i] = computeAverageColor(textures[i]); + cFloor = rgbToIndex(0,1,1); cCeiling = rgbToIndex(1,0,1); diff --git a/general.hpp b/general.hpp index df63115..8b55e08 100644 --- a/general.hpp +++ b/general.hpp @@ -236,4 +236,21 @@ void initGeneral() zBuffer[i] = 0; } +unsigned char computeAverageColor(const unsigned char *texture) +{ + uint32_t sumR = 0; + uint32_t sumG = 0; + uint32_t sumB = 0; + uint32_t pixels = texture[0] * texture[1]; + + for (uint16_t i = 0; i < pixels; ++i) + { + sumR += texture[2 + i] & 0b00000111; + sumG += (texture[2 + i] & 0b00111000) >> 3; + sumB += (texture[2 + i] & 0b11000000) >> 6; + } + + return rgbToIndex(sumR / pixels,sumG / pixels,sumB / pixels); +} + #endif