From 634cc8003611d5bf33417860eb363bec72faba50 Mon Sep 17 00:00:00 2001 From: sariug Date: Mon, 9 Mar 2020 10:37:31 +0100 Subject: [PATCH 1/2] rendering direction line. clipping function is migrated. --- src/goxel.c | 52 +++++++++++----------------------------------------- 1 file changed, 11 insertions(+), 41 deletions(-) diff --git a/src/goxel.c b/src/goxel.c index 2ab2cb686..dfe916bd1 100644 --- a/src/goxel.c +++ b/src/goxel.c @@ -903,6 +903,17 @@ void goxel_render_view(const float viewport[4], bool render_mode) bbox_grow(goxel.image->active_layer->box,5,5,5,b); render_grid(rend, goxel.clipping.plane, goxel.clipping.color,b); + + // Render clipping direction pointer + + float end[3], box_size[3], max_length; + box_get_size(b, box_size); + max_length = max(box_size[0], max(box_size[1],box_size[2])); + for(int i = 0;i<3;i++) + end[i]=goxel.clipping.origin[i]+goxel.clipping.normal[i]*max_length; + uint8_t c[4]; + vec4_set(c, 255, 0, 0, 255); + render_line(rend, goxel.clipping.origin, end,c,0); } if (goxel.show_export_viewport) render_export_viewport(viewport); @@ -1152,47 +1163,6 @@ ACTION_REGISTER(export, .csig = "vp", ) -static layer_t *clip_layer(image_t *img, layer_t *layer) -{ - layer_t *clipped_layer; - - img = img?:goxel.image; - layer = layer?:img->active_layer; - layer->visible=false; // Hide the clipped layer - clipped_layer = image_duplicate_layer(img,layer); - - - mesh_iterator_t iter; - mesh_t *mesh2clip = clipped_layer->mesh; - mesh_accessor_t accessor; - int vp[3]; - float dot; - float p[3], p_vec[3]; - uint8_t new_value[4]; - for(int i = 0;i<4;i++) - new_value[i]=0; - iter = mesh_get_iterator(mesh2clip,MESH_ITER_VOXELS); - - accessor = mesh_get_accessor(mesh2clip); - while (mesh_iter(&iter, vp)) { - vec3_set(p, vp[0] + 0.5, vp[1] + 0.5, vp[2] + 0.5); - for(int i = 0;i<3;i++) - p_vec[i]=p[i]- goxel.clipping.origin[i]; - vec3_normalize(p_vec,p_vec); - dot = vec3_dot(p_vec, goxel.clipping.normal); - if (dot>0) - mesh_set_at(mesh2clip, &accessor, vp,new_value); -} - return clipped_layer; -} - -ACTION_REGISTER(clip_layer, - .help = "Clip the current layer", - .cfunc = clip_layer, - .csig = "vpp", - .flags = ACTION_TOUCH_IMAGE, -) - static layer_t *cut_as_new_layer(image_t *img, layer_t *layer, const float box[4][4]) { From db174dbd3ff5dc02bf757b76b44866936cf90fd4 Mon Sep 17 00:00:00 2001 From: sariug Date: Mon, 9 Mar 2020 12:13:13 +0100 Subject: [PATCH 2/2] the functions are migrated to clipping.c as well as action registeration --- src/clipping.c | 143 +++++++++++++++++++++++++++++++++++++++++ src/goxel.c | 13 ++-- src/gui/layers_panel.c | 25 +++++++ src/gui/view_panel.c | 41 ------------ 4 files changed, 175 insertions(+), 47 deletions(-) create mode 100644 src/clipping.c diff --git a/src/clipping.c b/src/clipping.c new file mode 100644 index 000000000..6f8a93b25 --- /dev/null +++ b/src/clipping.c @@ -0,0 +1,143 @@ +/* Goxel 3D voxels editor + * + * copyright (c) 2017 Guillaume Chereau + * + * Goxel is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + + * Goxel is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + + * You should have received a copy of the GNU General Public License along with + * goxel. If not, see . + */ + +#include "goxel.h" + +void clip_init(void) +{ + goxel.clipping.clip=true; + + float bbox[4][4]; + box_get_bbox(goxel.image->active_layer->box,bbox); + goxel.clipping.origin[0] = bbox[3][0]; + goxel.clipping.origin[1] = bbox[3][1]; + goxel.clipping.origin[2] = bbox[3][2]; +} +void clip_cancel() +{ + goxel.clipping.clip=false; +} + +static layer_t *clip_layer(image_t *img, layer_t *layer) +{ + layer_t *clipped_layer; + + img = img?:goxel.image; + layer = layer?:img->active_layer; + layer->visible=false; // Hide the clipped layer + + clipped_layer = image_duplicate_layer(img,layer); + + + mesh_iterator_t iter; + mesh_t *mesh2clip = clipped_layer->mesh; + mesh_accessor_t accessor; + int vp[3]; + float dot, p[3], p_vec[3]; + uint8_t new_value[4]; + for(int i = 0;i<4;i++) + new_value[i]=0; + iter = mesh_get_iterator(mesh2clip,MESH_ITER_VOXELS); + + accessor = mesh_get_accessor(mesh2clip); + while (mesh_iter(&iter, vp)) { + vec3_set(p, vp[0] + 0.5, vp[1] + 0.5, vp[2] + 0.5); + for(int i = 0;i<3;i++) + p_vec[i]=p[i]- goxel.clipping.origin[i]; + vec3_normalize(p_vec,p_vec); + dot = vec3_dot(p_vec, goxel.clipping.normal); + if (dot>0) + mesh_set_at(mesh2clip, &accessor, vp,new_value); +} + goxel.clipping.clip=false; + // Update name + strcat(clipped_layer->name,"_clipped"); + return clipped_layer; +} + +static void clipping_normal_x() +{ + vec3_set(goxel.clipping.normal, 1, 0, 0); +} +static void clipping_normal_y() +{ + vec3_set(goxel.clipping.normal, 0, 1, 0); +} +static void clipping_normal_z() +{ + vec3_set(goxel.clipping.normal, 0, 0, 1); +} +static void clipping_normal_cam() +{ + camera_t *camera = goxel.image->active_camera; + float cam_normal[3]; + vec3_sub(camera->mat[3],goxel.clipping.origin, cam_normal); + vec3_normalize(cam_normal,goxel.clipping.normal); +} +static void clipping_invert_normal() +{ + vec3_mul(goxel.clipping.normal,-1,goxel.clipping.normal); +} +ACTION_REGISTER(clipping_invert_normal, + .help = "Cliping normal is being inverted.", + .cfunc = clipping_invert_normal, + .csig = "vpp", + .flags = ACTION_TOUCH_IMAGE, +) +ACTION_REGISTER(clipping_normal_x, + .help = "Cliping normal is X axis", + .cfunc = clipping_normal_x, + .csig = "vpp", + .flags = ACTION_TOUCH_IMAGE, +) +ACTION_REGISTER(clipping_normal_y, + .help = "Cliping normal is Y axis", + .cfunc = clipping_normal_y, + .csig = "vpp", + .flags = ACTION_TOUCH_IMAGE, +) +ACTION_REGISTER(clipping_normal_z, + .help = "Cliping normal is Z axis", + .cfunc = clipping_normal_z, + .csig = "vpp", + .flags = ACTION_TOUCH_IMAGE, +) +ACTION_REGISTER(clipping_normal_cam, + .help = "Cliping normal is Camera direction", + .cfunc = clipping_normal_cam, + .csig = "vpp", + .flags = ACTION_TOUCH_IMAGE, +) +ACTION_REGISTER(clip_init, + .help = "Start clipping the current layer", + .cfunc = clip_init, + .csig = "vpp", + .flags = ACTION_TOUCH_IMAGE, +) +ACTION_REGISTER(clip_layer, + .help = "Clip the current layer", + .cfunc = clip_layer, + .csig = "vpp", + .flags = ACTION_TOUCH_IMAGE, +) +ACTION_REGISTER(clip_cancel, + .help = "Cancel clipping", + .cfunc = clip_cancel, + .csig = "vpp", + .flags = ACTION_TOUCH_IMAGE, +) \ No newline at end of file diff --git a/src/goxel.c b/src/goxel.c index dfe916bd1..015eac66d 100644 --- a/src/goxel.c +++ b/src/goxel.c @@ -900,15 +900,16 @@ void goxel_render_view(const float viewport[4], bool render_mode) if (goxel.clipping.clip && !mesh_is_empty(goxel.image->active_layer->mesh)) { float b[4][4]; - bbox_grow(goxel.image->active_layer->box,5,5,5,b); + float end[3], box_size[3], max_length; + box_get_size(goxel.image->active_layer->box, box_size); + bbox_grow(goxel.image->active_layer->box,box_size[0]/10, + box_size[1]/10, + box_size[2]/10,b); render_grid(rend, goxel.clipping.plane, goxel.clipping.color,b); - + // Render clipping direction pointer - - float end[3], box_size[3], max_length; - box_get_size(b, box_size); - max_length = max(box_size[0], max(box_size[1],box_size[2])); + max_length = max(box_size[0], max(box_size[1],box_size[2]))*1.25; for(int i = 0;i<3;i++) end[i]=goxel.clipping.origin[i]+goxel.clipping.normal[i]*max_length; uint8_t c[4]; diff --git a/src/gui/layers_panel.c b/src/gui/layers_panel.c index 27c53b7fb..a8de85213 100644 --- a/src/gui/layers_panel.c +++ b/src/gui/layers_panel.c @@ -75,6 +75,7 @@ void gui_layers_panel(void) gui_group_begin(NULL); gui_action_button("img_duplicate_layer", "Duplicate", 1, ""); gui_action_button("img_clone_layer", "Clone", 1, ""); + gui_action_button("clip_init", "Clip", 1, ""); gui_action_button("img_merge_visible_layers", "Merge visible", 1, ""); layer = goxel.image->active_layer; @@ -129,4 +130,28 @@ void gui_layers_panel(void) } gui_combo_end(); } + + if(goxel.clipping.clip) { + if(gui_collapsing_header("Clipping Parameters",false)) + { + gui_vector_float("Origin", goxel.clipping.origin, 0.1, .0f, .0f, NULL); + gui_vector_float("Normal", goxel.clipping.normal, 0.1, -1.0f, 1.0f, NULL); + + gui_action_button("clipping_normal_x", "Normal X", .5, ""); + gui_same_line(); + gui_action_button("clipping_normal_y", "Normal Y", 1, ""); + gui_action_button("clipping_normal_z", "Normal Z", .5, ""); + gui_same_line(); + gui_action_button("clipping_normal_cam", "Normal Cam", 1, ""); + + gui_action_button("clipping_invert_normal", "Invert Normal", 1, ""); + } + gui_action_button("clip_layer", "Apply Clipping", 1, ""); + gui_action_button("clip_cancel", "Cancel", 1, ""); + + // Update Plane + plane_from_normal(goxel.clipping.plane,goxel.clipping.origin,goxel.clipping.normal); + + } + } diff --git a/src/gui/view_panel.c b/src/gui/view_panel.c index da87516fa..c2a8c057f 100644 --- a/src/gui/view_panel.c +++ b/src/gui/view_panel.c @@ -65,45 +65,4 @@ void gui_view_panel(void) EFFECT_MC_SMOOTH, NULL); } - gui_checkbox("Clip", &goxel.clipping.clip, NULL); - if(goxel.clipping.clip) { - gui_vector_float("Origin", goxel.clipping.origin, 0.1, .0f, .0f, NULL); - gui_vector_float("Normal", goxel.clipping.normal, 0.1, -1.0f, 1.0f, NULL); - if(gui_button("Normal X", .5, 0)) - { - goxel.clipping.normal[0] = 1; - goxel.clipping.normal[1] = 0; - goxel.clipping.normal[2] = 0; - } - gui_same_line(); - if(gui_button("Normal Y", 1, 0)) - { - goxel.clipping.normal[0] = 0; - goxel.clipping.normal[1] = 1; - goxel.clipping.normal[2] = 0; - } - if(gui_button("Normal Z", .5, 0)) - { - goxel.clipping.normal[0] = 0; - goxel.clipping.normal[1] = 0; - goxel.clipping.normal[2] = 1; - } gui_same_line(); - if(gui_button("Normal Cam", 1, 0)) - { - // camera_t *camera = goxel.image->active_camera; - // camera->view_mat; - // goxel.image->active_layer->box; - // goxel.clipping.normal=goxel. - } - if(gui_button("Invert Normal", 1, 0)) - { - for(int i = 0; i<3;i++) - goxel.clipping.normal[i] = -goxel.clipping.normal[i]; - } - gui_action_button("clip_layer", "Clip", 1, ""); - - // Update Plane - plane_from_normal(goxel.clipping.plane,goxel.clipping.origin,goxel.clipping.normal); - } - }