DecodeStudios

Decode.cpp NanoGUI Sample code

Dec 7th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. /*
  2.     src/example3.cpp -- C++ version of an example application that shows
  3.     how to use nanogui in an application with an already created and managed
  4.     glfw context.
  5.  
  6.     NanoGUI was developed by Wenzel Jakob <[email protected]>.
  7.     The widget drawing code is based on the NanoVG demo application
  8.     by Mikko Mononen.
  9.  
  10.     All rights reserved. Use of this source code is governed by a
  11.     BSD-style license that can be found in the LICENSE.txt file.
  12. */
  13.  
  14. // GLFW
  15. //
  16. #if defined(NANOGUI_GLAD)
  17. #if defined(NANOGUI_SHARED) && !defined(GLAD_GLAPI_EXPORT)
  18. #define GLAD_GLAPI_EXPORT
  19. #endif
  20.  
  21. #include <glad/glad.h>
  22. #else
  23. #if defined(__APPLE__)
  24. #define GLFW_INCLUDE_GLCOREARB
  25. #else
  26. #define GL_GLEXT_PROTOTYPES
  27. #endif
  28. #endif
  29.  
  30. #include <GLFW/glfw3.h>
  31. #include <nanogui/nanogui.h>
  32. #include <iostream>
  33.  
  34. using namespace nanogui;
  35.  
  36. enum test_enum {
  37.     Item1 = 0,
  38.     Item2,
  39.     Item3
  40. };
  41.  
  42. bool bvar = true;
  43. int ivar = 12345678;
  44. double dvar = 3.1415926;
  45. float fvar = (float)dvar;
  46. std::string strval = "A string";
  47. test_enum enumval = Item2;
  48. Color colval(0.5f, 0.5f, 0.7f, 1.f);
  49.  
  50. Screen *screen = nullptr;
  51.  
  52. int main(int /* argc */, char ** /* argv */) {
  53.  
  54.     glfwInit();
  55.  
  56.     glfwSetTime(0);
  57.  
  58.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  59.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  60.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  61.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  62.  
  63.     glfwWindowHint(GLFW_SAMPLES, 0);
  64.     glfwWindowHint(GLFW_RED_BITS, 8);
  65.     glfwWindowHint(GLFW_GREEN_BITS, 8);
  66.     glfwWindowHint(GLFW_BLUE_BITS, 8);
  67.     glfwWindowHint(GLFW_ALPHA_BITS, 8);
  68.     glfwWindowHint(GLFW_STENCIL_BITS, 8);
  69.     glfwWindowHint(GLFW_DEPTH_BITS, 24);
  70.     glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
  71.  
  72.     // Create a GLFWwindow object
  73.     GLFWwindow* window = glfwCreateWindow(800, 800, "example3", nullptr, nullptr);
  74.     if (window == nullptr) {
  75.         std::cout << "Failed to create GLFW window" << std::endl;
  76.         glfwTerminate();
  77.         return -1;
  78.     }
  79.     glfwMakeContextCurrent(window);
  80.  
  81. #if defined(NANOGUI_GLAD)
  82.     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  83.         throw std::runtime_error("Could not initialize GLAD!");
  84.     glGetError(); // pull and ignore unhandled errors like GL_INVALID_ENUM
  85. #endif
  86.  
  87.     glClearColor(0.2f, 0.25f, 0.3f, 1.0f);
  88.     glClear(GL_COLOR_BUFFER_BIT);
  89.  
  90.     // Create a nanogui screen and pass the glfw pointer to initialize
  91.     screen = new Screen();
  92.     screen->initialize(window, true);
  93.  
  94.     int width, height;
  95.     glfwGetFramebufferSize(window, &width, &height);
  96.     glViewport(0, 0, width, height);
  97.     glfwSwapInterval(0);
  98.     glfwSwapBuffers(window);
  99.  
  100.     // Create nanogui gui
  101.     bool enabled = true;
  102.     FormHelper *gui = new FormHelper(screen);
  103.     ref<Window> nanoguiWindow = gui->addWindow(Eigen::Vector2i(10, 10), "Form helper example");
  104.     gui->addGroup("Basic types");
  105.     gui->addVariable("bool", bvar)->setTooltip("Test tooltip.");
  106.     gui->addVariable("string", strval);
  107.  
  108.     gui->addGroup("Validating fields");
  109.     gui->addVariable("int", ivar)->setSpinnable(true);
  110.     gui->addVariable("float", fvar)->setTooltip("Test.");
  111.     gui->addVariable("double", dvar)->setSpinnable(true);
  112.  
  113.     gui->addGroup("Complex types");
  114.     gui->addVariable("Enumeration", enumval, enabled)->setItems({ "Item 1", "Item 2", "Item 3" });
  115.     gui->addVariable("Color", colval)
  116.         ->setFinalCallback([](const Color &c) {
  117.         std::cout << "ColorPicker Final Callback: ["
  118.             << c.r() << ", "
  119.             << c.g() << ", "
  120.             << c.b() << ", "
  121.             << c.w() << "]" << std::endl;
  122.     });
  123.  
  124.     gui->addGroup("Other widgets");
  125.     gui->addButton("A button", []() { std::cout << "Button pressed." << std::endl; })->setTooltip("Testing a much longer tooltip, that will wrap around to new lines multiple times.");;
  126.  
  127.     screen->setVisible(true);
  128.     screen->performLayout();
  129.     nanoguiWindow->center();
  130.  
  131.     glfwSetCursorPosCallback(window,
  132.         [](GLFWwindow *, double x, double y) {
  133.         screen->cursorPosCallbackEvent(x, y);
  134.     }
  135.     );
  136.  
  137.     glfwSetMouseButtonCallback(window,
  138.         [](GLFWwindow *, int button, int action, int modifiers) {
  139.         screen->mouseButtonCallbackEvent(button, action, modifiers);
  140.     }
  141.     );
  142.  
  143.     glfwSetKeyCallback(window,
  144.         [](GLFWwindow *, int key, int scancode, int action, int mods) {
  145.         screen->keyCallbackEvent(key, scancode, action, mods);
  146.     }
  147.     );
  148.  
  149.     glfwSetCharCallback(window,
  150.         [](GLFWwindow *, unsigned int codepoint) {
  151.         screen->charCallbackEvent(codepoint);
  152.     }
  153.     );
  154.  
  155.     glfwSetDropCallback(window,
  156.         [](GLFWwindow *, int count, const char **filenames) {
  157.         screen->dropCallbackEvent(count, filenames);
  158.     }
  159.     );
  160.  
  161.     glfwSetScrollCallback(window,
  162.         [](GLFWwindow *, double x, double y) {
  163.         screen->scrollCallbackEvent(x, y);
  164.     }
  165.     );
  166.  
  167.     glfwSetFramebufferSizeCallback(window,
  168.         [](GLFWwindow *, int width, int height) {
  169.         screen->resizeCallbackEvent(width, height);
  170.     }
  171.     );
  172.  
  173.     // Game loop
  174.     while (!glfwWindowShouldClose(window)) {
  175.         // Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
  176.         glfwPollEvents();
  177.  
  178.         glClearColor(0.2f, 0.25f, 0.3f, 1.0f);
  179.         glClear(GL_COLOR_BUFFER_BIT);
  180.  
  181.         // Draw nanogui
  182.         screen->drawContents();
  183.         screen->drawWidgets();
  184.  
  185.         glfwSwapBuffers(window);
  186.     }
  187.  
  188.     // Terminate GLFW, clearing any resources allocated by GLFW.
  189.     glfwTerminate();
  190.  
  191.     return 0;
  192. }
Add Comment
Please, Sign In to add comment