2023-05-22 10:35:30 +00:00
|
|
|
#include "glad/glad.h"
|
2023-05-23 07:03:04 +00:00
|
|
|
#include "shader.h"
|
2023-05-22 10:35:30 +00:00
|
|
|
#include <GLFW/glfw3.h>
|
2023-05-23 07:03:04 +00:00
|
|
|
#include <cmath>
|
2023-05-22 10:35:30 +00:00
|
|
|
|
|
|
|
// 三角形顶点
|
2023-05-22 17:21:52 +00:00
|
|
|
float vertices[] = {
|
|
|
|
// 第一个三角形
|
|
|
|
0.4f, 0.4f, 0.0f, // 右上角
|
|
|
|
0.4f, -0.4f, 0.0f, // 右下角
|
|
|
|
-0.4f, -0.4f, 0.0f, // 左下角
|
|
|
|
-0.4f, 0.4f, 0.0f // 左上角
|
|
|
|
};
|
|
|
|
|
|
|
|
// 索引
|
|
|
|
unsigned int indices[] = {
|
|
|
|
0, 1, 3, // 第一个三角形
|
|
|
|
1, 2, 3 // 第二个三角形
|
|
|
|
};
|
2023-05-22 10:35:30 +00:00
|
|
|
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
|
|
|
void processInput(GLFWwindow *window);
|
|
|
|
|
|
|
|
// settings
|
|
|
|
const unsigned int SCR_WIDTH = 800;
|
|
|
|
const unsigned int SCR_HEIGHT = 600;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 创建窗口
|
|
|
|
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 设置视口
|
|
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
|
|
|
|
|
|
// 创建着色器程序
|
2023-05-23 07:03:04 +00:00
|
|
|
Shader shader = Shader("src/simple.vs", "src/uniformcolor.fs");
|
2023-05-22 10:35:30 +00:00
|
|
|
|
2023-05-22 17:21:52 +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-22 17:21:52 +00:00
|
|
|
// 创建索引缓冲对象
|
|
|
|
glGenBuffers(1, &EBO);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
|
|
|
|
GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// 设置顶点属性指针
|
2023-05-22 10:35:30 +00:00
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
|
|
|
|
// 循环渲染
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
|
|
|
// 处理输入
|
|
|
|
processInput(window);
|
|
|
|
|
|
|
|
// 设置为白色
|
|
|
|
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2023-05-23 07:03:04 +00:00
|
|
|
// 激活着色器
|
|
|
|
shader.use();
|
|
|
|
|
|
|
|
// 更新uniform颜色
|
|
|
|
float timeValue = glfwGetTime();
|
|
|
|
float greenValue = (sin(timeValue) / 2.0f) + 0.5f;
|
|
|
|
shader.setFloat4("ourColor", 1.0f, greenValue, 0.0f, 0.0f);
|
|
|
|
shader.setFloat1("xOffset", cos(timeValue) / 2.0f);
|
|
|
|
shader.setFloat1("yOffset", sin(timeValue) / 2.0f);
|
|
|
|
|
|
|
|
// 绘制三角形
|
2023-05-22 10:35:30 +00:00
|
|
|
glBindVertexArray(VAO);
|
2023-05-23 07:03:04 +00:00
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
2023-05-22 17:21:52 +00:00
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
|
|
glBindVertexArray(0);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 窗口大小改变时回调该函数
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
}
|