Loading...
Logo
Processing Forum
I'm trying to make some leaves in 3d using an image texture of a leaf with transparency, but it's drawing the background color on the transparent parts. It's hard to explain so I took a picture of what's happening for you.

The picture has transparency, how can I get it to draw the transparency?

[update]

Here's code to recreate it,

Copy code
  1. PImage img;

  2. void setup() {
  3.   rectMode(CENTER);
  4.   
  5.   size(400,400, P3D);
  6.   img = loadImage("leaf.png");
  7.   
  8. };

  9. void draw() {
  10.   background(frameCount%255);
  11.   image(img, 20, 20, 150, 150);
  12.   translate(50,50,-1);
  13.   image(img, 20, 20, 150, 150);
  14. }

Replies(4)

Seems to work fine using my version of Processing 1.5.1. The only change I made to your code was to call loadImage() with the URL of the leaf image (since I didn't bother saving it locally). What version of Processing are you using?


I'm using the newest version 2.0b7. And that's not fully it, unfortunately :/

 See the upper leaf should be over the lower leaf. I do  translate(50,50,-1); before drawing the lower one which should put it beneath the first leaf. 

Should I fill out a bug report, it looks like this could be a bug
This is something related to opengl in general rather than being processing specific. Either google terms like "alpha depth sort" or take a look at these pages:
In your example this would be for Processing 2.0b7:
Copy code
  1. PImage img = loadImage("http://www.burrconstruction.com/img/leaf.png");
  2.  
  3. void setup() {
  4.   size(400, 400, P3D); // P3D is OPENGL in 2.0xx
  5. }
  6.  
  7. void draw() {
  8.   background(frameCount%255);
  9.   pushMatrix();
  10.   translate(50,50,-1);
  11.   image(img, 20, 20, 150, 150);
  12.   popMatrix();
  13.   image(img, 20, 20, 150, 150);
  14. }
With a thousand objects this would be same idea. You sort them by depth, then draw them from back to front.

In Processing 1.5.1 you had a helper function for this called hint(ENABLE_DEPTH_SORT) which would do the sorting for you. So for Processing 1.5.1 you could use the following code and get the same result:
Copy code
  1. import processing.opengl.*;
  2.  
  3. PImage img = loadImage("http://www.burrconstruction.com/img/leaf.png");
  4.  
  5. void setup() {
  6.   size(400, 400, OPENGL);
  7.   hint(ENABLE_DEPTH_SORT);
  8. }
  9.  
  10. void draw() {
  11.   background(frameCount%255);
  12.   image(img, 20, 20, 150, 150);
  13.   translate(50, 50, -1);
  14.   image(img, 20, 20, 150, 150);
  15. }
While it's still 'present' in 2.0xx, it seems the whole hint() method has been removed from the 2.0xx reference and hint(ENABLE_DEPTH_SORT) seems to be no longer functional.

Hmmm, yeah I see now, I checked the code in PGraphics3D. The sorting method is empty and commented out. I guess I'll have to make my own.