diff options
Diffstat (limited to 'source/blender/editors/sculpt_paint/paint_utils.c')
-rw-r--r-- | source/blender/editors/sculpt_paint/paint_utils.c | 161 |
1 files changed, 73 insertions, 88 deletions
diff --git a/source/blender/editors/sculpt_paint/paint_utils.c b/source/blender/editors/sculpt_paint/paint_utils.c index f34d0ff3f7b..7b66632fa42 100644 --- a/source/blender/editors/sculpt_paint/paint_utils.c +++ b/source/blender/editors/sculpt_paint/paint_utils.c @@ -46,17 +46,15 @@ #include "BLI_listbase.h" #include "BLI_rect.h" -#include "BLF_translation.h" +#include "BLT_translation.h" -#include "BKE_scene.h" #include "BKE_brush.h" #include "BKE_context.h" #include "BKE_DerivedMesh.h" -#include "BKE_material.h" #include "BKE_image.h" +#include "BKE_material.h" #include "BKE_paint.h" #include "BKE_report.h" -#include "BKE_image.h" #include "RNA_access.h" #include "RNA_define.h" @@ -66,16 +64,12 @@ #include "IMB_colormanagement.h" #include "IMB_imbuf_types.h" +#include "IMB_imbuf.h" -#include "RE_shader_ext.h" #include "RE_render_ext.h" #include "ED_view3d.h" #include "ED_screen.h" -#include "ED_uvedit.h" - -#include "IMB_imbuf_types.h" -#include "IMB_imbuf.h" #include "BLI_sys_types.h" #include "ED_mesh.h" /* for face mask functions */ @@ -183,7 +177,7 @@ float paint_get_tex_pixel(MTex *mtex, float u, float v, struct ImagePool *pool, float co[3] = {u, v, 0.0f}; externtex(mtex, co, &intensity, - rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool); + rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false); return intensity; } @@ -195,7 +189,7 @@ void paint_get_tex_pixel_col(MTex *mtex, float u, float v, float rgba[4], struct float intensity; hasrgb = externtex(mtex, co, &intensity, - rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool); + rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false); if (!hasrgb) { rgba[0] = intensity; rgba[1] = intensity; @@ -283,24 +277,16 @@ static void imapaint_tri_weights(float matrix[4][4], GLint view[4], static void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, const int xy[2], float uv[2]) { DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH); - MTFace *tf_base, *tf; - Material *ma; - TexPaintSlot *slot; - int numfaces = dm->getNumTessFaces(dm), a, findex; + const int tottri = dm->getNumLoopTri(dm); + int i, findex; float p[2], w[3], absw, minabsw; - MFace mf; - MVert mv[4]; float matrix[4][4], proj[4][4]; GLint view[4]; - - /* compute barycentric coordinates */ - - /* double lookup */ - const int *index_mf_to_mpoly = dm->getTessFaceDataArray(dm, CD_ORIGINDEX); + const ImagePaintMode mode = scene->toolsettings->imapaint.mode; + const MLoopTri *lt = dm->getLoopTriArray(dm); + const MPoly *mpoly = dm->getPolyArray(dm); + const MLoop *mloop = dm->getLoopArray(dm); const int *index_mp_to_orig = dm->getPolyDataArray(dm, CD_ORIGINDEX); - if (index_mf_to_mpoly == NULL) { - index_mp_to_orig = NULL; - } /* get the needed opengl matrices */ glGetIntegerv(GL_VIEWPORT, view); @@ -314,56 +300,50 @@ static void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, c uv[0] = uv[1] = 0.0; /* test all faces in the derivedmesh with the original index of the picked face */ - for (a = 0; a < numfaces; a++) { - findex = index_mf_to_mpoly ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, a) : a; + /* face means poly here, not triangle, indeed */ + for (i = 0; i < tottri; i++, lt++) { + findex = index_mp_to_orig ? index_mp_to_orig[lt->poly] : lt->poly; if (findex == faceindex) { - dm->getTessFace(dm, a, &mf); - - ma = dm->mat[mf.mat_nr]; - slot = &ma->texpaintslot[ma->paint_active_slot]; - - dm->getVert(dm, mf.v1, &mv[0]); - dm->getVert(dm, mf.v2, &mv[1]); - dm->getVert(dm, mf.v3, &mv[2]); - if (mf.v4) - dm->getVert(dm, mf.v4, &mv[3]); - - if (!(slot && slot->uvname && (tf_base = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, slot->uvname)))) - tf_base = CustomData_get_layer(&dm->faceData, CD_MTFACE); + const MLoopUV *mloopuv; + const MPoly *mp = &mpoly[lt->poly]; + const MLoopUV *tri_uv[3]; + float tri_co[3][3]; + + dm->getVertCo(dm, mloop[lt->tri[0]].v, tri_co[0]); + dm->getVertCo(dm, mloop[lt->tri[1]].v, tri_co[1]); + dm->getVertCo(dm, mloop[lt->tri[2]].v, tri_co[2]); + + if (mode == IMAGEPAINT_MODE_MATERIAL) { + const Material *ma; + const TexPaintSlot *slot; + + ma = dm->mat[mp->mat_nr]; + slot = &ma->texpaintslot[ma->paint_active_slot]; + + if (!(slot && slot->uvname && + (mloopuv = CustomData_get_layer_named(&dm->loopData, CD_MLOOPUV, slot->uvname)))) + { + mloopuv = CustomData_get_layer(&dm->loopData, CD_MLOOPUV); + } + } + else { + mloopuv = CustomData_get_layer(&dm->loopData, CD_MLOOPUV); + } - tf = &tf_base[a]; + tri_uv[0] = &mloopuv[lt->tri[0]]; + tri_uv[1] = &mloopuv[lt->tri[1]]; + tri_uv[2] = &mloopuv[lt->tri[2]]; p[0] = xy[0]; p[1] = xy[1]; - if (mf.v4) { - /* the triangle with the largest absolute values is the one - * with the most negative weights */ - imapaint_tri_weights(matrix, view, mv[0].co, mv[1].co, mv[3].co, p, w); - absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]); - if (absw < minabsw) { - uv[0] = tf->uv[0][0] * w[0] + tf->uv[1][0] * w[1] + tf->uv[3][0] * w[2]; - uv[1] = tf->uv[0][1] * w[0] + tf->uv[1][1] * w[1] + tf->uv[3][1] * w[2]; - minabsw = absw; - } - - imapaint_tri_weights(matrix, view, mv[1].co, mv[2].co, mv[3].co, p, w); - absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]); - if (absw < minabsw) { - uv[0] = tf->uv[1][0] * w[0] + tf->uv[2][0] * w[1] + tf->uv[3][0] * w[2]; - uv[1] = tf->uv[1][1] * w[0] + tf->uv[2][1] * w[1] + tf->uv[3][1] * w[2]; - minabsw = absw; - } - } - else { - imapaint_tri_weights(matrix, view, mv[0].co, mv[1].co, mv[2].co, p, w); - absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]); - if (absw < minabsw) { - uv[0] = tf->uv[0][0] * w[0] + tf->uv[1][0] * w[1] + tf->uv[2][0] * w[2]; - uv[1] = tf->uv[0][1] * w[0] + tf->uv[1][1] * w[1] + tf->uv[2][1] * w[2]; - minabsw = absw; - } + imapaint_tri_weights(matrix, view, UNPACK3(tri_co), p, w); + absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]); + if (absw < minabsw) { + uv[0] = tri_uv[0]->uv[0] * w[0] + tri_uv[1]->uv[0] * w[1] + tri_uv[2]->uv[0] * w[2]; + uv[1] = tri_uv[0]->uv[1] * w[0] + tri_uv[1]->uv[1] * w[1] + tri_uv[2]->uv[1] * w[2]; + minabsw = absw; } } } @@ -372,15 +352,15 @@ static void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, c } /* returns 0 if not found, otherwise 1 */ -static int imapaint_pick_face(ViewContext *vc, const int mval[2], unsigned int *r_index, unsigned int totface) +static int imapaint_pick_face(ViewContext *vc, const int mval[2], unsigned int *r_index, unsigned int totpoly) { - if (totface == 0) + if (totpoly == 0) return 0; /* sample only on the exact position */ - *r_index = view3d_sample_backbuf(vc, mval[0], mval[1]); + *r_index = ED_view3d_backbuf_sample(vc, mval[0], mval[1]); - if ((*r_index) == 0 || (*r_index) > (unsigned int)totface) { + if ((*r_index) == 0 || (*r_index) > (unsigned int)totpoly) { return 0; } @@ -390,12 +370,12 @@ static int imapaint_pick_face(ViewContext *vc, const int mval[2], unsigned int * } -static Image *imapaint_face_image(DerivedMesh *dm, int face_index) +static Image *imapaint_face_image(Object *ob, Mesh *me, int face_index) { Image *ima; - MFace *mf = dm->getTessFaceArray(dm) + face_index; - Material *ma = dm->mat[mf->mat_nr]; - ima = ma->texpaintslot[ma->paint_active_slot].ima; + MPoly *mp = me->mpoly + face_index; + Material *ma = give_current_material(ob, mp->mat_nr + 1); + ima = ma && ma->texpaintslot ? ma->texpaintslot[ma->paint_active_slot].ima : NULL; return ima; } @@ -438,6 +418,7 @@ void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_pr } color = BKE_palette_color_add(palette); + palette->active_color = BLI_listbase_count(&palette->colors) - 1; } @@ -449,26 +430,24 @@ void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_pr bool use_material = (imapaint->mode == IMAGEPAINT_MODE_MATERIAL); if (ob) { + Mesh *me = (Mesh *)ob->data; DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH); ViewContext vc; const int mval[2] = {x, y}; unsigned int faceindex; - unsigned int totface = dm->getNumTessFaces(dm); - MTFace *dm_mtface = dm->getTessFaceDataArray(dm, CD_MTFACE); - - DM_update_materials(dm, ob); + unsigned int totpoly = me->totpoly; - if (dm_mtface) { + if (dm->getLoopDataArray(dm, CD_MLOOPUV)) { view3d_set_viewcontext(C, &vc); view3d_operator_needs_opengl(C); - if (imapaint_pick_face(&vc, mval, &faceindex, totface)) { + if (imapaint_pick_face(&vc, mval, &faceindex, totpoly)) { Image *image; if (use_material) - image = imapaint_face_image(dm, faceindex); + image = imapaint_face_image(ob, me, faceindex); else image = imapaint->canvas; @@ -486,12 +465,15 @@ void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_pr if (u < 0.0f) u += 1.0f; if (v < 0.0f) v += 1.0f; - u = u * ibuf->x - 0.5f; - v = v * ibuf->y - 0.5f; + u = u * ibuf->x; + v = v * ibuf->y; if (ibuf->rect_float) { float rgba_f[4]; - bilinear_interpolation_color_wrap(ibuf, NULL, rgba_f, u, v); + if (U.gameflags & USER_DISABLE_MIPMAP) + nearest_interpolation_color_wrap(ibuf, NULL, rgba_f, u, v); + else + bilinear_interpolation_color_wrap(ibuf, NULL, rgba_f, u, v); straight_to_premul_v4(rgba_f); if (use_palette) { linearrgb_to_srgb_v3_v3(color->rgb, rgba_f); @@ -503,7 +485,10 @@ void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_pr } else { unsigned char rgba[4]; - bilinear_interpolation_color_wrap(ibuf, rgba, NULL, u, v); + if (U.gameflags & USER_DISABLE_MIPMAP) + nearest_interpolation_color_wrap(ibuf, rgba, NULL, u, v); + else + bilinear_interpolation_color_wrap(ibuf, rgba, NULL, u, v); if (use_palette) { rgb_uchar_to_float(color->rgb, rgba); } @@ -587,7 +572,7 @@ void BRUSH_OT_curve_preset(wmOperatorType *ot) ot->poll = brush_curve_preset_poll; prop = RNA_def_enum(ot->srna, "shape", prop_shape_items, CURVE_PRESET_SMOOTH, "Mode", ""); - RNA_def_property_translation_context(prop, BLF_I18NCONTEXT_ID_CURVE); /* Abusing id_curve :/ */ + RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_CURVE); /* Abusing id_curve :/ */ } |