2012-01-19 02:06:09 +00:00
|
|
|
/*
|
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2009 by Nicholas Bishop
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): Jason Wilkins, Tom Musgrove.
|
|
|
|
|
*
|
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** \file blender/editors/sculpt_paint/paint_cursor.c
|
|
|
|
|
* \ingroup edsculpt
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_math.h"
|
2012-08-21 20:34:05 +00:00
|
|
|
#include "BLI_rect.h"
|
2012-01-19 02:06:09 +00:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_brush_types.h"
|
|
|
|
|
#include "DNA_color_types.h"
|
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
#include "DNA_scene_types.h"
|
|
|
|
|
#include "DNA_screen_types.h"
|
|
|
|
|
#include "DNA_userdef_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BKE_brush.h"
|
|
|
|
|
#include "BKE_context.h"
|
|
|
|
|
#include "BKE_paint.h"
|
|
|
|
|
|
|
|
|
|
#include "WM_api.h"
|
|
|
|
|
|
|
|
|
|
#include "BIF_gl.h"
|
|
|
|
|
#include "BIF_glutil.h"
|
|
|
|
|
|
|
|
|
|
#include "ED_view3d.h"
|
|
|
|
|
|
|
|
|
|
#include "paint_intern.h"
|
|
|
|
|
/* still needed for sculpt_stroke_get_location, should be
|
2012-03-03 16:31:46 +00:00
|
|
|
* removed eventually (TODO) */
|
2012-01-19 02:06:09 +00:00
|
|
|
#include "sculpt_intern.h"
|
|
|
|
|
|
|
|
|
|
/* TODOs:
|
2012-03-03 16:31:46 +00:00
|
|
|
*
|
|
|
|
|
* Some of the cursor drawing code is doing non-draw stuff
|
|
|
|
|
* (e.g. updating the brush rake angle). This should be cleaned up
|
|
|
|
|
* still.
|
|
|
|
|
*
|
|
|
|
|
* There is also some ugliness with sculpt-specific code.
|
2012-01-19 02:06:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
typedef struct Snapshot {
|
|
|
|
|
float size[3];
|
|
|
|
|
float ofs[3];
|
|
|
|
|
float rot;
|
2012-05-05 00:58:22 +00:00
|
|
|
int BKE_brush_size_get;
|
2012-01-19 02:06:09 +00:00
|
|
|
int winx;
|
|
|
|
|
int winy;
|
|
|
|
|
int brush_map_mode;
|
|
|
|
|
int curve_changed_timestamp;
|
|
|
|
|
} Snapshot;
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
static int same_snap(Snapshot *snap, Brush *brush, ViewContext *vc)
|
2012-01-19 02:06:09 +00:00
|
|
|
{
|
2012-03-28 03:47:33 +00:00
|
|
|
MTex *mtex = &brush->mtex;
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
return (((mtex->tex) &&
|
2012-03-28 03:47:33 +00:00
|
|
|
equals_v3v3(mtex->ofs, snap->ofs) &&
|
|
|
|
|
equals_v3v3(mtex->size, snap->size) &&
|
|
|
|
|
mtex->rot == snap->rot) &&
|
|
|
|
|
|
|
|
|
|
/* make brush smaller shouldn't cause a resample */
|
2012-05-15 04:44:13 +00:00
|
|
|
((mtex->brush_map_mode == MTEX_MAP_MODE_VIEW &&
|
2012-05-05 00:58:22 +00:00
|
|
|
(BKE_brush_size_get(vc->scene, brush) <= snap->BKE_brush_size_get)) ||
|
|
|
|
|
(BKE_brush_size_get(vc->scene, brush) == snap->BKE_brush_size_get)) &&
|
2012-03-28 03:47:33 +00:00
|
|
|
|
|
|
|
|
(mtex->brush_map_mode == snap->brush_map_mode) &&
|
|
|
|
|
(vc->ar->winx == snap->winx) &&
|
|
|
|
|
(vc->ar->winy == snap->winy));
|
2012-01-19 02:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
static void make_snap(Snapshot *snap, Brush *brush, ViewContext *vc)
|
2012-01-19 02:06:09 +00:00
|
|
|
{
|
|
|
|
|
if (brush->mtex.tex) {
|
|
|
|
|
snap->brush_map_mode = brush->mtex.brush_map_mode;
|
|
|
|
|
copy_v3_v3(snap->ofs, brush->mtex.ofs);
|
|
|
|
|
copy_v3_v3(snap->size, brush->mtex.size);
|
|
|
|
|
snap->rot = brush->mtex.rot;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
snap->brush_map_mode = -1;
|
2012-03-28 03:47:33 +00:00
|
|
|
snap->ofs[0] = snap->ofs[1] = snap->ofs[2] = -1;
|
|
|
|
|
snap->size[0] = snap->size[1] = snap->size[2] = -1;
|
2012-01-19 02:06:09 +00:00
|
|
|
snap->rot = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-05 00:58:22 +00:00
|
|
|
snap->BKE_brush_size_get = BKE_brush_size_get(vc->scene, brush);
|
2012-01-19 02:06:09 +00:00
|
|
|
snap->winx = vc->ar->winx;
|
|
|
|
|
snap->winy = vc->ar->winy;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
static int load_tex(Sculpt *sd, Brush *br, ViewContext *vc)
|
2012-01-19 02:06:09 +00:00
|
|
|
{
|
|
|
|
|
static GLuint overlay_texture = 0;
|
|
|
|
|
static int init = 0;
|
|
|
|
|
static int tex_changed_timestamp = -1;
|
|
|
|
|
static int curve_changed_timestamp = -1;
|
|
|
|
|
static Snapshot snap;
|
|
|
|
|
static int old_size = -1;
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
GLubyte *buffer = NULL;
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
int size;
|
|
|
|
|
int j;
|
|
|
|
|
int refresh;
|
|
|
|
|
|
|
|
|
|
#ifndef _OPENMP
|
|
|
|
|
(void)sd; /* quied unused warning */
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED && !br->mtex.tex) return 0;
|
|
|
|
|
|
|
|
|
|
refresh =
|
2012-03-28 03:47:33 +00:00
|
|
|
!overlay_texture ||
|
|
|
|
|
(br->mtex.tex &&
|
|
|
|
|
(!br->mtex.tex->preview ||
|
|
|
|
|
br->mtex.tex->preview->changed_timestamp[0] != tex_changed_timestamp)) ||
|
|
|
|
|
!br->curve ||
|
|
|
|
|
br->curve->changed_timestamp != curve_changed_timestamp ||
|
|
|
|
|
!same_snap(&snap, br, vc);
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
if (refresh) {
|
|
|
|
|
if (br->mtex.tex && br->mtex.tex->preview)
|
|
|
|
|
tex_changed_timestamp = br->mtex.tex->preview->changed_timestamp[0];
|
|
|
|
|
|
|
|
|
|
if (br->curve)
|
|
|
|
|
curve_changed_timestamp = br->curve->changed_timestamp;
|
|
|
|
|
|
|
|
|
|
make_snap(&snap, br, vc);
|
|
|
|
|
|
2012-05-15 04:44:13 +00:00
|
|
|
if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
|
2012-05-05 00:58:22 +00:00
|
|
|
int s = BKE_brush_size_get(vc->scene, br);
|
2012-01-19 02:06:09 +00:00
|
|
|
int r = 1;
|
|
|
|
|
|
|
|
|
|
for (s >>= 1; s > 0; s >>= 1)
|
|
|
|
|
r++;
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
size = (1 << r);
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
if (size < 256)
|
|
|
|
|
size = 256;
|
|
|
|
|
|
|
|
|
|
if (size < old_size)
|
|
|
|
|
size = old_size;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
size = 512;
|
|
|
|
|
|
|
|
|
|
if (old_size != size) {
|
|
|
|
|
if (overlay_texture) {
|
|
|
|
|
glDeleteTextures(1, &overlay_texture);
|
|
|
|
|
overlay_texture = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init = 0;
|
|
|
|
|
|
|
|
|
|
old_size = size;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
buffer = MEM_mallocN(sizeof(GLubyte) * size * size, "load_tex");
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
#pragma omp parallel for schedule(static) if (sd->flags & SCULPT_USE_OPENMP)
|
2012-03-28 03:47:33 +00:00
|
|
|
for (j = 0; j < size; j++) {
|
2012-01-19 02:06:09 +00:00
|
|
|
int i;
|
|
|
|
|
float y;
|
|
|
|
|
float len;
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
for (i = 0; i < size; i++) {
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
// largely duplicated from tex_strength
|
|
|
|
|
|
|
|
|
|
const float rotation = -br->mtex.rot;
|
2012-05-05 00:58:22 +00:00
|
|
|
float radius = BKE_brush_size_get(vc->scene, br);
|
2012-03-28 03:47:33 +00:00
|
|
|
int index = j * size + i;
|
2012-01-19 02:06:09 +00:00
|
|
|
float x;
|
|
|
|
|
float avg;
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
x = (float)i / size;
|
|
|
|
|
y = (float)j / size;
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
x -= 0.5f;
|
|
|
|
|
y -= 0.5f;
|
|
|
|
|
|
|
|
|
|
if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) {
|
|
|
|
|
x *= vc->ar->winx / radius;
|
|
|
|
|
y *= vc->ar->winy / radius;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
x *= 2;
|
|
|
|
|
y *= 2;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
len = sqrtf(x * x + y * y);
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
if ((br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) || len <= 1) {
|
|
|
|
|
/* it is probably worth optimizing for those cases where
|
2012-03-03 16:31:46 +00:00
|
|
|
* the texture is not rotated by skipping the calls to
|
|
|
|
|
* atan2, sqrtf, sin, and cos. */
|
2012-01-19 02:06:09 +00:00
|
|
|
if (br->mtex.tex && (rotation > 0.001f || rotation < -0.001f)) {
|
|
|
|
|
const float angle = atan2f(y, x) + rotation;
|
|
|
|
|
|
|
|
|
|
x = len * cosf(angle);
|
|
|
|
|
y = len * sinf(angle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
x *= br->mtex.size[0];
|
|
|
|
|
y *= br->mtex.size[1];
|
|
|
|
|
|
|
|
|
|
x += br->mtex.ofs[0];
|
|
|
|
|
y += br->mtex.ofs[1];
|
|
|
|
|
|
|
|
|
|
avg = br->mtex.tex ? paint_get_tex_pixel(br, x, y) : 1;
|
|
|
|
|
|
|
|
|
|
avg += br->texture_sample_bias;
|
|
|
|
|
|
2012-05-15 04:44:13 +00:00
|
|
|
if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW)
|
2012-05-05 00:58:22 +00:00
|
|
|
avg *= BKE_brush_curve_strength(br, len, 1); /* Falloff curve */
|
2012-01-19 02:06:09 +00:00
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
buffer[index] = 255 - (GLubyte)(255 * avg);
|
2012-01-19 02:06:09 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
buffer[index] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!overlay_texture)
|
|
|
|
|
glGenTextures(1, &overlay_texture);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2012-03-28 03:47:33 +00:00
|
|
|
size = old_size;
|
2012-01-19 02:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, overlay_texture);
|
|
|
|
|
|
|
|
|
|
if (refresh) {
|
|
|
|
|
if (!init) {
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size, size, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
|
|
|
|
|
init = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
|
MEM_freeN(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
|
|
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
2012-05-15 04:44:13 +00:00
|
|
|
if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
|
2012-01-19 02:06:09 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int project_brush_radius(ViewContext *vc,
|
2012-03-28 03:47:33 +00:00
|
|
|
float radius,
|
|
|
|
|
const float location[3])
|
2012-01-19 02:06:09 +00:00
|
|
|
{
|
|
|
|
|
float view[3], nonortho[3], ortho[3], offset[3], p1[2], p2[2];
|
|
|
|
|
|
|
|
|
|
ED_view3d_global_to_vector(vc->rv3d, location, view);
|
|
|
|
|
|
2012-07-06 23:56:59 +00:00
|
|
|
/* create a vector that is not orthogonal to view */
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
if (fabsf(view[0]) < 0.1f) {
|
|
|
|
|
nonortho[0] = view[0] + 1.0f;
|
|
|
|
|
nonortho[1] = view[1];
|
|
|
|
|
nonortho[2] = view[2];
|
|
|
|
|
}
|
|
|
|
|
else if (fabsf(view[1]) < 0.1f) {
|
|
|
|
|
nonortho[0] = view[0];
|
|
|
|
|
nonortho[1] = view[1] + 1.0f;
|
|
|
|
|
nonortho[2] = view[2];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
nonortho[0] = view[0];
|
|
|
|
|
nonortho[1] = view[1];
|
|
|
|
|
nonortho[2] = view[2] + 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-06 23:56:59 +00:00
|
|
|
/* get a vector in the plane of the view */
|
2012-01-19 02:06:09 +00:00
|
|
|
cross_v3_v3v3(ortho, nonortho, view);
|
|
|
|
|
normalize_v3(ortho);
|
|
|
|
|
|
2012-07-06 23:56:59 +00:00
|
|
|
/* make a point on the surface of the brush tagent to the view */
|
2012-01-19 02:06:09 +00:00
|
|
|
mul_v3_fl(ortho, radius);
|
|
|
|
|
add_v3_v3v3(offset, location, ortho);
|
|
|
|
|
|
2012-07-06 23:56:59 +00:00
|
|
|
/* project the center of the brush, and the tangent point to the view onto the screen */
|
2012-10-05 03:20:14 +00:00
|
|
|
if ((ED_view3d_project_float_global(vc->ar, location, p1, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_SUCCESS) &&
|
|
|
|
|
(ED_view3d_project_float_global(vc->ar, offset, p2, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_SUCCESS))
|
|
|
|
|
{
|
|
|
|
|
/* the distance between these points is the size of the projected brush in pixels */
|
|
|
|
|
return len_v2v2(p1, p2);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
BLI_assert(0); /* assert because the code that sets up the vectors should disallow this */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-01-19 02:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
static int sculpt_get_brush_geometry(bContext *C, ViewContext *vc,
|
|
|
|
|
int x, int y, int *pixel_radius,
|
|
|
|
|
float location[3])
|
2012-01-19 02:06:09 +00:00
|
|
|
{
|
|
|
|
|
Scene *scene = CTX_data_scene(C);
|
2012-06-04 07:29:45 +00:00
|
|
|
Paint *paint = paint_get_active_from_context(C);
|
2012-01-19 02:06:09 +00:00
|
|
|
Brush *brush = paint_brush(paint);
|
|
|
|
|
float window[2];
|
|
|
|
|
int hit;
|
|
|
|
|
|
|
|
|
|
window[0] = x + vc->ar->winrct.xmin;
|
|
|
|
|
window[1] = y + vc->ar->winrct.ymin;
|
|
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (vc->obact->sculpt && vc->obact->sculpt->pbvh &&
|
2012-05-20 19:49:27 +00:00
|
|
|
sculpt_stroke_get_location(C, location, window))
|
|
|
|
|
{
|
2012-01-19 02:06:09 +00:00
|
|
|
*pixel_radius =
|
2012-03-28 03:47:33 +00:00
|
|
|
project_brush_radius(vc,
|
2012-05-05 00:58:22 +00:00
|
|
|
BKE_brush_unprojected_radius_get(scene, brush),
|
2012-03-28 03:47:33 +00:00
|
|
|
location);
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
if (*pixel_radius == 0)
|
2012-05-05 00:58:22 +00:00
|
|
|
*pixel_radius = BKE_brush_size_get(scene, brush);
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
mul_m4_v3(vc->obact->obmat, location);
|
|
|
|
|
|
|
|
|
|
hit = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2012-03-28 03:47:33 +00:00
|
|
|
Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
|
|
|
|
|
Brush *brush = paint_brush(&sd->paint);
|
2012-01-19 02:06:09 +00:00
|
|
|
|
2012-05-05 00:58:22 +00:00
|
|
|
*pixel_radius = BKE_brush_size_get(scene, brush);
|
2012-01-19 02:06:09 +00:00
|
|
|
hit = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Draw an overlay that shows what effect the brush's texture will
|
2012-03-03 16:31:46 +00:00
|
|
|
* have on brush strength */
|
2012-01-19 02:06:09 +00:00
|
|
|
/* TODO: sculpt only for now */
|
|
|
|
|
static void paint_draw_alpha_overlay(Sculpt *sd, Brush *brush,
|
2012-03-28 03:47:33 +00:00
|
|
|
ViewContext *vc, int x, int y)
|
2012-01-19 02:06:09 +00:00
|
|
|
{
|
|
|
|
|
rctf quad;
|
|
|
|
|
|
|
|
|
|
/* check for overlay mode */
|
2012-03-24 06:38:07 +00:00
|
|
|
if (!(brush->flag & BRUSH_TEXTURE_OVERLAY) ||
|
2012-05-15 04:44:13 +00:00
|
|
|
!(ELEM(brush->mtex.brush_map_mode, MTEX_MAP_MODE_VIEW, MTEX_MAP_MODE_TILED)))
|
2012-04-28 06:31:57 +00:00
|
|
|
{
|
2012-01-19 02:06:09 +00:00
|
|
|
return;
|
2012-04-28 06:31:57 +00:00
|
|
|
}
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
/* save lots of GL state
|
2012-03-03 16:31:46 +00:00
|
|
|
* TODO: check on whether all of these are needed? */
|
2012-03-28 03:47:33 +00:00
|
|
|
glPushAttrib(GL_COLOR_BUFFER_BIT |
|
|
|
|
|
GL_CURRENT_BIT |
|
|
|
|
|
GL_DEPTH_BUFFER_BIT |
|
|
|
|
|
GL_ENABLE_BIT |
|
|
|
|
|
GL_LINE_BIT |
|
|
|
|
|
GL_POLYGON_BIT |
|
|
|
|
|
GL_STENCIL_BUFFER_BIT |
|
|
|
|
|
GL_TRANSFORM_BIT |
|
|
|
|
|
GL_VIEWPORT_BIT |
|
2012-01-19 02:06:09 +00:00
|
|
|
GL_TEXTURE_BIT);
|
|
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (load_tex(sd, brush, vc)) {
|
2012-01-19 02:06:09 +00:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
|
|
|
|
|
|
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
|
|
|
|
glDepthMask(GL_FALSE);
|
|
|
|
|
glDepthFunc(GL_ALWAYS);
|
|
|
|
|
|
|
|
|
|
glMatrixMode(GL_TEXTURE);
|
|
|
|
|
glPushMatrix();
|
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
2012-05-15 04:44:13 +00:00
|
|
|
if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
|
2012-01-19 02:06:09 +00:00
|
|
|
/* brush rotation */
|
|
|
|
|
glTranslatef(0.5, 0.5, 0);
|
|
|
|
|
glRotatef((double)RAD2DEGF((brush->flag & BRUSH_RAKE) ?
|
|
|
|
|
sd->last_angle : sd->special_rotation),
|
2012-03-28 03:47:33 +00:00
|
|
|
0.0, 0.0, 1.0);
|
2012-01-19 02:06:09 +00:00
|
|
|
glTranslatef(-0.5f, -0.5f, 0);
|
|
|
|
|
|
|
|
|
|
/* scale based on tablet pressure */
|
2012-05-05 00:58:22 +00:00
|
|
|
if (sd->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush)) {
|
2012-01-19 02:06:09 +00:00
|
|
|
glTranslatef(0.5f, 0.5f, 0);
|
2012-03-28 03:47:33 +00:00
|
|
|
glScalef(1.0f / sd->pressure_value, 1.0f / sd->pressure_value, 1);
|
2012-01-19 02:06:09 +00:00
|
|
|
glTranslatef(-0.5f, -0.5f, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (sd->draw_anchored) {
|
2012-01-19 02:06:09 +00:00
|
|
|
const float *aim = sd->anchored_initial_mouse;
|
|
|
|
|
const rcti *win = &vc->ar->winrct;
|
2012-03-28 03:47:33 +00:00
|
|
|
quad.xmin = aim[0] - sd->anchored_size - win->xmin;
|
|
|
|
|
quad.ymin = aim[1] - sd->anchored_size - win->ymin;
|
|
|
|
|
quad.xmax = aim[0] + sd->anchored_size - win->xmin;
|
|
|
|
|
quad.ymax = aim[1] + sd->anchored_size - win->ymin;
|
2012-01-19 02:06:09 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2012-05-05 00:58:22 +00:00
|
|
|
const int radius = BKE_brush_size_get(vc->scene, brush);
|
2012-01-19 02:06:09 +00:00
|
|
|
quad.xmin = x - radius;
|
|
|
|
|
quad.ymin = y - radius;
|
|
|
|
|
quad.xmax = x + radius;
|
|
|
|
|
quad.ymax = y + radius;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
quad.xmin = 0;
|
|
|
|
|
quad.ymin = 0;
|
2012-09-15 11:48:20 +00:00
|
|
|
quad.xmax = BLI_rcti_size_x(&vc->ar->winrct);
|
|
|
|
|
quad.ymax = BLI_rcti_size_y(&vc->ar->winrct);
|
2012-01-19 02:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* set quad color */
|
|
|
|
|
glColor4f(U.sculpt_paint_overlay_col[0],
|
|
|
|
|
U.sculpt_paint_overlay_col[1],
|
|
|
|
|
U.sculpt_paint_overlay_col[2],
|
|
|
|
|
brush->texture_overlay_alpha / 100.0f);
|
|
|
|
|
|
|
|
|
|
/* draw textured quad */
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
glTexCoord2f(0, 0);
|
|
|
|
|
glVertex2f(quad.xmin, quad.ymin);
|
|
|
|
|
glTexCoord2f(1, 0);
|
|
|
|
|
glVertex2f(quad.xmax, quad.ymin);
|
|
|
|
|
glTexCoord2f(1, 1);
|
|
|
|
|
glVertex2f(quad.xmax, quad.ymax);
|
|
|
|
|
glTexCoord2f(0, 1);
|
|
|
|
|
glVertex2f(quad.xmin, quad.ymax);
|
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glPopAttrib();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Special actions taken when paint cursor goes over mesh */
|
|
|
|
|
/* TODO: sculpt only for now */
|
|
|
|
|
static void paint_cursor_on_hit(Sculpt *sd, Brush *brush, ViewContext *vc,
|
2012-03-28 03:47:33 +00:00
|
|
|
const float location[3])
|
2012-01-19 02:06:09 +00:00
|
|
|
{
|
|
|
|
|
float unprojected_radius, projected_radius;
|
|
|
|
|
|
|
|
|
|
/* update the brush's cached 3D radius */
|
2012-05-05 00:58:22 +00:00
|
|
|
if (!BKE_brush_use_locked_size(vc->scene, brush)) {
|
2012-01-19 02:06:09 +00:00
|
|
|
/* get 2D brush radius */
|
2012-03-24 06:38:07 +00:00
|
|
|
if (sd->draw_anchored)
|
2012-01-19 02:06:09 +00:00
|
|
|
projected_radius = sd->anchored_size;
|
|
|
|
|
else {
|
2012-03-24 06:38:07 +00:00
|
|
|
if (brush->flag & BRUSH_ANCHORED)
|
2012-01-19 02:06:09 +00:00
|
|
|
projected_radius = 8;
|
|
|
|
|
else
|
2012-05-05 00:58:22 +00:00
|
|
|
projected_radius = BKE_brush_size_get(vc->scene, brush);
|
2012-01-19 02:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* convert brush radius from 2D to 3D */
|
|
|
|
|
unprojected_radius = paint_calc_object_space_radius(vc, location,
|
2012-03-28 03:47:33 +00:00
|
|
|
projected_radius);
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
/* scale 3D brush radius by pressure */
|
2012-05-05 00:58:22 +00:00
|
|
|
if (sd->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush))
|
2012-01-19 02:06:09 +00:00
|
|
|
unprojected_radius *= sd->pressure_value;
|
|
|
|
|
|
|
|
|
|
/* set cached value in either Brush or UnifiedPaintSettings */
|
2012-05-05 00:58:22 +00:00
|
|
|
BKE_brush_unprojected_radius_set(vc->scene, brush, unprojected_radius);
|
2012-01-19 02:06:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused))
|
|
|
|
|
{
|
|
|
|
|
Scene *scene = CTX_data_scene(C);
|
2012-06-04 07:29:45 +00:00
|
|
|
Paint *paint = paint_get_active_from_context(C);
|
2012-01-19 02:06:09 +00:00
|
|
|
Brush *brush = paint_brush(paint);
|
|
|
|
|
ViewContext vc;
|
|
|
|
|
float final_radius;
|
|
|
|
|
float translation[2];
|
|
|
|
|
float outline_alpha, *outline_col;
|
|
|
|
|
|
|
|
|
|
/* set various defaults */
|
|
|
|
|
translation[0] = x;
|
|
|
|
|
translation[1] = y;
|
|
|
|
|
outline_alpha = 0.5;
|
|
|
|
|
outline_col = brush->add_col;
|
2012-05-05 00:58:22 +00:00
|
|
|
final_radius = BKE_brush_size_get(scene, brush);
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
/* check that brush drawing is enabled */
|
2012-03-24 06:38:07 +00:00
|
|
|
if (!(paint->flags & PAINT_SHOW_BRUSH))
|
2012-01-19 02:06:09 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* can't use stroke vc here because this will be called during
|
2012-03-03 16:31:46 +00:00
|
|
|
* mouse over too, not just during a stroke */
|
2012-01-19 02:06:09 +00:00
|
|
|
view3d_set_viewcontext(C, &vc);
|
|
|
|
|
|
|
|
|
|
/* TODO: as sculpt and other paint modes are unified, this
|
2012-03-03 16:31:46 +00:00
|
|
|
* special mode of drawing will go away */
|
2012-03-24 06:38:07 +00:00
|
|
|
if (vc.obact->sculpt) {
|
2012-01-19 02:06:09 +00:00
|
|
|
Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
|
|
|
|
|
float location[3];
|
|
|
|
|
int pixel_radius, hit;
|
|
|
|
|
|
|
|
|
|
/* this is probably here so that rake takes into
|
2012-03-03 16:31:46 +00:00
|
|
|
* account the brush movements before the stroke
|
|
|
|
|
* starts, but this doesn't really belong in draw code
|
|
|
|
|
* TODO) */
|
2012-01-19 02:06:09 +00:00
|
|
|
{
|
|
|
|
|
const float u = 0.5f;
|
|
|
|
|
const float v = 1 - u;
|
|
|
|
|
const float r = 20;
|
|
|
|
|
|
|
|
|
|
const float dx = sd->last_x - x;
|
|
|
|
|
const float dy = sd->last_y - y;
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
if (dx * dx + dy * dy >= r * r) {
|
2012-01-19 02:06:09 +00:00
|
|
|
sd->last_angle = atan2(dx, dy);
|
|
|
|
|
|
2012-03-28 03:47:33 +00:00
|
|
|
sd->last_x = u * sd->last_x + v * x;
|
|
|
|
|
sd->last_y = u * sd->last_y + v * y;
|
2012-01-19 02:06:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* test if brush is over the mesh */
|
|
|
|
|
hit = sculpt_get_brush_geometry(C, &vc, x, y, &pixel_radius, location);
|
|
|
|
|
|
|
|
|
|
/* draw overlay */
|
|
|
|
|
paint_draw_alpha_overlay(sd, brush, &vc, x, y);
|
|
|
|
|
|
2012-05-05 00:58:22 +00:00
|
|
|
if (BKE_brush_use_locked_size(scene, brush))
|
|
|
|
|
BKE_brush_size_set(scene, brush, pixel_radius);
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
/* check if brush is subtracting, use different color then */
|
|
|
|
|
/* TODO: no way currently to know state of pen flip or
|
2012-03-03 16:31:46 +00:00
|
|
|
* invert key modifier without starting a stroke */
|
2012-03-24 06:38:07 +00:00
|
|
|
if ((!(brush->flag & BRUSH_INVERTED) ^
|
2012-03-28 03:47:33 +00:00
|
|
|
!(brush->flag & BRUSH_DIR_IN)) &&
|
|
|
|
|
ELEM5(brush->sculpt_tool, SCULPT_TOOL_DRAW,
|
|
|
|
|
SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY,
|
|
|
|
|
SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE))
|
2012-04-28 06:31:57 +00:00
|
|
|
{
|
2012-01-19 02:06:09 +00:00
|
|
|
outline_col = brush->sub_col;
|
2012-04-28 06:31:57 +00:00
|
|
|
}
|
2012-01-19 02:06:09 +00:00
|
|
|
|
|
|
|
|
/* only do if brush is over the mesh */
|
2012-03-24 06:38:07 +00:00
|
|
|
if (hit)
|
2012-01-19 02:06:09 +00:00
|
|
|
paint_cursor_on_hit(sd, brush, &vc, location);
|
|
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (sd->draw_anchored) {
|
2012-01-19 02:06:09 +00:00
|
|
|
final_radius = sd->anchored_size;
|
|
|
|
|
translation[0] = sd->anchored_initial_mouse[0] - vc.ar->winrct.xmin;
|
|
|
|
|
translation[1] = sd->anchored_initial_mouse[1] - vc.ar->winrct.ymin;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* make lines pretty */
|
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
|
glEnable(GL_LINE_SMOOTH);
|
|
|
|
|
|
|
|
|
|
/* set brush color */
|
|
|
|
|
glColor4f(outline_col[0], outline_col[1], outline_col[2], outline_alpha);
|
|
|
|
|
|
|
|
|
|
/* draw brush outline */
|
|
|
|
|
glTranslatef(translation[0], translation[1], 0);
|
2012-03-28 03:47:33 +00:00
|
|
|
glutil_draw_lined_arc(0.0, M_PI * 2.0, final_radius, 40);
|
2012-01-19 02:06:09 +00:00
|
|
|
glTranslatef(-translation[0], -translation[1], 0);
|
|
|
|
|
|
|
|
|
|
/* restore GL state */
|
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
glDisable(GL_LINE_SMOOTH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Public API */
|
|
|
|
|
|
|
|
|
|
void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
|
|
|
|
|
{
|
2012-06-04 07:29:45 +00:00
|
|
|
Paint *p = paint_get_active_from_context(C);
|
2012-01-19 02:06:09 +00:00
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (p && !p->paint_cursor)
|
2012-01-19 02:06:09 +00:00
|
|
|
p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);
|
|
|
|
|
}
|