mirror of
https://git.coom.tech/drummyfish/small3dlib.git
synced 2024-11-20 20:29:58 +01:00
Compute normals
This commit is contained in:
parent
c48d868007
commit
c9bcb0ace5
2 changed files with 66 additions and 6 deletions
|
@ -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]];
|
||||
|
|
16
small3dlib.h
16
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);
|
||||
|
|
Loading…
Reference in a new issue