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)))
382 /* save lots of GL state
383 * TODO: check on whether all of these are needed? */
384 glPushAttrib(GL_COLOR_BUFFER_BIT|
390 GL_STENCIL_BUFFER_BIT|
395 if (load_tex(sd, brush, vc)) {
398 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
399 glDepthMask(GL_FALSE);
400 glDepthFunc(GL_ALWAYS);
402 glMatrixMode(GL_TEXTURE);
406 if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_FIXED) {
408 glTranslatef(0.5, 0.5, 0);
409 glRotatef((double)RAD2DEGF((brush->flag & BRUSH_RAKE) ?
410 sd->last_angle : sd->special_rotation),
412 glTranslatef(-0.5f, -0.5f, 0);
414 /* scale based on tablet pressure */
415 if (sd->draw_pressure && brush_use_size_pressure(vc->scene, brush)) {
416 glTranslatef(0.5f, 0.5f, 0);
417 glScalef(1.0f/sd->pressure_value, 1.0f/sd->pressure_value, 1);
418 glTranslatef(-0.5f, -0.5f, 0);
421 if (sd->draw_anchored) {
422 const float *aim = sd->anchored_initial_mouse;
423 const rcti *win = &vc->ar->winrct;
424 quad.xmin = aim[0]-sd->anchored_size - win->xmin;
425 quad.ymin = aim[1]-sd->anchored_size - win->ymin;
426 quad.xmax = aim[0]+sd->anchored_size - win->xmin;
427 quad.ymax = aim[1]+sd->anchored_size - win->ymin;
430 const int radius= brush_size(vc->scene, brush);
431 quad.xmin = x - radius;
432 quad.ymin = y - radius;
433 quad.xmax = x + radius;
434 quad.ymax = y + radius;
440 quad.xmax = vc->ar->winrct.xmax - vc->ar->winrct.xmin;
441 quad.ymax = vc->ar->winrct.ymax - vc->ar->winrct.ymin;
445 glColor4f(U.sculpt_paint_overlay_col[0],
446 U.sculpt_paint_overlay_col[1],
447 U.sculpt_paint_overlay_col[2],
448 brush->texture_overlay_alpha / 100.0f);
450 /* draw textured quad */
453 glVertex2f(quad.xmin, quad.ymin);
455 glVertex2f(quad.xmax, quad.ymin);
457 glVertex2f(quad.xmax, quad.ymax);
459 glVertex2f(quad.xmin, quad.ymax);
468 /* Special actions taken when paint cursor goes over mesh */
469 /* TODO: sculpt only for now */
470 static void paint_cursor_on_hit(Sculpt *sd, Brush *brush, ViewContext *vc,
471 const float location[3])
473 float unprojected_radius, projected_radius;
475 /* update the brush's cached 3D radius */
476 if (!brush_use_locked_size(vc->scene, brush)) {
477 /* get 2D brush radius */
478 if (sd->draw_anchored)
479 projected_radius = sd->anchored_size;
481 if (brush->flag & BRUSH_ANCHORED)
482 projected_radius = 8;
484 projected_radius = brush_size(vc->scene, brush);
487 /* convert brush radius from 2D to 3D */
488 unprojected_radius = paint_calc_object_space_radius(vc, location,
491 /* scale 3D brush radius by pressure */
492 if (sd->draw_pressure && brush_use_size_pressure(vc->scene, brush))
493 unprojected_radius *= sd->pressure_value;
495 /* set cached value in either Brush or UnifiedPaintSettings */
496 brush_set_unprojected_radius(vc->scene, brush, unprojected_radius);
500 static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused))
502 Scene *scene = CTX_data_scene(C);
503 Paint *paint = paint_get_active(scene);
504 Brush *brush = paint_brush(paint);
507 float translation[2];
508 float outline_alpha, *outline_col;
510 /* set various defaults */
514 outline_col = brush->add_col;
515 final_radius = brush_size(scene, brush);
517 /* check that brush drawing is enabled */
518 if (!(paint->flags & PAINT_SHOW_BRUSH))
521 /* can't use stroke vc here because this will be called during
522 * mouse over too, not just during a stroke */
523 view3d_set_viewcontext(C, &vc);
525 /* TODO: as sculpt and other paint modes are unified, this
526 * special mode of drawing will go away */
527 if (vc.obact->sculpt) {
528 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
530 int pixel_radius, hit;
532 /* this is probably here so that rake takes into
533 * account the brush movements before the stroke
534 * starts, but this doesn't really belong in draw code
537 const float u = 0.5f;
538 const float v = 1 - u;
541 const float dx = sd->last_x - x;
542 const float dy = sd->last_y - y;
544 if (dx*dx + dy*dy >= r*r) {
545 sd->last_angle = atan2(dx, dy);
547 sd->last_x = u*sd->last_x + v*x;
548 sd->last_y = u*sd->last_y + v*y;
552 /* test if brush is over the mesh */
553 hit = sculpt_get_brush_geometry(C, &vc, x, y, &pixel_radius, location);
556 paint_draw_alpha_overlay(sd, brush, &vc, x, y);
558 if (brush_use_locked_size(scene, brush))
559 brush_set_size(scene, brush, pixel_radius);
561 /* check if brush is subtracting, use different color then */
562 /* TODO: no way currently to know state of pen flip or
563 * invert key modifier without starting a stroke */
564 if ((!(brush->flag & BRUSH_INVERTED) ^
565 !(brush->flag & BRUSH_DIR_IN)) &&
566 ELEM5(brush->sculpt_tool, SCULPT_TOOL_DRAW,
567 SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY,
568 SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE))
569 outline_col = brush->sub_col;
571 /* only do if brush is over the mesh */
573 paint_cursor_on_hit(sd, brush, &vc, location);
575 if (sd->draw_anchored) {
576 final_radius = sd->anchored_size;
577 translation[0] = sd->anchored_initial_mouse[0] - vc.ar->winrct.xmin;
578 translation[1] = sd->anchored_initial_mouse[1] - vc.ar->winrct.ymin;
582 /* make lines pretty */
584 glEnable(GL_LINE_SMOOTH);
586 /* set brush color */
587 glColor4f(outline_col[0], outline_col[1], outline_col[2], outline_alpha);
589 /* draw brush outline */
590 glTranslatef(translation[0], translation[1], 0);
591 glutil_draw_lined_arc(0.0, M_PI*2.0, final_radius, 40);
592 glTranslatef(-translation[0], -translation[1], 0);
594 /* restore GL state */
596 glDisable(GL_LINE_SMOOTH);
601 void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
603 Paint *p = paint_get_active(CTX_data_scene(C));
605 if (p && !p->paint_cursor)
606 p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);