1
0
Fork 0
2018-soft-3d-renderer/src/transform.cpp

105 lines
1.9 KiB
C++
Raw Normal View History

#include "matrix.h"
#include "transform.h"
#include <cmath>
// PUBLIC FUNCTIONS
Matrix Transform_Translate(float x, float y, float z)
{
Matrix result;
result.e41 = x;
result.e42 = y;
result.e43 = z;
return result;
}
Matrix Transform_Rotate(float x, float y, float z)
{
// YXZ Euler rotation
float cosThetaY = cosf(y);
float sinThetaY = sinf(y);
Matrix tRotateY;
tRotateY.e11 = cosThetaY;
tRotateY.e13 = -sinThetaY;
tRotateY.e31 = sinThetaY;
tRotateY.e33 = cosThetaY;
float cosThetaX = cosf(x);
float sinThetaX = sinf(x);
Matrix tRotateX;
tRotateX.e22 = cosThetaX;
tRotateX.e23 = sinThetaX;
tRotateX.e32 = -sinThetaX;
tRotateX.e33 = cosThetaX;
float cosThetaZ = cosf(z);
float sinThetaZ = sinf(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 s)
{
Matrix result;
result.e11 = s;
result.e22 = s;
result.e33 = s;
return result;
}
Matrix Transform_View(Point &position, float rotation[])
{
Matrix tInvTranslate = Transform_Translate(-position.x, -position.y, -position.z);
Matrix tInvRotate = Transform_Rotate(-rotation[0], -rotation[1], -rotation[2]);
Matrix result = tInvTranslate * tInvRotate;
return result;
}
Matrix Transform_Perspective(float zoomX, float zoomY, float nearClip, float farClip)
{
Matrix result;
result.e11 = zoomX;
result.e22 = zoomY;
result.e33 = (farClip + nearClip) / (farClip - nearClip);
result.e34 = 1;
result.e43 = (-2.0f * farClip * nearClip) / (farClip - nearClip);
return result;
}
Matrix Transform_Screen(float xScale, float yScale)
{
Matrix result;
result.e11 = xScale;
result.e41 = xScale;
result.e22 = -yScale;
result.e42 = yScale;
return result;
}