From 71b12308801901e2a73d3605c7cadf8826cb8056 Mon Sep 17 00:00:00 2001 From: Miloslav Ciz Date: Wed, 14 Aug 2024 15:19:54 +0200 Subject: [PATCH] Change some names --- renderer.h | 133 +++++++++++++++++++++++++++++------------------------ 1 file changed, 72 insertions(+), 61 deletions(-) diff --git a/renderer.h b/renderer.h index f25dcaf..c1887db 100644 --- a/renderer.h +++ b/renderer.h @@ -33,15 +33,13 @@ struct { S3L_Scene scene; - S3L_Model3D models[9]; /**< 0: whole map model, - 1, 2, 3, 4, 5, 6, 7, 8: nearest map chunks - models */ - /* TODO: separate the whole model from the array? it's confusing if it's - in the array and then doesn't get rendered? */ + + S3L_Model3D mapModel; ///< whole map model + S3L_Model3D models[8]; + /**< 0, 1, 2, 3, 4, 5, 6, 7: nearest map chunk models */ uint8_t loadedChunks[8]; - S3L_Model3D *mapModel; S3L_Unit mapVerts[LCR_SETTING_MAX_MAP_VERTICES * 3]; S3L_Index mapTris[LCR_SETTING_MAX_MAP_TRIANGLES * 3]; @@ -71,7 +69,7 @@ void LCR_pixelFunc3D(S3L_PixelInfo *pixel) if (pixel->triangleIndex != LCR_renderer.previousTriID) { const S3L_Index *t = - LCR_renderer.models[1 + pixel->modelIndex].triangles + + LCR_renderer.models[pixel->modelIndex].triangles + 3 * pixel->triangleIndex; S3L_Unit *v[3]; @@ -174,12 +172,12 @@ void LCR_pixelFunc3D(S3L_PixelInfo *pixel) LCR_drawPixelXYUnsafe(pixel->x,pixel->y,color); } -S3L_Index _LCR_addMapVert(S3L_Unit x, S3L_Unit y, S3L_Unit z) +S3L_Index _LCR_rendererAddMapVert(S3L_Unit x, S3L_Unit y, S3L_Unit z) { S3L_Index index = 0; S3L_Unit *verts = LCR_renderer.mapVerts; - while (index < LCR_renderer.mapModel->vertexCount) // if exists, return index + while (index < LCR_renderer.mapModel.vertexCount) // if exists, return index { if (verts[0] == x && verts[1] == y && verts[2] == z) return index; @@ -189,13 +187,13 @@ S3L_Index _LCR_addMapVert(S3L_Unit x, S3L_Unit y, S3L_Unit z) } // if it doesn't exist, add it - if (LCR_renderer.mapModel->vertexCount < LCR_SETTING_MAX_MAP_VERTICES) + if (LCR_renderer.mapModel.vertexCount < LCR_SETTING_MAX_MAP_VERTICES) { *verts = x; verts++; *verts = y; verts++; *verts = z; - LCR_renderer.mapModel->vertexCount++; - return LCR_renderer.mapModel->vertexCount - 1; + LCR_renderer.mapModel.vertexCount++; + return LCR_renderer.mapModel.vertexCount - 1; } LCR_log("couldn't add map vertex!"); @@ -203,25 +201,25 @@ S3L_Index _LCR_addMapVert(S3L_Unit x, S3L_Unit y, S3L_Unit z) return 0; } -void _LCR_addMapTri(S3L_Index a, S3L_Index b, S3L_Index c, uint8_t mat) +void _LCR_rendererAddMapTri(S3L_Index a, S3L_Index b, S3L_Index c, uint8_t mat) { - if (LCR_renderer.mapModel->triangleCount < LCR_SETTING_MAX_MAP_TRIANGLES) + if (LCR_renderer.mapModel.triangleCount < LCR_SETTING_MAX_MAP_TRIANGLES) { S3L_Index *t = - &(LCR_renderer.mapTris[LCR_renderer.mapModel->triangleCount * 3]); + &(LCR_renderer.mapTris[LCR_renderer.mapModel.triangleCount * 3]); *t = a; t++; *t = b; t++; *t = c; - LCR_renderer.mapTriangleData[LCR_renderer.mapModel->triangleCount] = + LCR_renderer.mapTriangleData[LCR_renderer.mapModel.triangleCount] = mat; - LCR_renderer.mapModel->triangleCount++; + LCR_renderer.mapModel.triangleCount++; } } -void _LCR_swapMapTris(unsigned int index1, unsigned int index2) +void _LCR_rendererSwapMapTris(unsigned int index1, unsigned int index2) { uint8_t tmpMat; S3L_Index tmpIndex, @@ -240,7 +238,7 @@ void _LCR_swapMapTris(unsigned int index1, unsigned int index2) LCR_renderer.mapTriangleData[index2] = tmpMat; } -int _LCR_quadCoversTri(const S3L_Unit quad[8], const S3L_Unit tri[6]) +int _LCR_rendererQuadCoversTri(const S3L_Unit quad[8], const S3L_Unit tri[6]) { for (int i = 0; i < 3; ++i) // for each triangle point { @@ -284,7 +282,7 @@ int _LCR_quadCoversTri(const S3L_Unit quad[8], const S3L_Unit tri[6]) other, in return values lowest bit means whether t1 is covered and the second lowest bit means whether t2 is covered. */ -uint8_t _LCR_checkMapTriCover(const S3L_Index *t1, +uint8_t _LCR_rendererCheckMapTriCover(const S3L_Index *t1, const S3L_Index *t2) { if ((t1[0] == t2[0] || t1[0] == t2[1] || t1[0] == t2[2]) && @@ -338,7 +336,7 @@ uint8_t _LCR_checkMapTriCover(const S3L_Index *t1, points2D[13] = (4 * points2D[7] + 3 * points2D[9] + points2D[11]) / 8; // first: does the triangle alone cover the other one? - if (_LCR_quadCoversTri(points2D + 6,points2D)) + if (_LCR_rendererQuadCoversTri(points2D + 6,points2D)) result |= 1 << j; else { @@ -346,7 +344,7 @@ uint8_t _LCR_checkMapTriCover(const S3L_Index *t1, S3L_Index *t3 = LCR_renderer.mapTris; - for (int i = 0; i < LCR_renderer.mapModel->triangleCount; ++i) + for (int i = 0; i < LCR_renderer.mapModel.triangleCount; ++i) { uint8_t sharedVerts = (t3[0] == t2[0] || t3[0] == t2[1] || t3[0] == t2[2]) | @@ -372,7 +370,7 @@ uint8_t _LCR_checkMapTriCover(const S3L_Index *t1, points2D[12] = LCR_renderer.mapVerts[3 * t3[freeVert] + coordX]; points2D[13] = LCR_renderer.mapVerts[3 * t3[freeVert] + coordY]; - if (_LCR_quadCoversTri(points2D + 6,points2D)) + if (_LCR_rendererQuadCoversTri(points2D + 6,points2D)) { result |= 1 << j; break; @@ -419,24 +417,24 @@ void _LCR_cullHiddenMapTris(void) We'll be moving the covered triangles to the end of the array, then at the end we'll just shorten the array by number of removed triangles. */ - while (i < LCR_renderer.mapModel->triangleCount - n) + while (i < LCR_renderer.mapModel.triangleCount - n) { t2 = t1 + 3; // t2 is the the other triangle against which we check int t1Covered = 0; - for (int j = i + 1; j < LCR_renderer.mapModel->triangleCount; ++j) + for (int j = i + 1; j < LCR_renderer.mapModel.triangleCount; ++j) { - uint8_t cover = _LCR_checkMapTriCover(t1,t2); + uint8_t cover = _LCR_rendererCheckMapTriCover(t1,t2); t1Covered |= cover & 0x01; if (cover & 0x02) { - if (j < LCR_renderer.mapModel->triangleCount - n) + if (j < LCR_renderer.mapModel.triangleCount - n) { - _LCR_swapMapTris(j, - LCR_renderer.mapModel->triangleCount - 1 - n); + _LCR_rendererSwapMapTris(j, + LCR_renderer.mapModel.triangleCount - 1 - n); n++; } @@ -447,8 +445,8 @@ void _LCR_cullHiddenMapTris(void) if (t1Covered) { - _LCR_swapMapTris(i, - LCR_renderer.mapModel->triangleCount - 1 - n); + _LCR_rendererSwapMapTris(i, + LCR_renderer.mapModel.triangleCount - 1 - n); n++; // we stay at this position because we've swapped the triangle here @@ -460,17 +458,17 @@ void _LCR_cullHiddenMapTris(void) } } - LCR_renderer.mapModel->triangleCount -= n; // cut off the removed triangles + LCR_renderer.mapModel.triangleCount -= n; // cut off the removed triangles // remove unused vertices: i = 0; - while (i < LCR_renderer.mapModel->vertexCount) + while (i < LCR_renderer.mapModel.vertexCount) { int used = 0; - for (int j = 0; j < LCR_renderer.mapModel->triangleCount * 3; ++j) + for (int j = 0; j < LCR_renderer.mapModel.triangleCount * 3; ++j) if (LCR_renderer.mapTris[j] == i) { used = 1; @@ -483,13 +481,13 @@ void _LCR_cullHiddenMapTris(void) { for (int j = 0; j < 3; ++j) LCR_renderer.mapVerts[3 * i + j] = - LCR_renderer.mapVerts[(LCR_renderer.mapModel->vertexCount - 1) * 3 + j]; + LCR_renderer.mapVerts[(LCR_renderer.mapModel.vertexCount - 1) * 3 + j]; - for (int j = 0; j < LCR_renderer.mapModel->triangleCount * 3; ++j) - if (LCR_renderer.mapTris[j] == LCR_renderer.mapModel->vertexCount - 1) + for (int j = 0; j < LCR_renderer.mapModel.triangleCount * 3; ++j) + if (LCR_renderer.mapTris[j] == LCR_renderer.mapModel.vertexCount - 1) LCR_renderer.mapTris[j] = i; - LCR_renderer.mapModel->vertexCount--; + LCR_renderer.mapModel.vertexCount--; } } } @@ -515,7 +513,7 @@ void _LCR_makeMapChunks(void) chunkCorner[1] -= LCR_MAP_SIZE_BLOCKS * LCR_RENDERER_UNIT / 4; chunkCorner[2] -= LCR_MAP_SIZE_BLOCKS * LCR_RENDERER_UNIT / 2; - for (int i = start; i < LCR_renderer.mapModel->triangleCount; ++i) + for (int i = start; i < LCR_renderer.mapModel.triangleCount; ++i) { const S3L_Unit *v = LCR_renderer.mapVerts + 3 * tri[0]; @@ -526,7 +524,7 @@ void _LCR_makeMapChunks(void) v[2] >= chunkCorner[2] && v[2] < chunkCorner[2] + LCR_RENDERER_CHUNK_SIZE_HORIZONTAL) { - _LCR_swapMapTris(i,start); + _LCR_rendererSwapMapTris(i,start); start++; } @@ -546,7 +544,8 @@ uint8_t _LCR_buildMapModel(void) uint8_t blockShapeBytes[LCR_MAP_BLOCK_SHAPE_MAX_BYTES]; uint8_t blockShapeByteCount; - S3L_model3DInit(LCR_renderer.mapVerts,0,LCR_renderer.mapTris,0,LCR_renderer.mapModel); + S3L_model3DInit(LCR_renderer.mapVerts,0,LCR_renderer.mapTris,0, + &LCR_renderer.mapModel); for (int j = 0; j < LCR_currentMap.blockCount; ++j) { @@ -584,7 +583,7 @@ uint8_t _LCR_buildMapModel(void) ((vx == 0) << 2) | ((vx == LCR_BLOCK_SHAPE_COORD_MAX) << 3) | ((vz == 0) << 4) | ((vz == LCR_BLOCK_SHAPE_COORD_MAX) << 5); - triIndices[vi] = _LCR_addMapVert( + triIndices[vi] = _LCR_rendererAddMapVert( originOffset + (((S3L_Unit) bx) * LCR_RENDERER_UNIT) + (LCR_RENDERER_UNIT * ((S3L_Unit) vx)) / LCR_BLOCK_SHAPE_COORD_MAX, (originOffset + (((S3L_Unit) by) * LCR_RENDERER_UNIT)) / 2 + @@ -647,7 +646,7 @@ uint8_t _LCR_buildMapModel(void) } } - _LCR_addMapTri(triIndices[0],triIndices[1],triIndices[2],triData); + _LCR_rendererAddMapTri(triIndices[0],triIndices[1],triIndices[2],triData); } vi = 0; @@ -664,7 +663,7 @@ uint8_t _LCR_buildMapModel(void) return 1; } -void _LCR_computeLOD(void) +void _LCR_rendererComputeLOD(void) { LCR_log("computing LOD"); @@ -689,17 +688,15 @@ uint8_t LCR_rendererInit(void) { LCR_log("initializing renderer"); - LCR_renderer.mapModel = LCR_renderer.models; - if (!_LCR_buildMapModel()) return 0; _LCR_makeMapChunks(); - _LCR_computeLOD(); + _LCR_rendererComputeLOD(); S3L_sceneInit( - LCR_renderer.models + 1,8,&LCR_renderer.scene); + LCR_renderer.models,8,&LCR_renderer.scene); return 1; } @@ -809,7 +806,7 @@ void LCR_rendererDrawRect(int x, int y, unsigned int w, unsigned int h, } } -void _LCR_drawLODBlock(int blockX, int blockY, int blockZ, unsigned int size, +void _LCR_rendererDrawLODBlock(int blockX, int blockY, int blockZ, unsigned int size, uint16_t color) { S3L_Vec4 p, r; @@ -965,15 +962,15 @@ void LCR_rendererDrawSky(int sky, S3L_Unit offsetH, S3L_Unit offsetV) } } -void _LCR_loadMapChunk(uint8_t chunk, int8_t x, int8_t y, int8_t z) +void _LCR_rendererLoadMapChunk(uint8_t chunk, int8_t x, int8_t y, int8_t z) { - LCR_renderer.models[chunk + 1] = LCR_renderer.models[0]; + LCR_renderer.models[chunk] = LCR_renderer.mapModel; if (x < 0 || x >= LCR_RENDERER_CHUNK_RESOLUTION || y < 0 || y >= LCR_RENDERER_CHUNK_RESOLUTION || z < 0 || z >= LCR_RENDERER_CHUNK_RESOLUTION) { - LCR_renderer.models[chunk + 1].triangleCount = 0; + LCR_renderer.models[chunk].triangleCount = 0; LCR_renderer.loadedChunks[chunk] = 0; } else @@ -984,21 +981,25 @@ void _LCR_loadMapChunk(uint8_t chunk, int8_t x, int8_t y, int8_t z) int triCount = (blockNum == LCR_RENDERER_CHUNKS_TOTAL - 1 ? - (LCR_renderer.mapModel->triangleCount - 1) : + (LCR_renderer.mapModel.triangleCount - 1) : LCR_renderer.chunkStarts[blockNum + 1]) - LCR_renderer.chunkStarts[blockNum]; if (triCount < 0) triCount = 0; - LCR_renderer.models[chunk + 1].triangles = + LCR_renderer.models[chunk].triangles = LCR_renderer.mapTris + LCR_renderer.chunkStarts[blockNum] * 3; - LCR_renderer.models[chunk + 1].triangleCount = triCount; + LCR_renderer.models[chunk].triangleCount = triCount; } } -void _LCR_loadMapChunks(void) +/** + Loads the map models with 8 chunks that are nearest to a certain point + towards which camera is looking. +*/ +void _LCR_rendererLoadMapChunks(void) { int8_t camChunk[3], chunkOffsets[3]; S3L_Vec4 cp = LCR_renderer.scene.camera.transform.translation; @@ -1029,14 +1030,18 @@ void _LCR_loadMapChunks(void) (cf.z >= (LCR_RENDERER_CHUNK_SIZE_HORIZONTAL / 2)) ? 1 : -1; for (uint8_t i = 0; i < 8; ++i) - _LCR_loadMapChunk(i, + _LCR_rendererLoadMapChunk(i, camChunk[0] + ((i & 0x01) ? chunkOffsets[0] : 0), camChunk[1] + ((i & 0x02) ? chunkOffsets[1] : 0), camChunk[2] + ((i & 0x04) ? chunkOffsets[2] : 0)); } +/** + Draws the LOD overlay. +*/ void LCR_rendererDrawLOD(void) { +#if LCR_SETTING_LOD_DISTANCE < 64 for (unsigned int i = 0; i < LCR_RENDERER_LOD_BLOCKS; ++i) if (LCR_renderer.gridLOD[i]) { @@ -1046,18 +1051,24 @@ void LCR_rendererDrawLOD(void) bz = (i / 8) * 8 + 4; by = (i % 8) * 8 + 4; + int var = 0; + for (unsigned int j = 0; j < 8; ++j) { if (byte & 0x01) { + var = var < 2 ? var + 1 : 0; bx = j * 8 + 4; - _LCR_drawLODBlock(bx,by,bz,(LCR_EFFECTIVE_RESOLUTION_X * 3) / 2, - LCR_SETTING_LOD_COLOR); - } // TODO: this ^ + + _LCR_rendererDrawLODBlock(bx,by,bz, + (LCR_EFFECTIVE_RESOLUTION_X * (2 + var)) / 2, + LCR_SETTING_LOD_COLOR | (0x4108 >> var)); + } byte >>= 1; } } +#endif } void LCR_rendererDraw(void) @@ -1065,7 +1076,7 @@ void LCR_rendererDraw(void) LCR_renderer.previousTriID = -1; S3L_newFrame(); - _LCR_loadMapChunks(); + _LCR_rendererLoadMapChunks(); // TODO: call only once in a while? LCR_rendererDrawSky(2, LCR_renderer.scene.camera.transform.rotation.y,