使用摄像机的3个轴外加一个平移向量来创建一个矩阵,可以用这个矩阵乘以任何向量来将其表换到那个坐标空间。这就是,矩阵的作用。现有3个相互垂直的轴和一个定义摄像机空间的位置坐标,可以创建自己的LookAt矩阵:
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));
欧拉角时可以表示3D空间中任何旋转的3个值,包括俯仰角(Pitch)、偏航角(Yaw)和滚转角(Roll)
俯仰角描述往上或往下看,偏航角表示往左和往右看,滚转角代表如何翻滚摄像机。
对于摄像机系统,只关注俯仰角和偏航角。
可以把俯仰角和偏航角转化为用来自由旋转视角的摄像机的3维方向向量。
direction.x=cos(glm::radians(pitch))*cos(glm::radians(yaw));
direction.y=sin(glm::radians(pitch));
direction.z=cos(glm::radians(pitch))*sin(glm::radians(yaw));
偏航角和俯仰角是通过鼠标或手柄移动获得的,水平的移动影响偏航角,垂直的移动影响俯仰角。
初始yaw设为-90.0f,pitch设为0.0f
且偏航角要设在-89.0f到89.0f之间,因为在90度视角时会发生逆转。
完整的鼠标回调函数如下所示:
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;
front.x = cos(glm::radians(pitch))*cos(glm::radians(yaw));
front.y = sin(glm::radians(pitch));
front.z = cos(glm::radians(pitch))*sin(glm::radians(yaw));
cameraFront = glm::normalize(front);
}
注册鼠标回调函数如下:
glfwSetCursorPosCallback(window, mouse_callback);
fov(视野)定义了可以看到场景中多大的范围,当视野变小时,场景投影出来的空间就会减小,产生放大的感觉。鼠标滚轮的回调函数如下:
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), 800.0f / 600.0f, 0.1f, 100.0f);
注册鼠标滚轮的回调函数如下:
glfwSetScrollCallback(window, scroll_callback);
/*OpenGL之窗口初始化*/
#include
#include
#include
#include
#include
#include
#include
#include
#include "Shader.h"
#define STB_IMAGE_IMPLEMENTATION
#define screenWidth 800
#define screenHeight 600
#include "stb_image.h"
void framebuffer_size_callback(GLFWwindow* windows, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void processInput(GLFWwindow *windows);
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
unsigned int shaderProgram;
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile; //文件输入流
std::ifstream fShaderFile;
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);
float fov = 45.0f;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
float lastX = 400, lastY = 300;
float yaw=-90.0, pitch=0; //yaw=-90.0使z轴坐标开始为-1
int success; //定义一个整型变量来表示是否成功编译
char infoLog[512]; //存储错误消息的容器
unsigned int VBO; //顶点缓冲对象
unsigned int VAO; //顶点数组对象
unsigned int EBO;
unsigned int vertexShader; //通过ID引用
unsigned int fragmentShader;
unsigned int texture1; //创建纹理的ID
unsigned int texture2;
bool firstMouse = true;
int main()
{
glfwInit(); //初始化GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //将主版本号和此版本号都设为3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //明确告诉GLFW使用的是核心模式
GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "LearnOpenGL", NULL, NULL); //创建窗口对象
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); //将窗口的上下文设置为当前线程的主上下文
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); //注册窗口的大小改变时,视口也应该被调整的函数
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); //隐藏光标并捕捉它
glfwSetCursorPosCallback(window, mouse_callback);//注册鼠标回调函数
glfwSetScrollCallback(window, scroll_callback); //注册鼠标滚轮的回调函数
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) // 调用任何OpenGL的函数之前需要初始化GLAD:给GLAD传入了用来加载系统相关的OpenGL函数指针地址的函数
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, screenWidth, screenHeight); //设置渲染窗口的尺寸大小,即视口,这样OpenGL才知道怎样根据窗口大小显示数据和坐标,前两个参数控制窗口左下角的位置。第三个和第四个参数控制渲染窗口的宽度和高度(像素)
Shader ourShader("vshader.vs", "fshader.fs");
int nrAttributes;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
std::cout << "Maximun nr of vertex attributes supported:" << nrAttributes << std::endl;
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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),
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 indices[] = {//注意索引从0开始
0,1,3,//第一个三角形
1,2,3 //第二个三角形
};
glGenBuffers(1, &VBO); //创建顶点缓冲对象
glGenVertexArrays(1, &VAO); //创建顶点数组对象
// glGenBuffers(1, &EBO); //创建索引缓冲对象
//绑定VAO
glBindVertexArray(VAO);
//把顶点数组复制到缓冲中供OpenGL使用
glBindBuffer(GL_ARRAY_BUFFER, VBO); //绑定顶点缓冲对象
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); //绑定索引缓冲对象
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);//复制顶点数组到一个顶点缓冲中,供OpenGL使用
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);//复制索引数组到一个索引缓冲中,供OpenGL使用
/*设置顶点属性指针,第一个参数指定要配置的顶点属性,第二个参数指定顶点属性的大小,第三个参数指定数据的类型,
第四个参数定义是否希望数据被标准化,第五个参数叫做步长,最后一个参数类型是void*(为偏移量)*/
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(0); //以顶点属性位置值作为参数,启用顶点属性
glEnableVertexAttribArray(1);
//glEnableVertexAttribArray(2);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/*
/当目标是GL_ELEMENT_ARRAY_BUFFER的时候,VAO会储存glBindBuffer的函数调用。
这也意味着它也会储存解绑调用,所以确保你没有在解绑VAO之前解绑索引数组缓冲,否则它就没有这个EBO配置
*/
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
/*生成纹理的过程*/
glGenTextures(1, &texture1); // 函数首先需要输入生成纹理的数量,然后把它们存储在第二个参数的unsigned int数组中
glGenTextures(1, &texture2); // 函数首先需要输入生成纹理的数量,然后把它们存储在第二个参数的unsigned int数组中
//加载并生成纹理
int width1, height1, nrChannels1;
int width2, height2, nrChannels2;
stbi_set_flip_vertically_on_load(true);//指示stb_image.h在y轴上翻转加载纹理
unsigned char *data1 = stbi_load("container.jpg", &width1, &height1, &nrChannels1, 0);
unsigned char *data2 = stbi_load("awesomeface.png", &width2, &height2, &nrChannels2, 0);
if (data1)
{
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width1, height1, 0, GL_RGB, GL_UNSIGNED_BYTE, data1);//第一个参数指定纹理目标,第二个参数为纹理指定多级渐远纹理级别,0为基本级别,第三个参数代表希望把纹理存储为何种格式,第七、八个参数定义了源图的格式和数据类型,最后一个参数是真正的图像数据
glGenerateMipmap(GL_TEXTURE_2D); //为当前绑定的纹理自动生成所有需要的多级渐远纹理
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
if (data2)
{
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);//第一个参数指定纹理目标,第二个参数为纹理指定多级渐远纹理级别,0为基本级别,第三个参数代表希望把纹理存储为何种格式,第七、八个参数定义了源图的格式和数据类型,最后一个参数是真正的图像数据
glGenerateMipmap(GL_TEXTURE_2D); //为当前绑定的纹理自动生成所有需要的多级渐远纹理
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data1);
stbi_image_free(data2);
ourShader.use();
ourShader.setInt("texture1", 0);//通过使用着色器类告诉OpenGL每个着色器采样器属于哪个纹理单元
ourShader.setInt("texture2", 1);
unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");
unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model");
unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view");
unsigned int projectionLoc = glGetUniformLocation(ourShader.ID, "projection");
glEnable(GL_DEPTH_TEST); //当片段想输出它的颜色时,OpenGl会将它的深度值和z缓冲进行比较,如果当前片段在其他片段之后,将会被丢弃,否则会覆盖
//渲染循环,在主动关闭它之前不断绘制图像并能够接受用户输入
while (!glfwWindowShouldClose(window)) //在循环的开始前检查一次GLFW是否被要求退出,是的话该函数返回true,渲染循环便结束
{
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glm::mat4 trans = glm::mat4(1.0f); //一般步骤为先缩放,再旋转,再平移
trans = glm::translate(trans, glm::vec3(0.5, -0.5, 0.0));
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.0, 1.0));//实际的变换顺序与阅读顺序相反
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); //设置清空屏幕所用的自定义颜色,为状态设置函数
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //通过调用glClear函数来清空屏幕的颜色缓冲深度缓冲(否则前一帧的深度信息仍然保存在缓冲中),为状态使用函数
//绑定纹理到相应纹理单元(此处为0和1)
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
ourShader.use(); //当渲染一个物体时要使用着色器程序
//model = glm::rotate(model, (float)glfwGetTime()*glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
glm::mat4 view = glm::mat4(1.0f);
//view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
float radius = 10.0f;
float camX = sin(glfwGetTime())*radius;
float camZ = cos(glfwGetTime())*radius;
view = glm::lookAt(cameraPos,cameraPos+cameraFront, cameraUp);
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(fov), (float)screenWidth / screenHeight, 0.1f, 100.0f);
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(VAO); //绑定VAO
for (int i = 0; i < 10; i++) //如果
{
glm::mat4 model = glm::mat4(1.0f); //区别在于它们在世界的位置及旋转角度不同
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f*i;
if (i % 3 == 0)angle = glfwGetTime()*25.0f; //将编号3的倍数的箱子和第一个箱子进行旋转
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
trans = glm::mat4(1.0f);
trans = glm::translate(trans, glm::vec3(-0.5, 0.5, 0.0));
float scale = sin(glfwGetTime());
trans = glm::scale(trans, glm::vec3(scale, scale, scale));//实际的变换顺序与阅读顺序相反
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
//glDrawElements(GL_TRIANGLES,36, GL_UNSIGNED_INT, 0); //绘制想要的物体
//glDrawArrays(GL_TRIANGLES, 0, 36);
glfwSwapBuffers(window); //函数会交换颜色缓冲(是一个储存着GLFW窗口每一个像素颜色的值得大缓冲)
glfwPollEvents(); //函数检查有没有触发什么事件(比如键盘输入,鼠标移动等)、更新窗口状态
}
//删除所有分配的资源
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
//glDeleteBuffers(1, &EBO);
glfwTerminate(); //渲染循环结束后需要正确释放/删除之前的分配的所有资源
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) //视口根据窗口大小调整的函数,两个整数表示窗口的新维度
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window) //输入的控制
{
float cameraSpeed = 2.5f*deltaTime;
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;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp))*cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) // glfwGetKey函数需要一个窗口以及一个按键作为输入,函数将会返回这个按键是否正在被按下
glfwSetWindowShouldClose(window, true);
}
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;
front.x = cos(glm::radians(pitch))*cos(glm::radians(yaw));
front.y = sin(glm::radians(pitch));
front.z = cos(glm::radians(pitch))*sin(glm::radians(yaw));
cameraFront = glm::normalize(front);
}
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;
}