diff --git a/programs/city.c b/programs/city.c index 7f4f758..71b91c9 100644 --- a/programs/city.c +++ b/programs/city.c @@ -111,7 +111,7 @@ void sampleTexture(uint8_t *texture, int32_t u, int32_t v, uint8_t *r, uint8_t * } uint32_t previousTriangle = -1; -S3L_Unit uv0[2], uv1[2], uv2[2]; +S3L_Vec4 uv0, uv1, uv2; void drawPixel(S3L_PixelInfo *p) { @@ -131,28 +131,7 @@ void drawPixel(S3L_PixelInfo *p) uvs = carUVs; } - int16_t index; - - index = p->triangleIndex * 3; - - int16_t i0 = uvIndices[index]; - int16_t i1 = uvIndices[index + 1]; - int16_t i2 = uvIndices[index + 2]; - - index = i0 * 2; - - uv0[0] = uvs[index]; - uv0[1] = uvs[index + 1]; - - index = i1 * 2; - - uv1[0] = uvs[index]; - uv1[1] = uvs[index + 1]; - - index = i2 * 2; - - uv2[0] = uvs[index]; - uv2[1] = uvs[index + 1]; + S3L_getIndexedTriangleValues(p->triangleIndex,uvIndices,uvs,2,&uv0,&uv1,&uv2); previousTriangle = p->triangleID; } @@ -161,8 +140,8 @@ void drawPixel(S3L_PixelInfo *p) S3L_Unit uv[2]; - uv[0] = S3L_interpolateBarycentric(uv0[0],uv1[0],uv2[0],p->barycentric); - uv[1] = S3L_interpolateBarycentric(uv0[1],uv1[1],uv2[1],p->barycentric); + uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric); + uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric); sampleTexture(cityTexture,uv[0] / 2,uv[1] / 2,&r,&g,&b); diff --git a/programs/hqOffline.c b/programs/hqOffline.c index 81643c1..52a018e 100644 --- a/programs/hqOffline.c +++ b/programs/hqOffline.c @@ -90,7 +90,7 @@ S3L_Vec4 toLightDirection; S3L_Vec4 n0, n1, n2, v0, v1, v2; -S3L_Unit uv0[2], uv1[2], uv2[2]; +S3L_Vec4 uv0, uv1, uv2; void sampleTexture(uint8_t *texture, int w, int h, float x, float y, uint8_t color[3]) { @@ -170,59 +170,25 @@ void drawPixel(S3L_PixelInfo *p) if (p->triangleID != previousTriangle) { - int index = scene.models[p->modelIndex].triangles[p->triangleIndex * 3] * 3; - n0.x = normals[index]; - v0.x = scene.models[p->modelIndex].vertices[index]; - index++; - n0.y = normals[index]; - v0.y = scene.models[p->modelIndex].vertices[index]; - index++; - n0.z = normals[index]; - v0.z = scene.models[p->modelIndex].vertices[index]; + S3L_getIndexedTriangleValues( + p->triangleIndex, + scene.models[p->modelIndex].triangles, + normals,3,&n0,&n1,&n2); - index = scene.models[p->modelIndex].triangles[p->triangleIndex * 3 + 1] * 3; - - n1.x = normals[index]; - v1.x = scene.models[p->modelIndex].vertices[index]; - index++; - n1.y = normals[index]; - v1.y = scene.models[p->modelIndex].vertices[index]; - index++; - n1.z = normals[index]; - v1.z = scene.models[p->modelIndex].vertices[index]; - - index = scene.models[p->modelIndex].triangles[p->triangleIndex * 3 + 2] * 3; - - n2.x = normals[index]; - v2.x = scene.models[p->modelIndex].vertices[index]; - index++; - n2.y = normals[index]; - v2.y = scene.models[p->modelIndex].vertices[index]; - index++; - n2.z = normals[index]; - v2.z = scene.models[p->modelIndex].vertices[index]; + S3L_getIndexedTriangleValues( + p->triangleIndex, + scene.models[p->modelIndex].triangles, + scene.models[p->modelIndex].vertices,3,&v0,&v1,&v2); if (p->modelIndex != WATER_MODEL_INDEX && p->modelIndex != ISLAND_MODEL_INDEX) { - index = treeUVIndices[p->triangleIndex * 3] * 2; - uv0[0] = treeUVs[index]; - index++; - uv0[1] = treeUVs[index]; - - index = treeUVIndices[p->triangleIndex * 3 + 1] * 2; - - uv1[0] = treeUVs[index]; - index++; - uv1[1] = treeUVs[index]; - - index = treeUVIndices[p->triangleIndex * 3 + 2] * 2; - - uv2[0] = treeUVs[index]; - index++; - uv2[1] = treeUVs[index]; + S3L_getIndexedTriangleValues( + p->triangleIndex, + scene.models[p->modelIndex].triangles, + treeUVs,2,&uv0,&uv1,&uv2); } previousTriangle = p->triangleID; @@ -297,8 +263,8 @@ void drawPixel(S3L_PixelInfo *p) specularIntensity = 0.2; specularPower = 20.0; - u = S3L_interpolateBarycentric(uv0[0],uv1[0],uv2[0],p->barycentric) / ((float) S3L_FRACTIONS_PER_UNIT); - v = S3L_interpolateBarycentric(uv0[1],uv1[1],uv2[1],p->barycentric) / ((float) S3L_FRACTIONS_PER_UNIT); + u = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric) / ((float) S3L_FRACTIONS_PER_UNIT); + v = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric) / ((float) S3L_FRACTIONS_PER_UNIT); } S3L_normalizeVec3(&normal); diff --git a/programs/level.c b/programs/level.c index f7500c4..af3cc91 100644 --- a/programs/level.c +++ b/programs/level.c @@ -103,29 +103,7 @@ void drawPixel(S3L_PixelInfo *p) texture = level3Texture; } - int16_t index; - - index = p->triangleIndex * 3; - - int16_t i0 = uvIndices[index]; - int16_t i1 = uvIndices[index + 1]; - int16_t i2 = uvIndices[index + 2]; - - index = i0 * 2; - - uv0.x = uvs[index]; - uv0.y = uvs[index + 1]; - - index = i1 * 2; - - uv1.x = uvs[index]; - uv1.y = uvs[index + 1]; - - index = i2 * 2; - - uv2.x = uvs[index]; - uv2.y = uvs[index + 1]; - + S3L_getIndexedTriangleValues(p->triangleIndex,uvIndices,uvs,2,&uv0,&uv1,&uv2); previousTriangle = p->triangleID; } diff --git a/small3dlib.h b/small3dlib.h index 9c6a43b..44a67db 100644 --- a/small3dlib.h +++ b/small3dlib.h @@ -521,7 +521,9 @@ void S3L_triangleNormal(S3L_Vec4 t0, S3L_Vec4 t1, S3L_Vec4 t2, for each triangle, each index pointing into 'values' array, which contains the values, each one consisting of 'numComponents' components (e.g. 2 for UV coordinates). The three values are retrieved into 'v0', 'v1' and 'v2' - vectors (into x, y, z and w, depending on 'numComponents'). */ + vectors (into x, y, z and w, depending on 'numComponents'). This function is + meant to be used per-triangle (typically from a cache), NOT per-pixel, as it + is not as fast as possible! */ void S3L_getIndexedTriangleValues( S3L_Index triangleIndex,