From 41caa6030dd8a1d1770be94a0e413ee67af138d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Fri, 28 Jun 2019 01:25:32 +0200 Subject: [PATCH] Fix index overflows --- programs/highPoly.c | 14 ++++++++------ small3dlib.h | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/programs/highPoly.c b/programs/highPoly.c index 1082423..32fde45 100644 --- a/programs/highPoly.c +++ b/programs/highPoly.c @@ -82,19 +82,21 @@ void drawPixel(S3L_PixelInfo *p) S3L_normalizeVec3(&normal); - uint8_t shading = - S3L_clamp((S3L_dotProductVec3(normal,toLight) + S3L_FRACTIONS_PER_UNIT) / 4,0,255); + S3L_Unit shading = + (S3L_dotProductVec3(normal,toLight) + S3L_FRACTIONS_PER_UNIT) / 2; + + shading = S3L_interpolate(shading,0,p->depth,32 * S3L_FRACTIONS_PER_UNIT); int index = (p->y * S3L_RESOLUTION_X + p->x) * 3; - frameBuffer[index] = shading; - frameBuffer[index + 1] = shading; - frameBuffer[index + 2] = shading; + frameBuffer[index] = S3L_clamp(S3L_interpolateByUnitFrom0(200,shading),0,255); + frameBuffer[index + 1] = S3L_clamp(S3L_interpolateByUnitFrom0(255,shading),0,255); + frameBuffer[index + 2] = S3L_clamp(S3L_interpolateByUnitFrom0(150,shading),0,255); } int main() { - S3L_setVec4(&toLight,10,10,10,0); + S3L_setVec4(&toLight,10,-10,-10,0); S3L_normalizeVec3(&toLight); diff --git a/small3dlib.h b/small3dlib.h index 38f7710..79e6ea6 100644 --- a/small3dlib.h +++ b/small3dlib.h @@ -982,7 +982,7 @@ void S3L_getIndexedTriangleValues( S3L_Vec4 *v1, S3L_Vec4 *v2) { - S3L_Index i0, i1; + uint32_t i0, i1; S3L_Unit *value; i0 = triangleIndex * 3; @@ -1046,7 +1046,7 @@ void S3L_computeModelNormals(S3L_Model3D model, S3L_Unit *dst, (model.triangles[j + 2] == i)) { S3L_Vec4 t0, t1, t2; - S3L_Index vIndex; + uint32_t vIndex; #define getVertex(n)\ vIndex = model.triangles[j + n] * 3;\ @@ -2437,7 +2437,7 @@ void _S3L_projectVertex( S3L_Vec4 *result, S3L_Unit focalLength) { - S3L_Index vertexIndex = model->triangles[triangleIndex * 3 + vertex] * 3; + uint32_t vertexIndex = model->triangles[triangleIndex * 3 + vertex] * 3; result->x = model->vertices[vertexIndex]; result->y = model->vertices[vertexIndex + 1];