From a7657c09519c6a1c6fc003f8793d4328f61e3297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Tue, 18 Jun 2019 18:26:17 +0200 Subject: [PATCH] Add triangleID to PixelInfo --- programs/city.c | 6 ++---- programs/hqOffline.c | 6 ++---- programs/modelViewer.c | 4 ++-- small3dlib.h | 10 ++++++++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/programs/city.c b/programs/city.c index 9cdf21c..cf0a625 100644 --- a/programs/city.c +++ b/programs/city.c @@ -75,12 +75,11 @@ void sampleTexture(uint8_t *texture, int32_t u, int32_t v, uint8_t *r, uint8_t * } int16_t previousTriangle = -1; -int16_t previousModel = -1; S3L_Unit uv0[2], uv1[2], uv2[2]; void drawPixel(S3L_PixelInfo *p) { - if (p->triangleIndex != previousTriangle || p->modelIndex != previousModel) + if (p->triangleID != previousTriangle) { S3L_Index *uvIndices; S3L_Unit *uvs; @@ -119,8 +118,7 @@ void drawPixel(S3L_PixelInfo *p) uv2[0] = uvs[index]; uv2[1] = uvs[index + 1]; - previousTriangle = p->triangleIndex; - previousModel = p->modelIndex; + previousTriangle = p->triangleID; } uint8_t r,g,b; diff --git a/programs/hqOffline.c b/programs/hqOffline.c index 883c0cd..95d9a49 100644 --- a/programs/hqOffline.c +++ b/programs/hqOffline.c @@ -85,7 +85,6 @@ S3L_Model3D models[MODELS_TOTAL]; S3L_Scene scene; int previousTriangle = -1; -int previousModel = -1; S3L_Vec4 toLightDirection; @@ -169,7 +168,7 @@ void drawPixel(S3L_PixelInfo *p) normals = waterNormals; break; } - if (p->triangleIndex != previousTriangle || p->modelIndex != previousModel) + if (p->triangleID != previousTriangle) { int index = scene.models[p->modelIndex].triangles[p->triangleIndex * 3] * 3; @@ -226,8 +225,7 @@ void drawPixel(S3L_PixelInfo *p) uv2[1] = treeUVs[index]; } - previousTriangle = p->triangleIndex; - previousModel = p->modelIndex; + previousTriangle = p->triangleID; } S3L_correctBarycentricCoords(p->barycentric); diff --git a/programs/modelViewer.c b/programs/modelViewer.c index c0957e4..ef9e374 100644 --- a/programs/modelViewer.c +++ b/programs/modelViewer.c @@ -158,7 +158,7 @@ S3L_Vec4 n0, n1, n2, nt; void drawPixel(S3L_PixelInfo *p) { - if (p->triangleIndex != previousTriangle) + if (p->triangleID != previousTriangle) { int16_t index; @@ -253,7 +253,7 @@ void drawPixel(S3L_PixelInfo *p) l2 = 256 + S3L_clamp(S3L_dotProductVec3(n2,toLight),-511,511) / 2; } - previousTriangle = p->triangleIndex; + previousTriangle = p->triangleID; } if (wire) diff --git a/small3dlib.h b/small3dlib.h index cedd772..0a63f49 100644 --- a/small3dlib.h +++ b/small3dlib.h @@ -472,8 +472,11 @@ typedef struct for the price of some performance). The sum of the three coordinates will always be exactly S3L_FRACTIONS_PER_UNIT. */ - S3L_Index triangleIndex; ///< Triangle index. - S3L_Index modelIndex; + S3L_Index modelIndex; ///< Model index within the scene. + S3L_Index triangleIndex; ///< Triangle index within the model. + uint32_t triangleID; /**< Unique ID of the triangle withing the whole + scene. This can be used e.g. by a cache to + quickly find out if a triangle has changed. */ S3L_Unit depth; ///< Depth (only if depth is turned on). S3L_Unit previousZ; /**< Z-buffer value (not necessarily world depth in S3L_Units!) that was in the z-buffer on the @@ -1444,7 +1447,9 @@ void S3L_initPixelInfo(S3L_PixelInfo *p) // TODO: maybe non-pointer for p p->barycentric[0] = S3L_FRACTIONS_PER_UNIT; p->barycentric[1] = 0; p->barycentric[2] = 0; + p->modelIndex = 0; p->triangleIndex = 0; + p->triangleID = 0; p->depth = 0; p->previousZ = 0; } @@ -1631,6 +1636,7 @@ void S3L_drawTriangle( S3L_initPixelInfo(&p); p.modelIndex = modelIndex; p.triangleIndex = triangleIndex; + p.triangleID = modelIndex << 16 | triangleIndex; #if !S3L_STRICT_NEAR_CULLING point0.z = point0.z >= S3L_NEAR ? point0.z : S3L_NEAR;