在OpenGL中,我们通过把场景中的所有物体往反方向移动的方式来模拟相机的移动。
1.摄像机/观察空间
讨论摄像机/观察空间(Camera/view space)的时候,是在讨论以摄像机的视角作为场景原点时场景中所有的顶点坐标:观察矩阵把所有的世界左边变换为相对于摄像机位置与方向的观察坐标。
为了定义一个摄像机,需要用到它在世界空间中的位置、观察的方向、一个指向它右侧的向量以及指向它上方的向量。
摄像机的位置:摄像机位置简单来说就是世界空间中一个指向摄像机位置的向量(如下图1)。
glm::vec3 cameraPos = glm::vec3(0.0f,0.0f,3.0f);
摄像机方向:指的是摄像机指向哪个方向(如下图2)。一般我们让摄像机指向场景原点(0,0,0),用场景原点减去摄像机位置向量的结果就是摄像机的指向向量;
glm::vec3 cameraTarget = glm::vec3(0.0f,0.0f,0.0f);
//为了让方向向量指向摄像机的z轴正方向,交换相减顺序
glm::vec3 cameraDirection = glm::normalize(cameraPos - cameraTarget);
右轴:代表摄像机空间的x轴的正方向。为了获取右向量,我们先定义一个上向量(如0,1,0)。接着把上向量和第二步得到的方向向量进行叉乘。得到的结果即为右向量(如下图3);
glm::vec3 up=glm::vec3(0.0f,1.0f,0.0f);
//交换叉乘顺序得到相反的指向x轴负方向的向量
glm::vec3 cameraRight = glm::normalize(glm::cross(up,cameraDirection));
上轴:我们把右向量和方向向量进行叉乘得到上轴(如下图4)。
glm::vec3 cameraUp = glm::cross(cameraDirection,cameraRight);
图片来源(LearnOpenGL CN)
2.LookAt矩阵
顾名思义,LookAt矩阵会创建一个看着给定目标的观察矩阵。
其中R是右向量,U是上向量,D是方向向量,P是摄像机位置。GLM已经提供了这些支持,我们只需要定义一个摄像机位置,一个目标位置和一个表示世界空间中的上向量(计算右向量使用的上向量)。接着GLM就会创建一个LookAt矩阵,我们可以把它当作我们的观察矩阵。
glm::mat4 Camera::GetViewMatrix()
{
return glm::lookAt(Position,Position + Forward,Up);
}
3.自由移动
我们为GLFW的键盘输入定义一个processInput函数,并且添加几个按键命令。使用GLFW的glfwGetKey函数,它需要一个窗口以及一个按键作为输入。这个函数将会返回这个按键是否正在被按下。
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
camera.speedZ = 1.0f;
}
else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
camera.speedZ = -1.0f;
}
else
{
camera.speedZ = 0;
}
}
图形程序和游戏通常会跟踪一个时间差(Deltatime)变量,它储存了渲染上一帧所用的时间。我们把所有速度都去乘以deltaTime值。结果就是,如果我们的deltaTime很大,就意味着上一帧的渲染花费了更多时间,所以这一帧的速度需要变得更高来平衡渲染所花去的时间。使用这种方法时,无论你的电脑快还是慢,摄像机的速度都会相应平衡,这样每个用户的体验就都一样了。
float deltaTime = 0.0f; //当前帧与上一帧的时间差
float lastFrame = 0.0f; //上一帧时间
每一帧计算出新的deltaTime
float currentFrame = glfwGetTime();
deltaTime = currentFrame -lastFrame;
lastFrame = currentFrame;
//控制移动速度
float cameraSpeed = 2.5f *deltaTime;
4.视角移动
为了能够改变视角,我们需要根据鼠标的输入改变 cameraFront 向量,这里我们
将用到数学里的欧拉角的相关知识。
欧拉角是可以表示 3D 空间中任何旋转的 3 个值,一共有 3 种欧拉角:俯仰角(Pitch)、偏航角(Yaw)和滚转角
(Roll),如下图所示:
俯仰角是描述我们如何往上或往下看的角(左一图),偏航角表示我们往左往右看的程度(中间图),滚转角代表如何翻滚摄像机(右一)。欧拉角都有一个值来表示,把三个角结合起来就能够计算3D空间中任何的旋转向量(不考虑翻滚角)。
5.鼠标输入
偏航角和俯仰角是通过鼠标移动获得的,水平移动影响偏航角,竖直移动影响俯仰角。通过存储上一帧鼠标的位置,计算在当前帧中我们当前计算鼠标位置与上一帧的位置相差多少。
首先我们告诉GLFW,它应该隐藏光标,并捕捉它,捕捉光标表示的是如果焦点在你的程序上,光标应该停留在窗口中(除非程序失去焦点或者退出)。我们可以用GLFW为我们提供的方法来配置:
glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_DISABLED);
让GLFW监听鼠标移动事件,使用的函数如下:
void mouse_callback(GLFWwindow* window,double xpos,double ypos)
{
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
yaw+=xoffset;
pitch +=yoffset;
//pitch和yaw的变化
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);
}
引入sensitivity(灵敏度)来控制鼠标移动对视角影响的幅度。
float sensitivity = 0.1f;
xoffset *=sensitivity;
yoffset *=sensivity;
我们使用一个bool变量检验我们是否是第一次获取鼠标输入,如果是,那么我们先把鼠标的初始位置更新为xpos和ypos,以此来来解决跳动问题(鼠标刚移动进窗口时,鼠标函数就被调用,此时的xpos和ypos通常是离屏幕很远的地方,因而产生一个很大的偏移量,就会跳动)。
if(firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
6.缩放
我们用视野定义我们看到的场景范围,当视野变小时,场景投影出来的空间就会减小,产生放大的感觉。
我们在processInput函数中用S和W来实现缩放
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
camera.speedZ = 1.0f;
}
else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
camera.speedZ = -1.0f;
}
else
{
camera.speedZ = 0;
}
}
7.创建相机类
同之前创建着色器类,我们在我们的解决方案右键单击->添加->新建项目->c++类,或者添加一个camera.h文件,再添加一个camera.cpp文件。
camera.h文件:
#pragma once
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
class Camera
{
public:
Camera(glm::vec3 position, glm::vec3 target, glm::vec3 worldup);
Camera(glm::vec3 position, float pitch, float yaw, glm::vec3 worldup);
~Camera();
glm::vec3 Position;
glm::vec3 Forward;
glm::vec3 Right;
glm::vec3 Up;
glm::vec3 WorldUp;
float Pitch;
float Yaw;
float SenseX = 0.001f;
float SenseY = 0.001f;
float speedZ = 0;
glm::mat4 GetViewMatrix();
void ProcessMouseMovement(float deltaX, float deltaY);
void UpdataCameraPos();
private:
void UpdataCameraVectors();
};
camera.cpp
#include "Camera.h"
Camera::Camera(glm::vec3 position, glm::vec3 target, glm::vec3 worldup)
{
Position = position;
WorldUp = worldup;
Forward = glm::normalize(target - position);
Right = glm::normalize(glm::cross(Forward, WorldUp));
Up = glm::normalize(glm::cross(Forward, Right));
}
Camera::Camera(glm::vec3 position, float pitch, float yaw, glm::vec3 worldup)
{
Position = position;
WorldUp = worldup;
Pitch = pitch;
Yaw = yaw;
Forward.x = glm::cos(Pitch)*glm::sin(Yaw);
Forward.y = glm::sin(Pitch);
Forward.z = glm::cos(Pitch)*glm::cos(Yaw);
Right = glm::normalize(glm::cross(Forward, WorldUp));
Up = glm::normalize(glm::cross(Forward, Right));
}
Camera::~Camera()
{
}
glm::mat4 Camera::GetViewMatrix()
{
return glm::lookAt(Position,Position+Forward,WorldUp);
}
void Camera::ProcessMouseMovement(float deltaX, float deltaY)
{
Pitch += deltaY*SenseX;
Yaw += deltaX*SenseY;
UpdataCameraVectors();
}
void Camera::UpdataCameraPos()
{
Position += Forward*speedZ*0.1f;
}
void Camera::UpdataCameraVectors()
{
Forward.x = glm::cos(Pitch)*glm::sin(Yaw);
Forward.y = glm::sin(Pitch);
Forward.z = glm::cos(Pitch)*glm::cos(Yaw);
Right = glm::normalize(glm::cross(Forward, WorldUp));
Up = glm::normalize(glm::cross(Forward, Right));
}
main.cpp
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include"Shader.h"
#include"Camera.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
float lastX;
float lastY;
bool firstMouse = true;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
Camera camera(glm::vec3(0, 0, 3.0f), glm::radians(15.0f), glm::radians(180.0f), glm::vec3(0, 1.0f, 0));
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
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); // uncomment this statement to fix compilation on OS X
#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);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// 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);
// build and compile our shader zprogram
// ------------------------------------
Shader ourShader("VertexSource.txt", "fragmentSource.txt");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.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0.0f, 0.0f, 0.0f),
glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
//float vertices[] = {
// // positions // texture coords
// 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
// 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
// -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
// -0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
//};
//unsigned int indices[] = {
// 0, 1, 3, // first triangle
// 1, 2, 3 // second triangle
//};
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
//glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
/*glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),indices, GL_STATIC_DRAW);*/
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// load and create a texture
// -------------------------
unsigned int texture1, texture2;
// texture 1
// ---------
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
unsigned char *data = stbi_load("container.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
// texture 2
// ---------
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
data = stbi_load("awesomeface.png", &width, &height, &nrChannels, 0);
if (data)
{
// note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
// tell opengl for each sampler to which texture unit it belongs to (only has to be done once)
// -------------------------------------------------------------------------------------------
ourShader.use();
ourShader.setInt("texture1", 0);
ourShader.setInt("texture2", 1);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // also clear the depth buffer now!
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
// activate shader
ourShader.use();
glm::mat4 model = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
model = glm::rotate(model, glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
//view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
view = camera.GetViewMatrix();
projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
// retrieve the matrix uniform locations
unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model");
unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
// note: currently we set the projection matrix each frame, but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once.
ourShader.setMat4("projection", projection);
// render container
glBindVertexArray(VAO);
for (unsigned int i = 0; i < 10; i++)
{
glm::mat4 model;
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f*i;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
ourShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
camera.UpdataCameraPos();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
//glDeleteBuffers(1, &EBO);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
camera.speedZ = 1.0f;
}
else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
camera.speedZ = -1.0f;
}
else
{
camera.speedZ = 0;
}
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse == true)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float deltaX, deltaY;
deltaX = xpos - lastX;
deltaY = ypos - lastY;
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(deltaX,deltaY);
}
参考文章LearnOpenGL CN