From d550baa11e6c5fbf84441e4dccac1a6dcc66a099 Mon Sep 17 00:00:00 2001 From: SepComet <2428390463@qq.com> Date: Mon, 16 Mar 2026 14:30:57 +0800 Subject: [PATCH] view matrix + half projection matrix --- .gitattributes | 2 ++ CPU-Software-Renderer/Math/MathUtil.h | 5 ++++ CPU-Software-Renderer/Scene/Camera.cpp | 35 ++++++++++++++++++++------ CPU-Software-Renderer/Scene/Camera.h | 4 ++- 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f934725 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Disable line-ending normalization in this repository. +* -text diff --git a/CPU-Software-Renderer/Math/MathUtil.h b/CPU-Software-Renderer/Math/MathUtil.h index 8d1208b..996267a 100644 --- a/CPU-Software-Renderer/Math/MathUtil.h +++ b/CPU-Software-Renderer/Math/MathUtil.h @@ -92,5 +92,10 @@ namespace Math 0, 0, 0, 1 ); } + + static Vector3 cross(const Vector3& vec1, const Vector3& vec2) + { + return vec1.cross(vec2); + } }; } \ No newline at end of file diff --git a/CPU-Software-Renderer/Scene/Camera.cpp b/CPU-Software-Renderer/Scene/Camera.cpp index d838be8..0879549 100644 --- a/CPU-Software-Renderer/Scene/Camera.cpp +++ b/CPU-Software-Renderer/Scene/Camera.cpp @@ -2,6 +2,7 @@ #include "Matrix4x4.h" #include "Camera.h" #include +#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 + ); + } +} \ No newline at end of file diff --git a/CPU-Software-Renderer/Scene/Camera.h b/CPU-Software-Renderer/Scene/Camera.h index 5643cc8..f61a089 100644 --- a/CPU-Software-Renderer/Scene/Camera.h +++ b/CPU-Software-Renderer/Scene/Camera.h @@ -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; }; }