mirror of
https://git.coom.tech/drummyfish/small3dlib.git
synced 2024-11-23 20:59:58 +01:00
Continue matrices
This commit is contained in:
parent
9eccbe986a
commit
8932e333b6
1 changed files with 25 additions and 31 deletions
56
s3l.h
56
s3l.h
|
@ -263,7 +263,7 @@ static inline void S3L_makeTranslationMat(S3L_Unit offsetX, S3L_Unit offsetY,
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
S3L_Vec4 offset;
|
S3L_Vec4 translation;
|
||||||
S3L_Vec4 rotation; /**< Euler angles. Rortation is applied in this order:
|
S3L_Vec4 rotation; /**< Euler angles. Rortation is applied in this order:
|
||||||
1. z = around z (roll) CW looking along z+
|
1. z = around z (roll) CW looking along z+
|
||||||
2. x = around x (pitch) CW looking along x+
|
2. x = around x (pitch) CW looking along x+
|
||||||
|
@ -273,7 +273,7 @@ typedef struct
|
||||||
|
|
||||||
static inline void S3L_initTransoform3D(S3L_Transform3D *t)
|
static inline void S3L_initTransoform3D(S3L_Transform3D *t)
|
||||||
{
|
{
|
||||||
S3L_initVec4(&(t->offset));
|
S3L_initVec4(&(t->translation));
|
||||||
S3L_initVec4(&(t->rotation));
|
S3L_initVec4(&(t->rotation));
|
||||||
t->scale.x = S3L_FRACTIONS_PER_UNIT;
|
t->scale.x = S3L_FRACTIONS_PER_UNIT;
|
||||||
t->scale.y = S3L_FRACTIONS_PER_UNIT;
|
t->scale.y = S3L_FRACTIONS_PER_UNIT;
|
||||||
|
@ -797,37 +797,24 @@ static inline void S3L_rotate2DPoint(S3L_Unit *x, S3L_Unit *y, S3L_Unit angle)
|
||||||
(angleCos * (*y)) / S3L_FRACTIONS_PER_UNIT;
|
(angleCos * (*y)) / S3L_FRACTIONS_PER_UNIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void S3L_makeWorldMatrix(S3L_Transform3D worldTransform, S3L_Mat4 *m)
|
||||||
|
|
||||||
|
|
||||||
// FIXME: rewrite this to just apply one matrix, EFFICIENCY!!!
|
|
||||||
static inline void S3L_mapModelToWorld(S3L_Vec4 point,
|
|
||||||
S3L_Transform3D *modelTransform, S3L_Vec4 *newPoint)
|
|
||||||
{
|
{
|
||||||
newPoint->x = point.x;
|
S3L_makeTranslationMat(
|
||||||
newPoint->y = point.y;
|
worldTransform.translation.x,
|
||||||
newPoint->z = point.z;
|
worldTransform.translation.y,
|
||||||
|
worldTransform.translation.z,
|
||||||
S3L_rotate2DPoint(&(newPoint->x),&(newPoint->y),modelTransform->rotation.z);
|
m);
|
||||||
S3L_rotate2DPoint(&(newPoint->z),&(newPoint->y),modelTransform->rotation.x);
|
|
||||||
S3L_rotate2DPoint(&(newPoint->z),&(newPoint->x),modelTransform->rotation.y);
|
|
||||||
|
|
||||||
newPoint->x += modelTransform->offset.x;
|
|
||||||
newPoint->y += modelTransform->offset.y;
|
|
||||||
newPoint->z += modelTransform->offset.z;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void S3L_mapWorldToCamera(S3L_Vec4 point,
|
void S3L_makeCameraMatrix(S3L_Transform3D cameraTransform, S3L_Mat4 *m)
|
||||||
S3L_Transform3D *cameraTransform, S3L_Vec4 *newPoint)
|
|
||||||
{
|
{
|
||||||
newPoint->x = point.x - cameraTransform->offset.x;
|
S3L_makeTranslationMat(
|
||||||
newPoint->y = point.y - cameraTransform->offset.y;
|
-1 * cameraTransform.translation.x,
|
||||||
newPoint->z = point.z - cameraTransform->offset.z;
|
-1 * cameraTransform.translation.y,
|
||||||
|
-1 * cameraTransform.translation.z,
|
||||||
|
m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static inline void S3L_mapCameraToScreen(S3L_Vec4 point, S3L_Camera *camera,
|
static inline void S3L_mapCameraToScreen(S3L_Vec4 point, S3L_Camera *camera,
|
||||||
S3L_ScreenCoord *screenX, S3L_ScreenCoord *screenY)
|
S3L_ScreenCoord *screenX, S3L_ScreenCoord *screenY)
|
||||||
{
|
{
|
||||||
|
@ -851,9 +838,17 @@ void S3L_drawModel(
|
||||||
S3L_Index coordIndex = 0;
|
S3L_Index coordIndex = 0;
|
||||||
|
|
||||||
S3L_ScreenCoord sX0, sY0, sX1, sY1, sX2, sY2;
|
S3L_ScreenCoord sX0, sY0, sX1, sY1, sX2, sY2;
|
||||||
S3L_Vec4 pointModel, pointWorld, pointCamera;
|
S3L_Vec4 pointModel;
|
||||||
S3L_Unit indexIndex;
|
S3L_Unit indexIndex;
|
||||||
|
|
||||||
|
pointModel.w = S3L_FRACTIONS_PER_UNIT; // has to be "1.0" for translation
|
||||||
|
|
||||||
|
S3L_Mat4 mat1, mat2;
|
||||||
|
|
||||||
|
S3L_makeWorldMatrix(modelTransform,&mat1);
|
||||||
|
S3L_makeCameraMatrix(camera.transform,&mat2);
|
||||||
|
S3L_mat4Xmat4(&mat1,&mat2);
|
||||||
|
|
||||||
while (triangleIndex < triangleCount)
|
while (triangleIndex < triangleCount)
|
||||||
{
|
{
|
||||||
#define mapCoords(n)\
|
#define mapCoords(n)\
|
||||||
|
@ -864,9 +859,8 @@ void S3L_drawModel(
|
||||||
++indexIndex;\
|
++indexIndex;\
|
||||||
pointModel.z = coords[indexIndex];\
|
pointModel.z = coords[indexIndex];\
|
||||||
++coordIndex;\
|
++coordIndex;\
|
||||||
S3L_mapModelToWorld(pointModel,&modelTransform,&pointWorld);\
|
S3L_vec4Xmat4(&pointModel,&mat1);\
|
||||||
S3L_mapWorldToCamera(pointWorld,&camera.transform,&pointCamera);\
|
S3L_mapCameraToScreen(pointModel,&camera,&sX##n,&sY##n);
|
||||||
S3L_mapCameraToScreen(pointCamera,&camera,&sX##n,&sY##n);
|
|
||||||
|
|
||||||
mapCoords(0)
|
mapCoords(0)
|
||||||
mapCoords(1)
|
mapCoords(1)
|
||||||
|
|
Loading…
Reference in a new issue