-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathopengl_embed.cpp
358 lines (296 loc) · 13.7 KB
/
opengl_embed.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//
// Created by Alan Freitas on 26/08/20.
//
#include "opengl_embed.h"
#include <future>
#include <iostream>
#include <matplot/core/figure_registry.h>
#include <matplot/core/figure_type.h>
#include <matplot/util/common.h>
#include <thread>
namespace matplot::backend {
opengl_embed::opengl_embed() { create_shaders(); }
opengl_embed::opengl_embed(bool create_shaders_at_construction) {
if (create_shaders_at_construction) {
create_shaders();
}
}
void opengl_embed::create_shaders() {
// Create shaders
const char *draw_2d_single_color_vertex_shader_source =
"#version 330 core\n"
"layout (location = 0) in vec2 aPos;\n"
"uniform float windowHeight;\n"
"uniform float windowWidth;\n"
//"out vec4 vertexColor;\n"
"void main()\n"
"{\n"
" gl_Position = vec4((aPos.x/windowWidth)*2-1, (aPos.y/windowHeight)*2-1, 0.0, 1.0);"
//" vertexColor = aColor;\n"
"}\0";
unsigned int draw_2d_single_color_vertex_shader;
draw_2d_single_color_vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(draw_2d_single_color_vertex_shader, 1, &draw_2d_single_color_vertex_shader_source, NULL);
glCompileShader(draw_2d_single_color_vertex_shader);
int success;
char info_log[512];
glGetShaderiv(draw_2d_single_color_vertex_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(draw_2d_single_color_vertex_shader, 512, NULL, info_log);
throw std::runtime_error(
std::string("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n") +
info_log);
}
// create and compile fragment shader
const char *draw_2d_single_color_fragment_shader_source =
"#version 330 core\n"
"out vec4 FragColor;\n"
"\n"
"uniform vec4 ourColor;\n"
"\n"
"void main()\n"
"{\n"
" FragColor = ourColor;\n"
"}";
unsigned int draw_2d_single_color_fragment_shader;
draw_2d_single_color_fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(draw_2d_single_color_fragment_shader, 1, &draw_2d_single_color_fragment_shader_source, NULL);
glCompileShader(draw_2d_single_color_fragment_shader);
glGetShaderiv(draw_2d_single_color_fragment_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(draw_2d_single_color_fragment_shader, 512, NULL, info_log);
throw std::runtime_error(
std::string("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n") +
info_log);
}
// Link shaders into shader program
draw_2d_single_color_shader_program_ = glCreateProgram();
glAttachShader(draw_2d_single_color_shader_program_, draw_2d_single_color_vertex_shader);
glAttachShader(draw_2d_single_color_shader_program_, draw_2d_single_color_fragment_shader);
glLinkProgram(draw_2d_single_color_shader_program_);
// check if linking was successful
glGetProgramiv(draw_2d_single_color_shader_program_, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(draw_2d_single_color_shader_program_, 512, NULL, info_log);
throw std::runtime_error(
std::string("ERROR::SHADER_PROGRAM::LINKING_FAILED\n") +
info_log);
}
// Delete the shader objects
glDeleteShader(draw_2d_single_color_vertex_shader);
glDeleteShader(draw_2d_single_color_fragment_shader);
// glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &n_vertex_attributes_available_);
// std::cout << "Maximum number of vertex attributes supported: " << n_vertex_attributes_available_ << std::endl;
}
opengl_embed::~opengl_embed() {
glDeleteProgram(draw_2d_single_color_shader_program_);
}
bool opengl_embed::is_interactive() { return true; }
const std::string &opengl_embed::output() {
throw std::logic_error("output not implemented yet");
}
const std::string &opengl_embed::output_format() {
throw std::logic_error("output_format not implemented yet");
}
bool opengl_embed::output(const std::string &filename) {
throw std::logic_error("output not implemented yet");
}
bool opengl_embed::output(const std::string &filename,
const std::string &file_format) {
throw std::logic_error("output not implemented yet");
}
unsigned int opengl_embed::width() {
GLint m_viewport[4];
glGetIntegerv( GL_VIEWPORT, m_viewport );
return m_viewport[2];
}
unsigned int opengl_embed::height() {
GLint m_viewport[4];
glGetIntegerv( GL_VIEWPORT, m_viewport );
return m_viewport[3];
}
void opengl_embed::width(unsigned int new_width) {
throw std::logic_error("width not implemented yet");
}
void opengl_embed::height(unsigned int new_height) {
throw std::logic_error("height not implemented yet");
}
unsigned int opengl_embed::position_x() {
throw std::logic_error("position_x not implemented yet");
}
unsigned int opengl_embed::position_y() {
throw std::logic_error("position_y not implemented yet");
}
void opengl_embed::position_x(unsigned int new_position_x) {
throw std::logic_error("position_x not implemented yet");
}
void opengl_embed::position_y(unsigned int new_position_y) {
throw std::logic_error("position_y not implemented yet");
}
bool opengl_embed::new_frame() { return true; }
bool opengl_embed::render_data() {
return true;
}
void opengl_embed::draw_rectangle(const double x1, const double x2,
const double y1, const double y2,
const std::array<float, 4> &color) {
// Create and bind vertex array object
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// Copy vertex data into the buffer's memory
std::vector<float> vertices = {
// x, y, z, r, g, b
static_cast<float>(x2), static_cast<float>(y2), // top right
static_cast<float>(x2), static_cast<float>(y1), // bottom right
static_cast<float>(x1), static_cast<float>(y1), // bottom left
static_cast<float>(x1), static_cast<float>(y2) // top left
};
// Create and bind vertex buffer object
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_DYNAMIC_DRAW);
std::vector<unsigned int> indices = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
// Element buffer
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_DYNAMIC_DRAW);
// Set the vertex attributes pointers
int vertex_attribute_location = 0;
size_t stride = 2 * sizeof(float);
glVertexAttribPointer(vertex_attribute_location, 2, GL_FLOAT, GL_FALSE, static_cast<GLsizei>(stride), (void *)0);
glEnableVertexAttribArray(0);
// Set the color attribute pointers
// int color_attribute_location = 1;
// glVertexAttribPointer(color_attribute_location, 3, GL_FLOAT, GL_FALSE, stride, (void *)(3*sizeof(float)));
// glEnableVertexAttribArray(1);
// Activate our shader program
glUseProgram(draw_2d_single_color_shader_program_);
// Set window size
int windowHeightLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "windowHeight");
if (windowHeightLocation == -1) {
throw std::runtime_error("can't find uniform location");
}
glUniform1f(windowHeightLocation, static_cast<float>(height()));
int windowWidthLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "windowWidth");
if (windowWidthLocation == -1) {
throw std::runtime_error("can't find uniform location");
}
glUniform1f(windowWidthLocation, static_cast<float>(width()));
// Set color
int vertexColorLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "ourColor");
if (vertexColorLocation == -1) {
throw std::runtime_error("can't find uniform location");
}
glUniform4f(vertexColorLocation, color[1], color[2], color[3], 1.f-color[0]);
// Bind element buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indices.size()), GL_UNSIGNED_INT, nullptr);
// Unbind our vertex array
glBindVertexArray(0);
glDeleteBuffers(1, &EBO);
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
}
void opengl_embed::draw_background(const std::array<float, 4> &color) {
glClearColor(color[1], color[2], color[3], 1.f - color[0]);
glClear(GL_COLOR_BUFFER_BIT);
}
void opengl_embed::show(class matplot::figure_type *f) {
backend_interface::show(f);
}
bool opengl_embed::supports_fonts() { return false; }
void opengl_embed::draw_path(const std::vector<double> &x,
const std::vector<double> &y,
const std::array<float, 4> &color) {
// Copy vertex data into the buffer's memory
std::vector<float> vertices;
for (size_t i = 0; i < x.size(); ++i) {
vertices.emplace_back(static_cast<float>(x[i]));
vertices.emplace_back(static_cast<float>(y[i]));
}
// Create and bind vertex array object
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// Create and bind vertex buffer object
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_DYNAMIC_DRAW);
// Set the vertex attributes pointers
int vertex_attribute_location = 0;
size_t stride = 2 * sizeof(float);
glVertexAttribPointer(vertex_attribute_location, 2, GL_FLOAT, GL_FALSE, static_cast<GLsizei>(stride), (void *)0);
glEnableVertexAttribArray(0);
// Activate our shader program
glUseProgram(draw_2d_single_color_shader_program_);
// Set window size
int windowHeightLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "windowHeight");
if (windowHeightLocation == -1) {
throw std::runtime_error("can't find uniform location");
}
glUniform1f(windowHeightLocation, static_cast<float>(height()));
int windowWidthLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "windowWidth");
if (windowWidthLocation == -1) {
throw std::runtime_error("can't find uniform location");
}
glUniform1f(windowWidthLocation, static_cast<float>(width()));
// Set color
int vertexColorLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "ourColor");
if (vertexColorLocation == -1) {
throw std::runtime_error("can't find uniform location");
}
glUniform4f(vertexColorLocation, color[1], color[2], color[3], 1.f-color[0]);
// Bind element buffer
glBindVertexArray(VAO);
glDrawArrays(GL_LINE_STRIP, 0, static_cast<GLsizei>(x.size()));
// Unbind our vertex array
glBindVertexArray(0);
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
}
void opengl_embed::draw_markers(const std::vector<double> &x,
const std::vector<double> &y,
const std::vector<double> &z) {
throw std::logic_error("draw_markers not implemented yet");
}
void opengl_embed::draw_text(const std::vector<double> &x,
const std::vector<double> &y,
const std::vector<double> &z) {
throw std::logic_error("draw_text not implemented yet");
}
void opengl_embed::draw_image(const std::vector<std::vector<double>> &x,
const std::vector<std::vector<double>> &y,
const std::vector<std::vector<double>> &z) {
throw std::logic_error("draw_image not implemented yet");
}
void opengl_embed::draw_triangle(const std::vector<double> &x,
const std::vector<double> &y,
const std::vector<double> &z) {
throw std::logic_error("draw_triangle not implemented yet");
}
void opengl_embed::process_input(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}
void opengl_embed::framebuffer_size_callback(GLFWwindow *window, int width, int height) {
// Make sure the viewport matches the new window dimensions;
// Note that width and height will be significantly larger than
// specified on retina displays.
glViewport(0, 0, width, height);
auto f = gcf();
auto b = f->backend();
auto ogl_b = std::dynamic_pointer_cast<opengl_embed>(b);
if (ogl_b != nullptr) {
f->draw();
}
}
} // namespace matplot::backend