tool classes when you learn opengl——camera class

how to understand the camera class. we should give a few comments about it.
let’s begin the picture.
这里写图片描述

we use the camera position subtract the target position. this gives the reverse vector that the camera forwards.
这里写图片描述

next, we will explain how to get view matrix.
view matrix is in view coordinate system. so we should define three perpendicular axes.
1/ forward axis
2/ up vector
3/ right vector.

we know the position of the camera, and the target we look at. this two position gives the forward vector.
up vector we can define it as the +y axis.
then we can use cross produce to get the right vector. picture like this:
这里写图片描述
last, we can use forward vector cross right vector to get up vector.
after these steps we can get three perpendicular vectors. then we can create the view matrix like this:
这里写图片描述

the following parts will explain how to tune the camera front vector. if we change the camera’s front vector. we will feel the first person viewpoint effect.

we will give a vector to represent the front vector of the camera.
这里写图片描述
vector r is forward vector of the camera.
we get
x = f.cosb.cosa
y = f.sinb
z = f.cosb.sina
then, we should normalized this vector.

last we should know how to change the value by receiving mouse input.
这里写图片描述
we can add the y2-y1 to pitch angle. we can add the x2 - x1 to yaw value.
afther this this key points you will understand the following code segment:

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; 
    lastX = xpos;
    lastY = ypos;

    float sensitivity = 0.05;
    xoffset *= sensitivity;
    yoffset *= sensitivity;

    yaw   += xoffset;
    pitch += yoffset;

    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);
}

we also limit the pitch angle between -89 to 89.

next article will give a simple demo to feel the result by changing the camera view parameters.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值