文章目录
1.摄像机/观察空间
以摄像机为原点的右手坐标系,x轴向右,z轴从屏幕指向自己(因此摄像机方向是z轴负方向)
如何构造模拟一个摄像机:
①摄像机位置:随便设置一个,如glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
②摄像机方向:设置指向目标(如原点),glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);
然后计算方向cameraDirection = cameraTarget - cameraPos,计算完的方向指向z轴负方向,如果要让其指向正方向,则用cameraDirection = cameraPos - cameraTarget
③摄像机右轴(x轴):定义上向量,用上向量叉乘cameraDirection,
即:glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraDirection));
④摄像机上轴(y轴):cameraDirection叉乘x轴
glm::vec3 cameraUp = glm::cross(cameraDirection, cameraRight);
2.LookAt(观察矩阵)
R为右向量,U为上向量,D为方向向量,P为位置
①问题:为什么平移向量带负号?
假设摄像机在世界空间下,从原点移动到p点,则P为平移向量,但因为实际上摄像机移动的实现是世界向相反的方向移动,即在观察空间下,世界向-P方向移动等价于摄像机在世界空间下向P方向移动
②glm下的LookAt:
glm::mat4 view;
view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
三个参数:位置、目标、上向量
3.欧拉角
练习
代码部分
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb_image.h>
#include <iostream>
#include <filesystem>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <LearnOPenGL/shader_m.h>
#include <LearnOPenGL/camera.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <learnopengl/shader_m.h>
#include <learnopengl/camera.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
// camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
// timing
float deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f;
//mix
float mixValue = 0.2f;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
// tell GLFW to capture our mouse
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST)