view matrix + half projection matrix

This commit is contained in:
SepComet 2026-03-16 14:30:57 +08:00
parent 67e4855211
commit d550baa11e
4 changed files with 38 additions and 8 deletions

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
# Disable line-ending normalization in this repository.
* -text

View File

@ -92,5 +92,10 @@ namespace Math
0, 0, 0, 1
);
}
static Vector3 cross(const Vector3& vec1, const Vector3& vec2)
{
return vec1.cross(vec2);
}
};
}

View File

@ -2,6 +2,7 @@
#include "Matrix4x4.h"
#include "Camera.h"
#include <Vector3.h>
#include "MathUtil.h"
namespace Scene
{
@ -9,20 +10,20 @@ namespace Scene
{
using namespace Math;
Vector3 forward = (target - position).normalized();
Vector3 right = forward.cross(up).normalized();
if (right.magnitude() < 1e-5) right = forward.cross(Vector3(0, 1, 0));
Vector3 cameraUp = right.cross(forward);
Vector3 backward = (position - target).normalized();
Vector3 right = MathUtil::cross(up, backward).normalized();
if (right.magnitude() < 1e-5) right = MathUtil::cross(Vector3(0, 1, 0), backward);
Vector3 cameraUp = MathUtil::cross(backward, right);
return Matrix4x4(
right.x, right.y, right.z, -right.dot(position),
cameraUp.x, cameraUp.y, cameraUp.z, -cameraUp.dot(position),
-forward.x, -forward.y, -forward.z, forward.dot(position),
backward.x, backward.y, backward.z, -backward.dot(position),
0, 0, 0, 1
);
}
Math::Matrix4x4 Camera::get_projection_matrix(float aspectRatio) const
Math::Matrix4x4 Camera::get_orthographic_projection_matrix(float aspectRatio) const
{
using namespace Math;
@ -30,4 +31,24 @@ namespace Scene
);
}
Math::Matrix4x4 Camera::get_perspective_projection_matrix(float width, float height) const
{
using namespace Math;
const float l = -width * 0.5f;
const float r = width * 0.5f;
const float b = -height * 0.5f;
const float t = height * 0.5f;
const float n = nearPlane;
const float f = farPlane;
const float length = n - f;
return Matrix4x4(
2.0f / width, 0, 0, -(r + l) / (r - l) + 1,
0, 2.0f / height, 0, -(t + b) / (t - b) + 1,
0, 0, 2.0f / length, -(n + f) / (n - f) + 1,
0, 0, 0, 1
);
}
}

View File

@ -38,6 +38,8 @@ namespace Scene
Math::Matrix4x4 get_view_matrix() const;
Math::Matrix4x4 get_projection_matrix(float aspectRatio) const;
Math::Matrix4x4 get_orthographic_projection_matrix(float aspectRatio) const;
Math::Matrix4x4 get_perspective_projection_matrix(float width, float height) const;
};
}