mypaint and pressure from mouse speed

Martin Renold martinxyz at gmx.ch
Tue Aug 5 21:02:05 CEST 2008


On Tue, Aug 05, 2008 at 12:50:09PM +0200, LukasT.dev at gmail.com wrote:
> Hi,
> i would like to ask you where in the code is located the routine for
> mouse pressure according speed? I would like to look at it as i'm trying to
> implement a brush that takes pressure into account and i would love to     
> have support for "mouse pressure".
>
> Thanks
> Have a nice day
> LukasT
> Krita developer

Hi Lukas,

For Krita? Cool :)
So I took the liberty to CC: the Krita mailing list. (I'm subscribed btw.)

OK first of all MyPaint does not calculate mouse pressure from the mouse
speed (btw. interesting idea). However a brush can be configured such that
the input "speed" has the same effect as the input "pressure". It all
depends on the brush you have selected. If you tell me which brush you are
using I can explain it to you more precisely.

So I guess you are actually looking for the code that handles the speed
input. I'm refering to MyPaint 0.5.x source (SVN uses the same code, but it
has moved around since).

gtkmybrush.c line 268:
  norm_speed = sqrt(SQR(norm_dx) + SQR(norm_dy));

This calculates the "normalized" speed (speed divided by current brush
radius and time since last event) from the X11 x/y event coordinates.

The speed is not used directly, but it is lowpass filtered. This makes it
more smooth so it does not jump around too fast. This is done at
gtkmybrush.c line 295 (a bit complicated):

  { // slow speed
    float fac;
    fac = 1.0 - exp_decay (settings[BRUSH_SPEED1_SLOWNESS], b->dtime);
    b->states[STATE_NORM_SPEED1_SLOW] += (norm_speed - b->states[STATE_NORM_SPEED1_SLOW]) * fac;

To understand this, imagine that 'b->dtime' (the time passed since the last
event) is constant. Then the above code does simply something like this:

    speed_filtered = 0.9 * speed_filtered + 0.1 * speed_unfiltered;

The exp_decay() is just to do the same thing with variable time steps.

Now the "speed_filtered" value can be used for anything... for example to
control the opacity and the radius.

Something you may want to know about the radius is that all calculations are
done logarithmic. This means that when you add something to the logarithmic
radius, like the speed, you are in fact multiplying the pixel-radius by some
value.

That's basically it, I hope I did not talk nonsense ;-)
Feel free to ask for details, and good luck.

bye,
Martin


More information about the kimageshop mailing list