Masking PGraphics with PGraphics
in
Programming Questions
•
2 months ago
I've been struggling with this for a couple hours now and looked around... my problem seems to be this bug (
https://github.com/processing/processing/issues/1738) that was fixed by late May, but I'm still having the problem using 2.0.1 from late June.
I'm trying to mask one PGraphics object with another, or ideally I'd like to mask the main window with the PGraphics (which I'm guessing is the same thing). If I just draw the mask on top, I can see that the mask is fine, but when I try to use it as a mask, then nothing happens: the original image just remains completely unmasked.
Also, when I do try to mask, I get the error "mask() is not available with this renderer." I've tried using both the default renderer and P2D, and both get the same error.
I'm applying the mask correctly as far as I can tell... is there something I'm doing wrong here? Do I need to go grab and compile the version off of Github to get that bug fix?
Here's the main file:
I'm trying to mask one PGraphics object with another, or ideally I'd like to mask the main window with the PGraphics (which I'm guessing is the same thing). If I just draw the mask on top, I can see that the mask is fine, but when I try to use it as a mask, then nothing happens: the original image just remains completely unmasked.
Also, when I do try to mask, I get the error "mask() is not available with this renderer." I've tried using both the default renderer and P2D, and both get the same error.
I'm applying the mask correctly as far as I can tell... is there something I'm doing wrong here? Do I need to go grab and compile the version off of Github to get that bug fix?
Here's the main file:
- PGraphics rotate_light;
PGraphics frame;
int playerX, playerY;
void setup()
{
size(800,600);
frame = createGraphics(width, height);
frame.imageMode(CENTER);
frame.ellipseMode(CENTER);
imageMode(CORNER);
light_initialize();
rotate_light = createGraphics(width, height);
rotate_light.imageMode(CENTER);
frameRate(60);
}
void draw()
{
// CLEAR PREVIOUS FRAME
frame.beginDraw();
frame.background(255, 0, 0);
// DRAW NEW FRAME
frame.fill(255);
frame.ellipse(width/2, height/2, width, height);
frame.fill(128);
frame.ellipse(width/2, height/2, width/2, height/2);
// APPLY FLASHLIGHT MASK
rotate_light.beginDraw();
rotate_light.background(0);
rotate_light.translate(width/2, height/2);
rotate_light.rotate(-point_dir());
rotate_light.image(light_img, 0, 0);
rotate_light.rotate(point_dir());
rotate_light.translate(-width/2, -height/2);
rotate_light.endDraw();
// ERROR HERE -------------------------------
// This shows that the mask is the image expected
frame.image(rotate_light, width/2, height/2);
// This shows that using it as a mask doesn't
//frame.mask(rotate_light);
// END --------------------------------------
// FINISH DRAWING
frame.endDraw();
image(frame, 0, 0);
}
float point_dir()
{
return (float)Math.atan2(mouseX - width/2, mouseY - height/2) - PI/2;
}
And here's where the mask is initialized:
- public static float light_direction = 0f;
public static PGraphics light_img;
public static int light_sz;
private static final float flashlight_reach = 0.85f;
private static final float flashlight_width = radians(30);
private static final int flashlight_blur = 25;
void light_initialize()
{
light_sz = (int)(Math.sqrt(height * height + width * width) + 1);
light_img = createGraphics(light_sz, light_sz);
light_img.beginDraw();
light_img.ellipseMode(CENTER);
light_img.background(0);
light_img.stroke(128);
light_img.fill(255);
light_img.arc(light_sz/2, light_sz/2,
flashlight_reach * width, flashlight_reach * height,
-flashlight_width, flashlight_width);
light_img.filter(BLUR, flashlight_blur);
light_img.endDraw();
}
Thanks much.
1