From 2b4516f408a42e9857d9589e8ff0620f5328415f Mon Sep 17 00:00:00 2001 From: Miloslav Ciz Date: Wed, 31 Jul 2024 16:07:25 +0200 Subject: [PATCH] Improve renderer a bit --- assets.h | 46 ++++++++++++++++++++++++++++++++-------------- renderer.h | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/assets.h b/assets.h index f946d57..d1a86e7 100644 --- a/assets.h +++ b/assets.h @@ -25,11 +25,19 @@ static const uint8_t map1[] = 10, 10, 0, - LCR_MAP_BLOCK(LCR_BLOCK_FULL,32,32,32,LCR_BLOCK_MATERIAL_CONCRETE,0), - LCR_MAP_BLOCK(LCR_BLOCK_FULL,33,32,32,LCR_BLOCK_MATERIAL_CONCRETE,0), + LCR_MAP_BLOCK(LCR_BLOCK_FULL,32,0,32,LCR_BLOCK_MATERIAL_CONCRETE,0), + LCR_MAP_BLOCK(LCR_BLOCK_FULL,33,63,32,LCR_BLOCK_MATERIAL_CONCRETE,0), LCR_MAP_BLOCK(LCR_BLOCK_RAMP_CURVED_WALL,34,32,32,LCR_BLOCK_MATERIAL_CONCRETE,/*LCR_BLOCK_TRANSFORM_ROT_90*/0), LCR_MAP_BLOCK(LCR_BLOCK_RAMP_CURVED_WALL,35,32,32,LCR_BLOCK_MATERIAL_CONCRETE,LCR_BLOCK_TRANSFORM_ROT_90), + LCR_MAP_BLOCK(LCR_BLOCK_FULL,38,35,32,LCR_BLOCK_MATERIAL_CONCRETE,0), + LCR_MAP_BLOCK(LCR_BLOCK_FULL,39,35,32,LCR_BLOCK_MATERIAL_CONCRETE,0), + LCR_MAP_BLOCK(LCR_BLOCK_FULL,40,35,32,LCR_BLOCK_MATERIAL_CONCRETE,0), + LCR_MAP_BLOCK(LCR_BLOCK_FULL,41,35,32,LCR_BLOCK_MATERIAL_CONCRETE,0), + LCR_MAP_BLOCK(LCR_BLOCK_FULL,42,35,32,LCR_BLOCK_MATERIAL_CONCRETE,0), + LCR_MAP_BLOCK(LCR_BLOCK_FULL,43,35,32,LCR_BLOCK_MATERIAL_CONCRETE,0), + LCR_MAP_BLOCK(LCR_BLOCK_FULL,44,35,32,LCR_BLOCK_MATERIAL_CONCRETE,0), + LCR_MAP_BLOCK(LCR_BLOCK_BOTTOM_LEFT,34,32,33,LCR_BLOCK_MATERIAL_CONCRETE,LCR_BLOCK_TRANSFORM_ROT_90), LCR_MAP_BLOCK(LCR_BLOCK_FULL,32,33,32,LCR_BLOCK_MATERIAL_CONCRETE,0), @@ -4803,7 +4811,7 @@ static const uint8_t LCR_images[] = 0x00,0x00,0x02,0x02,0x02,0x02,0x02,0x00,0x00,0x00,0x02,0x00,0x02,0x02,0x02,0x02, 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00,0x00,0x00,0x02, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x02,0xc2,0xc2,0x02,0xc2, - 0xc2,0xc2,0xc2,0xbe,0xbe,0xc2,0xc2,0xc2,0xc2,0xc2,0xc2,0xc2,0xc2,0x02,0x02,0x00, + 0xc2,0xc2,0xc2,0x00,0x00,0xc2,0xc2,0xc2,0xc2,0xc2,0xc2,0xc2,0xc2,0x02,0x02,0x00, 0x00,0x00,0x00,0x02,0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, @@ -4811,7 +4819,7 @@ static const uint8_t LCR_images[] = 0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x02, 0x02,0x02,0x02,0x02,0x02,0xc2,0x02,0x02,0x02,0x02,0x02,0x00,0x00,0x02,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0xc2,0xc2, - 0xc2,0xc2,0xbe,0xbe,0xc2,0xc2,0xc2,0xc2,0xc2,0xc2,0xc2,0x02,0x02,0x02,0x02,0x02, + 0xc2,0xc2,0x00,0x00,0xc2,0xc2,0xc2,0xc2,0xc2,0xc2,0xc2,0x02,0x02,0x02,0x02,0x02, 0x00,0x00,0x00,0x02,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, @@ -4865,22 +4873,22 @@ static const uint8_t LCR_images[] = 0x01,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0xbb,0xbb,0xbb, 0xbb,0xbb,0x03,0x03,0x03,0x03,0x3a,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x02,0x02,0x02,0x02,0xc2,0x02,0x02,0x02,0x02,0x02,0x02,0x02, - 0x02,0xbe,0x02,0x02,0x02,0xc2,0x02,0x02,0x00,0x00,0x02,0x02,0x01,0x00,0x02,0x02, + 0x02,0x00,0x02,0x02,0x02,0xc2,0x02,0x02,0x00,0x00,0x02,0x02,0x01,0x00,0x02,0x02, 0x01,0x02,0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0xc2,0x02, 0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xcb,0x00,0x00, 0x00,0x00,0x00,0x00,0xcb,0x02,0x02,0xc2,0x02,0xcb,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x01,0xd2,0x01,0x01,0x01,0x01,0x01,0x03,0x01,0x03,0x03,0x03,0x03,0xd2, 0x01,0x03,0x03,0xbb,0xbb,0x03,0x03,0xbb,0xbb,0xbb,0x03,0xbb,0xbb,0xbb,0xbb,0xbb, 0xbb,0x97,0x03,0x03,0x03,0x03,0x03,0x03,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0xc2,0x02,0x02,0x02,0xbe, - 0x02,0xbe,0xbe,0xc2,0xc2,0x02,0x02,0x02,0x02,0x02,0x02,0x01,0x02,0x01,0x01,0x02, + 0x00,0x00,0x00,0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0xc2,0x02,0x02,0x02,0x00, + 0x02,0x00,0x00,0xc2,0xc2,0x02,0x02,0x02,0x02,0x02,0x02,0x01,0x02,0x01,0x01,0x02, 0x02,0x01,0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x02,0x02,0x02, 0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xcb,0x00,0x02,0x02,0xcb,0x02,0x02,0x02,0x02,0x02,0x00,0x00,0x00, 0x00,0x00,0x00,0xd2,0x01,0x01,0x01,0x01,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03, 0x03,0x03,0x03,0xbb,0xbb,0x03,0x03,0xbb,0xbb,0xbb,0xbb,0xbb,0xbb,0x03,0xbb,0xbb, 0xbb,0x97,0x97,0x97,0x97,0xbb,0xbb,0x03,0x03,0x01,0x01,0x01,0x01,0x01,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x02,0x02,0x02,0x02,0x02,0xbe,0x02,0xbe,0x02, + 0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x02,0x02,0x02,0x02,0x02,0x00,0x02,0x00,0x02, 0x02,0x02,0xc2,0x02,0x02,0x02,0x00,0x01,0x01,0x02,0x00,0x02,0x02,0x02,0x01,0x01, 0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x01,0x01,0x00,0x00,0x02,0x02,0x02, 0x02,0x02,0x02,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xcb, @@ -4888,7 +4896,7 @@ static const uint8_t LCR_images[] = 0x01,0x01,0xd2,0x00,0x01,0xd2,0xd2,0x03,0x03,0x03,0x01,0x01,0x03,0x03,0x03,0x03, 0x03,0x03,0x03,0x03,0x03,0x03,0x03,0xbb,0xbb,0xbb,0xbb,0xbb,0xbb,0x03,0xbb,0xbb, 0x97,0x97,0xbb,0xbb,0x97,0x97,0x03,0xbb,0x03,0x03,0x03,0x03,0x3a,0x01,0x01,0x01, - 0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x02,0xc2,0x02,0x02,0x02,0x02,0x02,0xbe, + 0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x02,0xc2,0x02,0x02,0x02,0x02,0x02,0x00, 0x02,0x02,0x02,0x02,0x00,0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x02,0x02, 0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, @@ -5848,6 +5856,10 @@ void LCR_loadImage(unsigned int index) _LCR_currentImagePixel = _LCR_currentImage; } +/** + Changes currently loaded image's brightness either up or down by modifying + its palette. This can be used for shading. +*/ void LCR_imageChangeBrightness(int up) { if (up) @@ -5859,16 +5871,22 @@ void LCR_imageChangeBrightness(int up) ((_LCR_currentImagePalette[i] >> 1) & 0x7bef); } +/** + Samples currently loaded image at given pixels coordinates (with wrapping). + This is slower than reading the image pixel by pixel. +*/ uint16_t LCR_sampleImage(int x, int y) { - x = (y * LCR_IMAGE_SIZE + x) % (LCR_IMAGE_SIZE * LCR_IMAGE_SIZE); - - if (x < 0) // TODO: optimize - x += LCR_IMAGE_SIZE * LCR_IMAGE_SIZE; - + // TODO: bottleneck, later on optimize here + x = (y % LCR_IMAGE_SIZE) * LCR_IMAGE_SIZE + (x % LCR_IMAGE_SIZE); + x += (x < 0) * (LCR_IMAGE_SIZE * LCR_IMAGE_SIZE); return _LCR_currentImagePalette[_LCR_currentImage[x]]; } +/** + Gets the next pixel of currently loaded image. This is faster than sampling + the image by pixel coordinates. +*/ uint16_t LCR_getNextImagePixel(void) { uint16_t r = _LCR_currentImagePalette[*_LCR_currentImagePixel]; diff --git a/renderer.h b/renderer.h index 504fbeb..a223a3b 100644 --- a/renderer.h +++ b/renderer.h @@ -138,10 +138,8 @@ void _LCR_addMapTriangle(S3L_Index a, S3L_Index b, S3L_Index c) { S3L_Index *t = &(LCR_renderer.mapTriangles[LCR_renderer.mapModel->triangleCount * 3]); - *t = a; - t++; - *t = b; - t++; + *t = a; t++; + *t = b; t++; *t = c; LCR_renderer.mapModel->triangleCount++; @@ -316,11 +314,11 @@ void _LCR_rendererCullHiddenMapTriangles(void) /* We'll be moving the covered triangles to the end of the array, then at the - end we'll just shorten the array. + end we'll just shorten the array by number of removed triangles. */ while (i < LCR_renderer.mapModel->triangleCount - n) { - t2 = t1 + 3; + t2 = t1 + 3; // t2 is the the other triangle against which we check int t1Covered = 0; @@ -334,21 +332,22 @@ void _LCR_rendererCullHiddenMapTriangles(void) { if (j < LCR_renderer.mapModel->triangleCount - n) { - _LCR_rendererSwapTriangles(t2, - LCR_renderer.mapTriangles + (LCR_renderer.mapModel->triangleCount - 1 - n) * 3); + _LCR_rendererSwapTriangles(t2,LCR_renderer.mapTriangles + + (LCR_renderer.mapModel->triangleCount - 1 - n) * 3); n++; } } - t2 += 3; + t2 += 3; // check next triangle } if (t1Covered) { - _LCR_rendererSwapTriangles(t1, - LCR_renderer.mapTriangles + (LCR_renderer.mapModel->triangleCount - 1 - n) * 3); + _LCR_rendererSwapTriangles(t1,LCR_renderer.mapTriangles + + (LCR_renderer.mapModel->triangleCount - 1 - n) * 3); n++; + // we stay at this position because we've swapped the triangle here } else { @@ -357,7 +356,7 @@ void _LCR_rendererCullHiddenMapTriangles(void) } } - LCR_renderer.mapModel->triangleCount -= n; + LCR_renderer.mapModel->triangleCount -= n; // cut off the removed triangles // remove unused vertices: @@ -412,7 +411,9 @@ uint8_t _LCR_rendererBuildMapModel(void) LCR_mapBlockGetTransform(LCR_currentMap.blocks + j * LCR_BLOCK_SIZE), blockShapeBytes,&blockShapeByteCount); - uint8_t vx, vy, vz, vi = 0; + uint8_t vx, vy, vz, vi = 0, + isAtFloor = by == 0, + isAtCeiling = by == LCR_MAP_SIZE_BLOCKS - 1; S3L_Index triangleIndices[3]; S3L_Unit originOffset = -1 * LCR_MAP_SIZE_BLOCKS / 2 * LCR_RENDERER_UNIT; @@ -421,6 +422,9 @@ uint8_t _LCR_rendererBuildMapModel(void) { LCR_decodeMapBlockCoords(blockShapeBytes[i],&vx,&vy,&vz); + isAtFloor &= vy == 0; + isAtCeiling &= vy == 12; + triangleIndices[vi] = _LCR_addMapVertex( originOffset + (((S3L_Unit) bx) * LCR_RENDERER_UNIT) + (LCR_RENDERER_UNIT * ((S3L_Unit) vx)) / 12, (originOffset + (((S3L_Unit) by) * LCR_RENDERER_UNIT)) / 2 + (LCR_RENDERER_UNIT / 2 * ((S3L_Unit) vy)) / 12, @@ -430,13 +434,20 @@ uint8_t _LCR_rendererBuildMapModel(void) vi++; else { - _LCR_addMapTriangle( - triangleIndices[0],triangleIndices[1],triangleIndices[2]); + // don't add triangles completely at the floor or ceiling of the map + if (!isAtFloor && !isAtCeiling) + _LCR_addMapTriangle( + triangleIndices[0],triangleIndices[1],triangleIndices[2]); + vi = 0; + isAtFloor = by == 0; + isAtCeiling = by == LCR_MAP_SIZE_BLOCKS - 1; } } } + // TODO: also cull the triangles in the loop by some N steps + _LCR_rendererCullHiddenMapTriangles(); return 1;