xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
opengl.cc
浏览该文件的文档.
2//
3#include <GLFW/glfw3.h>
4//
5#ifdef GL_BACKEND_GLBINDING
6# include <glbinding-aux/ContextInfo.h>
7# include <glbinding/gl/functions.h>
8# include <glbinding/glbinding.h>
9using namespace gl; // 把 前缀去掉,直接写 glClear 等
10#endif
11
12#include <cstdlib>
13#include <iostream>
14
15//----------------------------------------
16// 回调:窗口大小变化
17void framebuffer_size_callback(GLFWwindow* window, int w, int h) {
18 glViewport(0, 0, w, h);
19}
20
21//----------------------------------------
22// 初始化 GLFW + 创建窗口
24 if (!glfwInit()) {
25 std::cerr << "GLFW init failed\n";
26 std::exit(EXIT_FAILURE);
27 }
28
29 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
30 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
31 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
32
33#ifdef __APPLE__
34 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
35#endif
36
37 GLFWwindow* win =
38 glfwCreateWindow(800, 600, "glbinding + GLFW", nullptr, nullptr);
39 if (!win) {
40 std::cerr << "Failed to create GLFW window\n";
41 glfwTerminate();
42 std::exit(EXIT_FAILURE);
43 }
44
45 glfwMakeContextCurrent(win);
46 glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
47 return win;
48}
49
50//----------------------------------------
51// 初始化 glbinding(只需一次)
53#ifdef GL_BACKEND_GLBINDING
54 glbinding::initialize(glfwGetProcAddress, false);
55#elif defined(GL_BACKEND_GLAD)
56 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
57 throw std::runtime_error("Failed to initialize GLAD");
58 }
59#else
60# error "No OpenGL backend defined"
61#endif
62}
63
64//----------------------------------------
65// 编译着色器
66GLuint make_shader(const char* vs_src, const char* fs_src) {
67 auto compile = [](GLenum type, const char* src) -> GLuint {
68 GLuint sh = glCreateShader(type);
69 glShaderSource(sh, 1, &src, nullptr);
70 glCompileShader(sh);
71
72 GLint ok;
73 glGetShaderiv(sh, GL_COMPILE_STATUS, &ok);
74 if (!ok) {
75 GLsizei len;
76 glGetShaderiv(sh, GL_INFO_LOG_LENGTH, &len);
77 std::string log(len, '\0');
78 glGetShaderInfoLog(sh, len, &len, log.data());
79 std::cerr << "Shader compile error:\n" << log << '\n';
80 std::exit(EXIT_FAILURE);
81 }
82 return sh;
83 };
84
85 GLuint prog = glCreateProgram();
86 GLuint vs = compile(GL_VERTEX_SHADER, vs_src);
87 GLuint fs = compile(GL_FRAGMENT_SHADER, fs_src);
88
89 glAttachShader(prog, vs);
90 glAttachShader(prog, fs);
91 glLinkProgram(prog);
92
93 GLint ok;
94 glGetProgramiv(prog, GL_LINK_STATUS, &ok);
95 if (!ok) {
96 GLsizei len;
97 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
98 std::string log(len, '\0');
99 glGetProgramInfoLog(prog, len, &len, log.data());
100 std::cerr << "Program link error:\n" << log << '\n';
101 std::exit(EXIT_FAILURE);
102 }
103
104 glDeleteShader(vs);
105 glDeleteShader(fs);
106 return prog;
107}
108
109//----------------------------------------
110int main() {
111 GLFWwindow* win = init_glfw();
113
114 // 顶点数据
115 float vertices[] = {-0.5f, -0.5f, 0.0f, 0.5f, -0.5f,
116 0.0f, 0.0f, 0.5f, 0.0f};
117
118 GLuint vao, vbo;
119 glGenVertexArrays(1, &vao);
120 glGenBuffers(1, &vbo);
121
122 glBindVertexArray(vao);
123 glBindBuffer(GL_ARRAY_BUFFER, vbo);
124 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
125 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
126 glEnableVertexAttribArray(0);
127
128 // 着色器
129 const char* vs = R"(
130 #version 330 core
131 layout (location = 0) in vec3 aPos;
132 void main() { gl_Position = vec4(aPos, 1.0); }
133 )";
134 const char* fs = R"(
135 #version 330 core
136 out vec4 FragColor;
137 void main() { FragColor = vec4(0.2, 0.5, 0.8, 1.0); }
138 )";
139
140 GLuint prog = make_shader(vs, fs);
141
142 // 主循环
143 while (!glfwWindowShouldClose(win)) {
144 if (glfwGetKey(win, GLFW_KEY_ESCAPE) == GLFW_PRESS)
145 glfwSetWindowShouldClose(win, true);
146
147 glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
148 glClear(GL_COLOR_BUFFER_BIT);
149
150 glUseProgram(prog);
151 glBindVertexArray(vao);
152 glDrawArrays(GL_TRIANGLES, 0, 3);
153
154 glfwSwapBuffers(win);
155 glfwPollEvents();
156 }
157
158 glDeleteVertexArrays(1, &vao);
159 glDeleteBuffers(1, &vbo);
160 glDeleteProgram(prog);
161
162 glfwTerminate();
163 return 0;
164}
Definition typedef.hpp:7
int GLint
Definition typedef.hpp:17
int GLsizei
Definition typedef.hpp:18
unsigned int GLuint
Definition typedef.hpp:21
void init_glbackend()
Definition opengl.cc:52
void framebuffer_size_callback(GLFWwindow *window, int w, int h)
Definition opengl.cc:17
GLuint make_shader(const char *vs_src, const char *fs_src)
Definition opengl.cc:66
GLFWwindow * init_glfw()
Definition opengl.cc:23
int main()
Definition opengl.cc:110
struct GLFWwindow GLFWwindow
Definition typedef.hpp:6