2023-05-25 08:55:12 +00:00
|
|
|
|
#include "source.h"
|
2023-05-22 10:35:30 +00:00
|
|
|
|
|
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
|
|
|
|
void processInput(GLFWwindow *window);
|
2023-05-25 08:55:12 +00:00
|
|
|
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
|
|
|
|
|
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
|
|
|
|
// 设置 glfw
|
|
|
|
|
glfwInit();
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 创建窗口
|
2023-05-24 08:39:49 +00:00
|
|
|
|
GLFWwindow *window =
|
|
|
|
|
glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
if (window == NULL) {
|
|
|
|
|
std::cout << "Failed to create GLFW window" << std::endl;
|
|
|
|
|
glfwTerminate();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
|
|
|
|
|
|
// 加载glad
|
|
|
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
|
|
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置视口
|
2023-05-25 08:55:12 +00:00
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
2023-05-25 08:55:12 +00:00
|
|
|
|
glfwSetCursorPosCallback(window, mouse_callback);
|
|
|
|
|
glfwSetScrollCallback(window, scroll_callback);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
|
|
|
|
|
// 创建着色器程序
|
2023-05-25 10:13:29 +00:00
|
|
|
|
Shader shader = Shader("src/cube.vs", "src/cube.fs");
|
|
|
|
|
Shader lightShader = Shader("src/light.vs", "src/light.fs");
|
2023-05-23 07:20:42 +00:00
|
|
|
|
|
2023-05-23 07:03:04 +00:00
|
|
|
|
unsigned int VAO, VBO, EBO;
|
2023-05-22 10:35:30 +00:00
|
|
|
|
// 创建顶点数组对象
|
|
|
|
|
glGenVertexArrays(1, &VAO);
|
|
|
|
|
glBindVertexArray(VAO);
|
|
|
|
|
|
|
|
|
|
// 创建顶点缓冲对象
|
|
|
|
|
glGenBuffers(1, &VBO);
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
2023-05-23 07:20:42 +00:00
|
|
|
|
// 顶点属性
|
2023-05-25 10:13:29 +00:00
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
glEnableVertexAttribArray(0);
|
2023-05-24 09:17:49 +00:00
|
|
|
|
|
2023-05-25 10:13:29 +00:00
|
|
|
|
// 法向量属性
|
|
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
|
2023-05-23 07:20:42 +00:00
|
|
|
|
(void *)(3 * sizeof(float)));
|
|
|
|
|
glEnableVertexAttribArray(1);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
|
2023-05-25 10:13:29 +00:00
|
|
|
|
// 设置光源
|
|
|
|
|
unsigned int lightVAO;
|
|
|
|
|
glGenVertexArrays(1, &lightVAO);
|
|
|
|
|
glBindVertexArray(lightVAO);
|
|
|
|
|
// 只需要绑定VBO不用再次设置VBO的数据,因为箱子的VBO数据中已经包含了正确的立方体顶点数据
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
|
|
|
// 设置灯立方体的顶点属性(对我们的灯来说仅仅只有位置数据)
|
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
|
|
|
|
|
glEnableVertexAttribArray(0);
|
2023-05-24 07:16:30 +00:00
|
|
|
|
|
2023-05-24 09:17:49 +00:00
|
|
|
|
// 开启深度测试
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
2023-05-22 10:35:30 +00:00
|
|
|
|
// 循环渲染
|
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
2023-05-25 08:55:12 +00:00
|
|
|
|
|
|
|
|
|
// 计算帧时间差
|
|
|
|
|
float currentFrame = glfwGetTime();
|
|
|
|
|
deltaTime = currentFrame - lastFrame;
|
|
|
|
|
lastFrame = currentFrame;
|
|
|
|
|
|
2023-05-22 10:35:30 +00:00
|
|
|
|
// 处理输入
|
|
|
|
|
processInput(window);
|
|
|
|
|
|
|
|
|
|
// 设置为白色
|
2023-05-25 10:13:29 +00:00
|
|
|
|
glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
|
2023-05-24 09:24:34 +00:00
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
|
2023-05-25 10:32:58 +00:00
|
|
|
|
lightPos.x = 1.0f + sin(glfwGetTime()) * 2.0f;
|
|
|
|
|
lightPos.y = sin(glfwGetTime() / 2.0f) * 1.0f;
|
2023-05-25 10:13:29 +00:00
|
|
|
|
|
2023-05-25 10:32:58 +00:00
|
|
|
|
glm::mat4 view = camera.GetViewMatrix();
|
2023-05-25 10:13:29 +00:00
|
|
|
|
glm::mat4 projection =
|
|
|
|
|
glm::perspective(glm::radians(camera.Zoom),
|
|
|
|
|
(float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
2023-05-25 10:32:58 +00:00
|
|
|
|
glm::mat4 model = glm::mat4(1.0f);
|
2023-05-23 07:03:04 +00:00
|
|
|
|
|
|
|
|
|
// 绘制三角形
|
2023-05-24 07:16:30 +00:00
|
|
|
|
shader.use();
|
2023-05-25 10:32:58 +00:00
|
|
|
|
shader.setVec3("objectColor", 1.0f, 0.5f, 0.31f);
|
|
|
|
|
shader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
|
|
|
|
|
shader.setVec3("lightPos", lightPos);
|
|
|
|
|
shader.setVec3("viewPos", camera.Position);
|
|
|
|
|
shader.setMat4("model", model);
|
2023-05-25 10:13:29 +00:00
|
|
|
|
shader.setMat4("projection", projection);
|
|
|
|
|
shader.setMat4("view", view);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
glBindVertexArray(VAO);
|
2023-05-25 10:32:58 +00:00
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
2023-05-24 09:24:34 +00:00
|
|
|
|
|
2023-05-25 10:32:58 +00:00
|
|
|
|
model = glm::mat4(1.0f);
|
2023-05-25 10:13:29 +00:00
|
|
|
|
model = glm::translate(model, lightPos);
|
|
|
|
|
model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
|
2023-05-25 10:32:58 +00:00
|
|
|
|
|
|
|
|
|
lightShader.use();
|
2023-05-25 10:13:29 +00:00
|
|
|
|
lightShader.setMat4("projection", projection);
|
|
|
|
|
lightShader.setMat4("view", view);
|
|
|
|
|
lightShader.setMat4("model", model);
|
|
|
|
|
|
2023-05-25 10:32:58 +00:00
|
|
|
|
glBindVertexArray(lightVAO);
|
2023-05-25 10:13:29 +00:00
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
2023-05-25 08:55:12 +00:00
|
|
|
|
|
2023-05-22 10:35:30 +00:00
|
|
|
|
// 检查并调用事件,交换缓冲
|
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
|
glfwPollEvents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 释放资源
|
|
|
|
|
glfwTerminate();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理输入
|
|
|
|
|
void processInput(GLFWwindow *window) {
|
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
|
|
|
|
glfwSetWindowShouldClose(window, true);
|
2023-05-25 08:55:12 +00:00
|
|
|
|
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);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 窗口大小改变时回调该函数
|
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
|
|
|
|
glViewport(0, 0, width, height);
|
2023-05-25 08:55:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
|
|
|
|
|
camera.ProcessMouseMovement(xpos, ypos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
|
|
|
|
|
camera.ProcessMouseScroll(yoffset);
|
2023-05-22 10:35:30 +00:00
|
|
|
|
}
|