1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-21 20:39:57 +01:00

Start sorting

This commit is contained in:
Miloslav Číž 2019-05-28 12:07:25 +02:00
parent 9448f5ca36
commit d083bb7f65
2 changed files with 88 additions and 38 deletions

View file

@ -1790,32 +1790,63 @@ static inline int8_t S3L_triangleIsVisible(
return 1; return 1;
} }
static inline void _S3L_drawModelTriangle( typedef struct
S3L_Scene scene,
S3L_Index modelIndex,
S3L_Index triangleIndex,
S3L_Mat4 *transformMatrix,
S3L_Camera *camera)
{ {
uint8_t modelIndex;
S3L_Index triangleIndex;
uint16_t sortValue;
} S3L_TriangleToSort;
#if S3L_SORT != S3L_SORT_NONE
S3L_TriangleToSort S3L_sortArray[S3L_MAX_TRIANGES_DRAWN];
uint16_t S3L_sortArrayLength;
#endif
void S3L_drawScene(S3L_Scene scene)
{
S3L_Mat4 matFinal, matCamera;
S3L_Vec4 modelVertex, transformed0, transformed1, transformed2; S3L_Vec4 modelVertex, transformed0, transformed1, transformed2;
S3L_Index vertexIndex; S3L_Index vertexIndex;
S3L_Model3D model; S3L_Model3D model;
S3L_Index modelIndex, triangleIndex;
model = scene.models[modelIndex];
modelVertex.w = S3L_FRACTIONS_PER_UNIT; // has to be "1.0" for translation
#define project(n)\ #define project(n)\
vertexIndex = model.triangles[triangleIndex * 3 + n] * 3;\ vertexIndex = model.triangles[triangleIndex * 3 + n] * 3;\
modelVertex.x = model.vertices[vertexIndex];\ modelVertex.x = model.vertices[vertexIndex];\
modelVertex.y = model.vertices[vertexIndex + 1];\ modelVertex.y = model.vertices[vertexIndex + 1];\
modelVertex.z = model.vertices[vertexIndex + 2];\ modelVertex.z = model.vertices[vertexIndex + 2];\
S3L_vec3Xmat4(&modelVertex,transformMatrix);\ S3L_vec3Xmat4(&modelVertex,&matFinal);\
transformed##n.x = modelVertex.x;\ transformed##n.x = modelVertex.x;\
transformed##n.y = modelVertex.y;\ transformed##n.y = modelVertex.y;\
transformed##n.z = modelVertex.z;\ transformed##n.z = modelVertex.z;\
transformed##n.w = S3L_FRACTIONS_PER_UNIT;\ transformed##n.w = S3L_FRACTIONS_PER_UNIT;\
S3L_perspectiveDivide(&transformed##n,camera->focalLength); S3L_perspectiveDivide(&transformed##n,scene.camera.focalLength);
S3L_makeCameraMatrix(scene.camera.transform,&matCamera);
#if S3L_SORT != S3L_SORT_NONE
uint16_t previousModel = 0;
S3L_sortArrayLength = 0;
#endif
for (modelIndex = 0; modelIndex < scene.modelCount; ++modelIndex)
{
S3L_makeWorldMatrix(scene.models[modelIndex].transform,&matFinal);
S3L_mat4Xmat4(&matFinal,&matCamera);
S3L_Index triangleCount = scene.models[modelIndex].triangleCount;
triangleIndex = 0;
#if S3L_SORT != S3L_SORT_NONE
previousModel = modelIndex;
#endif
while (triangleIndex < triangleCount)
{
model = scene.models[modelIndex];
modelVertex.w = S3L_FRACTIONS_PER_UNIT; // has to be "1.0" for translat.
/* TODO: maybe create an option that would use a cache here to not /* TODO: maybe create an option that would use a cache here to not
transform the same point twice? */ transform the same point twice? */
@ -1824,39 +1855,56 @@ static inline void _S3L_drawModelTriangle(
project(1) project(1)
project(2) project(2)
#undef project
if (S3L_triangleIsVisible(transformed0,transformed1,transformed2, if (S3L_triangleIsVisible(transformed0,transformed1,transformed2,
model.config.backfaceCulling)) model.config.backfaceCulling))
{ {
S3L_drawTriangle(transformed0,transformed1,transformed2,&(model.config), #if S3L_SORT == S3L_SORT_NONE
camera,modelIndex,triangleIndex); // without sorting draw right away
S3L_drawTriangle(transformed0,transformed1,transformed2,
&(model.config),&(scene.camera),modelIndex,triangleIndex);
#else
// with sorting add to a sort list
S3L_sortArray[S3L_sortArrayLength].modelIndex = modelIndex;
S3L_sortArray[S3L_sortArrayLength].triangleIndex = triangleIndex;
S3L_sortArray[S3L_sortArrayLength].sortValue =
(transformed0.z + transformed1.z + transformed2.z) >> 2;
S3L_sortArrayLength++;
#endif
} }
}
void S3L_drawScene(S3L_Scene scene)
{
S3L_Mat4 matFinal, matCamera;
S3L_makeCameraMatrix(scene.camera.transform,&matCamera);
for (S3L_Index modelIndex; modelIndex < scene.modelCount; ++modelIndex)
{
S3L_makeWorldMatrix(scene.models[modelIndex].transform,&matFinal);
S3L_mat4Xmat4(&matFinal,&matCamera);
S3L_Index triangleCount = scene.models[modelIndex].triangleCount;
S3L_Index triangleIndex = 0;
while (triangleIndex < triangleCount)
{
_S3L_drawModelTriangle(
scene,modelIndex,triangleIndex,&matFinal,&(scene.camera));
++triangleIndex; ++triangleIndex;
} }
} }
#if S3L_SORT != S3L_SORT_NONE
// TODO: sort
for (S3L_Index i = 0; i < S3L_sortArrayLength; ++i)
{
modelIndex = S3L_sortArray[i].modelIndex;
triangleIndex = S3L_sortArray[i].triangleIndex;
model = scene.models[modelIndex];
modelVertex.w = S3L_FRACTIONS_PER_UNIT; // has to be "1.0" for translat.
if (modelIndex != previousModel)
{
S3L_makeWorldMatrix(model.transform,&matFinal);
S3L_mat4Xmat4(&matFinal,&matCamera);
previousModel = modelIndex;
}
project(0)
project(1)
project(2)
S3L_drawTriangle(transformed0,transformed1,transformed2,
&(model.config),&(scene.camera),modelIndex,triangleIndex);
}
#endif
#undef project
} }
#endif #endif

View file

@ -7,7 +7,9 @@
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#define S3L_Z_BUFFER 1 #define S3L_Z_BUFFER 0
#define S3L_SORT S3L_SORT_BACK_TO_FRONT
#define S3L_PIXEL_FUNCTION drawPixel #define S3L_PIXEL_FUNCTION drawPixel