前言
本文参考文献,侵删!LearnOpenGL - 你好,三角形
本文将假设您完成了 OpenGL 的配置,并且使用 VAO、VBO 渲染图形
由于时间问题,代码的详细讲解将在几日内给出~
代码
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
// 窗口大小
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
// 顶点着色器源码
const char* vertexShaderSource = R"(
#version 330 core
layout (location = 0) in vec3 aPos;
uniform float rotation;
mat3 getRotationMatrix(float angle) {
float s = sin(angle);
float c = cos(angle);
return mat3(
c, -s, 0.0,
s, c, 0.0,
0.0, 0.0, 1.0
);
}
void main()
{
mat3 rotationMatrix = getRotationMatrix(rotation);
gl_Position = vec4(rotationMatrix * aPos, 1.0);
}
)";
// 片段着色器源码
const char* fragmentShaderSource = R"(
#version 330 core
out vec4 FragColor;
uniform float time;
void main()
{
float red = sin(time);
float green = cos(time);
float blue = 0.5 + 0.5 * sin(2.0 * time);
FragColor = vec4(red, green, blue, 1.0);
}
)";
int main() {
// 初始化GLFW
if (!glfwInit()) {
std::cout << "GLFW initialization failed" << std::endl;
return -1;
}
// 配置GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// 创建窗口对象
GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Triangle", nullptr, nullptr