# OpenGL(九) 摄像机

### 摄像机/观察空间

#### 1. 摄像机位置

glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);


#### 2. 摄像机方向

glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraDirection = glm::normalize(cameraPos - cameraTarget);


#### 3. 右轴

glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);


#### 4. 上轴

glm::vec3 cameraUp = glm::cross(cameraDirection, cameraRight);  // 不归一化，是因为右向量和方向向量已经归一化了


### Look At

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


glm::LookAt函数需要一个位置、目标和上向量。它会创建一个和在上一节使用的一样的观察矩阵

glm::mat4 view = glm::mat4(1.0f);   // 初始化观察矩阵
观察矩阵通常是由一系列的位移和旋转的组合来完成，平移/旋转场景从而使得特定的对象被变换到摄像机的前方
//view = glm::translate(view, glm::vec3(view_x, view_y, view_z));
float camX = sin(glfwGetTime()) * radius;   // 半径乘以sin，得到随时间遍历圆上的点
float camZ = cos(glfwGetTime()) * radius;
view = glm::lookAt(glm::vec3(camX, 0.0, camZ), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));  // 实现绕园旋转的观察矩阵


### 自由移动

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


LookAt函数现在成了：

// cameraPos+cameraFront = (0.0f, 0.0f, 2.0f)
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);


void processInput(GLFWwindow *window)
{
...
float cameraSpeed = 0.05f; // adjust accordingly
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
cameraPos += cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
cameraPos -= cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;   // cross(cameraFront, cameraUp) 创建了一个指向世界坐标系x负半轴的向量
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}


### 移动速度

float deltaTime = 0.0f; // 当前帧与上一帧的时间差
float lastFrame = 0.0f; // 上一帧的时间


float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;


void processInput(GLFWwindow *window)
{
// 保证了虽然不同处理器渲染帧率不同，但在同样的时间内，移动的速度(或者解释成距离)是相同的。
float cameraSpeed = 2.5f * deltaTime;
...
}


### 视角移动

#### 欧拉角

direction.y = sin(glm::radians(pitch)); // 注意我们先把角度转为弧度


direction.x = cos(glm::radians(pitch));


// 译注：direction代表摄像机的前轴(Front)，这个前轴是和本文第一幅图片的第二个摄像机的方向向量是相反的


### 鼠标输入

glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);


void mouse_callback(GLFWwindow* window, double xpos, double ypos);  // xpos, ypos采用的是uv坐标系坐标值


glfwSetCursorPosCallback(window, mouse_callback);


• 计算鼠标距上一帧的偏移量。
• 把偏移量添加到摄像机的俯仰角和偏航角中。
• 对偏航角和俯仰角进行最大和最小值的限制。
• 计算方向向量。

float lastX = 400, lastY = 300;


float xoffset = xpos - lastX;
float yoffset = lastY - ypos; // 注意这里是相反的，因为y坐标是从底部往顶部依次增大的（采用的uv坐标系）
lastX = xpos;
lastY = ypos;

float sensitivity = 0.05f;
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;
cameraFront = glm::normalize(front);


if(firstMouse) // 这个bool变量初始时是设定为true的
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}


// 全局变量
bool firstMouse = true;      // 鼠标首次进入窗口标志位
float lastX = 800.0f / 2.0;  // 鼠标在窗口的的x坐标，初始值为窗口的中心点
float lastY = 600.0f / 2.0;  // 鼠标在窗口的的y坐标，初始值为窗口的中心点
float yaw   = -90.0f;          // 偏航角，向左向右的角度
float pitch = 0.0f;          // 俯仰角，向上向下的角度

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;  // 注意这里是相反的，因为y坐标是从底部往顶部依次增大的
lastX = xpos;
lastY = ypos;

// 定义灵敏度
float sensitivity = 0.05f;
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;
cameraFront = glm::normalize(front);
}

glfwSetCursorPosCallback(window, mouse_callback);  // 注册鼠标的回调函数


### 缩放

float fov = 45.0f;           // 摄像机视野范围

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


projection = glm::perspective(glm::radians(fov), (float)width / (float)height, 0.1f, 100.0f);


glfwSetScrollCallback(window, scroll_callback);


### 未实现摄像机类的源代码

main.cpp

// 使用着色器类的代码
#define STB_IMAGE_IMPLEMENTATION
#include <glfw3.h>
#include <stb_image.h>   // 加载纹理库

// glm
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include <iostream>

void framebuffer_size_callback(GLFWwindow * window, int width, int height);  // 注册视口
void processInput(GLFWwindow* window);                                       // 按键的响应函数
void mouse_callback(GLFWwindow* window, double xpos, double ypos);           // 鼠标按键的回调函数
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);    // 鼠标滚轮回调函数

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

float mixValue = 0.2f;   // 两张纹理的混合比例

float view_z = 0.0f;  // 调整观察矩阵前后移动距离
float view_x = 0.0f;  // 调整观察矩阵左右移动
float view_y = 0.0f;  // 调整观察矩阵上下移动

glm::vec3 cameraPos   = glm::vec3(0.0f, 0.0f, 3.0f);    // 相机位置
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, 0.0f); //
glm::vec3 cameraUp    = glm::vec3(0.0f, 1.0f, 0.0f);     // 上向量

float deltaTime = 0.0f;      // 当前帧与上一帧的时间差
float lastFrame = 0.0f;      // 上一帧的时间

bool firstMouse = true;          // 鼠标首次进入窗口标志位
float lastX     = 800.0f / 2.0;  // 鼠标在窗口的的x坐标，初始值为窗口的中心点
float lastY     = 600.0f / 2.0;  // 鼠标在窗口的的y坐标，初始值为窗口的中心点
float yaw       = -90.0f;        // 偏航角，向左向右的角度
float pitch     = 0.0f;          // 俯仰角，向上向下的角度

float fov       = 45.0f;         // 摄像机视野范围

int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "learnOpenGL", nullptr, nullptr);

if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);

glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);  // 注册视口
glfwSetCursorPosCallback(window, mouse_callback);  // 注册鼠标按键的回调函数
glfwSetScrollCallback(window, scroll_callback);    // 注册鼠标滚轮的回调函数

glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);  // 设置鼠标光标停留在串口中并隐藏

{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}

// 配置全局opengl状态，启用深度测试
glEnable(GL_DEPTH_TEST);

// 创建着色器类对象

//float vertices[] = {
位置                    // 颜色              // 纹理坐标
//	0.5f,  0.5f,  0.0f,   1.0f, 0.0f, 0.0f,     1.0f, 1.0f,
//	0.5f, -0.5f,  0.0f,   0.0f, 1.0f, 0.0f,     1.0f, 0.0f,
//   -0.5f, -0.5f,  0.0f,   0.0f, 0.0f, 1.0f,     0.0f, 0.0f,
//   -0.5f,  0.5f,  0.0f,   1.0f, 1.0f, 0.0f,     0.0f, 1.0f,
//};

//unsigned int indices[] = {
//0, 1, 3,  // 第一个三角形
//1, 2, 3   // 第二个三角形
//};

// 局部空间坐标
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.5f, -0.5f,  1.0f, 1.0f,
-0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
-0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

-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.5f,  0.5f,  1.0f, 1.0f,
-0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
-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.5f, -0.5f,  0.0f, 1.0f,
-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
-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, 0.0f,
0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
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,  0.0f, 1.0f,
0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

-0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
-0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
-0.5f,  0.5f, -0.5f,  0.0f, 1.0f
};

unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

// 位置属性
//glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
//glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

颜色属性
//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float)));
//glEnableVertexAttribArray(1);

// 纹理坐标
//glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6*sizeof(float)));
//glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2);

// 加载并创建第一个纹理（opengl保证至少有16个纹理单元可供使用）
unsigned int texture1;   // 如果要加载多个纹理，这里设置为数组
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
// 为当前绑定的纹理对象设置环绕、过滤方式
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  // 缩小操作时过滤方式
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  // 放大操作时过滤方式
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// 加载纹理图片并附到纹理对象上
int width, height, nrChannels;
unsigned char* data = stbi_load("container.jpg", &width, &height, &nrChannels, 0);
if (data)
{
// 当调用glTexImage2D时，当前绑定的纹理对象就会被附加上纹理图像
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);

// 加载并创建第二个纹理
unsigned int texture2;
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
// 为当前绑定的第二个纹理对象设置环绕、过滤方式
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// 加载纹理图片并附到纹理对象上
data = stbi_load("awesomeface.png", &width, &height, &nrChannels, 0);
if (data)
{
// awesomeface.png具有透明度，因此是alpha通道，所以一定要告诉OpenGL数据类型是GL_RGBA
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 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);

// 解除绑定
//glBindBuffer(GL_ARRAY_BUFFER, 0);
//glBindVertexArray(0);

// 告诉opengl每个采样器属于哪个纹理单元(只需要做一次)

glm::vec3 cubePositions[] = {
glm::vec3( 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, -5.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)
};

while (!glfwWindowShouldClose(window))
{
// 计算渲染上一帧所用的时间
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;

processInput(window);

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
//glClear(GL_COLOR_BUFFER_BIT);  // 清理颜色缓冲
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // 清理颜色缓冲和深度缓冲

glActiveTexture(GL_TEXTURE0);              // 在绑定纹理之前先激活纹理单元,只有一个纹理时,可以不用手动激活，默认激活
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);

glm::mat4 view = glm::mat4(1.0f);         // 初始化观察矩阵为4x4单位矩阵
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);

glm::mat4 projection = glm::mat4(1.0f);   // 初始化投影矩阵
//projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
projection = glm::perspective(glm::radians(fov), (float)width / (float)height, 0.1f, 100.0f);

// 设置uniform变量之前必须先激活着色器程序对象
//glUniformMatrix4fv(modelLoc, 1, GL_FALSE, &model[0][0]);
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &view[0][0]);
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, &projection[0][0]);

// 绑定VAO后会绑定纹理,它会自动把纹理赋值给片段着色器的采样器
glBindVertexArray(VAO);   // 前面并没有把VAO解绑定，其实这里不重新绑定也是可以的，但有多个VAO就需要绑定
//glDrawArrays(GL_TRIANGLES, 0, 3);   // 从顶点数组对象中进行绘制
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);   // 矩形由两个三角形组成，故是6个顶点,画第一个箱子,会使用当前绑定的索引缓冲对象中的索引进行绘制
// 循环改变10次模型矩阵，并绘10个箱子
for (unsigned int i = 0; i < 10; ++i)
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * (i+1);
//if (i % 3 == 0)
//model = glm::rotate(model, (float)glfwGetTime() * glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(angle), glm::vec3(0.0f, 0.0f, 1.0f));  // 绕z轴旋转
//model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));

glDrawArrays(GL_TRIANGLES, 0, 36);
}
glfwSwapBuffers(window);
glfwPollEvents();
}

glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);

glfwTerminate();

return 0;
}

void framebuffer_size_callback(GLFWwindow * window, int width, int height)
{
glViewport(0, 0, width, height);  // 视口
}

// 处理所有输入:查询GLFW是否按下/释放了此帧的相关键，并做出相应的反应
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)  // 键盘esc
{
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)     // 键盘up
{
mixValue += 0.01f;
if (mixValue >= 1.0f)
mixValue = 1.0f;

}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)  // 键盘down
{
mixValue -= 0.01f;
if (mixValue <= 0.0f)
mixValue = 0.0f;
}

float cameraSpeed = 2.5f * deltaTime;                 // 通过时间差平衡不同硬件的移动速度
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
std::cout << "deltaTime = " << cameraSpeed << std::endl;
cameraPos += cameraSpeed * cameraFront;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
cameraPos -= cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;   // cross()表示向量叉乘
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}

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;  // 注意这里是相反的，因为y坐标是从底部往顶部依次增大的
lastX = xpos;
lastY = ypos;

// 定义灵明度
float sensitivity = 0.1f;
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;

cameraFront = glm::normalize(front);   // 归一化
}

void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
if (fov >= 1.0f && fov <= 90.0f)
fov -= yoffset;
if (fov <= 1.0f)
fov = 1.0f;
if (fov >= 90.0f)
fov = 90.0f;
}


### 摄像机类

camera.h

//#pragma once
#ifndef CAMERA_H
#define CAMERA_H

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <vector>

// 定义相机移动的几个可能选项。用作抽象，以远离窗口系统特定的输入方法
enum Camera_Movement {
FORWARD,
BACKWARD,
LEFT,
RIGHT
};

const float YAM         = -90.0f;
const float PITCH       = 0.0f;
const float SPEED       = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM        = 45.0f;

class Camera
{
public:
// 相机属性
glm::vec3 Position;
glm::vec3 Front;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 WorldUp;

// 欧拉角
float Yaw;    // 偏航角
float Pitch;  // 俯仰角

// 相机选项设置
float MovementSpeed;        // 速度
float MouseSensitivity;     // 鼠标滚轮灵敏度
float Zoom;                 // 摄像机视野角度

// 向量值构造函数
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAM, float pitch = PITCH);

// 标量值构造函数
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch);

// 返回使用欧拉角和LookAt矩阵计算的视图矩阵
glm::mat4 GetViewMatrix();

// 处理从任何类似键盘的输入系统接收的输入。接受摄像机定义ENUM形式的输入参数(从窗口系统中抽象)
void ProcessKeyboard(Camera_Movement direction, float deltaTime);

// 处理从鼠标输入系统接收到的输入。期望在x和y方向上的偏移量。
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true);

// 处理从鼠标滚轮事件接收的输入。只需要输入垂直轮轴
void ProcessMouseScroll(float yoffset);

private:
// 根据相机(更新后的)欧拉角计算前矢量
void updateCameraVectors();
};

#endif


camera.cpp

#include "../include/camera.h"

Camera::Camera(glm::vec3 position, glm::vec3 up, float yaw, float pitch) :
Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = position;
WorldUp = up;
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}

Camera::Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) :
Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = glm::vec3(posX, posY, posZ);
WorldUp = glm::vec3(upX, upY, upZ);
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}

// 根据相机(更新后的)欧拉角计算前矢量
void Camera::updateCameraVectors()
{
// 根据俯仰角pitch和偏航角yaw，计算新的前向量
glm::vec3 front;
Front = glm::normalize(front);  // 归一化

// 重新计算上向量和右向量
// 对向量进行归一化，因为它们的长度随着向上或向下看的次数的增加而接近于0，这会导致运动变慢
Right = glm::normalize(glm::cross(Front, WorldUp));
Up    = glm::normalize(glm::cross(Right, Front));
}

// 处理从任何类似键盘的输入系统接收的输入。接受摄像机定义ENUM形式的输入参数(从窗口系统中抽象)
void Camera::ProcessKeyboard(Camera_Movement direction, float deltaTime)
{
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position += Front * velocity;
if (direction == BACKWARD)
Position -= Front * velocity;
if (direction == LEFT)
Position -= Right * velocity;
if (direction == RIGHT)
Position += Right * velocity;
}

// 处理从鼠标输入系统接收到的输入。期望在x和y方向上的偏移量。
void Camera::ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
{
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;

Yaw   += xoffset;
Pitch += yoffset;

// 确保当音俯仰角pitch超出界限时，屏幕不会翻转
if (constrainPitch)
{
if (Pitch > 89.0f)
Pitch = 90.0f;
if (Pitch < -89.0f)
Pitch = -89.0f;
}

updateCameraVectors();
}

// 处理从鼠标滚轮事件接收的输入。只需要输入垂直轮轴
void Camera::ProcessMouseScroll(float yoffset)
{
if (Zoom >= 1.0f && Zoom <= 45.0f)
Zoom -= yoffset;
if (Zoom <= 1.0f)
Zoom = 1.0f;
if (Zoom >= 45.0f)
Zoom = 45.0f;
}

// 返回使用欧拉角和LookAt矩阵计算的视图矩阵
glm::mat4 Camera::GetViewMatrix()
{
return glm::lookAt(Position, Position + Front, Up);
}


#define STB_IMAGE_IMPLEMENTATION
#include <glfw3.h>
#include <stb_image.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include "../include/camera.h"

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);   // 设置视口回调函数
void mouse_callback(GLFWwindow* window, double xpos, double ypos);           // 鼠标位置回调函数
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);    // 鼠标滚轮回调函数
void processInput(GLFWwindow* window);                                       // 窗口按键响应回调函数

const unsigned int SCR_WIDTH  = 800;
const unsigned int SCR_HEIGHT = 600;

// 相机参数
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;

// 记录时间
float deltaTime = 0.0f;
float lastFrame = 0.0f;

int main()
{
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);
#endif

GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "learnOpenGL", nullptr, nullptr);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);

glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);

// 告诉GLFW抓住我们的鼠标(将鼠标锁定在窗口中，并隐藏)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}

// 开启深度测试
glEnable(GL_DEPTH_TEST);

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.5f, -0.5f,  1.0f, 1.0f,
-0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
-0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

-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.5f,  0.5f,  1.0f, 1.0f,
-0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
-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.5f, -0.5f,  0.0f, 1.0f,
-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
-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, 0.0f,
0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
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,  0.0f, 1.0f,
0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

-0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
-0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
-0.5f,  0.5f, -0.5f,  0.0f, 1.0f
};

// 10个立方体的世界空间位置
glm::vec3 cubePositions[] = {
glm::vec3(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)
};

unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

// 位置属性
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

// 纹理坐标属性
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2);

unsigned int texture[2];
glGenTextures(2, texture);
// 纹理1
glBindTexture(GL_TEXTURE_2D, texture[0]);
// 设置纹理环绕、过滤方式
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int width, height, nrChannels;
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);

// 纹理2
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
data = stbi_load("../awesomeface.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);  // PNG是RGBA存储
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

// 告诉opengl每个采样器属于哪个纹理单元(只需要做一次)

while (!glfwWindowShouldClose(window))
{
// 计算每帧渲染时间
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;

processInput(window);

// 渲染
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// 将纹理绑定到相应的纹理单元上,供采样器采样
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture[1]);

// 将投影矩阵传递给着色器(注意，它可以改变每一帧)
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);

// 观察矩阵（将世界坐标系转换到观察空间，即相机坐标系）
glm::mat4 view = camera.GetViewMatrix();

// 创建模型矩阵并绘出10个立方体
glBindVertexArray(VAO);
for (unsigned int i = 0; i < 10; i++)
{
glm::mat4 model = glm::mat4(1.0f);  // 确保初始化为单位矩阵，最新的glm版本，默认初始化为0矩阵
// 变换以先缩放、再旋转、最后位移的顺序(故为位移矩阵x旋转矩阵x缩放矩阵x待变换矩阵)
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));

glDrawArrays(GL_TRIANGLES, 0, 36);
}

// glfw:交换缓冲区和轮询IO事件(按下/释放键，移动鼠标等)
glfwSwapBuffers(window);
glfwPollEvents();
}

glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);

// 终止，清除所有以前分配的GLFW资源
glfwTerminate();
return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}

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;  // 颠倒了，因为y坐标从下到上

lastX = xpos;
lastY = ypos;

camera.ProcessMouseMovement(xoffset, yoffset);
}

void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}

void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);

if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
}


### 利用OpenCV加载图片

#### 通过opencv3.4.2源码+cmake3.12.1-win32-x86编译32位的opencv库

• 配置包含路径
• 配置库路径
• 配置链接器的输入(将生成的静态库添加进去)
• 配置环境变量(配置生成的.dll文件路径)

#### 修改main.cpp

#include <opencv.hpp>

    unsigned int texture[2];
glGenTextures(2, texture);
// 纹理1
glBindTexture(GL_TEXTURE_2D, texture[0]);
// 设置纹理环绕、过滤方式
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int width, height, nrChannels;

// cv::imshow("1", tmp);

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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, tmp.data);    // opencv默认是以BGR存储的
glGenerateMipmap(GL_TEXTURE_2D);    // 生成多级渐进纹理
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);   // 释放图片数据

// 纹理2
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
data = stbi_load("awesomeface.png", &width, &height, &nrChannels, 0);
tmp = cv::imread("awesomeface.png", -1);     // 用opencv加载png图片
if (data)
{
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);  // PNG是RGBA存储
// opencv中PNG是BGRA存储
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, tmp.data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

// 告诉opengl每个采样器属于哪个纹理单元(只需要做一次)


//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, tmp.data);    // opencv默认是以BGR存储的

//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);  // PNG是RGBA存储
// opencv中PNG是BGRA存储
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, tmp.data);


10-03 273
08-21 1万+
11-27 1049
09-08 135
02-13 2703
12-22 161
11-28 9423
02-25
08-12
05-11 1075
09-07 1万+
01-11 184
01-11 419
07-11 4002