-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmwe_glfw.cpp
More file actions
187 lines (145 loc) · 6.49 KB
/
mwe_glfw.cpp
File metadata and controls
187 lines (145 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <glad/glad.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "graphics/window/window.hpp"
#include <cstdio>
#include <cstdlib>
#include <iostream>
unsigned int SCREEN_WIDTH = 640;
unsigned int SCREEN_HEIGHT = 480;
static const char *vertex_shader_text = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
static const char *fragment_shader_text = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
static void error_callback(int error, const char *description) { fprintf(stderr, "Error: %s\n", description); }
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
struct OpenGLDrawingData {
GLuint vbo_name;
GLuint ibo_name;
GLuint vao_name;
};
OpenGLDrawingData prepare_drawing_data_and_opengl_drawing_data() {
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
};
GLuint indices[] = {
// note that we start from 0!
0, 1, 3, // first Triangle
1, 2, 3 // second Triangle
};
// vbo: vertex buffer object
// vao: vertex array object
// ibo: index buffer object
GLuint vbo_name, vao_name, ibo_name;
glGenVertexArrays(1, &vao_name);
glGenBuffers(1, &vbo_name);
glGenBuffers(1, &ibo_name);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and
// then configure vertex attributes(s).
glBindVertexArray(vao_name);
glBindBuffer(GL_ARRAY_BUFFER, vbo_name);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_name);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered
// vbo_name as the vertex attribute's bound vertex buffer object so afterwards
// we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// remember: do NOT unbind the ibo_name while a vao_name is active as the
// bound element buffer object IS stored in the vao_name; keep the ibo_name
// bound.
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// You can unbind the vao_name afterwards so other vao_name calls won't
// accidentally modify this vao_name, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't
// unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
return {vbo_name, ibo_name, vao_name};
}
GLuint create_shader_program() {
GLuint vertex_shader;
GLuint fragment_shader;
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
int success;
char infoLog[512];
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertex_shader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
// check for shader compile errors
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragment_shader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// set up the shader program
GLuint shader_program;
shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
// check for linking errors
glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shader_program, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
// we no longer need the shader's since they've been compiled into the
// shader_program
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return shader_program;
}
int main() {
GLFWwindow *window =
initialize_glfw_glad_and_return_window(SCREEN_WIDTH, SCREEN_HEIGHT, "glfw window", false, false, false);
auto [vbo_name, ibo_name, vao_name] = prepare_drawing_data_and_opengl_drawing_data();
GLuint shader_program = create_shader_program();
int width, height;
while (!glfwWindowShouldClose(window)) {
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw our first triangle
glUseProgram(shader_program);
glBindVertexArray(vao_name); // seeing as we only have a single VAO there's
// no need to bind it every time, but we'll do
// so to keep things a bit more organized
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &vao_name);
glDeleteBuffers(1, &vbo_name);
glDeleteBuffers(1, &ibo_name);
glDeleteProgram(shader_program);
glfwTerminate();
exit(EXIT_SUCCESS);
}