OpenGL学习总结(三)
来源:互联网 发布:机构调研数据 编辑:程序博客网 时间:2024/06/06 12:21
构造摄像机/观察矩阵
1、摄像机与观察空间
在总结二中曾经提到过,局部空间要经过模型矩阵、观察矩阵、投影矩阵这三个变换矩阵后才能到达裁剪空间,而模型矩阵和投影矩阵都已经介绍过了,只有观察矩阵没有被提及,因为当我们讨论观察空间(View Space)的时候,是在讨论假设某一点存在一个摄像机并以它的视角作为场景原点,从而要改变场景中的所有顶点坐标,所以观察矩阵把所有的世界坐标变换为相对于摄像机位置与方向的观察坐标。要定义这个摄像机的话,我们需要它在世界空间中的位置、观察的方向、一个指向它右测的向量以及一个指向它上方的向量。
2、摄像机位置
摄像机位置简单来说就是世界空间中一个指向摄像机位置的向量。
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
3、摄像机方向
知道了摄像机位置之后我们只需要给它随便安排一个目标位置即摄像机所要拍摄的位置,然后通过向量减法就能够得到摄像机方向向量。
glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);glm::vec3 cameraDirection = glm::normalize(cameraPos - cameraTarget);
4 、摄像机右轴
右轴代表摄像机空间的x轴的正方向。为获取右向量我们需要先使用一个小技巧:先定义一个上向量(Up Vector)。接下来把上向量和第二步得到的方向向量进行叉乘。两个向量叉乘的结果会同时垂直于两向量,因此我们会得到指向x轴正方向的那个向量(如果我们交换两个向量叉乘的顺序就会得到相反的指向x轴负方向的向量)。
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f); glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraDirection));
5、摄像机上轴
现在我们已经有了x轴向量和z轴向量,获取一个指向摄像机的正y轴向量就相对简单了:我们把右向量和方向向量进行叉乘:
glm::vec3 cameraUp = glm::cross(cameraDirection, cameraRight);
6、LookAt矩阵
使用矩阵的好处之一是如果你使用3个相互垂直(或非线性)的轴定义了一个坐标空间,你可以用这3个轴外加一个平移向量来创建一个矩阵,并且你可以用这个矩阵乘以任何向量来将其变换到那个坐标空间。这正是LookAt矩阵所做的,现在我们有了3个相互垂直的轴和一个定义摄像机空间的位置坐标,我们可以创建我们自己的LookAt矩阵了:
其中R是右向量,U是上向量,D是方向向量P是摄像机位置向量。注意,位置向量是相反的,因为我们最终希望把世界平移到与我们自身移动的相反方向。把这个LookAt矩阵作为观察矩阵可以很高效地把所有世界坐标变换到刚刚定义的观察空间。LookAt矩阵就像它的名字表达的那样:它会创建一个看着(Look at)给定目标的观察矩阵。
幸运的是,GLM已经提供了这些支持。我们要做的只是定义一个摄像机位置,一个目标位置和一个表示世界空间中的上向量的向量(我们计算右向量使用的那个上向量)。接着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));
7、自由移动
为了能够使摄像机自由移动,我们需要将摄像机位置和目标位置用变量表示:
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);//摄像机位置glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);//摄像机方向glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);//世界空间上向量view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);//cameraPos + cameraFront即为摄像机目标位置
使用键盘输入所需要的回调函数:
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode){ ... GLfloat cameraSpeed = 0.05f; if(key == GLFW_KEY_W) cameraPos += cameraSpeed * cameraFront; if(key == GLFW_KEY_S) cameraPos -= cameraSpeed * cameraFront; if(key == GLFW_KEY_A) cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed; if(key == GLFW_KEY_D) cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed; }//实现了通过输入WASD来进行前、左、后、右移动
8、视角变化——欧拉角
欧拉角(Euler Angle)是可以表示3D空间中任何旋转的3个值,由莱昂哈德·欧拉(Leonhard Euler)在18世纪提出。一共有3种欧拉角:俯仰角(Pitch)、偏航角(Yaw)和滚转角(Roll),下面的图片展示了它们的含义:
俯仰角是描述我们如何往上或往下看的角,可以在第一张图中看到。第二张图展示了偏航角,偏航角表示我们往左和往右看的程度。滚转角代表我们如何翻滚摄像机,通常在太空飞船的摄像机中使用。每个欧拉角都有一个值来表示,把三个角结合起来我们就能够计算3D空间中任何的旋转向量了。
因为我们需要的只是方向向量——cameraFront罢了,它没有长短概念(通常我们把方向向量转化成单位向量),所以我们只需要在得到俯仰角或偏航角后通过最基本的三角函数就能够得到方向向量在三维空间中的三个分量——x,y,z。例如:俯仰角
偏航角:
接下来就是通过鼠标输入信息来获得俯仰角和偏航角的改变量:
1、计算鼠标距上一帧的偏移量。2、把偏移量添加到摄像机的俯仰角和偏航角中。3、对偏航角和俯仰角进行最大和最小值的限制。4、计算方向向量。
void mouse_callback(GLFWwindow* window, double xpos, double ypos){ if(firstMouse)//第一次得到鼠标信息时只需要保存坐标即可 { lastX = xpos; lastY = ypos; firstMouse = false; } GLfloat xoffset = xpos - lastX;//获得X轴偏移量 GLfloat yoffset = lastY - ypos;//获得Y轴偏移量 lastX = xpos; lastY = ypos; GLfloat 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);//进行正规化,即变成单位向量}
9、视角缩放
作为我们摄像机系统的一个附加内容,我们还会来实现一个缩放(Zoom)接口。在之前的教程中我们说视野(Field of View)或fov定义了我们可以看到场景中多大的范围。当视野变小时,场景投影出来的空间就会减小,产生放大(Zoom In)了的感觉。我们会使用鼠标的滚轮来放大。与鼠标移动、键盘输入一样,我们需要一个鼠标滚轮的回调函数:
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset){ if(fov >= 1.0f && fov <= 45.0f) fov -= yoffset; if(fov <= 1.0f) fov = 1.0f; if(fov >= 45.0f) fov = 45.0f;}
10、摄像机类
#pragma once#include<vector>#include<GL\glew.h>#include<glm\glm.hpp>#include<glm\gtc\matrix_transform.hpp>enum Camera_Movement{ FORWARD, BACKWARD, LEFT, RIGHT};const GLfloat YAW = -90.0f;//设置初始偏航角const GLfloat PITCH = 0.0f;//设置初始俯仰角const GLfloat SPEED = 3.0f;//设置初始摄像机速度const GLfloat SENSITIVTY = 0.25f;//设置初始灵敏度const GLfloat ZOOM = 45.0f;//设置初始视野大小class Camera{public: glm::vec3 Position;//摄像机位置 glm::vec3 Front;//摄像机方向向量 glm::vec3 Up;//摄像机上轴 glm::vec3 Right;//摄像机右轴 glm::vec3 WorldUp;//世界空间上轴 GLfloat Yaw;//偏航角 GLfloat Pitch;//俯仰角 GLfloat MovementSpeed;//移动速度 GLfloat MouseSensitivity;//鼠标灵敏度 GLfloat Zoom;//视野 //初始化摄像机 Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), GLfloat yaw = YAW, GLfloat pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM) { this->Position = position; this->WorldUp = up; this->Yaw = yaw; this->Pitch = pitch; this->updateCameraVectors(); } // Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw, GLfloat pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM) { this->Position = glm::vec3(posX, posY, posZ); this->WorldUp = glm::vec3(upX, upY, upZ); this->Yaw = yaw; this->Pitch = pitch; this->updateCameraVectors(); } //得到更新后的摄像机观察矩阵 glm::mat4 GetViewMatrix() { return glm::lookAt(this->Position, this->Position + this->Front, this->Up); } //键盘输入移动函数 void ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime) { GLfloat velocity = this->MovementSpeed * deltaTime; if (direction == FORWARD) this->Position += this->Front * velocity; if (direction == BACKWARD) this->Position -= this->Front * velocity; if (direction == LEFT) this->Position -= this->Right * velocity; if (direction == RIGHT) this->Position += this->Right * velocity; } //鼠标输入视角改变函数 void ProcessMouseMovement(GLfloat xoffset, GLfloat yoffset, GLboolean constrainPitch = true) { xoffset *= this->MouseSensitivity; yoffset *= this->MouseSensitivity; this->Yaw += xoffset; this->Pitch += yoffset; if (constrainPitch) { if (this->Pitch > 89.0f) this->Pitch = 89.0f; if (this->Pitch < -89.0f) this->Pitch = -89.0f; } this->updateCameraVectors(); } //鼠标滚轮缩放函数 void ProcessMouseScroll(GLfloat yoffset) { if (this->Zoom >= 1.0f && this->Zoom <= 45.0f) this->Zoom -= yoffset; if (this->Zoom <= 1.0f) this->Zoom = 1.0f; if (this->Zoom >= 45.0f) this->Zoom = 45.0f; }private: //更新摄像机坐标 void updateCameraVectors() { glm::vec3 front; front.x = cos(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch)); front.y = sin(glm::radians(this->Pitch)); front.z = sin(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch)); this->Front = glm::normalize(front); this->Right = glm::normalize(glm::cross(this->Front, this->WorldUp)); this->Up = glm::normalize(glm::cross(this->Right, this->Front)); }};
11、主程序中的代码
// 摄像机初始化Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));bool keys[1024];GLfloat lastX = 400, lastY = 300;bool firstMouse = true;//得到观察矩阵glm::mat4 view = camera.GetViewMatrix();#pragma region "User input"void Do_Movement(){ if (keys[GLFW_KEY_W]) camera.ProcessKeyboard(FORWARD, deltaTime); if (keys[GLFW_KEY_S]) camera.ProcessKeyboard(BACKWARD, deltaTime); if (keys[GLFW_KEY_A]) camera.ProcessKeyboard(LEFT, deltaTime); if (keys[GLFW_KEY_D]) camera.ProcessKeyboard(RIGHT, deltaTime);}void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode){ if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE); if (action == GLFW_PRESS) keys[key] = true; else if (action == GLFW_RELEASE) keys[key] = false;}void mouse_callback(GLFWwindow* window, double xpos, double ypos){ if (firstMouse) { lastX = xpos; lastY = ypos; firstMouse = false; } GLfloat xoffset = xpos - lastX; GLfloat yoffset = lastY - ypos; lastX = xpos; lastY = ypos; camera.ProcessMouseMovement(xoffset, yoffset);}void scroll_callback(GLFWwindow* window, double xoffset, double yoffset){ camera.ProcessMouseScroll(yoffset);}#pragma endregion
- OpenGL学习总结(三)
- 学习OpenGL总结三
- openGL画线---openGL学习笔记(三)
- OpenGL学习笔记(三)
- OpenGL入门学习(三)
- OpenGL学习笔记(三)
- OpenGL入门学习(三)
- OpenGL入门学习(三)
- OPENGL学习笔记(三)
- OpenGL学习笔记(三)
- OpenGL学习总结(一)
- OpenGL学习总结(二)
- OpenGL学习总结(四)
- OpenGL学习总结(五)
- OpenGL学习总结(六)
- OpenGL学习总结(七)
- OpenGL学习总结(八)
- OpenGL 学习总结(一)
- EM算法的学习,看完这几篇就懂了
- Vue.js入门 - 介绍
- 2016年年终报告总结
- 人脸识别基本知识
- Android端弹幕效果。
- OpenGL学习总结(三)
- Error: watch ENOSPC 的解决方案
- QT webkit 各个类之间关系--QWebView-QWebPage
- 对正向传播、反向传播和精度检查的理解
- 【HDU 2037】今年暑假不AC 贪心
- 每日积累小细节
- Ubuntu 16.04 安装网易云音乐
- anroid studio installation failed with message install_canceled_by_user
- dpkg apt