1
0
Fork 0
2018-soft-3d-renderer/Source/Transform.cpp

106 lines
1.8 KiB
C++

#include "Matrix.hpp"
#include "Transform.hpp"
#include <cmath>
Matrix Transform_Translate(Point& translation)
{
Matrix result;
result.e41 = translation.x;
result.e42 = translation.y;
result.e43 = translation.z;
return result;
}
Matrix Transform_Rotate(Point& rotation)
{
// YXZ Euler rotation
float cosThetaY = cosf(rotation.y);
float sinThetaY = sinf(rotation.y);
Matrix tRotateY;
tRotateY.e11 = cosThetaY;
tRotateY.e13 = -sinThetaY;
tRotateY.e31 = sinThetaY;
tRotateY.e33 = cosThetaY;
float cosThetaX = cosf(rotation.x);
float sinThetaX = sinf(rotation.x);
Matrix tRotateX;
tRotateX.e22 = cosThetaX;
tRotateX.e23 = sinThetaX;
tRotateX.e32 = -sinThetaX;
tRotateX.e33 = cosThetaX;
float cosThetaZ = cosf(rotation.z);
float sinThetaZ = sinf(rotation.z);
Matrix tRotateZ;
tRotateZ.e11 = cosThetaZ;
tRotateZ.e12 = sinThetaZ;
tRotateZ.e21 = -sinThetaZ;
tRotateZ.e22 = cosThetaZ;
Matrix result = tRotateY * tRotateX * tRotateZ;
return result;
}
Matrix Transform_Scale(float scale)
{
Matrix result;
result.e11 = scale;
result.e22 = scale;
result.e33 = scale;
return result;
}
Matrix Transform_View(Camera& camera)
{
Point invPosition = -camera.position;
Matrix tInvTranslate = Transform_Translate(invPosition);
Point invRotation = -camera.rotation;
Matrix tInvRotate = Transform_Rotate(invRotation);
Matrix result = tInvTranslate * tInvRotate;
return result;
}
Matrix Transform_Perspective(Camera& camera)
{
Matrix result;
result.e11 = camera.xZoom;
result.e22 = camera.yZoom;
result.e33 = camera.zClipBias0;
result.e34 = 1;
result.e43 = camera.zClipBias1;
return result;
}
Matrix Transform_Screen(Camera& camera)
{
Matrix result;
result.e11 = camera.xScale;
result.e41 = camera.xScale;
result.e22 = -camera.yScale;
result.e42 = camera.yScale;
return result;
}