Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / curve / editcurve_paint.c
index 7472a0c89570f4e52015b40c5ad77b3aeca5e070..ad128b91c5afa642dfe88278e1389d4ea6bef9f7 100644 (file)
 
 #include "BKE_context.h"
 #include "BKE_curve.h"
-#include "BKE_depsgraph.h"
 #include "BKE_fcurve.h"
 #include "BKE_main.h"
 #include "BKE_report.h"
 
+#include "DEG_depsgraph.h"
+
 #include "WM_api.h"
 #include "WM_types.h"
 
 #include "ED_curve.h"
 
 #include "BIF_gl.h"
-#include "BIF_glutil.h"
+
+#include "GPU_batch.h"
+#include "GPU_batch_presets.h"
+#include "GPU_immediate.h"
+#include "GPU_immediate_util.h"
+#include "GPU_matrix.h"
+#include "GPU_state.h"
 
 #include "curve_intern.h"
 
@@ -89,6 +96,8 @@ struct StrokeElem {
 };
 
 struct CurveDrawData {
+       Depsgraph *depsgraph;
+
        short init_event_type;
        short curve_type;
 
@@ -129,7 +138,6 @@ struct CurveDrawData {
        } prev;
 
        ViewContext vc;
-       bglMats mats;
        enum {
                CURVE_DRAW_IDLE = 0,
                CURVE_DRAW_PAINTING = 1,
@@ -209,7 +217,7 @@ static bool stroke_elem_project(
                {
                        const double depth = (double)ED_view3d_depth_read_cached(&cdd->vc, mval_i);
                        if ((depth > depths->depth_range[0]) && (depth < depths->depth_range[1])) {
-                               if (ED_view3d_depth_unproject(ar, &cdd->mats, mval_i, depth, r_location_world)) {
+                               if (ED_view3d_depth_unproject(ar, mval_i, depth, r_location_world)) {
                                        is_location_world_set = true;
                                        if (r_normal_world) {
                                                zero_v3(r_normal_world);
@@ -218,7 +226,7 @@ static bool stroke_elem_project(
                                        if (surface_offset != 0.0f) {
                                                const float offset = cdd->project.use_surface_offset_absolute ? 1.0f : radius;
                                                float normal[3];
-                                               if (ED_view3d_depth_read_cached_normal(&cdd->vc, &cdd->mats, mval_i, normal)) {
+                                               if (ED_view3d_depth_read_cached_normal(&cdd->vc, mval_i, normal)) {
                                                        madd_v3_v3fl(r_location_world, normal, offset * surface_offset);
                                                        if (r_normal_world) {
                                                                copy_v3_v3(r_normal_world, normal);
@@ -360,43 +368,46 @@ static void curve_draw_stroke_3d(const struct bContext *UNUSED(C), ARegion *UNUS
                return;
        }
 
-       View3D *v3d = cdd->vc.v3d;
        Object *obedit = cdd->vc.obedit;
        Curve *cu = obedit->data;
 
-       UI_ThemeColor(TH_WIRE);
-
        if (cu->ext2 > 0.0f) {
-               GLUquadricObj *qobj = gluNewQuadric();
-
-               gluQuadricDrawStyle(qobj, GLU_FILL);
-
                BLI_mempool_iter iter;
                const struct StrokeElem *selem;
 
                const float  location_zero[3] = {0};
                const float *location_prev = location_zero;
 
+               float color[3];
+               UI_GetThemeColor3fv(TH_WIRE, color);
+
+               GPUBatch *sphere = GPU_batch_preset_sphere(0);
+               GPU_batch_program_set_builtin(sphere, GPU_SHADER_3D_UNIFORM_COLOR);
+               GPU_batch_uniform_3fv(sphere, "color", color);
+
                /* scale to edit-mode space */
-               glPushMatrix();
-               glMultMatrixf(obedit->obmat);
+               GPU_matrix_push();
+               GPU_matrix_mul(obedit->obmat);
 
                BLI_mempool_iternew(cdd->stroke_elem_pool, &iter);
                for (selem = BLI_mempool_iterstep(&iter); selem; selem = BLI_mempool_iterstep(&iter)) {
-                       glTranslatef(
+                       GPU_matrix_translate_3f(
                                selem->location_local[0] - location_prev[0],
                                selem->location_local[1] - location_prev[1],
                                selem->location_local[2] - location_prev[2]);
                        location_prev = selem->location_local;
+
                        const float radius = stroke_elem_radius(cdd, selem);
-                       gluSphere(qobj, radius, 12, 8);
+
+                       GPU_matrix_push();
+                       GPU_matrix_scale_1f(radius);
+                       GPU_batch_draw(sphere);
+                       GPU_matrix_pop();
 
                        location_prev = selem->location_local;
                }
 
-               glPopMatrix();
-
-               gluDeleteQuadric(qobj);
+               GPU_matrix_pop();
        }
 
        if (stroke_len > 1) {
@@ -413,30 +424,37 @@ static void curve_draw_stroke_3d(const struct bContext *UNUSED(C), ARegion *UNUS
                }
 
                {
-                       glEnable(GL_BLEND);
-                       glEnable(GL_LINE_SMOOTH);
-
-                       glEnableClientState(GL_VERTEX_ARRAY);
-                       glVertexPointer(3, GL_FLOAT, 0, coord_array);
-
-                       cpack(0x0);
-                       glLineWidth(3.0f);
-                       glDrawArrays(GL_LINE_STRIP, 0, stroke_len);
-
-                       if (v3d->zbuf)
-                               glDisable(GL_DEPTH_TEST);
+                       GPUVertFormat *format = immVertexFormat();
+                       uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
+                       immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
+
+                       GPU_depth_test(false);
+                       GPU_blend(true);
+                       GPU_line_smooth(true);
+                       GPU_line_width(3.0f);
+
+                       imm_cpack(0x0);
+                       immBegin(GPU_PRIM_LINE_STRIP, stroke_len);
+                       for (int i = 0; i < stroke_len; i++) {
+                               immVertex3fv(pos, coord_array[i]);
+                       }
+                       immEnd();
 
-                       cpack(0xffffffff);
-                       glLineWidth(1.0f);
-                       glDrawArrays(GL_LINE_STRIP, 0, stroke_len);
+                       GPU_line_width(1.0f);
 
-                       if (v3d->zbuf)
-                               glEnable(GL_DEPTH_TEST);
+                       imm_cpack(0xffffffff);
+                       immBegin(GPU_PRIM_LINE_STRIP, stroke_len);
+                       for (int i = 0; i < stroke_len; i++) {
+                               immVertex3fv(pos, coord_array[i]);
+                       }
+                       immEnd();
 
-                       glDisableClientState(GL_VERTEX_ARRAY);
+                       /* Reset defaults */
+                       GPU_depth_test(true);
+                       GPU_blend(false);
+                       GPU_line_smooth(false);
 
-                       glDisable(GL_BLEND);
-                       glDisable(GL_LINE_SMOOTH);
+                       immUnbindProgram();
                }
 
                MEM_freeN(coord_array);
@@ -526,7 +544,7 @@ static void curve_draw_event_add_first(wmOperator *op, const wmEvent *event)
                         CURVE_PAINT_SURFACE_PLANE_NORMAL_VIEW,
                         CURVE_PAINT_SURFACE_PLANE_NORMAL_SURFACE))
                {
-                       if (ED_view3d_depth_read_cached_normal(&cdd->vc, &cdd->mats, event->mval, normal)) {
+                       if (ED_view3d_depth_read_cached_normal(&cdd->vc, event->mval, normal)) {
                                if (cps->surface_plane == CURVE_PAINT_SURFACE_PLANE_NORMAL_VIEW) {
                                        float cross_a[3], cross_b[3];
                                        cross_v3_v3v3(cross_a, rv3d->viewinv[2], normal);
@@ -575,6 +593,8 @@ static bool curve_draw_init(bContext *C, wmOperator *op, bool is_invoke)
 
        struct CurveDrawData *cdd = MEM_callocN(sizeof(*cdd), __func__);
 
+       cdd->depsgraph = CTX_data_depsgraph(C);
+
        if (is_invoke) {
                ED_view3d_viewcontext_init(C, &cdd->vc);
                if (ELEM(NULL, cdd->vc.ar, cdd->vc.rv3d, cdd->vc.v3d, cdd->vc.win, cdd->vc.scene)) {
@@ -585,7 +605,9 @@ static bool curve_draw_init(bContext *C, wmOperator *op, bool is_invoke)
        }
        else {
                cdd->vc.bmain = CTX_data_main(C);
+               cdd->vc.depsgraph = CTX_data_depsgraph(C);
                cdd->vc.scene = CTX_data_scene(C);
+               cdd->vc.view_layer = CTX_data_view_layer(C);
                cdd->vc.obedit = CTX_data_edit_object(C);
        }
 
@@ -678,7 +700,7 @@ static void curve_draw_exec_precalc(wmOperator *op)
                        const struct StrokeElem *selem, *selem_first, *selem_last;
 
                        BLI_mempool_iternew(cdd->stroke_elem_pool, &iter);
-                       selem_first = BLI_mempool_iterstep(&iter);
+                       selem_first = selem_last = BLI_mempool_iterstep(&iter);
                        for (selem = BLI_mempool_iterstep(&iter); selem; selem = BLI_mempool_iterstep(&iter)) {
                                selem_last = selem;
                        }
@@ -755,7 +777,7 @@ static int curve_draw_exec(bContext *C, wmOperator *op)
        struct CurveDrawData *cdd = op->customdata;
 
        const CurvePaintSettings *cps = &cdd->vc.scene->toolsettings->curve_paint_settings;
-       Object *obedit = cdd->vc.scene->obedit;
+       Object *obedit = cdd->vc.obedit;
        Curve *cu = obedit->data;
        ListBase *nurblist = object_editcurve_get(obedit);
 
@@ -1014,7 +1036,7 @@ static int curve_draw_exec(bContext *C, wmOperator *op)
        cu->actvert = nu->pntsu - 1;
 
        WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
-       DAG_id_tag_update(obedit->data, 0);
+       DEG_id_tag_update(obedit->data, 0);
 
        curve_draw_exit(op);
 
@@ -1067,14 +1089,12 @@ static int curve_draw_invoke(bContext *C, wmOperator *op, const wmEvent *event)
                }
                else {
                        if ((cps->depth_mode == CURVE_PAINT_PROJECT_SURFACE) &&
-                           (v3d->drawtype > OB_WIRE))
+                           (v3d->shading.type > OB_WIRE))
                        {
-                               view3d_get_transformation(cdd->vc.ar, cdd->vc.rv3d, NULL, &cdd->mats);
-
                                /* needed or else the draw matrix can be incorrect */
                                view3d_operator_needs_opengl(C);
 
-                               ED_view3d_autodist_init(cdd->vc.bmain, cdd->vc.scene, cdd->vc.ar, cdd->vc.v3d, 0);
+                               ED_view3d_autodist_init(cdd->vc.depsgraph, cdd->vc.ar, cdd->vc.v3d, 0);
 
                                if (cdd->vc.rv3d->depths) {
                                        cdd->vc.rv3d->depths->damaged = true;
@@ -1093,7 +1113,7 @@ static int curve_draw_invoke(bContext *C, wmOperator *op, const wmEvent *event)
 
                        /* use view plane (when set or as fallback when surface can't be found) */
                        if (cdd->project.use_depth == false) {
-                               plane_co = ED_view3d_cursor3d_get(cdd->vc.scene, v3d);
+                               plane_co = ED_view3d_cursor3d_get(cdd->vc.scene, v3d)->location;
                                plane_no = rv3d->viewinv[2];
                                cdd->project.use_plane = true;
                        }