Shapes and strokes on a plane in P3D/OpenGL
in
Share your Work
•
11 months ago
Rendering several overlapping shapes on a plane in 3D seems to be quite tricky using P3D/OpenGL (in Processing 2.0). Without the right hints there is either "z-fighting" of the different shapes visible (causing headaches) and the strokes of all shapes are visible (including the covered ones).
I searched quite a while for a solution, eventually with the wrong key words, but in the end I found a solution that may is of use to some of the readers.
So if you draw several shapes on the same plane add the two following hints:
- hint(DISABLE_DEPTH_TEST); // avoids z-fighting
- hint(ENABLE_ACCURATE_2D); // strokes are drawn correctly
A small example
- void setup() {
- size(200, 200, OPENGL);
- }
- void draw() {
- background(255);
- smooth();
- hint(DISABLE_DEPTH_TEST); // avoids z-fighting
- hint(ENABLE_ACCURATE_2D); // strokes are drawn correctly
- // some motion
- translate(width/2, height/2, -200);
- rotateY(frameCount * 0.01);
- rotateX(frameCount * 0.01);
- stroke(150);
- strokeWeight(6); // increase the thickness
- fill(220, 0, 0);
- rect(-50, -50, 200, 200);
- fill(220, 220, 0);
- rect(-100, -100, 200, 200);
- }