transparancy in PImage
in
Programming Questions
•
3 years ago
Hello list,
I'm still at it with the glowing line. My problem now is that to achieve the 'glow' effect, the code I found must do a get on a PGraphics render context to create a PImage then perform some effects and then render. The problem is that at the .get stage the created PImage's background is opaque and I cannot seem to find a way to make it transparent.
I found numorous solutions on the list but I cannot seem to get any of them to work for me. Any thoughts?
My complete code:
I'm still at it with the glowing line. My problem now is that to achieve the 'glow' effect, the code I found must do a get on a PGraphics render context to create a PImage then perform some effects and then render. The problem is that at the .get stage the created PImage's background is opaque and I cannot seem to find a way to make it transparent.
- //lightsaber Glow
- lightsaberGlowRC.beginDraw();
- lightsaberGlowRC.background(255,255,255,0);
- lightsaberGlowRC.strokeWeight(lightsaberGlowStrokeWt);
- lightsaberGlowRC.stroke( unhex(lightsaberGlowClr) );
- lightsaberGlowRC.line(ctrX, ctrY, orbitX, orbitY);
- lightsaberGlowRC.endDraw();
- lightsaberGlowImage = lightsaberGlowRC.get(0, 0, lightsaberGlowRC.width, lightsaberGlowRC.height);
- lightsaberGlowImage.resize(0,width/2);
- lightsaberGlowImage.filter(BLUR,2);
- lightsaberGlowImage.resize(0,width);
- image(lightsaberGlowImage,0,0);
I found numorous solutions on the list but I cannot seem to get any of them to work for me. Any thoughts?
My complete code:
- import processing.opengl.*;
- import javax.media.opengl.*;
- PImage lightsaberGlowImage;
- //lightsaber Core
- PGraphics lightsaberCoreRC; //render context
- //variables
- String lightsaberCoreClr;
- int lightsaberCoreStrokeWt;
- //lightsaber Glow
- PGraphics lightsaberGlowRC; //render context
- //variables
- String lightsaberGlowClr;
- int lightsaberGlowStrokeWt;
- //Radar movement variables
- float orbit, orbitX, orbitY, ctrX, ctrY;
- float radarRadius;
- int rotationDur = 20;
- //intersecting circle
- float circleX, circleY, circleRadius;
- void setup() {
- size(500,500, OPENGL);
- orbitX = 0.0;
- orbitY = 0.0;
- ctrX = width/2.0;
- ctrY = height/2.0;
- circleX = 400.0;
- circleY = 400.0;
- circleRadius = 30.0;
- radarRadius= 500.0;
- //lightsaberCore
- lightsaberCoreRC = createGraphics(width, height, JAVA2D); //initialize render context like size()
- //initialize variables
- lightsaberCoreClr = "FFFFFFFF";
- lightsaberCoreStrokeWt = 5;
- //initial settings like setup()
- lightsaberCoreRC.beginDraw();
- lightsaberCoreRC.background(255, 255, 255, 0);
- lightsaberCoreRC.smooth();
- lightsaberCoreRC.endDraw();
- //lightsaber Glow
- lightsaberGlowRC = createGraphics(width, height, JAVA2D); //initialize render context like size()
- lightsaberGlowClr = "FF00FF00"; //green
- lightsaberGlowStrokeWt = lightsaberCoreStrokeWt*3;
- lightsaberGlowRC.beginDraw();
- lightsaberGlowRC.background(255,255,255,0);
- lightsaberGlowRC.smooth();
- lightsaberGlowRC.endDraw();
- }
- void draw() {
- background(255);
- //rotate radar
- if(frameCount>0.0)orbit = map((frameCount/24.0)%rotationDur, 0.0, rotationDur, 0.0, TWO_PI);
- orbitX = ctrX + (cos(orbit) * radarRadius );
- orbitY = ctrY + (sin(orbit) * radarRadius );
- //lightsaberCore
- lightsaberCoreRC.beginDraw();
- lightsaberCoreRC.background(255, 255, 255, 0);
- lightsaberCoreRC.strokeWeight(lightsaberCoreStrokeWt);
- lightsaberCoreRC.stroke( unhex(lightsaberCoreClr) );
- lightsaberCoreRC.line(ctrX, ctrY, orbitX, orbitY);
- lightsaberCoreRC.endDraw();
- //lightsaber Glow
- lightsaberGlowRC.beginDraw();
- lightsaberGlowRC.background(255,255,255,0);
- lightsaberGlowRC.strokeWeight(lightsaberGlowStrokeWt);
- lightsaberGlowRC.stroke( unhex(lightsaberGlowClr) );
- lightsaberGlowRC.line(ctrX, ctrY, orbitX, orbitY);
- lightsaberGlowRC.endDraw();
- lightsaberGlowImage = lightsaberGlowRC.get(0, 0, lightsaberGlowRC.width, lightsaberGlowRC.height);
- lightsaberGlowImage.resize(0,width/2);
- lightsaberGlowImage.filter(BLUR,2);
- lightsaberGlowImage.resize(0,width);
- image(lightsaberGlowImage,0,0);
- //for intersect detection
- if (circleLineIntersect(orbitX, orbitY, ctrX, ctrY, circleX, circleY, circleRadius) == true) {
- fill(255, 0, 255, 200);
- }
- else {
- fill(255, 128, 0, 200);
- }
- ellipseMode(RADIUS);
- ellipse(circleX, circleY, circleRadius, circleRadius);
- /////////////
- image(lightsaberCoreRC, 0, 0);
- }
- boolean circleLineIntersect(float x1, float y1, float x2, float y2, float cx, float cy, float cr ) {
- float dx = x2 - x1;
- float dy = y2 - y1;
- float a = dx * dx + dy * dy;
- float b = 2 * (dx * (x1 - cx) + dy * (y1 - cy));
- float c = cx * cx + cy * cy;
- c += x1 * x1 + y1 * y1;
- c -= 2 * (cx * x1 + cy * y1);
- c -= cr * cr;
- float bb4ac = b * b - 4 * a * c;
- //println(bb4ac);
- if (bb4ac < 0) { // Not intersecting
- return false;
- }
- else {
- float mu = (-b + sqrt( b*b - 4*a*c )) / (2*a);
- float ix1 = x1 + mu*(dx);
- float iy1 = y1 + mu*(dy);
- mu = (-b - sqrt(b*b - 4*a*c )) / (2*a);
- float ix2 = x1 + mu*(dx);
- float iy2 = y1 + mu*(dy);
- // The intersection points
- //ellipse(ix1, iy1, 10, 10);
- //ellipse(ix2, iy2, 10, 10);
- float testX;
- float testY;
- // Figure out which point is closer to the circle
- if (dist(x1, y1, cx, cy) < dist(x2, y2, cx, cy)) {
- testX = x2;
- testY = y2;
- } else {
- testX = x1;
- testY = y1;
- }
- if (dist(testX, testY, ix1, iy1) < dist(x1, y1, x2, y2) || dist(testX, testY, ix2, iy2) < dist(x1, y1, x2, y2)) {
- return true;
- } else {
- return false;
- }
- }
- }
1