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-23 07:20:42 +00:00
|
|
|
Shader shader = Shader("src/tex.vs", "src/tex.fs");
|
|
|
|
|
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-24 09:17:49 +00:00
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
|
2023-05-22 10:35:30 +00:00
|
|
|
glEnableVertexAttribArray(0);
|
2023-05-24 09:17:49 +00:00
|
|
|
|
|
|
|
// 纹理坐标属性
|
|
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * 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-24 07:16:30 +00:00
|
|
|
// 创建纹理
|
|
|
|
unsigned int texture1, texture2;
|
|
|
|
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_MIN_FILTER,
|
|
|
|
GL_LINEAR_MIPMAP_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
// 加载并生成纹理
|
|
|
|
int width, height, nrChannels;
|
|
|
|
// 图片翻转y轴
|
|
|
|
stbi_set_flip_vertically_on_load(true);
|
|
|
|
unsigned char *data =
|
|
|
|
stbi_load("resources/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);
|
|
|
|
|
|
|
|
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_MIN_FILTER,
|
|
|
|
GL_LINEAR_MIPMAP_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
// 加载并生成纹理
|
|
|
|
// 图片翻转y轴
|
|
|
|
stbi_set_flip_vertically_on_load(true);
|
|
|
|
data =
|
|
|
|
stbi_load("resources/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_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);
|
|
|
|
|
|
|
|
// 激活着色器
|
|
|
|
shader.use();
|
|
|
|
// 绑定纹理
|
|
|
|
shader.setInt("texture1", 0);
|
|
|
|
shader.setInt("texture2", 1);
|
2023-05-24 08:39:49 +00:00
|
|
|
|
|
|
|
// create transformations
|
2023-05-25 08:55:12 +00:00
|
|
|
glm::mat4 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));
|
2023-05-24 08:39:49 +00:00
|
|
|
glm::mat4 projection = glm::mat4(1.0f);
|
|
|
|
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
|
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);
|
|
|
|
|
|
|
|
// 设置为白色
|
|
|
|
glClearColor(1.0f, 1.0f, 1.0f, 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-23 07:20:42 +00:00
|
|
|
// 绑定纹理
|
2023-05-24 07:16:30 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture1);
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture2);
|
2023-05-23 07:03:04 +00:00
|
|
|
|
|
|
|
// 绘制三角形
|
2023-05-24 07:16:30 +00:00
|
|
|
shader.use();
|
2023-05-24 07:51:39 +00:00
|
|
|
|
2023-05-24 08:39:49 +00:00
|
|
|
// 设置变换矩阵
|
2023-05-24 07:51:39 +00:00
|
|
|
glm::mat4 trans(1.0f);
|
2023-05-24 09:24:34 +00:00
|
|
|
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.25, 0.0));
|
2023-05-24 08:39:49 +00:00
|
|
|
shader.setMat4("trans", trans);
|
2023-05-22 10:35:30 +00:00
|
|
|
glBindVertexArray(VAO);
|
2023-05-24 09:24:34 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 10; i++) {
|
|
|
|
glm::mat4 model(1.0f);
|
|
|
|
model = glm::translate(model, cubePositions[i]);
|
2023-05-25 08:55:12 +00:00
|
|
|
float angle = 20.0f * i + 50.0f;
|
2023-05-24 09:24:34 +00:00
|
|
|
model =
|
|
|
|
glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
|
|
|
shader.setMat4("model", model);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
|
|
}
|
2023-05-22 10:35:30 +00:00
|
|
|
|
2023-05-25 08:55:12 +00:00
|
|
|
float radius = 10.0f;
|
|
|
|
view = camera.GetViewMatrix();
|
|
|
|
shader.setMat4("view", view);
|
|
|
|
|
|
|
|
projection = glm::perspective(
|
|
|
|
glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
|
|
|
shader.setMat4("projection", projection);
|
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
|
|
|
}
|