OpenGL程序的配置与基本结构
第一步:下载配置
1.GLFW:一个专门针对OpenGL的C语言库
2.GLAD:OpenGL函数指针库
3.stb_image:纹理图片导入
4.GLM:OpenGL数学函数(向量,矩阵等的变换)
第二步:环境配置
创建项目并打开项目属性;
设置 c/c++目录 -> 包含目录;
设置 c/c++目录 -> 库目录;
设置 链接器 -> 输入 -> 附加依赖项(opengl32.lib;glfw3.lib;);
设置 链接器 -> 系统 -> 子系统(窗口);
设置 链接器 -> 高级 -> 入口点(mainCTRStartup);
添加项目已有源文件(glad.c);
新建源文件(stb_image.cpp):#define STB_IMAGE_IMPLEMENTATION #include <stb_image.h>
第三步:编写自定义类
1.Shader:着色器类
简化源代码,对着色器文件进行读取编译
新建头文件(Shader.h):
1 |
|
2.Camera:摄像机类
合并操作,简化代码
新建头文件(Camera.h)
1 |
|
第四步:编写着色器代码文件
顶点着色器:*.vs;片断着色器:*.fs;
*.vs:一般情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 布局输入
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
// 输出
out vec3 ourColor;
out vec2 TexCoord;
// 局外设定
uniform mat4 model; // 模型本身的变化矩阵
uniform mat4 view; // LookAt矩阵
uniform mat4 projection; // 投影矩阵
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0); // 乘法是由右往左看的
ourColor = aColor;
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}*.fs:纹理图片混合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
// 纹理采样器
uniform sampler2D texture[n]; // n组纹理采样器
void main()
{
// linearly interpolate between both textures (80% container, 20% awesomeface)
FragColor = mix(texture(texture[0], TexCoord), texture(texture[1], TexCoord), 0.2);
}
第五步:创建源文件
立即渲染模式
注:现在已经不常用了,因此只是简单介绍
1 |
|
核心模式
头文件引入,以及全局变量和函数定义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27// 环境配置头文件
// 自定义类头文件
// 函数声明
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;
// 摄像机初始化
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; // time between current frame and last frame
float lastFrame = 0.0f;glfw:初始化
1
2
3
4
5
6
7glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS Xglfw:窗口创建
1
2
3
4
5
6
7
8GLFWwindow* window = glfwCreateWindow(窗口宽度, 窗口高度, "窗口标题", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);窗口函数
1
2
3
4glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // 窗口大小改变的响应函数
glfwSetCursorPosCallback(window, mouse_callback); // 鼠标移动响应函数
glfwSetScrollCallback(window, scroll_callback); // 鼠标滚轮响应函数
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // 隐藏鼠标glad:加载OpenGL函数指针
1
2
3
4
5if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}Shader:建立编译着色器项目
1
Shader ourShader("shader.vs", "shader.fs"); // *.vs是顶点着色器;*.fs是片断着色器
设置顶点数据和缓冲并配置顶点属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46float vertices[n][] = { // n组顶点数组
// 位置(x,y,z) // 颜色(R,G,B) // 纹理(X,Y)
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[n][] = { // n组索引数组
0, 1, 3, // 第一个三角形
1, 2, 3 // 第二个三角形
};
glm::vec3 cubePositions[n] = { // n组模型位置
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 VBOs[n], VAOs[n], EBOs[n];
glGenVertexArrays(n, VAOs);
glGenBuffers(n, VBOs);
glGenBuffers(n, EBOs);
for(int m = 0;m < n;m++){
// 绑定顶点数组对象
glBindVertexArray(VAOs[m]);
// 绑定顶点缓冲对象
glBindBuffer(GL_ARRAY_BUFFER, VBOs[m]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[n]), vertices[n], GL_STATIC_DRAW);
// 绑定索引数组对象
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[m]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[n]), indices[n], GL_STATIC_DRAW);
// 位置属性传入
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * 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);
}纹理图片的导入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30unsigned int textures[n];
glGenTextures(n, textures);
for(int m = 0;m < n;m++){
glBindTexture(GL_TEXTURE_2D, textures[m]);
// 设置纹理覆盖参数
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);
// 加载图片,创建纹理并生成MIP map
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("图片路径和文件名", &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);
}
ourShader.use();
// 告诉OpenGL每个采样器的纹理单位的归属
for(int m = 0;m < n;m++){
ourShader.setInt("texture[m]",m); // *** 等于 片断着色器中uniform sampler2D的最后一个
}渲染循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41while (!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);
// 在相应的纹理单元上绑定纹理
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture[1]);
···等等
// 渲染内容
ourShader.use();
// 将投影矩阵传递给着色器 (请注意, 在这种情况下, 它可以更改每个帧)
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
ourShader.setMat4("projection", projection);
// 摄像机/视图变换
glm::mat4 view = camera.GetViewMatrix();
ourShader.setMat4("view", view);
glBindVertexArray(VAOs[m]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // EBO方式渲染图元
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); // 直接使用VBO的方式渲染图元
}
···等等
// glfw: 交换缓冲区以及挂起输入输出事件
glfwSwapBuffers(window);
glfwPollEvents();
}glfw:终止与释放
1
2
3
4
5// 可选: 释放所有资源一旦他们的目标已经使用过:
glDeleteVertexArrays(n, VAOs);
glDeleteBuffers(n, VBOs);
glfwTerminate();
return 0;其他函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39// 处理所有输入: 查询 GLFW 相关按键是否按下/松开此框架并相应做出反应
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);
}
// glfw: 每当鼠标移动时, 就会调用此回调
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);
}
// glfw: 每当鼠标滚轮滚动时, 就会调用此回调
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}
// glfw: 每当窗口大小改变时,就会调用此回调
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
OpenGL常用函数功能解析
OpenGL常见几何图元绘制
参考资料
- LearnOpenGL-CN:LearnOpenGL系列教程的中文版