Skip to content

Commit

Permalink
PICK_MODE
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouhang95 committed Oct 8, 2023
1 parent 7c33474 commit 66f3d79
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 81 deletions.
4 changes: 2 additions & 2 deletions ui/zenoedit/viewport/cameracontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
6 changes: 3 additions & 3 deletions ui/zenoedit/viewport/displaywidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
102 changes: 51 additions & 51 deletions ui/zenoedit/viewportinteraction/picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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++;
Expand All @@ -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 + " ";
}
Expand All @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions ui/zenoedit/viewportinteraction/picker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -45,8 +45,8 @@ class Picker
const std::unordered_set<std::string>& get_picked_prims();
const std::unordered_map<std::string, std::unordered_set<int>>& 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);
Expand All @@ -59,8 +59,8 @@ class Picker
private:
zenovis::Scene* scene() const;

std::unique_ptr<zenovis::IPicker> picker;

std::unique_ptr<zenovis::IPicker> picker;

ViewportWidget* m_pViewport;

std::function<void(float, int, int)> picked_depth_callback;
Expand All @@ -69,7 +69,7 @@ class Picker
std::unordered_set<std::string> selected_prims;
std::unordered_map<std::string, std::unordered_set<int>> selected_elements;

int select_mode_context;
zenovis::PICK_MODE select_mode_context;
std::unordered_set<std::string> selected_prims_context;
std::unordered_map<std::string, std::unordered_set<int>> selected_elements_context;

Expand Down
10 changes: 9 additions & 1 deletion zenovis/include/zenovis/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<zeno::vec4f> select_box = {};
std::unordered_set<std::string> selected = {};
int select_mode = 0;
PICK_MODE select_mode = PICK_MODE::PICK_OBJECT;
std::unordered_map<std::string, std::unordered_set<int>> selected_elements = {};
std::unique_ptr<Camera> camera;
std::unique_ptr<DrawOptions> drawOptions;
Expand Down
7 changes: 0 additions & 7 deletions zenovis/include/zenovis/bate/IGraphic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<zeno::IObject> objholder;
Expand Down
12 changes: 6 additions & 6 deletions zenovis/src/bate/FrameBufferPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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<unsigned int> selected_obj;
// fetch selected objects' ids
for (int i = 0; i < pixel_count; i++) {
Expand Down
8 changes: 4 additions & 4 deletions zenovis/src/bate/HudGraphicPrimHighlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<int> ind(selected_count);
Expand All @@ -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<vec2i> ind(selected_count);
Expand All @@ -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<vec3i> ind(selected_count);
int i = 0;
Expand Down

0 comments on commit 66f3d79

Please sign in to comment.