
Added Camera Class Added *= operator to the matrix class Added Transformation Queue vector to the Model Class Added Get Vertex, Polygon and Polygon Vertex methods to the Model Class Added DehomogenizeAllVerticies method to the Model Class Added GetPolygonVertexCount method to Polygon Class Added Ability to have multiple models in a "scene" in the Rasterizer Added DrawWireFrame method to the Rasterizer Class Added Aspect Ratio and View Matrix to the Rasterizer Class Added TransformTools namespace to hold shared transformation functions Added Dehomogenize method to the Vector Class Moved Transformation methods to a new shared name space
90 lines
3.2 KiB
C++
90 lines
3.2 KiB
C++
#include "TransformTools.h"
|
|
|
|
Matrix TransformTools::GetTranslateMatrix(const float x, const float y, const float z)
|
|
{
|
|
return Matrix({ 1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1 });
|
|
}
|
|
|
|
Matrix TransformTools::GetTranslateMatrix(const Vertex& position)
|
|
{
|
|
return GetTranslateMatrix(position.GetX(), position.GetY(), position.GetZ());
|
|
}
|
|
|
|
Matrix TransformTools::GetScaleMatrix(const float x, const float y, const float z)
|
|
{
|
|
return Matrix({ x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 });
|
|
}
|
|
|
|
Matrix TransformTools::GetRotationMatrix(const Axis rotAxis, const float rotDegrees)
|
|
{
|
|
float rotationRadian = DegreesToRadians(rotDegrees);
|
|
switch (rotAxis)
|
|
{
|
|
case (Axis::X):
|
|
return Matrix({ 1, 0, 0, 0, 0, cos(rotationRadian), -sin(rotationRadian), 0, 0, sin(rotationRadian), cos(rotationRadian), 0, 0, 0, 0, 1 });
|
|
case (Axis::Y):
|
|
return Matrix({ cos(rotationRadian), 0, sin(rotationRadian), 0, 0, 1, 0, 0, -sin(rotationRadian), 0, cos(rotationRadian), 0, 0, 0, 0, 1 });
|
|
case (Axis::Z):
|
|
return Matrix({ cos(rotationRadian), -sin(rotationRadian), 0, 0, sin(rotationRadian), cos(rotationRadian), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 });
|
|
default:
|
|
throw "Invalid axis selected";
|
|
}
|
|
}
|
|
|
|
Matrix TransformTools::GetRotationMatrixFromPoint(const Axis rotAxis, const float rotDegrees, const float x, const float y, const float z)
|
|
{
|
|
return GetTranslateMatrix(x, y, z) * GetRotationMatrix(rotAxis, rotDegrees) * GetTranslateMatrix(-x, -y, -z);
|
|
}
|
|
|
|
float TransformTools::DegreesToRadians(const float degrees)
|
|
{
|
|
return (degrees * PI) / 180;
|
|
}
|
|
|
|
Matrix TransformTools::GetOrthegraphicProjectionMatrix(float d)
|
|
{
|
|
return Matrix({ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, d, 0, 0, 0, 1 });
|
|
}
|
|
|
|
Matrix TransformTools::GetPerspectiveProjectionMatrix(float d, float aspectRatio)
|
|
{
|
|
float distanceAspect = d / aspectRatio;
|
|
|
|
return Matrix({ distanceAspect, 0, 0, 0, 0, d, 0, 0, 0, 0, d, 0, 0, 0, 1, 0 });
|
|
}
|
|
|
|
Matrix TransformTools::GetViewMatrix(float d, int width, int height)
|
|
{
|
|
float widthCenter = (float) width / 2.0f;
|
|
float heightCenter = (float) height / 2.0f;
|
|
float distanceCenter = (float) d / 2.0f;
|
|
|
|
return Matrix({ widthCenter, 0, 0, widthCenter, 0, -heightCenter, 0, heightCenter, 0, 0, distanceCenter, distanceCenter, 0, 0, 0, 1 });
|
|
}
|
|
|
|
Matrix TransformTools::GetCameraTranslateMatrix(const float x, const float y, const float z)
|
|
{
|
|
return Matrix({ 1, 0, 0, -x, 0, 1, 0, -y, 0, 0, 1, -z, 0, 0, 0, 1 });
|
|
}
|
|
|
|
Matrix TransformTools::GetCameraTranslateMatrix(const Vertex& position)
|
|
{
|
|
return GetCameraTranslateMatrix(position.GetX(), position.GetY(), position.GetZ());
|
|
}
|
|
|
|
|
|
Matrix TransformTools::GetCameraRotationMatrix(const Axis rotAxis, const float rotDegrees)
|
|
{
|
|
float rotationRadian = DegreesToRadians(rotDegrees);
|
|
switch (rotAxis)
|
|
{
|
|
case (Axis::X):
|
|
return Matrix({ 1, 0, 0, 0, 0, cos(rotationRadian), sin(rotationRadian), 0, 0, -sin(rotationRadian), cos(rotationRadian), 0, 0, 0, 0, 1 });
|
|
case (Axis::Y):
|
|
return Matrix({ cos(rotationRadian), 0, -sin(rotationRadian), 0, 0, 1, 0, 0, sin(rotationRadian), 0, cos(rotationRadian), 0, 0, 0, 0, 1 });
|
|
case (Axis::Z):
|
|
return Matrix({ cos(rotationRadian), sin(rotationRadian), 0, 0, -sin(rotationRadian), cos(rotationRadian), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 });
|
|
default:
|
|
throw "Invalid axis selected";
|
|
}
|
|
} |