2013-06-26 18:40:55 +00:00
|
|
|
/*
|
|
|
|
* This program 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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
2013-06-26 20:15:02 +00:00
|
|
|
* The Original Code is Copyright (C) 2013 Blender Foundation.
|
2013-06-26 18:40:55 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup edsculpt
|
2013-06-26 18:40:55 +00:00
|
|
|
*
|
|
|
|
* Utility functions for getting vertex locations while painting
|
2018-06-22 16:03:16 +02:00
|
|
|
* (since they may be instanced multiple times in an evaluated mesh)
|
2013-06-26 18:40:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2014-02-08 06:07:10 +11:00
|
|
|
#include "BLI_listbase.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_math.h"
|
2013-06-26 18:40:55 +00:00
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
|
|
|
#include "BKE_context.h"
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
#include "BKE_customdata.h"
|
2018-06-22 16:03:16 +02:00
|
|
|
#include "BKE_mesh_iterators.h"
|
2018-06-05 16:58:08 +02:00
|
|
|
#include "BKE_mesh_runtime.h"
|
2013-06-26 18:40:55 +00:00
|
|
|
|
2017-07-21 11:53:13 +02:00
|
|
|
#include "DEG_depsgraph.h"
|
2018-12-01 19:06:44 +03:00
|
|
|
#include "DEG_depsgraph_query.h"
|
2017-07-21 11:53:13 +02:00
|
|
|
|
2013-06-26 18:40:55 +00:00
|
|
|
#include "ED_screen.h"
|
|
|
|
#include "ED_view3d.h"
|
|
|
|
|
|
|
|
#include "paint_intern.h" /* own include */
|
|
|
|
|
|
|
|
/* Opaque Structs for internal use */
|
|
|
|
|
|
|
|
/* stored while painting */
|
|
|
|
struct VertProjHandle {
|
2018-09-27 15:54:10 +02:00
|
|
|
CoNo *vcosnos;
|
2013-06-26 18:40:55 +00:00
|
|
|
|
|
|
|
bool use_update;
|
|
|
|
|
2014-03-26 09:34:01 +11:00
|
|
|
/* use for update */
|
|
|
|
float *dists_sq;
|
|
|
|
|
2013-06-26 18:40:55 +00:00
|
|
|
Object *ob;
|
|
|
|
Scene *scene;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* only for passing to the callbacks */
|
|
|
|
struct VertProjUpdate {
|
|
|
|
struct VertProjHandle *vp_handle;
|
|
|
|
|
|
|
|
/* runtime */
|
2020-03-06 16:56:42 +01:00
|
|
|
ARegion *region;
|
2013-06-26 18:40:55 +00:00
|
|
|
const float *mval_fl;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Internal Init */
|
|
|
|
|
2018-02-02 13:44:13 +11:00
|
|
|
static void vpaint_proj_dm_map_cosnos_init__map_cb(
|
|
|
|
void *userData, int index, const float co[3], const float no_f[3], const short no_s[3])
|
2013-06-26 18:40:55 +00:00
|
|
|
{
|
|
|
|
struct VertProjHandle *vp_handle = userData;
|
2018-09-27 15:54:10 +02:00
|
|
|
CoNo *co_no = &vp_handle->vcosnos[index];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-06-26 18:40:55 +00:00
|
|
|
/* check if we've been here before (normal should not be 0) */
|
|
|
|
if (!is_zero_v3(co_no->no)) {
|
|
|
|
/* remember that multiple dm verts share the same source vert */
|
|
|
|
vp_handle->use_update = true;
|
|
|
|
return;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-06-26 18:40:55 +00:00
|
|
|
copy_v3_v3(co_no->co, co);
|
|
|
|
if (no_f) {
|
|
|
|
copy_v3_v3(co_no->no, no_f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
normal_short_to_float_v3(co_no->no, no_s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 07:30:05 +10:00
|
|
|
static void vpaint_proj_dm_map_cosnos_init(struct Depsgraph *depsgraph,
|
2018-12-01 19:06:44 +03:00
|
|
|
Scene *UNUSED(scene),
|
|
|
|
Object *ob,
|
2017-09-28 07:30:05 +10:00
|
|
|
struct VertProjHandle *vp_handle)
|
2013-06-26 18:40:55 +00:00
|
|
|
{
|
2018-12-01 19:06:44 +03:00
|
|
|
Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
|
|
|
|
Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
|
2013-06-26 18:40:55 +00:00
|
|
|
Mesh *me = ob->data;
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
|
|
|
|
CustomData_MeshMasks cddata_masks = CD_MASK_BAREMESH_ORIGINDEX;
|
|
|
|
Mesh *me_eval = mesh_get_eval_final(depsgraph, scene_eval, ob_eval, &cddata_masks);
|
2013-06-26 18:40:55 +00:00
|
|
|
|
2018-06-22 16:03:16 +02:00
|
|
|
memset(vp_handle->vcosnos, 0, sizeof(*vp_handle->vcosnos) * me->totvert);
|
|
|
|
BKE_mesh_foreach_mapped_vert(
|
|
|
|
me_eval, vpaint_proj_dm_map_cosnos_init__map_cb, vp_handle, MESH_FOREACH_USE_NORMAL);
|
2013-06-26 18:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Internal Update */
|
|
|
|
|
|
|
|
/* Same as init but take mouse location into account */
|
|
|
|
|
2018-02-02 13:44:13 +11:00
|
|
|
static void vpaint_proj_dm_map_cosnos_update__map_cb(
|
|
|
|
void *userData, int index, const float co[3], const float no_f[3], const short no_s[3])
|
2013-06-26 18:40:55 +00:00
|
|
|
{
|
|
|
|
struct VertProjUpdate *vp_update = userData;
|
2014-03-26 09:34:01 +11:00
|
|
|
struct VertProjHandle *vp_handle = vp_update->vp_handle;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 15:54:10 +02:00
|
|
|
CoNo *co_no = &vp_handle->vcosnos[index];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-26 09:34:01 +11:00
|
|
|
/* find closest vertex */
|
2013-06-26 18:40:55 +00:00
|
|
|
{
|
2014-03-26 09:34:01 +11:00
|
|
|
/* first find distance to this vertex */
|
|
|
|
float co_ss[2]; /* screenspace */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-02-02 13:44:13 +11:00
|
|
|
if (ED_view3d_project_float_object(
|
2020-03-06 16:56:42 +01:00
|
|
|
vp_update->region, co, co_ss, V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_NEAR) ==
|
2018-02-02 13:44:13 +11:00
|
|
|
V3D_PROJ_RET_OK) {
|
2014-03-26 09:34:01 +11:00
|
|
|
const float dist_sq = len_squared_v2v2(vp_update->mval_fl, co_ss);
|
|
|
|
if (dist_sq > vp_handle->dists_sq[index]) {
|
|
|
|
/* bail out! */
|
|
|
|
return;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-26 09:34:01 +11:00
|
|
|
vp_handle->dists_sq[index] = dist_sq;
|
2013-06-26 18:40:55 +00:00
|
|
|
}
|
2014-03-26 13:56:07 +11:00
|
|
|
else if (vp_handle->dists_sq[index] != FLT_MAX) {
|
|
|
|
/* already initialized & couldn't project this 'co' */
|
|
|
|
return;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2014-03-26 13:56:07 +11:00
|
|
|
}
|
2014-03-26 09:34:01 +11:00
|
|
|
/* continue with regular functionality */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-26 09:34:01 +11:00
|
|
|
copy_v3_v3(co_no->co, co);
|
|
|
|
if (no_f) {
|
|
|
|
copy_v3_v3(co_no->no, no_f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
normal_short_to_float_v3(co_no->no, no_s);
|
2013-06-26 18:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 07:30:05 +10:00
|
|
|
static void vpaint_proj_dm_map_cosnos_update(struct Depsgraph *depsgraph,
|
2018-04-06 12:07:27 +02:00
|
|
|
struct VertProjHandle *vp_handle,
|
2020-03-06 16:56:42 +01:00
|
|
|
ARegion *region,
|
2017-09-28 07:30:05 +10:00
|
|
|
const float mval_fl[2])
|
2013-06-26 18:40:55 +00:00
|
|
|
{
|
2020-03-06 16:56:42 +01:00
|
|
|
struct VertProjUpdate vp_update = {vp_handle, region, mval_fl};
|
2013-06-26 18:40:55 +00:00
|
|
|
|
|
|
|
Object *ob = vp_handle->ob;
|
2018-12-01 19:06:44 +03:00
|
|
|
Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
|
|
|
|
Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
|
2013-06-26 18:40:55 +00:00
|
|
|
Mesh *me = ob->data;
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
|
|
|
|
CustomData_MeshMasks cddata_masks = CD_MASK_BAREMESH_ORIGINDEX;
|
|
|
|
Mesh *me_eval = mesh_get_eval_final(depsgraph, scene_eval, ob_eval, &cddata_masks);
|
2013-06-26 18:40:55 +00:00
|
|
|
|
|
|
|
/* quick sanity check - we shouldn't have to run this if there are no modifiers */
|
2014-02-08 06:07:10 +11:00
|
|
|
BLI_assert(BLI_listbase_is_empty(&ob->modifiers) == false);
|
2013-06-26 18:40:55 +00:00
|
|
|
|
2018-06-22 16:03:16 +02:00
|
|
|
copy_vn_fl(vp_handle->dists_sq, me->totvert, FLT_MAX);
|
|
|
|
BKE_mesh_foreach_mapped_vert(
|
|
|
|
me_eval, vpaint_proj_dm_map_cosnos_update__map_cb, &vp_update, MESH_FOREACH_USE_NORMAL);
|
2013-06-26 18:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Public Functions */
|
|
|
|
|
2017-09-28 07:30:05 +10:00
|
|
|
struct VertProjHandle *ED_vpaint_proj_handle_create(struct Depsgraph *depsgraph,
|
2018-04-06 12:07:27 +02:00
|
|
|
Scene *scene,
|
|
|
|
Object *ob,
|
2018-09-27 15:54:10 +02:00
|
|
|
CoNo **r_vcosnos)
|
2013-06-26 18:40:55 +00:00
|
|
|
{
|
|
|
|
struct VertProjHandle *vp_handle = MEM_mallocN(sizeof(struct VertProjHandle), __func__);
|
|
|
|
Mesh *me = ob->data;
|
|
|
|
|
|
|
|
/* setup the handle */
|
2018-09-27 15:54:10 +02:00
|
|
|
vp_handle->vcosnos = MEM_mallocN(sizeof(CoNo) * me->totvert, "vertexcosnos map");
|
2013-06-26 18:40:55 +00:00
|
|
|
vp_handle->use_update = false;
|
|
|
|
|
|
|
|
/* sets 'use_update' if needed */
|
2018-04-06 12:07:27 +02:00
|
|
|
vpaint_proj_dm_map_cosnos_init(depsgraph, scene, ob, vp_handle);
|
2013-06-26 18:40:55 +00:00
|
|
|
|
|
|
|
if (vp_handle->use_update) {
|
2014-03-26 09:34:01 +11:00
|
|
|
vp_handle->dists_sq = MEM_mallocN(sizeof(float) * me->totvert, __func__);
|
2013-06-26 18:40:55 +00:00
|
|
|
|
|
|
|
vp_handle->ob = ob;
|
|
|
|
vp_handle->scene = scene;
|
|
|
|
}
|
|
|
|
else {
|
2014-03-26 09:34:01 +11:00
|
|
|
vp_handle->dists_sq = NULL;
|
2013-06-26 18:40:55 +00:00
|
|
|
|
|
|
|
vp_handle->ob = NULL;
|
|
|
|
vp_handle->scene = NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-26 09:34:01 +11:00
|
|
|
*r_vcosnos = vp_handle->vcosnos;
|
2013-06-26 18:40:55 +00:00
|
|
|
return vp_handle;
|
|
|
|
}
|
|
|
|
|
2017-09-28 07:30:05 +10:00
|
|
|
void ED_vpaint_proj_handle_update(struct Depsgraph *depsgraph,
|
2018-04-06 12:07:27 +02:00
|
|
|
struct VertProjHandle *vp_handle,
|
2020-03-06 16:56:42 +01:00
|
|
|
ARegion *region,
|
2017-09-28 07:30:05 +10:00
|
|
|
const float mval_fl[2])
|
2013-06-26 18:40:55 +00:00
|
|
|
{
|
|
|
|
if (vp_handle->use_update) {
|
2020-03-06 16:56:42 +01:00
|
|
|
vpaint_proj_dm_map_cosnos_update(depsgraph, vp_handle, region, mval_fl);
|
2013-06-26 18:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ED_vpaint_proj_handle_free(struct VertProjHandle *vp_handle)
|
|
|
|
{
|
|
|
|
if (vp_handle->use_update) {
|
2014-03-26 09:34:01 +11:00
|
|
|
MEM_freeN(vp_handle->dists_sq);
|
2013-06-26 18:40:55 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 09:34:01 +11:00
|
|
|
MEM_freeN(vp_handle->vcosnos);
|
2013-06-26 18:40:55 +00:00
|
|
|
MEM_freeN(vp_handle);
|
|
|
|
}
|