xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
graphics.cc
浏览该文件的文档.
1#include <gtest/gtest.h>
2
3#include <cstddef>
4#include <glm/glm.hpp>
5#include <glm/gtc/matrix_transform.hpp>
6#include <iostream>
7#include <xcmath/xcmath.hpp>
8//
10
11#include "gtest/gtest.h"
12
13xcmath::mat4f projection(float fov, float aspect, float near_, float far_) {
14 if (fov == 0.0f || aspect == 0.0f || near_ == 0.0f || far_ == 0.0f ||
15 near_ >= far_) {
16 // throw std::runtime_error("Invalid camera parameters");
17 }
18 xcmath::mat4f projectionMatrix{};
19 float f = 1.0f / tan(fov / 2.0f);
20 projectionMatrix[0][0] = f / aspect;
21 projectionMatrix[1][1] = f;
22 projectionMatrix[2][2] = (far_ + near_) / (near_ - far_);
23 projectionMatrix[3][2] = 2.0f * far_ * near_ / (near_ - far_);
24 projectionMatrix[2][3] = -1.0f;
25
26 return projectionMatrix;
27}
28xcmath::mat4f lookAt(const xcmath::vec3f &position, const xcmath::vec3f &target,
29 const xcmath::vec3f &up) {
30 if ((position == target).every()) {
31 // throw std::runtime_error("Position and target are the same");
32 }
33 xcmath::vec3f forward = (target - position).normalize();
34 if (std::abs(forward.dot(up)) >=
35 0.9999f) { // 使用一个阈值来判断是否接近平行
36 // throw std::runtime_error("Forward and up vectors are parallel");
37 }
38 xcmath::vec3f side = forward.cross(up).normalize();
39 xcmath::vec3f upward = side.cross(forward).normalize();
40
41 // 修正视图矩阵的第四行
42 xcmath::mat4f viewMatrix{
43 xcmath::vec4f(side.x(), upward.x(), -forward.x(), 0.0f),
44 xcmath::vec4f(side.y(), upward.y(), -forward.y(), 0.0f),
45 xcmath::vec4f(side.z(), upward.z(), -forward.z(), 0.0f),
46 xcmath::vec4f(-position.x(), -position.y(), -position.z(), 1.0f)};
47
48 // 使用一个小的阈值来检查行列式
49 const float epsilon = 1e-6f;
50 if (std::abs(viewMatrix.det()) < epsilon) {
51 // throw std::runtime_error("Invalid camera parameters");
52 }
53
54 return viewMatrix;
55}
56
57TEST(GraphicsTest, Projection) {
58 xcmath::mat4f projectionMatrix =
59 projection(xcmath::radians(55.0f), 1.321, 0.1f, 1000.0f);
60 glm::mat4 glmProjectionMatrix =
61 glm::perspective(glm::radians(55.0f), 1.321f, 0.1f, 1000.0f);
62 std::cout << projectionMatrix << std::endl;
63 for (int i = 0; i < 4; i++) {
64 for (int j = 0; j < 4; j++) {
65 std::cout << glmProjectionMatrix[i][j] << " ";
66 }
67 std::cout << std::endl;
68 }
69 for (size_t i; i < 16; i++) {
70 EXPECT_FLOAT_EQ(((&projectionMatrix[0][0]))[i],
71 (&glmProjectionMatrix[0][0])[i]);
72 }
73}
74
75TEST(GraphicsTest, LookAt) {
76 xcmath::mat4f viewMatrix =
77 lookAt(xcmath::vec3f(0.0f, 0.0f, 3.0f), xcmath::vec3f(0.0f, 0.0f, 1.0f),
78 xcmath::vec3f(0.0f, 1.0f, 0.0f));
79 glm::mat4 glmViewMatrix =
80 glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), glm::vec3(0.0f, 0.0f, 1.0f),
81 glm::vec3(0.0f, 1.0f, 0.0f));
82
83 std::cout << viewMatrix << std::endl;
84 for (int i = 0; i < 4; i++) {
85 for (int j = 0; j < 4; j++) {
86 std::cout << glmViewMatrix[i][j] << " ";
87 }
88 std::cout << std::endl;
89 }
90}
Matrix class template
Definition mat.hpp:27
Vector class template
Definition vec.hpp:206
constexpr _Tp dot(const vec< _Tp, _length > &other) const
Compute Euclidean inner product
Definition vec.hpp:631
constexpr _Tp & x()
Get the item at index 0
Definition vec.hpp:454
constexpr vec< _Tp, _length > cross(const vec< _Tp, _length > &other) const
Compute 3D cross product
Definition vec.hpp:617
constexpr _Tp & y()
Get the item at index 1
Definition vec.hpp:465
constexpr _Tp & z()
Get the item at index 2
Definition vec.hpp:476
xcmath::mat4f lookAt(const xcmath::vec3f &position, const xcmath::vec3f &target, const xcmath::vec3f &up)
Definition graphics.cc:28
xcmath::mat4f projection(float fov, float aspect, float near_, float far_)
Definition graphics.cc:13
TEST(GraphicsTest, Projection)
Definition graphics.cc:57
constexpr T radians(T degrees)
Convert degrees to radians
Definition function.hpp:25
vec4< float > vec4f