From c9bcb0ace535d241e32d9dade9db4e82f9697f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Thu, 6 Jun 2019 01:08:29 +0200 Subject: [PATCH] Compute normals --- programs/testSDL.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ small3dlib.h | 16 ++++++++----- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/programs/testSDL.c b/programs/testSDL.c index 8569827..3366c7a 100644 --- a/programs/testSDL.c +++ b/programs/testSDL.c @@ -134,6 +134,62 @@ int previousTriangle = 255; void drawPixel(S3L_PixelInfo *p) { + +S3L_Vec4 a,b,c,n,V; + +int tmpI = scene.models[p->modelIndex].triangles[p->triangleIndex * 3] * 3; + +a.x = scene.models[p->modelIndex].vertices[tmpI]; +tmpI++; +a.y = scene.models[p->modelIndex].vertices[tmpI]; +tmpI++; +a.z = scene.models[p->modelIndex].vertices[tmpI]; + +tmpI = scene.models[p->modelIndex].triangles[p->triangleIndex * 3 + 1] * 3; + +b.x = scene.models[p->modelIndex].vertices[tmpI]; +tmpI++; +b.y = scene.models[p->modelIndex].vertices[tmpI]; +tmpI++; +b.z = scene.models[p->modelIndex].vertices[tmpI]; + +tmpI = scene.models[p->modelIndex].triangles[p->triangleIndex * 3 + 2] * 3; + +c.x = scene.models[p->modelIndex].vertices[tmpI]; +tmpI++; +c.y = scene.models[p->modelIndex].vertices[tmpI]; +tmpI++; +c.z = scene.models[p->modelIndex].vertices[tmpI]; + +S3L_triangleNormal(a,b,c,&n); +/* +printf("--------\n"); +S3L_logVec4(a); +S3L_logVec4(b); +S3L_logVec4(c); +S3L_logVec4(n); +*/ +V.x = 10; +V.y = 10; +V.z = 10; + +S3L_normalizeVec3(&V); + +int16_t l = S3L_clamp(S3L_dotProductVec3(V,n) / 2,0,255); + +l &= 192; + +/* +setPixel(p->x,p->y, + S3L_clamp(128 + n.x / 4,0,255), + S3L_clamp(128 + n.y / 4,0,255), + S3L_clamp(128 + n.z / 4,0,255)); +*/ + +setPixel(p->x,p->y,l,l,l); + +return; + if (p->triangleIndex != previousTriangle) { l0 = houseVertexLighting[houseTriangleIndices[p->triangleIndex * 3]]; diff --git a/small3dlib.h b/small3dlib.h index ea7af70..e5cb308 100644 --- a/small3dlib.h +++ b/small3dlib.h @@ -774,13 +774,17 @@ void S3L_crossProduct(S3L_Vec4 a, S3L_Vec4 b, S3L_Vec4 *result) void S3L_triangleNormal(S3L_Vec4 t0, S3L_Vec4 t1, S3L_Vec4 t2, S3L_Vec4 *n) { - t1.x = t1.x - t0.x; - t1.y = t1.y - t0.y; - t1.z = t1.z - t0.z; + #define antiOverflow 32 - t2.x = t2.x - t0.x; - t2.y = t2.y - t0.y; - t2.z = t2.z - t0.z; + t1.x = (t1.x - t0.x) / antiOverflow; + t1.y = (t1.y - t0.y) / antiOverflow; + t1.z = (t1.z - t0.z) / antiOverflow; + + t2.x = (t2.x - t0.x) / antiOverflow; + t2.y = (t2.y - t0.y) / antiOverflow; + t2.z = (t2.z - t0.z) / antiOverflow; + + #undef antiOverflow S3L_crossProduct(t1,t2,n); S3L_normalizeVec3(n);