[Solved] Alpha blending not working on "flipped" shapes.

edited May 2016 in Questions about Code

I was about to post this question in libraries section, since I first noticed this issue while messing about with Planetarium, but then I've been able to reproduce something similar on simpler sketches.

So, the POV is in the middle of a series of rectangles; this picture should be perfectly symmetrical, but while the blending on the left is correct, on the right hand the shapes seen from the back blend only with the background. Here's the code producing this:


void draw() {
  background(0);  

  strokeWeight(0);
  translate((width-worldW)/2, (height-worldH)/2, 690);

  for (int j = 0; j < series; j++) {
    for (int i = 0; i < bars; i++) {
      [...]    
      pushMatrix();
        translate(level, barW*i, gap*j);
        scale(4, 1, 1);
        fill(i*25, 102, (bars-i)*25, 55);
        rect(0, 0, 10, barW);
      popMatrix();
    }
  }

}

I've already tried to draw a shape in place of the rect and flipping the normals but it isn't of any help. In the simple sketch below happens something similar, the blue rectangle blends only with the background and the grey shape, but masks the orange one, while the latter correctly blends with everything.


int a = 100;
int b = 200;

void setup() {
  size(800, 600, P3D);
  frameRate(30);
}

void draw() {
  background(0);
  
  //grey rect
  fill(202);
  rect(0, height/2, width, height/2);
  
  //blue rect
  pushMatrix();
  stroke(0, 127, 255);
  translate(0, 300, 0);
  rotateX(frameCount*0.018);
  fill(0, 127, 255, 127);
  rect(0, 0, width, 100);
  popMatrix();
  
  //orange rect
  pushMatrix(); 
  noStroke(); 
  translate((width/2), (height-b)/2, 0);
  rotateY(frameCount*0.015);
  fill(255, 127, 0, 127);
  rect(0, 0, a, b);
  popMatrix();

}

I've done a bit of searching but didn't find anything useful, sorry if this is something totally dumb, but I really can't figure out how to solve this problem.

Thanks.

Tagged:

Answers

  • Given this issue, I solved by splitting the loop and drawing the right part in the right order [from last row to front one].

Sign In or Register to comment.