forked from mitsuba-renderer/nanogui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample3.cpp
213 lines (175 loc) · 6.13 KB
/
example3.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
/*
src/example3.cpp -- C++ version of an example application that shows
how to use nanogui in an application with an already created and managed
GLFW context.
NanoGUI was developed by Wenzel Jakob <[email protected]>.
The widget drawing code is based on the NanoVG demo application
by Mikko Mononen.
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE.txt file.
*/
// GLFW
//
#if defined(NANOGUI_USE_OPENGL)
# if defined(NANOGUI_GLAD)
# if defined(NANOGUI_SHARED) && !defined(GLAD_GLAPI_EXPORT)
# define GLAD_GLAPI_EXPORT
# endif
# include <glad/glad.h>
# else
# if defined(__APPLE__)
# define GLFW_INCLUDE_GLCOREARB
# else
# define GL_GLEXT_PROTOTYPES
# endif
# endif
#elif defined(NANOGUI_USE_GLES)
# define GLFW_INCLUDE_ES2
#endif
#include <GLFW/glfw3.h>
#include <nanogui/nanogui.h>
#include <iostream>
using namespace nanogui;
enum test_enum {
Item1 = 0,
Item2,
Item3
};
bool bvar = true;
int ivar = 12345678;
double dvar = 3.1415926;
float fvar = (float)dvar;
std::string strval = "A string";
test_enum enumval = Item2;
Color colval(0.5f, 0.5f, 0.7f, 1.f);
Screen *screen = nullptr;
int main(int /* argc */, char ** /* argv */) {
glfwInit();
glfwSetTime(0);
#if defined(NANOGUI_USE_OPENGL)
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#elif defined(NANOGUI_USE_GLES)
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
#elif defined(NANOGUI_USE_METAL)
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
metal_init();
#endif
glfwWindowHint(GLFW_SAMPLES, 0);
glfwWindowHint(GLFW_RED_BITS, 8);
glfwWindowHint(GLFW_GREEN_BITS, 8);
glfwWindowHint(GLFW_BLUE_BITS, 8);
glfwWindowHint(GLFW_ALPHA_BITS, 8);
glfwWindowHint(GLFW_STENCIL_BITS, 8);
glfwWindowHint(GLFW_DEPTH_BITS, 24);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
// Create a GLFWwindow object
GLFWwindow* window = glfwCreateWindow(500, 700, "example3", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
#if defined(NANOGUI_GLAD)
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
throw std::runtime_error("Could not initialize GLAD!");
glGetError(); // pull and ignore unhandled errors like GL_INVALID_ENUM
#endif
// Create a nanogui screen and pass the glfw pointer to initialize
screen = new Screen();
screen->initialize(window, true);
#if defined(NANOGUI_USE_OPENGL) || defined(NANOGUI_USE_GLES)
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glfwSwapInterval(0);
glfwSwapBuffers(window);
#endif
// Create nanogui gui
bool enabled = true;
FormHelper *gui = new FormHelper(screen);
ref<Window> nanogui_window = gui->add_window(Vector2i(10, 10), "Form helper example");
gui->add_group("Basic types");
gui->add_variable("bool", bvar)->set_tooltip("Test tooltip.");
gui->add_variable("string", strval);
gui->add_group("Validating fields");
gui->add_variable("int", ivar)->set_spinnable(true);
gui->add_variable("float", fvar)->set_tooltip("Test.");
gui->add_variable("double", dvar)->set_spinnable(true);
gui->add_group("Complex types");
gui->add_variable("Enumeration", enumval, enabled)->set_items({ "Item 1", "Item 2", "Item 3" });
gui->add_variable("Color", colval);
gui->add_group("Other widgets");
gui->add_button("A button", []() { std::cout << "Button pressed." << std::endl; })
->set_tooltip("Testing a much longer tooltip, that will wrap around to new lines multiple times.");;
screen->set_visible(true);
screen->perform_layout();
nanogui_window->center();
screen->clear();
screen->draw_all();
glfwSetCursorPosCallback(window,
[](GLFWwindow *, double x, double y) {
screen->cursor_pos_callback_event(x, y);
}
);
glfwSetMouseButtonCallback(window,
[](GLFWwindow *, int button, int action, int modifiers) {
screen->mouse_button_callback_event(button, action, modifiers);
}
);
glfwSetKeyCallback(window,
[](GLFWwindow *, int key, int scancode, int action, int mods) {
screen->key_callback_event(key, scancode, action, mods);
}
);
glfwSetCharCallback(window,
[](GLFWwindow *, unsigned int codepoint) {
screen->char_callback_event(codepoint);
}
);
glfwSetDropCallback(window,
[](GLFWwindow *, int count, const char **filenames) {
screen->drop_callback_event(count, filenames);
}
);
glfwSetScrollCallback(window,
[](GLFWwindow *, double x, double y) {
screen->scroll_callback_event(x, y);
}
);
glfwSetFramebufferSizeCallback(window,
[](GLFWwindow *, int width, int height) {
screen->resize_callback_event(width, height);
}
);
// Game loop
while (!glfwWindowShouldClose(window)) {
// Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
#if defined(NANOGUI_USE_METAL)
// Important to periodically free memory used up by Metal command queues, etc.
void *pool = autorelease_init();
#endif
// Draw nanogui
screen->draw_setup();
screen->clear(); // glClear
screen->draw_contents();
screen->draw_widgets();
screen->draw_teardown();
#if defined(NANOGUI_USE_METAL)
autorelease_release(pool);
#endif
glfwSwapBuffers(window);
}
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
#if defined(NANOGUI_USE_METAL)
metal_shutdown();
#endif
return 0;
}