| 250 | bool opengl_embed::supports_fonts() { return false; } |
| 251 | |
| 252 | void opengl_embed::draw_path(const std::vector<double> &x, |
| 253 | const std::vector<double> &y, |
| 254 | const std::array<float, 4> &color) { |
| 255 | // Copy vertex data into the buffer's memory |
| 256 | std::vector<float> vertices; |
| 257 | for (size_t i = 0; i < x.size(); ++i) { |
| 258 | vertices.emplace_back(static_cast<float>(x[i])); |
| 259 | vertices.emplace_back(static_cast<float>(y[i])); |
| 260 | } |
| 261 | |
| 262 | // Create and bind vertex array object |
| 263 | unsigned int VAO; |
| 264 | glGenVertexArrays(1, &VAO); |
| 265 | glBindVertexArray(VAO); |
| 266 | |
| 267 | // Create and bind vertex buffer object |
| 268 | unsigned int VBO; |
| 269 | glGenBuffers(1, &VBO); |
| 270 | glBindBuffer(GL_ARRAY_BUFFER, VBO); |
| 271 | glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_DYNAMIC_DRAW); |
| 272 | |
| 273 | // Set the vertex attributes pointers |
| 274 | int vertex_attribute_location = 0; |
| 275 | size_t stride = 2 * sizeof(float); |
| 276 | glVertexAttribPointer(vertex_attribute_location, 2, GL_FLOAT, GL_FALSE, static_cast<GLsizei>(stride), (void *)0); |
| 277 | glEnableVertexAttribArray(0); |
| 278 | |
| 279 | // Activate our shader program |
| 280 | glUseProgram(draw_2d_single_color_shader_program_); |
| 281 | |
| 282 | // Set window size |
| 283 | int windowHeightLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "windowHeight"); |
| 284 | if (windowHeightLocation == -1) { |
| 285 | throw std::runtime_error("can't find uniform location"); |
| 286 | } |
| 287 | glUniform1f(windowHeightLocation, static_cast<float>(height())); |
| 288 | |
| 289 | int windowWidthLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "windowWidth"); |
| 290 | if (windowWidthLocation == -1) { |
| 291 | throw std::runtime_error("can't find uniform location"); |
| 292 | } |
| 293 | glUniform1f(windowWidthLocation, static_cast<float>(width())); |
| 294 | |
| 295 | // Set color |
| 296 | int vertexColorLocation = glGetUniformLocation(draw_2d_single_color_shader_program_, "ourColor"); |
| 297 | if (vertexColorLocation == -1) { |
| 298 | throw std::runtime_error("can't find uniform location"); |
| 299 | } |
| 300 | glUniform4f(vertexColorLocation, color[1], color[2], color[3], 1.f-color[0]); |
| 301 | |
| 302 | // Bind element buffer |
| 303 | glBindVertexArray(VAO); |
| 304 | glDrawArrays(GL_LINE_STRIP, 0, static_cast<GLsizei>(x.size())); |
| 305 | |
| 306 | // Unbind our vertex array |
| 307 | glBindVertexArray(0); |
| 308 | |
| 309 | glDeleteVertexArrays(1, &VAO); |