Learn OpenGL 笔记2.9 - Camera

基础知识:

1.Camera/View space

创建一个以相机位置为原点的具有 3 个垂直单位轴的坐标系。

2.Look At

使用这3 个方向轴加上一个位置向量创建一个矩阵,并且您可以通过乘以将任何向量转换这个矩阵。 这正是 LookAt 矩阵所做的,现在我们有 3 个垂直轴(方向轴)和一个位置向量来定义相机空间,我们可以创建我们自己的 LookAt 矩阵:

Where RR is the right vector, UU is the up vector, DD is the direction vector (三个方向向量)and PP is the camera's position vector. (一个位置向量)

OpenGL中:

glm::mat4 view;
//三个向量分别代表a position 相机位置, target 目标位置 and up vector 上方的方向
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.deltaTime的计算方法

//获取当前时间戳
float currentFrame = glfwGetTime();
//当前时间戳-上次时间戳
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame; 

 4.Euler angles(欧拉角)

Euler angles are 3 values that can represent any rotation in 3D, defined by Leonhard Euler somewhere in the 1700s. There are 3 Euler angles: pitch, yaw and roll.

The pitch is the angle that depicts how much we're looking up or down (上下盼望)as seen in the first image. The second image shows the yaw value which represents the magnitude we're looking to the left or to the right(左右盼望). The roll represents how much we roll as mostly used in space-flight cameras(滚筒洗衣机) 

欧拉角转方向向量:

先处理y轴与xy平面之间的数据

再处理x与y轴之间的数据

得出:

glm::vec3 direction;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(direction);

 

 

代码解析:

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {

        // camera/view transformation
        //第一个参数相机位置,第二个参数相机对准的目标位置=相机位置+相机方向
        glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
        ourShader.setMat4("view", view);
    }


//鼠标控制相机方向
// glfw: whenever the mouse moves, this callback is called
// -------------------------------------------------------
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
    if (firstMouse)
    {
        lastX = xpos;
        lastY = ypos;
        firstMouse = false;
    }

    float xoffset = xpos - lastX;
    float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
    lastX = xpos;
    lastY = ypos;

    float sensitivity = 0.1f; // change this value to your liking
    xoffset *= sensitivity;
    yoffset *= sensitivity;

    yaw += xoffset;
    pitch += yoffset;

    // make sure that when pitch is out of bounds, screen doesn't get flipped
    if (pitch > 89.0f)
        pitch = 89.0f;
    if (pitch < -89.0f)
        pitch = -89.0f;

    glm::vec3 front;
    front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
    front.y = sin(glm::radians(pitch));
    front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
    cameraFront = glm::normalize(front);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值