From 66f3d795c6fc672b119dbc52dd91bcc9300ec6f5 Mon Sep 17 00:00:00 2001 From: zhouhang95 <765229842@qq.com> Date: Sun, 8 Oct 2023 16:36:08 +0800 Subject: [PATCH] PICK_MODE --- ui/zenoedit/viewport/cameracontrol.cpp | 4 +- ui/zenoedit/viewport/displaywidget.cpp | 6 +- ui/zenoedit/viewportinteraction/picker.cpp | 102 +++++++++---------- ui/zenoedit/viewportinteraction/picker.h | 14 +-- zenovis/include/zenovis/Scene.h | 10 +- zenovis/include/zenovis/bate/IGraphic.h | 7 -- zenovis/src/bate/FrameBufferPicker.cpp | 12 +-- zenovis/src/bate/HudGraphicPrimHighlight.cpp | 8 +- 8 files changed, 82 insertions(+), 81 deletions(-) diff --git a/ui/zenoedit/viewport/cameracontrol.cpp b/ui/zenoedit/viewport/cameracontrol.cpp index f9427ed9d5..5a84adf49a 100644 --- a/ui/zenoedit/viewport/cameracontrol.cpp +++ b/ui/zenoedit/viewport/cameracontrol.cpp @@ -564,7 +564,7 @@ void CameraControl::fakeMouseReleaseEvent(QMouseEvent *event) { } else { m_picker->pick(releasePos.x(), releasePos.y()); m_picker->sync_to_scene(); - if (scene->select_mode == zenovis::PICK_OBJECT) + if (scene->select_mode == zenovis::PICK_MODE::PICK_OBJECT) onPrimSelected(); m_transformer->clear(); m_transformer->addObject(m_picker->get_picked_prims()); @@ -588,7 +588,7 @@ void CameraControl::fakeMouseReleaseEvent(QMouseEvent *event) { int y1 = releasePos.y(); m_picker->pick(x0, y0, x1, y1); m_picker->sync_to_scene(); - if (scene->select_mode == zenovis::PICK_OBJECT) + if (scene->select_mode == zenovis::PICK_MODE::PICK_OBJECT) onPrimSelected(); m_transformer->clear(); m_transformer->addObject(m_picker->get_picked_prims()); diff --git a/ui/zenoedit/viewport/displaywidget.cpp b/ui/zenoedit/viewport/displaywidget.cpp index dee18aba3d..366e67d461 100644 --- a/ui/zenoedit/viewport/displaywidget.cpp +++ b/ui/zenoedit/viewport/displaywidget.cpp @@ -1201,11 +1201,11 @@ void DisplayWidget::onNodeSelected(const QModelIndex &subgIdx, const QModelIndex // read selected mode auto select_mode_str = zeno::NodeSyncMgr::GetInstance().getInputValString(nodes[0], "mode"); if (select_mode_str == "triangle") - scene->select_mode = zenovis::PICK_MESH; + scene->select_mode = zenovis::PICK_MODE::PICK_MESH; else if (select_mode_str == "line") - scene->select_mode = zenovis::PICK_LINE; + scene->select_mode = zenovis::PICK_MODE::PICK_LINE; else - scene->select_mode = zenovis::PICK_VERTEX; + scene->select_mode = zenovis::PICK_MODE::PICK_VERTEX; // read selected elements string node_context; auto node_selected_str = zeno::NodeSyncMgr::GetInstance().getParamValString(nodes[0], "selected"); diff --git a/ui/zenoedit/viewportinteraction/picker.cpp b/ui/zenoedit/viewportinteraction/picker.cpp index 005baddf10..481ec91965 100644 --- a/ui/zenoedit/viewportinteraction/picker.cpp +++ b/ui/zenoedit/viewportinteraction/picker.cpp @@ -80,35 +80,35 @@ namespace zeno { // onPrimitiveSelected(); //} -Picker::Picker(ViewportWidget *pViewport) - : select_mode_context(-1) - , m_pViewport(pViewport) - , draw_mode(false) -{ +Picker::Picker(ViewportWidget *pViewport) + : select_mode_context(zenovis::PICK_MODE::PICK_NONE) + , m_pViewport(pViewport) + , draw_mode(false) +{ } -void Picker::initialize() -{ - auto scene = this->scene(); - ZASSERT_EXIT(scene); - picker = zenovis::makeFrameBufferPicker(scene); +void Picker::initialize() +{ + auto scene = this->scene(); + ZASSERT_EXIT(scene); + picker = zenovis::makeFrameBufferPicker(scene); } zenovis::Scene* Picker::scene() const -{ - auto sess = m_pViewport->getSession(); - ZASSERT_EXIT(sess, nullptr); - return sess->get_scene(); +{ + auto sess = m_pViewport->getSession(); + ZASSERT_EXIT(sess, nullptr); + return sess->get_scene(); } -void Picker::pick(int x, int y) { - auto scene = this->scene(); +void Picker::pick(int x, int y) { + auto scene = this->scene(); ZASSERT_EXIT(scene); // qDebug() << scene->select_mode; - // scene->select_mode = zenovis::PICK_MESH; + // scene->select_mode = zenovis::PICK_MODE::PICK_MESH; auto selected = picker->getPicked(x, y); - if (scene->select_mode == zenovis::PICK_OBJECT) { + if (scene->select_mode == zenovis::PICK_MODE::PICK_OBJECT) { if (selected.empty()) { selected_prims.clear(); return; @@ -145,23 +145,23 @@ void Picker::pick(int x, int y) { // onPrimitiveSelected(); } -void Picker::pick(int x0, int y0, int x1, int y1) { - auto scene = this->scene(); +void Picker::pick(int x0, int y0, int x1, int y1) { + auto scene = this->scene(); ZASSERT_EXIT(scene); auto selected = picker->getPicked(x0, y0, x1, y1); - // qDebug() << "pick: " << selected.c_str(); - if (scene->select_mode == zenovis::PICK_OBJECT) { + // qDebug() << "pick: " << selected.c_str(); + if (scene->select_mode == zenovis::PICK_MODE::PICK_OBJECT) { if (selected.empty()) { selected_prims.clear(); return; } - load_from_str(selected, zenovis::PICK_OBJECT); + load_from_str(selected, zenovis::PICK_MODE::PICK_OBJECT); } - else { + else { if (selected.empty()) { selected_elements.clear(); return; - } + } load_from_str(selected, scene->select_mode); if (picked_elems_callback) picked_elems_callback(selected_elements); } @@ -177,38 +177,38 @@ void Picker::add(const string& prim_name) { selected_prims.insert(prim_name); } -string Picker::just_pick_prim(int x, int y) { - auto scene = this->scene(); +string Picker::just_pick_prim(int x, int y) { + auto scene = this->scene(); ZASSERT_EXIT(scene, ""); - auto store_mode = scene->select_mode; - scene->select_mode = zenovis::PICK_OBJECT; - auto res = picker->getPicked(x, y); + auto store_mode = scene->select_mode; + scene->select_mode = zenovis::PICK_MODE::PICK_OBJECT; + auto res = picker->getPicked(x, y); scene->select_mode = store_mode; return res; } -void Picker::sync_to_scene() { - auto scene = this->scene(); - ZASSERT_EXIT(scene); - +void Picker::sync_to_scene() { + auto scene = this->scene(); + ZASSERT_EXIT(scene); + scene->selected.clear(); - for (const auto& s : selected_prims) - scene->selected.insert(s); + for (const auto& s : selected_prims) + scene->selected.insert(s); scene->selected_elements.clear(); - for (const auto& p : selected_elements) + for (const auto& p : selected_elements) scene->selected_elements.insert(p); } -void Picker::load_from_str(const string& str, int mode) { +void Picker::load_from_str(const string& str, zenovis::PICK_MODE mode) { if (str.empty()) return; // parse selected string std::regex reg(" "); std::sregex_token_iterator p(str.begin(), str.end(), reg, -1); std::sregex_token_iterator end; - if (mode == zenovis::PICK_OBJECT) { + if (mode == zenovis::PICK_MODE::PICK_OBJECT) { while (p != end) { selected_prims.insert(*p); p++; @@ -234,9 +234,9 @@ void Picker::load_from_str(const string& str, int mode) { } } -string Picker::save_to_str(int mode) { +string Picker::save_to_str(zenovis::PICK_MODE mode) { string res; - if (mode == zenovis::PICK_OBJECT) { + if (mode == zenovis::PICK_MODE::PICK_OBJECT) { for (const auto& p : selected_prims) res += p + " "; } @@ -249,25 +249,25 @@ string Picker::save_to_str(int mode) { return res; } -void Picker::save_context() { - auto scene = this->scene(); - ZASSERT_EXIT(scene); - +void Picker::save_context() { + auto scene = this->scene(); + ZASSERT_EXIT(scene); + select_mode_context = scene->select_mode; selected_prims_context = std::move(selected_prims); selected_elements_context = std::move(selected_elements); } void Picker::load_context() { - if (select_mode_context < 0) return; - - auto scene = this->scene(); - ZASSERT_EXIT(scene); - + if (select_mode_context == zenovis::PICK_MODE::PICK_NONE) return; + + auto scene = this->scene(); + ZASSERT_EXIT(scene); + scene->select_mode = select_mode_context; selected_prims = std::move(selected_prims_context); selected_elements = std::move(selected_elements_context); - select_mode_context = -1; + select_mode_context = zenovis::PICK_MODE::PICK_NONE; } void Picker::focus(const string& prim_name) { diff --git a/ui/zenoedit/viewportinteraction/picker.h b/ui/zenoedit/viewportinteraction/picker.h index d1181f99ac..ca60719185 100644 --- a/ui/zenoedit/viewportinteraction/picker.h +++ b/ui/zenoedit/viewportinteraction/picker.h @@ -34,8 +34,8 @@ bool test_in_selected_bounding( class Picker { -public: - Picker(ViewportWidget* pViewport); +public: + Picker(ViewportWidget* pViewport); void initialize(); void pick(int x, int y); void pick(int x0, int y0, int x1, int y1); @@ -45,8 +45,8 @@ class Picker const std::unordered_set& get_picked_prims(); const std::unordered_map>& get_picked_elems(); void sync_to_scene(); - void load_from_str(const std::string& str, int mode); - std::string save_to_str(int mode); + void load_from_str(const std::string& str, zenovis::PICK_MODE mode); + std::string save_to_str(zenovis::PICK_MODE mode); void save_context(); void load_context(); void focus(const std::string& prim_name); @@ -59,8 +59,8 @@ class Picker private: zenovis::Scene* scene() const; - std::unique_ptr picker; - + std::unique_ptr picker; + ViewportWidget* m_pViewport; std::function picked_depth_callback; @@ -69,7 +69,7 @@ class Picker std::unordered_set selected_prims; std::unordered_map> selected_elements; - int select_mode_context; + zenovis::PICK_MODE select_mode_context; std::unordered_set selected_prims_context; std::unordered_map> selected_elements_context; diff --git a/zenovis/include/zenovis/Scene.h b/zenovis/include/zenovis/Scene.h index 26fc5f3d50..6b6f8dfa83 100644 --- a/zenovis/include/zenovis/Scene.h +++ b/zenovis/include/zenovis/Scene.h @@ -19,10 +19,18 @@ struct GraphicsManager; struct ObjectsManager; struct RenderManager; +enum class PICK_MODE { + PICK_NONE, + PICK_OBJECT, + PICK_VERTEX, + PICK_LINE, + PICK_MESH +}; + struct Scene : zeno::disable_copy { std::optional select_box = {}; std::unordered_set selected = {}; - int select_mode = 0; + PICK_MODE select_mode = PICK_MODE::PICK_OBJECT; std::unordered_map> selected_elements = {}; std::unique_ptr camera; std::unique_ptr drawOptions; diff --git a/zenovis/include/zenovis/bate/IGraphic.h b/zenovis/include/zenovis/bate/IGraphic.h index 84ed001ced..2d95b6ba5f 100644 --- a/zenovis/include/zenovis/bate/IGraphic.h +++ b/zenovis/include/zenovis/bate/IGraphic.h @@ -37,13 +37,6 @@ enum { VIEW_COORD_SYS }; -enum { - PICK_OBJECT, - PICK_VERTEX, - PICK_LINE, - PICK_MESH -}; - struct IGraphic { std::string nameid; std::shared_ptr objholder; diff --git a/zenovis/src/bate/FrameBufferPicker.cpp b/zenovis/src/bate/FrameBufferPicker.cpp index 3937df6a6c..2249b22cf5 100644 --- a/zenovis/src/bate/FrameBufferPicker.cpp +++ b/zenovis/src/bate/FrameBufferPicker.cpp @@ -309,7 +309,7 @@ struct FrameBufferPicker : IPicker { vbo->attribute(0, sizeof(float) * 0, sizeof(float) * 3, GL_FLOAT, 3); bool pick_particle = false; - if (scene->select_mode == zenovis::PICK_OBJECT) { + if (scene->select_mode == PICK_MODE::PICK_OBJECT) { pick_particle = prim->tris->empty() && prim->quads->empty() && prim->polys->empty() && prim->loops->empty(); CHECK_GL(glEnable(GL_DEPTH_TEST)); CHECK_GL(glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE)); @@ -343,7 +343,7 @@ struct FrameBufferPicker : IPicker { CHECK_GL(glDisable(GL_DEPTH_TEST)); } - if (scene->select_mode == zenovis::PICK_VERTEX || pick_particle) { + if (scene->select_mode == PICK_MODE::PICK_VERTEX || pick_particle) { // ----- enable depth test ----- CHECK_GL(glEnable(GL_DEPTH_TEST)); CHECK_GL(glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE)); @@ -371,7 +371,7 @@ struct FrameBufferPicker : IPicker { CHECK_GL(glDisable(GL_DEPTH_TEST)); } - if (scene->select_mode == zenovis::PICK_LINE) { + if (scene->select_mode == PICK_MODE::PICK_LINE) { // ----- enable depth test ----- CHECK_GL(glEnable(GL_DEPTH_TEST)); CHECK_GL(glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE)); @@ -422,7 +422,7 @@ struct FrameBufferPicker : IPicker { CHECK_GL(glDisable(GL_DEPTH_TEST)); } - if (scene->select_mode == zenovis::PICK_MESH) { + if (scene->select_mode == PICK_MODE::PICK_MESH) { // ----- enable depth test ----- CHECK_GL(glEnable(GL_DEPTH_TEST)); CHECK_GL(glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE)); @@ -493,7 +493,7 @@ struct FrameBufferPicker : IPicker { fbo->unbind(); string result; - if (scene->select_mode == zenovis::PICK_OBJECT) { + if (scene->select_mode == PICK_MODE::PICK_OBJECT) { if (!pixel.has_object() || !id_table.count(pixel.obj_id)) return ""; result = id_table[pixel.obj_id]; } @@ -546,7 +546,7 @@ struct FrameBufferPicker : IPicker { fbo->unbind(); string result; - if (scene->select_mode == zenovis::PICK_OBJECT) { + if (scene->select_mode == PICK_MODE::PICK_OBJECT) { unordered_set selected_obj; // fetch selected objects' ids for (int i = 0; i < pixel_count; i++) { diff --git a/zenovis/src/bate/HudGraphicPrimHighlight.cpp b/zenovis/src/bate/HudGraphicPrimHighlight.cpp index 30fe3db6f5..105035f574 100644 --- a/zenovis/src/bate/HudGraphicPrimHighlight.cpp +++ b/zenovis/src/bate/HudGraphicPrimHighlight.cpp @@ -72,7 +72,7 @@ struct PrimitiveHighlight : IGraphicDraw { } virtual void draw() override { - if (scene->select_mode == zenovis::PICK_OBJECT) { + if (scene->select_mode == PICK_MODE::PICK_OBJECT) { for (const auto &prim_id : scene->selected) { // ----- get primitive ----- PrimitiveObject *prim = nullptr; @@ -148,7 +148,7 @@ struct PrimitiveHighlight : IGraphicDraw { vbo->attribute(0, sizeof(float) * 0, sizeof(float) * 3, GL_FLOAT, 3); // ----- draw selected vertices ----- - if (scene->select_mode == zenovis::PICK_VERTEX) { + if (scene->select_mode == PICK_MODE::PICK_VERTEX) { // prepare indices CHECK_GL(glEnable(GL_PROGRAM_POINT_SIZE)); vector ind(selected_count); @@ -164,7 +164,7 @@ struct PrimitiveHighlight : IGraphicDraw { } // ----- draw selected edges ----- - if (scene->select_mode == zenovis::PICK_LINE) { + if (scene->select_mode == PICK_MODE::PICK_LINE) { if (prim->lines->empty()) return; // prepare indices vector ind(selected_count); @@ -180,7 +180,7 @@ struct PrimitiveHighlight : IGraphicDraw { } // ----- draw selected meshes ----- - if (scene->select_mode == zenovis::PICK_MESH) { + if (scene->select_mode == PICK_MODE::PICK_MESH) { // prepare indices vector ind(selected_count); int i = 0;