Learn OpenGL 笔记2.9 - Camera

news/2024/7/19 8:02:52 标签: 图形学, opengles, Unity基础

基础知识:

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


http://www.niftyadmin.cn/n/1205085.html

相关文章

互联网地址

这个真心不容易记住啊&#xff0c;保存下来&#xff0c;备不时只需&#xff0c;过几天就考试了&#xff0c;真心想找回曾经的荣誉。 这个感觉讲的非常好&#xff1a;http://blog.sina.com.cn/s/blog_5753d0930101fxdf.html

存储过程 触发器 视图

视图 视图只是一种逻辑对象&#xff0c;是一种虚拟表&#xff0c;它并不是物理对象&#xff0c;因为视图不占物理存储空间&#xff0c;在视图中被查询的表称为视图的基表&#xff0c;大多数的select语句都可以用在创建视图中 优点&#xff1a;集中用户使用的数据&#xff0c;…

Learn OpenGL 笔记2.8 - Coordinate Systems

基础知识&#xff1a; 1.Coordinate Systems(坐标系) 顶点着色器&#xff0c;把坐标们转换为 normalized device coordinates (NDC) 标准化设备坐标&#xff0c;然后把这些坐标提供给rasterizer光栅化器&#xff0c;进行转换为屏幕上的 2D 坐标/像素。 Local space (or Obje…

二:SQL映射文件

二&#xff1a;SQL映射文件 1.SQL映射文件&#xff1a; &#xff08;1&#xff09;mapper&#xff1a;映射文件的根元素节点&#xff0c;只有一个属性namespace(命名空间) 作用&#xff1a;用于区分不同的mapper全局唯一 绑定dao接口即面向接口编程&#xff0c;当namespace绑定…

再拾算法考题

有股神吗&#xff1f; 有&#xff0c;小赛就是&#xff01; 经过严密的计算&#xff0c;小赛买了一支股票&#xff0c;他知道从他买股票的那天开始&#xff0c;股票会有以下变化&#xff1a;第一天不变&#xff0c;以后涨一天&#xff0c;跌一天&#xff0c;涨两天&#xff0…

Learn OpenGL 笔记3.1 - Colors

基础知识&#xff1a; 1.Colors 颜色使用通常缩写为 RGB 的红色、绿色和蓝色分量以数字方式表示。 仅使用这 3 个值的不同组合&#xff08;每个值在 [0,1] 的范围内)&#xff0c;我们几乎可以表示存在的任何颜色。 2.component-wise multiplication或者element-wise multipl…

Maven打包的三种方式(转载)

转自&#xff1a;http://blog.csdn.net/daiyutage/article/details/53739452

特殊序列点的坐标

C、 有形如以下布局的一组数据&#xff0c;给定一个整数N&#xff0c;计算N在如下数形的第几行第几个&#xff0c; 1 3 2 4 5 6 10 9 8 7 这里关键是开方运算&#xff0c;保证精度。确定每行开始元素的坐标。 代码如下&#xff1a; //Author:Burning Teng package test;…