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"
34 #include "BLI_utildefines.h"
36 #include "DNA_brush_types.h"
37 #include "DNA_color_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_userdef_types.h"
43 #include "BKE_brush.h"
44 #include "BKE_context.h"
45 #include "BKE_paint.h"
50 #include "BIF_glutil.h"
52 #include "ED_view3d.h"
54 #include "paint_intern.h"
55 /* still needed for sculpt_stroke_get_location, should be
56 * removed eventually (TODO) */
57 #include "sculpt_intern.h"
61 * Some of the cursor drawing code is doing non-draw stuff
62 * (e.g. updating the brush rake angle). This should be cleaned up
65 * There is also some ugliness with sculpt-specific code.
68 typedef struct Snapshot {
76 int curve_changed_timestamp;
79 static int same_snap(Snapshot *snap, Brush *brush, ViewContext *vc)
81 MTex *mtex = &brush->mtex;
83 return (((mtex->tex) &&
84 equals_v3v3(mtex->ofs, snap->ofs) &&
85 equals_v3v3(mtex->size, snap->size) &&
86 mtex->rot == snap->rot) &&
88 /* make brush smaller shouldn't cause a resample */
89 ((mtex->brush_map_mode == MTEX_MAP_MODE_FIXED &&
90 (brush_size(vc->scene, brush) <= snap->brush_size)) ||
91 (brush_size(vc->scene, brush) == snap->brush_size)) &&
93 (mtex->brush_map_mode == snap->brush_map_mode) &&
94 (vc->ar->winx == snap->winx) &&
95 (vc->ar->winy == snap->winy));
98 static void make_snap(Snapshot *snap, Brush *brush, ViewContext *vc)
100 if (brush->mtex.tex) {
101 snap->brush_map_mode = brush->mtex.brush_map_mode;
102 copy_v3_v3(snap->ofs, brush->mtex.ofs);
103 copy_v3_v3(snap->size, brush->mtex.size);
104 snap->rot = brush->mtex.rot;
107 snap->brush_map_mode = -1;
108 snap->ofs[0] = snap->ofs[1] = snap->ofs[2] = -1;
109 snap->size[0] = snap->size[1] = snap->size[2] = -1;
113 snap->brush_size = brush_size(vc->scene, brush);
114 snap->winx = vc->ar->winx;
115 snap->winy = vc->ar->winy;
118 static int load_tex(Sculpt *sd, Brush *br, ViewContext *vc)
120 static GLuint overlay_texture = 0;
122 static int tex_changed_timestamp = -1;
123 static int curve_changed_timestamp = -1;
124 static Snapshot snap;
125 static int old_size = -1;
127 GLubyte *buffer = NULL;
134 (void)sd; /* quied unused warning */
137 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED && !br->mtex.tex) return 0;
142 (!br->mtex.tex->preview ||
143 br->mtex.tex->preview->changed_timestamp[0] != tex_changed_timestamp)) ||
145 br->curve->changed_timestamp != curve_changed_timestamp ||
146 !same_snap(&snap, br, vc);
149 if (br->mtex.tex && br->mtex.tex->preview)
150 tex_changed_timestamp = br->mtex.tex->preview->changed_timestamp[0];
153 curve_changed_timestamp = br->curve->changed_timestamp;
155 make_snap(&snap, br, vc);
157 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_FIXED) {
158 int s = brush_size(vc->scene, br);
161 for (s >>= 1; s > 0; s >>= 1)
175 if (old_size != size) {
176 if (overlay_texture) {
177 glDeleteTextures(1, &overlay_texture);
186 buffer = MEM_mallocN(sizeof(GLubyte) * size * size, "load_tex");
188 #pragma omp parallel for schedule(static) if (sd->flags & SCULPT_USE_OPENMP)
189 for (j = 0; j < size; j++) {
194 for (i = 0; i < size; i++) {
196 // largely duplicated from tex_strength
198 const float rotation = -br->mtex.rot;
199 float radius = brush_size(vc->scene, br);
200 int index = j * size + i;
210 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) {
211 x *= vc->ar->winx / radius;
212 y *= vc->ar->winy / radius;
219 len = sqrtf(x * x + y * y);
221 if ((br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) || len <= 1) {
222 /* it is probably worth optimizing for those cases where
223 * the texture is not rotated by skipping the calls to
224 * atan2, sqrtf, sin, and cos. */
225 if (br->mtex.tex && (rotation > 0.001f || rotation < -0.001f)) {
226 const float angle = atan2f(y, x) + rotation;
228 x = len * cosf(angle);
229 y = len * sinf(angle);
232 x *= br->mtex.size[0];
233 y *= br->mtex.size[1];
235 x += br->mtex.ofs[0];
236 y += br->mtex.ofs[1];
238 avg = br->mtex.tex ? paint_get_tex_pixel(br, x, y) : 1;
240 avg += br->texture_sample_bias;
242 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_FIXED)
243 avg *= brush_curve_strength(br, len, 1); /* Falloff curve */
245 buffer[index] = 255 - (GLubyte)(255 * avg);
253 if (!overlay_texture)
254 glGenTextures(1, &overlay_texture);
260 glBindTexture(GL_TEXTURE_2D, overlay_texture);
264 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size, size, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
268 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
275 glEnable(GL_TEXTURE_2D);
277 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
279 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
281 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_FIXED) {
282 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
283 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
289 static int project_brush_radius(ViewContext *vc,
291 const float location[3])
293 float view[3], nonortho[3], ortho[3], offset[3], p1[2], p2[2];
295 ED_view3d_global_to_vector(vc->rv3d, location, view);
297 // create a vector that is not orthogonal to view
299 if (fabsf(view[0]) < 0.1f) {
300 nonortho[0] = view[0] + 1.0f;
301 nonortho[1] = view[1];
302 nonortho[2] = view[2];
304 else if (fabsf(view[1]) < 0.1f) {
305 nonortho[0] = view[0];
306 nonortho[1] = view[1] + 1.0f;
307 nonortho[2] = view[2];
310 nonortho[0] = view[0];
311 nonortho[1] = view[1];
312 nonortho[2] = view[2] + 1.0f;
315 // get a vector in the plane of the view
316 cross_v3_v3v3(ortho, nonortho, view);
319 // make a point on the surface of the brush tagent to the view
320 mul_v3_fl(ortho, radius);
321 add_v3_v3v3(offset, location, ortho);
323 // project the center of the brush, and the tangent point to the view onto the screen
324 project_float(vc->ar, location, p1);
325 project_float(vc->ar, offset, p2);
327 // the distance between these points is the size of the projected brush in pixels
328 return len_v2v2(p1, p2);
331 static int sculpt_get_brush_geometry(bContext *C, ViewContext *vc,
332 int x, int y, int *pixel_radius,
335 Scene *scene = CTX_data_scene(C);
336 Paint *paint = paint_get_active(scene);
337 Brush *brush = paint_brush(paint);
341 window[0] = x + vc->ar->winrct.xmin;
342 window[1] = y + vc->ar->winrct.ymin;
344 if (vc->obact->sculpt && vc->obact->sculpt->pbvh &&
345 sculpt_stroke_get_location(C, location, window)) {
347 project_brush_radius(vc,
348 brush_unprojected_radius(scene, brush),
351 if (*pixel_radius == 0)
352 *pixel_radius = brush_size(scene, brush);
354 mul_m4_v3(vc->obact->obmat, location);
359 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
360 Brush *brush = paint_brush(&sd->paint);
362 *pixel_radius = brush_size(scene, brush);
369 /* Draw an overlay that shows what effect the brush's texture will
370 * have on brush strength */
371 /* TODO: sculpt only for now */
372 static void paint_draw_alpha_overlay(Sculpt *sd, Brush *brush,
373 ViewContext *vc, int x, int y)
377 /* check for overlay mode */
378 if (!(brush->flag & BRUSH_TEXTURE_OVERLAY) ||
379 !(ELEM(brush->mtex.brush_map_mode, MTEX_MAP_MODE_FIXED, MTEX_MAP_MODE_TILED)))
384 /* save lots of GL state
385 * TODO: check on whether all of these are needed? */
386 glPushAttrib(GL_COLOR_BUFFER_BIT |
388 GL_DEPTH_BUFFER_BIT |
392 GL_STENCIL_BUFFER_BIT |
397 if (load_tex(sd, brush, vc)) {
400 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
401 glDepthMask(GL_FALSE);
402 glDepthFunc(GL_ALWAYS);
404 glMatrixMode(GL_TEXTURE);
408 if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_FIXED) {
410 glTranslatef(0.5, 0.5, 0);
411 glRotatef((double)RAD2DEGF((brush->flag & BRUSH_RAKE) ?
412 sd->last_angle : sd->special_rotation),
414 glTranslatef(-0.5f, -0.5f, 0);
416 /* scale based on tablet pressure */
417 if (sd->draw_pressure && brush_use_size_pressure(vc->scene, brush)) {
418 glTranslatef(0.5f, 0.5f, 0);
419 glScalef(1.0f / sd->pressure_value, 1.0f / sd->pressure_value, 1);
420 glTranslatef(-0.5f, -0.5f, 0);
423 if (sd->draw_anchored) {
424 const float *aim = sd->anchored_initial_mouse;
425 const rcti *win = &vc->ar->winrct;
426 quad.xmin = aim[0] - sd->anchored_size - win->xmin;
427 quad.ymin = aim[1] - sd->anchored_size - win->ymin;
428 quad.xmax = aim[0] + sd->anchored_size - win->xmin;
429 quad.ymax = aim[1] + sd->anchored_size - win->ymin;
432 const int radius = brush_size(vc->scene, brush);
433 quad.xmin = x - radius;
434 quad.ymin = y - radius;
435 quad.xmax = x + radius;
436 quad.ymax = y + radius;
442 quad.xmax = vc->ar->winrct.xmax - vc->ar->winrct.xmin;
443 quad.ymax = vc->ar->winrct.ymax - vc->ar->winrct.ymin;
447 glColor4f(U.sculpt_paint_overlay_col[0],
448 U.sculpt_paint_overlay_col[1],
449 U.sculpt_paint_overlay_col[2],
450 brush->texture_overlay_alpha / 100.0f);
452 /* draw textured quad */
455 glVertex2f(quad.xmin, quad.ymin);
457 glVertex2f(quad.xmax, quad.ymin);
459 glVertex2f(quad.xmax, quad.ymax);
461 glVertex2f(quad.xmin, quad.ymax);
470 /* Special actions taken when paint cursor goes over mesh */
471 /* TODO: sculpt only for now */
472 static void paint_cursor_on_hit(Sculpt *sd, Brush *brush, ViewContext *vc,
473 const float location[3])
475 float unprojected_radius, projected_radius;
477 /* update the brush's cached 3D radius */
478 if (!brush_use_locked_size(vc->scene, brush)) {
479 /* get 2D brush radius */
480 if (sd->draw_anchored)
481 projected_radius = sd->anchored_size;
483 if (brush->flag & BRUSH_ANCHORED)
484 projected_radius = 8;
486 projected_radius = brush_size(vc->scene, brush);
489 /* convert brush radius from 2D to 3D */
490 unprojected_radius = paint_calc_object_space_radius(vc, location,
493 /* scale 3D brush radius by pressure */
494 if (sd->draw_pressure && brush_use_size_pressure(vc->scene, brush))
495 unprojected_radius *= sd->pressure_value;
497 /* set cached value in either Brush or UnifiedPaintSettings */
498 brush_set_unprojected_radius(vc->scene, brush, unprojected_radius);
502 static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused))
504 Scene *scene = CTX_data_scene(C);
505 Paint *paint = paint_get_active(scene);
506 Brush *brush = paint_brush(paint);
509 float translation[2];
510 float outline_alpha, *outline_col;
512 /* set various defaults */
516 outline_col = brush->add_col;
517 final_radius = brush_size(scene, brush);
519 /* check that brush drawing is enabled */
520 if (!(paint->flags & PAINT_SHOW_BRUSH))
523 /* can't use stroke vc here because this will be called during
524 * mouse over too, not just during a stroke */
525 view3d_set_viewcontext(C, &vc);
527 /* TODO: as sculpt and other paint modes are unified, this
528 * special mode of drawing will go away */
529 if (vc.obact->sculpt) {
530 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
532 int pixel_radius, hit;
534 /* this is probably here so that rake takes into
535 * account the brush movements before the stroke
536 * starts, but this doesn't really belong in draw code
539 const float u = 0.5f;
540 const float v = 1 - u;
543 const float dx = sd->last_x - x;
544 const float dy = sd->last_y - y;
546 if (dx * dx + dy * dy >= r * r) {
547 sd->last_angle = atan2(dx, dy);
549 sd->last_x = u * sd->last_x + v * x;
550 sd->last_y = u * sd->last_y + v * y;
554 /* test if brush is over the mesh */
555 hit = sculpt_get_brush_geometry(C, &vc, x, y, &pixel_radius, location);
558 paint_draw_alpha_overlay(sd, brush, &vc, x, y);
560 if (brush_use_locked_size(scene, brush))
561 brush_set_size(scene, brush, pixel_radius);
563 /* check if brush is subtracting, use different color then */
564 /* TODO: no way currently to know state of pen flip or
565 * invert key modifier without starting a stroke */
566 if ((!(brush->flag & BRUSH_INVERTED) ^
567 !(brush->flag & BRUSH_DIR_IN)) &&
568 ELEM5(brush->sculpt_tool, SCULPT_TOOL_DRAW,
569 SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY,
570 SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE))
572 outline_col = brush->sub_col;
575 /* only do if brush is over the mesh */
577 paint_cursor_on_hit(sd, brush, &vc, location);
579 if (sd->draw_anchored) {
580 final_radius = sd->anchored_size;
581 translation[0] = sd->anchored_initial_mouse[0] - vc.ar->winrct.xmin;
582 translation[1] = sd->anchored_initial_mouse[1] - vc.ar->winrct.ymin;
586 /* make lines pretty */
588 glEnable(GL_LINE_SMOOTH);
590 /* set brush color */
591 glColor4f(outline_col[0], outline_col[1], outline_col[2], outline_alpha);
593 /* draw brush outline */
594 glTranslatef(translation[0], translation[1], 0);
595 glutil_draw_lined_arc(0.0, M_PI * 2.0, final_radius, 40);
596 glTranslatef(-translation[0], -translation[1], 0);
598 /* restore GL state */
600 glDisable(GL_LINE_SMOOTH);
605 void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
607 Paint *p = paint_get_active(CTX_data_scene(C));
609 if (p && !p->paint_cursor)
610 p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);