集成数据库GLM
图形或者游戏开发中,必不可少需要用到矩阵、向量的运算。
GLM是最常用的数学库,对OpenGL支持非常好。
这节简述GLM集成过程.
clone GLM库
glm地址: github.com/g-truc/glm
以submodule形式集成到项目中
bash
git submodule add https://github.com/g-truc/glm Sandbox/Hazel/vendor/glm
如果你的git不能正常访问github,也可以手动下载下来,copy到vendor目录下
CMake更新
GLM只有头文件,所以只需要添加include目录
Sandbox/Hazel/CMakeLists.txt
bash
target_include_directories(${PROJECT_NAME} PUBLIC
vendor/spdlog/include
vendor/GLFW/include
vendor/imgui
vendor/imgui/backends
vendor/glm
...
测试GLM
参考GLM readme.md说明文档
在SandBoxApp中写一段测试GLM的代码,这段测试代码是生成一个相机矩阵,如果不理解先不用管这是干啥的.
Sandbox/src/SandBoxApp.cpp
c++
#include <glm/vec3.hpp> // glm::vec3
#include <glm/vec4.hpp> // glm::vec4
#include <glm/mat4x4.hpp> // glm::mat4
#include <glm/ext/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective
#include <glm/ext/scalar_constants.hpp> // glm::pi
glm::mat4 camera(float Translate, glm::vec2 const& Rotate)
{
glm::mat4 Projection = glm::perspective(glm::pi<float>() * 0.25f, 4.0f / 3.0f, 0.1f, 100.f);
glm::mat4 View = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -Translate));
View = glm::rotate(View, Rotate.y, glm::vec3(-1.0f, 0.0f, 0.0f));
View = glm::rotate(View, Rotate.x, glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 Model = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f));
return Projection * View * Model;
}
class ExampleLayer : public Hazel::Layer {
public:
ExampleLayer() : Layer("Example") {
auto cam = camera(1.0, glm::vec2());
HZ_INFO("GLM test ------ {0}", cam[0][0]);
}
能正常运行,看到日志即可