blendMode() does not work when looking backward in 3D

edited January 2014 in Questions about Code

blendModes does not work when looking from -Z to +Z axis in 3D. How do I make it work when looking in any direction?

addBlendMode

import peasy.*;
PeasyCam cam;
double[] defaultCam = {
  0.5, 0, 0, 1000
};

void setup() {
  size(850, 400, P3D); 
  smooth(16);
  cam = new PeasyCam(this, defaultCam[3]);
  cam.setMinimumDistance(0.00001);
  cam.setMaximumDistance(999999999);
  cam.setRotations(defaultCam[0], defaultCam[1], defaultCam[2]);
  cam.lookAt(0, 0, 0);
} // end of setup

void draw() {
  background(18, 18, 20);
  float cameraZ = ((height/2.0) / tan(PI*60.0/360.0));
  perspective(PI/3.0, (float) width/height, cameraZ/100.0, cameraZ*100.0);

  // draw axis
  noFill();
  strokeWeight(1);
  stroke(0, 255, 0, 128);
  line(0, 0, 0, 0, 80, 0);   // y
  stroke(255, 0, 0, 128);
  line(0, 0, 0, 80, 0, 0);  // x
  stroke(0, 0, 255, 128);
  line(0, 0, 0, 0, 0, 80);  // z
  stroke(255, 50);
  box(10);
  stroke(255);

  //hint(DISABLE_DEPTH_TEST);
  blendMode(ADD);
  stroke(255);
  fill(#3C92D6, 100);
  for (int i=0; i<4; i++) {
    pushMatrix();
    translate(50, 0, i*100);
    rect(0, 0, 100, 100);
    popMatrix();
  }
} 
Tagged:

Comments

  • It's not the blendMode command that fails, it's the depth sorting.

    Uncommenting line 35 should resolve this issue, is there a reason you commented it out? Not the desired effect or do you need the depth test for other purposes?

  • Is it possible to make it work with depth test?

  • edited January 2014

    Sure, you just have to implement some sort of depth-sorting yourself. In your case a simple check if the camera's Z coordinate is positive/negative should suffice to choose the rect render order.

    Replace lines 39-44 with the following code:

    if(cam.getPosition()[2] >= 0)
      for (int i=0; i<4; i++) {
        pushMatrix();
        translate(50, 0, i*100);
        rect(0, 0, 100, 100);
        popMatrix();
      }
    else
      for (int i=3; i>=0; i--) {
        pushMatrix();
        translate(50, 0, i*100);
        rect(0, 0, 100, 100);
        popMatrix();
      }
    
  • edited January 2014

    If your real project needs a more complex sorting algorithm, just give us a shout, I'm sure we are able to help you. :)

Sign In or Register to comment.