-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
157 lines (123 loc) · 6.23 KB
/
main.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
/*
This file is part of Magnum.
Original authors — credit is appreciated but not required:
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 —
Vladimír Vondruš <[email protected]>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute
this software, either in source code form or as a compiled binary, for any
purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of
this software dedicate any and all copyright interest in the software to
the public domain. We make this dedication for the benefit of the public
at large and to the detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <imgui.h>
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/ImGuiIntegration/Context.h>
#include <Magnum/Math/Color.h>
#include <Magnum/MeshTools/Compile.h>
#include <Magnum/Platform/Context.h>
#include <Magnum/Platform/GlfwApplication.h>
#include <Magnum/Primitives/Square.h>
#include <Magnum/Shaders/Flat.h>
#include <Magnum/Trade/MeshData2D.h>
using namespace Magnum;
int main(int argc, char** argv) {
/* Initialize the library */
if(!glfwInit()) return -1;
/* Create a windowed mode window and its OpenGL context */
GLFWwindow* const window0 = glfwCreateWindow(
800, 600, "window0", nullptr, nullptr);
GLFWwindow* const window1 = glfwCreateWindow(
640, 480, "window1", nullptr, window0);
GLFWwindow* const window2 = glfwCreateWindow(
640, 480, "window2", nullptr, window0);
if(!window0 || !window1 || !window2) {
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window0);
{
/* Create Magnum context in an isolated scope */
Platform::GLContext ctx{ argc, argv };
ImGuiIntegration::Context _imgui{ NoCreate };
_imgui = ImGuiIntegration::Context(Vector2{ GL::defaultFramebuffer.viewport().size() },
GL::defaultFramebuffer.viewport().size(), GL::defaultFramebuffer.viewport().size());
GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add,
GL::Renderer::BlendEquation::Add);
GL::Renderer::setBlendFunction(GL::Renderer::BlendFunction::SourceAlpha,
GL::Renderer::BlendFunction::OneMinusSourceAlpha);
using namespace Math::Literals;
bool hideImGui = false;
glfwSetWindowUserPointer(window0, &hideImGui);
glfwSetKeyCallback(window0, [](GLFWwindow* window, int key, int scancode, int action, int mods) {
bool* hideImGui = static_cast<bool*>(glfwGetWindowUserPointer(window));
if (action == GLFW_PRESS) {
*hideImGui = !*hideImGui;
}
});
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window0)) {
{
glfwMakeContextCurrent(window0);
GL::defaultFramebuffer.clear(GL::FramebufferClear::Color);
if (!hideImGui) {
_imgui.newFrame();
{
ImGui::Text("Hello, world!");
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
1000.0 / Double(ImGui::GetIO().Framerate), Double(ImGui::GetIO().Framerate));
}
/* Set appropriate states. If you only draw imgui UI, it is sufficient to
do this once in the constructor. */
GL::Renderer::enable(GL::Renderer::Feature::Blending);
GL::Renderer::disable(GL::Renderer::Feature::FaceCulling);
GL::Renderer::disable(GL::Renderer::Feature::DepthTest);
GL::Renderer::enable(GL::Renderer::Feature::ScissorTest);
_imgui.drawFrame();
/* Reset state. Only needed if you want to draw something else with
different state next frame. */
GL::Renderer::disable(GL::Renderer::Feature::ScissorTest);
GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
GL::Renderer::enable(GL::Renderer::Feature::FaceCulling);
GL::Renderer::disable(GL::Renderer::Feature::Blending);
}
glfwSwapBuffers(window0);
}
{
glfwMakeContextCurrent(window1);
GL::Renderer::setClearColor(0xa5c9ea_rgbf);
GL::defaultFramebuffer.clear(GL::FramebufferClear::Color | GL::FramebufferClear::Depth);
MeshTools::compile(Primitives::squareSolid())
.draw(Shaders::Flat2D{}
.setTransformationProjectionMatrix(Matrix3::scaling({ 0.2f, 0.3f }))
.setColor(0xff0000_rgbf));
glfwSwapBuffers(window1);
}
{
glfwMakeContextCurrent(window2);
GL::Renderer::setClearColor(0xa5c9ea_rgbf);
GL::defaultFramebuffer.clear(GL::FramebufferClear::Color | GL::FramebufferClear::Depth);
MeshTools::compile(Primitives::squareSolid())
.draw(Shaders::Flat2D{}
.setTransformationProjectionMatrix(Matrix3::scaling({ 0.2f, 0.3f }))
.setColor(0x00ff00_rgbf));
glfwSwapBuffers(window2);
}
/* Poll for and process events */
glfwPollEvents();
}
}
glfwTerminate();
}