2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2009 by Nicholas Bishop
19 * All rights reserved.
21 * Contributor(s): Jason Wilkins, Tom Musgrove.
23 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/sculpt_paint/paint_cursor.c
31 #include "MEM_guardedalloc.h"
35 #include "BLI_utildefines.h"
37 #include "DNA_brush_types.h"
38 #include "DNA_color_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_userdef_types.h"
44 #include "BKE_brush.h"
45 #include "BKE_context.h"
46 #include "BKE_paint.h"
51 #include "BIF_glutil.h"
53 #include "ED_view3d.h"
55 #include "paint_intern.h"
56 /* still needed for sculpt_stroke_get_location, should be
57 * removed eventually (TODO) */
58 #include "sculpt_intern.h"
62 * Some of the cursor drawing code is doing non-draw stuff
63 * (e.g. updating the brush rake angle). This should be cleaned up
66 * There is also some ugliness with sculpt-specific code.
69 typedef struct Snapshot {
73 int BKE_brush_size_get;
77 int curve_changed_timestamp;
80 static int same_snap(Snapshot *snap, Brush *brush, ViewContext *vc)
82 MTex *mtex = &brush->mtex;
84 return (((mtex->tex) &&
85 equals_v3v3(mtex->ofs, snap->ofs) &&
86 equals_v3v3(mtex->size, snap->size) &&
87 mtex->rot == snap->rot) &&
89 /* make brush smaller shouldn't cause a resample */
90 ((mtex->brush_map_mode == MTEX_MAP_MODE_VIEW &&
91 (BKE_brush_size_get(vc->scene, brush) <= snap->BKE_brush_size_get)) ||
92 (BKE_brush_size_get(vc->scene, brush) == snap->BKE_brush_size_get)) &&
94 (mtex->brush_map_mode == snap->brush_map_mode) &&
95 (vc->ar->winx == snap->winx) &&
96 (vc->ar->winy == snap->winy));
99 static void make_snap(Snapshot *snap, Brush *brush, ViewContext *vc)
101 if (brush->mtex.tex) {
102 snap->brush_map_mode = brush->mtex.brush_map_mode;
103 copy_v3_v3(snap->ofs, brush->mtex.ofs);
104 copy_v3_v3(snap->size, brush->mtex.size);
105 snap->rot = brush->mtex.rot;
108 snap->brush_map_mode = -1;
109 snap->ofs[0] = snap->ofs[1] = snap->ofs[2] = -1;
110 snap->size[0] = snap->size[1] = snap->size[2] = -1;
114 snap->BKE_brush_size_get = BKE_brush_size_get(vc->scene, brush);
115 snap->winx = vc->ar->winx;
116 snap->winy = vc->ar->winy;
119 static int load_tex(Sculpt *sd, Brush *br, ViewContext *vc)
121 static GLuint overlay_texture = 0;
123 static int tex_changed_timestamp = -1;
124 static int curve_changed_timestamp = -1;
125 static Snapshot snap;
126 static int old_size = -1;
128 GLubyte *buffer = NULL;
135 (void)sd; /* quied unused warning */
138 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED && !br->mtex.tex) return 0;
143 (!br->mtex.tex->preview ||
144 br->mtex.tex->preview->changed_timestamp[0] != tex_changed_timestamp)) ||
146 br->curve->changed_timestamp != curve_changed_timestamp ||
147 !same_snap(&snap, br, vc);
150 if (br->mtex.tex && br->mtex.tex->preview)
151 tex_changed_timestamp = br->mtex.tex->preview->changed_timestamp[0];
154 curve_changed_timestamp = br->curve->changed_timestamp;
156 make_snap(&snap, br, vc);
158 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
159 int s = BKE_brush_size_get(vc->scene, br);
162 for (s >>= 1; s > 0; s >>= 1)
176 if (old_size != size) {
177 if (overlay_texture) {
178 glDeleteTextures(1, &overlay_texture);
187 buffer = MEM_mallocN(sizeof(GLubyte) * size * size, "load_tex");
189 #pragma omp parallel for schedule(static) if (sd->flags & SCULPT_USE_OPENMP)
190 for (j = 0; j < size; j++) {
195 for (i = 0; i < size; i++) {
197 // largely duplicated from tex_strength
199 const float rotation = -br->mtex.rot;
200 float radius = BKE_brush_size_get(vc->scene, br);
201 int index = j * size + i;
211 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) {
212 x *= vc->ar->winx / radius;
213 y *= vc->ar->winy / radius;
220 len = sqrtf(x * x + y * y);
222 if ((br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) || len <= 1) {
223 /* it is probably worth optimizing for those cases where
224 * the texture is not rotated by skipping the calls to
225 * atan2, sqrtf, sin, and cos. */
226 if (br->mtex.tex && (rotation > 0.001f || rotation < -0.001f)) {
227 const float angle = atan2f(y, x) + rotation;
229 x = len * cosf(angle);
230 y = len * sinf(angle);
233 x *= br->mtex.size[0];
234 y *= br->mtex.size[1];
236 x += br->mtex.ofs[0];
237 y += br->mtex.ofs[1];
239 avg = br->mtex.tex ? paint_get_tex_pixel(br, x, y) : 1;
241 avg += br->texture_sample_bias;
243 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW)
244 avg *= BKE_brush_curve_strength(br, len, 1); /* Falloff curve */
246 buffer[index] = 255 - (GLubyte)(255 * avg);
254 if (!overlay_texture)
255 glGenTextures(1, &overlay_texture);
261 glBindTexture(GL_TEXTURE_2D, overlay_texture);
265 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size, size, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
269 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
276 glEnable(GL_TEXTURE_2D);
278 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
279 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
280 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
282 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
283 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
284 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
290 static int project_brush_radius(ViewContext *vc,
292 const float location[3])
294 float view[3], nonortho[3], ortho[3], offset[3], p1[2], p2[2];
296 ED_view3d_global_to_vector(vc->rv3d, location, view);
298 /* create a vector that is not orthogonal to view */
300 if (fabsf(view[0]) < 0.1f) {
301 nonortho[0] = view[0] + 1.0f;
302 nonortho[1] = view[1];
303 nonortho[2] = view[2];
305 else if (fabsf(view[1]) < 0.1f) {
306 nonortho[0] = view[0];
307 nonortho[1] = view[1] + 1.0f;
308 nonortho[2] = view[2];
311 nonortho[0] = view[0];
312 nonortho[1] = view[1];
313 nonortho[2] = view[2] + 1.0f;
316 /* get a vector in the plane of the view */
317 cross_v3_v3v3(ortho, nonortho, view);
320 /* make a point on the surface of the brush tagent to the view */
321 mul_v3_fl(ortho, radius);
322 add_v3_v3v3(offset, location, ortho);
324 /* project the center of the brush, and the tangent point to the view onto the screen */
325 project_float(vc->ar, location, p1);
326 project_float(vc->ar, offset, p2);
328 /* the distance between these points is the size of the projected brush in pixels */
329 return len_v2v2(p1, p2);
332 static int sculpt_get_brush_geometry(bContext *C, ViewContext *vc,
333 int x, int y, int *pixel_radius,
336 Scene *scene = CTX_data_scene(C);
337 Paint *paint = paint_get_active_from_context(C);
338 Brush *brush = paint_brush(paint);
342 window[0] = x + vc->ar->winrct.xmin;
343 window[1] = y + vc->ar->winrct.ymin;
345 if (vc->obact->sculpt && vc->obact->sculpt->pbvh &&
346 sculpt_stroke_get_location(C, location, window))
349 project_brush_radius(vc,
350 BKE_brush_unprojected_radius_get(scene, brush),
353 if (*pixel_radius == 0)
354 *pixel_radius = BKE_brush_size_get(scene, brush);
356 mul_m4_v3(vc->obact->obmat, location);
361 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
362 Brush *brush = paint_brush(&sd->paint);
364 *pixel_radius = BKE_brush_size_get(scene, brush);
371 /* Draw an overlay that shows what effect the brush's texture will
372 * have on brush strength */
373 /* TODO: sculpt only for now */
374 static void paint_draw_alpha_overlay(Sculpt *sd, Brush *brush,
375 ViewContext *vc, int x, int y)
379 /* check for overlay mode */
380 if (!(brush->flag & BRUSH_TEXTURE_OVERLAY) ||
381 !(ELEM(brush->mtex.brush_map_mode, MTEX_MAP_MODE_VIEW, MTEX_MAP_MODE_TILED)))
386 /* save lots of GL state
387 * TODO: check on whether all of these are needed? */
388 glPushAttrib(GL_COLOR_BUFFER_BIT |
390 GL_DEPTH_BUFFER_BIT |
394 GL_STENCIL_BUFFER_BIT |
399 if (load_tex(sd, brush, vc)) {
402 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
403 glDepthMask(GL_FALSE);
404 glDepthFunc(GL_ALWAYS);
406 glMatrixMode(GL_TEXTURE);
410 if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
412 glTranslatef(0.5, 0.5, 0);
413 glRotatef((double)RAD2DEGF((brush->flag & BRUSH_RAKE) ?
414 sd->last_angle : sd->special_rotation),
416 glTranslatef(-0.5f, -0.5f, 0);
418 /* scale based on tablet pressure */
419 if (sd->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush)) {
420 glTranslatef(0.5f, 0.5f, 0);
421 glScalef(1.0f / sd->pressure_value, 1.0f / sd->pressure_value, 1);
422 glTranslatef(-0.5f, -0.5f, 0);
425 if (sd->draw_anchored) {
426 const float *aim = sd->anchored_initial_mouse;
427 const rcti *win = &vc->ar->winrct;
428 quad.xmin = aim[0] - sd->anchored_size - win->xmin;
429 quad.ymin = aim[1] - sd->anchored_size - win->ymin;
430 quad.xmax = aim[0] + sd->anchored_size - win->xmin;
431 quad.ymax = aim[1] + sd->anchored_size - win->ymin;
434 const int radius = BKE_brush_size_get(vc->scene, brush);
435 quad.xmin = x - radius;
436 quad.ymin = y - radius;
437 quad.xmax = x + radius;
438 quad.ymax = y + radius;
444 quad.xmax = BLI_RCT_SIZE_X(&vc->ar->winrct);
445 quad.ymax = BLI_RCT_SIZE_Y(&vc->ar->winrct);
449 glColor4f(U.sculpt_paint_overlay_col[0],
450 U.sculpt_paint_overlay_col[1],
451 U.sculpt_paint_overlay_col[2],
452 brush->texture_overlay_alpha / 100.0f);
454 /* draw textured quad */
457 glVertex2f(quad.xmin, quad.ymin);
459 glVertex2f(quad.xmax, quad.ymin);
461 glVertex2f(quad.xmax, quad.ymax);
463 glVertex2f(quad.xmin, quad.ymax);
472 /* Special actions taken when paint cursor goes over mesh */
473 /* TODO: sculpt only for now */
474 static void paint_cursor_on_hit(Sculpt *sd, Brush *brush, ViewContext *vc,
475 const float location[3])
477 float unprojected_radius, projected_radius;
479 /* update the brush's cached 3D radius */
480 if (!BKE_brush_use_locked_size(vc->scene, brush)) {
481 /* get 2D brush radius */
482 if (sd->draw_anchored)
483 projected_radius = sd->anchored_size;
485 if (brush->flag & BRUSH_ANCHORED)
486 projected_radius = 8;
488 projected_radius = BKE_brush_size_get(vc->scene, brush);
491 /* convert brush radius from 2D to 3D */
492 unprojected_radius = paint_calc_object_space_radius(vc, location,
495 /* scale 3D brush radius by pressure */
496 if (sd->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush))
497 unprojected_radius *= sd->pressure_value;
499 /* set cached value in either Brush or UnifiedPaintSettings */
500 BKE_brush_unprojected_radius_set(vc->scene, brush, unprojected_radius);
504 static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused))
506 Scene *scene = CTX_data_scene(C);
507 Paint *paint = paint_get_active_from_context(C);
508 Brush *brush = paint_brush(paint);
511 float translation[2];
512 float outline_alpha, *outline_col;
514 /* set various defaults */
518 outline_col = brush->add_col;
519 final_radius = BKE_brush_size_get(scene, brush);
521 /* check that brush drawing is enabled */
522 if (!(paint->flags & PAINT_SHOW_BRUSH))
525 /* can't use stroke vc here because this will be called during
526 * mouse over too, not just during a stroke */
527 view3d_set_viewcontext(C, &vc);
529 /* TODO: as sculpt and other paint modes are unified, this
530 * special mode of drawing will go away */
531 if (vc.obact->sculpt) {
532 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
534 int pixel_radius, hit;
536 /* this is probably here so that rake takes into
537 * account the brush movements before the stroke
538 * starts, but this doesn't really belong in draw code
541 const float u = 0.5f;
542 const float v = 1 - u;
545 const float dx = sd->last_x - x;
546 const float dy = sd->last_y - y;
548 if (dx * dx + dy * dy >= r * r) {
549 sd->last_angle = atan2(dx, dy);
551 sd->last_x = u * sd->last_x + v * x;
552 sd->last_y = u * sd->last_y + v * y;
556 /* test if brush is over the mesh */
557 hit = sculpt_get_brush_geometry(C, &vc, x, y, &pixel_radius, location);
560 paint_draw_alpha_overlay(sd, brush, &vc, x, y);
562 if (BKE_brush_use_locked_size(scene, brush))
563 BKE_brush_size_set(scene, brush, pixel_radius);
565 /* check if brush is subtracting, use different color then */
566 /* TODO: no way currently to know state of pen flip or
567 * invert key modifier without starting a stroke */
568 if ((!(brush->flag & BRUSH_INVERTED) ^
569 !(brush->flag & BRUSH_DIR_IN)) &&
570 ELEM5(brush->sculpt_tool, SCULPT_TOOL_DRAW,
571 SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY,
572 SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE))
574 outline_col = brush->sub_col;
577 /* only do if brush is over the mesh */
579 paint_cursor_on_hit(sd, brush, &vc, location);
581 if (sd->draw_anchored) {
582 final_radius = sd->anchored_size;
583 translation[0] = sd->anchored_initial_mouse[0] - vc.ar->winrct.xmin;
584 translation[1] = sd->anchored_initial_mouse[1] - vc.ar->winrct.ymin;
588 /* make lines pretty */
590 glEnable(GL_LINE_SMOOTH);
592 /* set brush color */
593 glColor4f(outline_col[0], outline_col[1], outline_col[2], outline_alpha);
595 /* draw brush outline */
596 glTranslatef(translation[0], translation[1], 0);
597 glutil_draw_lined_arc(0.0, M_PI * 2.0, final_radius, 40);
598 glTranslatef(-translation[0], -translation[1], 0);
600 /* restore GL state */
602 glDisable(GL_LINE_SMOOTH);
607 void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
609 Paint *p = paint_get_active_from_context(C);
611 if (p && !p->paint_cursor)
612 p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);