From ad0a80eb858df1308e4a81b77c05d9da21f47ec5 Mon Sep 17 00:00:00 2001 From: Adrian Scripca Date: Wed, 3 May 2023 12:41:54 +0300 Subject: [PATCH] Added glm. Updated widget to expose mouse focus. Added custom renderer with stippled line. --- .gitmodules | 5 +- CMakeLists.txt | 6 +- grbl.cpp | 2 +- libs/glm | 1 + libs/nanogui | 2 +- main.cpp | 256 +++++++++++++++++------------------ program.nc | 2 +- render.cpp | 354 +++++++++++++++++++++++++++++++++++++++++++++++++ render.h | 58 ++++++++ 9 files changed, 546 insertions(+), 140 deletions(-) create mode 160000 libs/glm create mode 100644 render.cpp create mode 100644 render.h diff --git a/.gitmodules b/.gitmodules index bed7096..abbd48c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,9 @@ [submodule "libs/nanogui"] path = libs/nanogui - url = https://github.com/mitsuba-renderer/nanogui.git + url = https://github.com/benishor/nanogui.git [submodule "libs/gtest"] path = libs/gtest url = https://github.com/google/googletest.git +[submodule "libs/glm"] + path = libs/glm + url = https://github.com/g-truc/glm.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 62b662d..2a4ffaf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,9 @@ set(NANOGUI_INSTALL OFF CACHE BOOL " " FORCE) # Add the configurations from nanogui add_subdirectory(libs/nanogui) add_subdirectory(libs/gtest) +add_subdirectory(libs/glm) include_directories(libs/gtest/googletest/include) +include_directories(libs/glm) # For reliability of parallel build, make the NanoGUI targets dependencies set_property(TARGET glfw glfw_objects nanogui PROPERTY FOLDER "dependencies") @@ -23,5 +25,5 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_VISIBILITY_PRESET hidden) -add_executable(sender main.cpp grbl.h grbl.cpp grbl_test.cpp grbl_communication.h grbl_communication.cpp grbl_machine.h grbl_machine.cpp string_utils.h) -target_link_libraries(sender nanogui gtest gtest_main) \ No newline at end of file +add_executable(sender main.cpp grbl.h grbl.cpp grbl_test.cpp grbl_communication.h grbl_communication.cpp grbl_machine.h grbl_machine.cpp string_utils.h render.h render.cpp) +target_link_libraries(sender nanogui GL gtest gtest_main) \ No newline at end of file diff --git a/grbl.cpp b/grbl.cpp index 71737bb..c5ef1f6 100644 --- a/grbl.cpp +++ b/grbl.cpp @@ -36,7 +36,7 @@ grbl::program::program(std::string filename) { } static auto comment_re = std::regex(R"(\(([^)]*)\))"); -static auto gcode_re = std::regex(R"(([a-zA-Z0-9\s.]+).*(\(([^)]*)\))?)"); +static auto gcode_re = std::regex(R"(([a-zA-Z0-9\s.\-]+)\s*(\(([^)]*)\))?)"); bool grbl::program::load_from_stream(std::istream& in) { instructions.clear(); diff --git a/libs/glm b/libs/glm new file mode 160000 index 0000000..efec5db --- /dev/null +++ b/libs/glm @@ -0,0 +1 @@ +Subproject commit efec5db081e3aad807d0731e172ac597f6a39447 diff --git a/libs/nanogui b/libs/nanogui index 2ee903c..449aa33 160000 --- a/libs/nanogui +++ b/libs/nanogui @@ -1 +1 @@ -Subproject commit 2ee903c96480d4aee54542ea3c340c13cc06dc32 +Subproject commit 449aa332f0b438090d8da2e4ccf5241a28931728 diff --git a/main.cpp b/main.cpp index 3356985..dfeeafb 100644 --- a/main.cpp +++ b/main.cpp @@ -51,9 +51,18 @@ #include #include "grbl.h" #include +#include #include "grbl_communication.h" #include "grbl_machine.h" #include "string_utils.h" +#include "render.h" + +#include // glm::vec3 +#include // glm::vec4 +#include // glm::mat4 +#include // glm::translate, glm::rotate, glm::scale +#include // glm::perspective +#include // glm::pi using namespace nanogui; @@ -74,6 +83,13 @@ public: grbl::program pgm; Button *btnLoadProgram, *btnCheckProgram, *btnRunProgram; + + grbl::program_renderer renderer; + + glm::vec3 cam_target = glm::vec3(0); + glm::vec3 cam_src = glm::vec3(0); + glm::vec2 cam_rotate = {0, 0}; + SenderApp() : Screen(Vector2i(1024, 768), "GRBL Sender") { inc_ref(); window = new Window(this, "Machine status"); @@ -150,8 +166,9 @@ public: btnLoadProgram = new Button(pgm_actions, "Load"); btnLoadProgram->set_callback([&] { auto path = file_dialog( - {{"nc", "G-Code files"}, - {"ngc", "G-Code files"}}, false); + {{"gcode", "G-Code files"}, + {"nc", "G-Code files"}, + {"ngc", "G-Code files"}}, true); btnCheckProgram->set_background_color(colBg); btnRunProgram->set_background_color(colBg); @@ -159,6 +176,16 @@ public: if (pgm.load_from_file(path)) { btnCheckProgram->set_enabled(true); // btnRunProgram->set_enabled(true); + renderer.update(pgm, cnc); + + auto max_pos = renderer.get_extents_max(); + auto min_pos = renderer.get_extents_min(); + + cam_target = (max_pos - min_pos) / 2.0f; + cam_src = cam_target; + cam_src.z = (max_pos.x - min_pos.x); + + cam_rotate = {0, 0}; } else { btnCheckProgram->set_enabled(false); btnRunProgram->set_enabled(false); @@ -180,51 +207,51 @@ public: }); btnRunProgram->set_tooltip("Execute program"); - // Alternative construction notation using variadic template - btnLoadProgram = window->add