forked from alandefreitas/matplotplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathogl_embed_main.cpp
More file actions
79 lines (65 loc) 路 2.42 KB
/
Copy pathogl_embed_main.cpp
File metadata and controls
79 lines (65 loc) 路 2.42 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
#include <matplot/backend/opengl_embed.h>
#include <matplot/matplot.h>
int main() {
using namespace matplot;
// Initialize
if (!glfwInit()) {
glfwTerminate();
throw std::runtime_error("Failed to initialize GLFW");
}
// Initialization hints
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// Create window
GLFWwindow *window =
glfwCreateWindow(backend::opengl_embed::default_screen_width,
backend::opengl_embed::default_screen_height,
"My Existing Application", nullptr, nullptr);
if (window == nullptr) {
glfwTerminate();
throw std::runtime_error("Failed to create GLFW window");
}
// Set window as context
glfwMakeContextCurrent(window);
// Set callback when we resize the window
glfwSetFramebufferSizeCallback(
window, backend::opengl_embed::framebuffer_size_callback);
// Load OpenGL function pointers
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
throw std::runtime_error("Failed to initialize GLAD");
}
// Create figure with backend
auto f = figure<backend::opengl_embed>(true);
auto ax = f->current_axes();
ax->xlim({0.,2. * pi});
ax->ylim({-1.5,1.5});
ax->yticks(iota(-1.5,0.5,+1.5));
ax->xticks(iota(0.,1.,2. * pi));
// Start rendering
while (!glfwWindowShouldClose(window)) {
// Check if there is any input to process
backend::opengl_embed::process_input(window);
// Create plots
float timeValue = glfwGetTime();
std::vector<double> x = linspace(0., 2. * pi);
std::vector<double> y = transform(x, [&](auto x) { return sin(x + timeValue); });
ax->hold(off);
ax->plot(x, y, "-o");
ax->hold(on);
ax->plot(x, transform(y, [](auto y) { return -y; }), "--xr");
ax->plot(x, transform(x, [](auto x) { return x / pi - 1.; }), "-:gs");
ax->plot({1.0, 0.7, 0.4, 0.0, -0.4, -0.7, -1}, "k");
// Draw the figure
f->draw();
// Swap buffers and poll IO events
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
glfwTerminate();
return 0;
}