We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › zbuffer and background
Page Index Toggle Pages: 1
zbuffer and background (Read 1849 times)
zbuffer and background
Jun 22nd, 2005, 1:51am
 
if one shape is drawn only once, in front of another shape that is drawn every cycle, and there is no background, the shape in front is never drawn over.

easiest explained with this code:

Code:

PGraphics3 g3;
int numPx;
boolean clearit = false;

boolean firstframe = true;
boolean secondframe = false;

void setup () {
size(400, 400, P3D);
numPx = width*height;
g3 = (PGraphics3) g;
noStroke();
}

void draw () {
//g3.zbuffer = new float[numPx];

if (secondframe) {
translate(0, 0, 150);
fill(0, 255, 0);
rect(100, 100, 100, 100);
translate(0, 0, -150);
secondframe = false;
}

if (firstframe) {
background(0);
firstframe = false;
secondframe = true;
}

fill(255, 0, 0);
rect(100, 0, 100, height);
}


basically, background is drawn on the first frame.  green square is drawn on the next.  red stripe is drawn every frame.  one would think that drawing the red rect later would draw over the green square, no?

after some digging, i found out a bit about the z-buffer...clearing it fixes this problem.  uncomment the first line in draw() to see what i mean.

so, what *exactly* is the z-buffer?  is it a processing thing, or a java thing, or a drawing-with-computers thing in general?  pleez edumacate me Smiley
Re: zbuffer and background
Reply #1 - Jun 22nd, 2005, 1:51am
 
and here's a gl version of the same thing.  just for reference.

Code:

import processing.opengl.*;
import net.java.games.jogl.*;

GL gl;
boolean firstframe = true;
boolean secondframe = false;

void setup () {
size(400, 400, OPENGL);
gl = ((PGraphicsGL)g).gl;
noStroke();
}

void draw () {
//gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

if (secondframe) {
translate(0, 0, 150);
fill(0, 255, 0);
rect(100, 100, 100, 100);
translate(0, 0, -150);
secondframe = false;
}

if (firstframe) {
background(0);
firstframe = false;
secondframe = true;
}

fill(255, 0, 0);
rect(100, 0, 100, height);
}
Re: zbuffer and background
Reply #2 - Jun 22nd, 2005, 10:07am
 
z-buffer can be categorized in the general drawing-with-computers Smiley
Basically it's an array of floats whose size is your viewport's size. Each value of the array stores the distance (z value) of the pixel to the viewer. It is very useful in 3D when intersecting objects are drawn, because a simple comparison allows you to remove hidden surfaces. Somewhere in the drawing pipeline, before a pixel is drawn, its z-value is compared to what is already on the frame buffer : either it is lower/equal and the 'coming' pixel is drawn, either it is greater and the pixel is not drawn.
Re: zbuffer and background
Reply #3 - Jun 22nd, 2005, 5:28pm
 
ahh.  thanks for the explanation v3ga, much needed.

still a bit confused tho -- if i clear the z-buffer, i'm not clearing any pixel color information, just the depth (z) information, right?  so why is it that when i clear the z-buffer in the P3D example above, the pixels of the green square disappear completely, instead of just being marked as sitting at depth 0?

basically, what i'm getting at is that i'm trying to create objects in 3D space without using background(), and i'm running into all sorts of goofy problems like this one.  and also this one: http://processing.org/faq/bugs.html#opengl --
Quote:
Objects with alpha (lines or shapes with an alpha fill or stroke, images with alpha, all fonts) are displayed in OpenGL based on their drawing order. This creates some annoying effects like making things opaque if they're drawn out of order with objects above or beneath them. This is simply how OpenGL works, but is something that we'd like to develop a workaround for in the future. Currently, you should just try to draw things back-to-front as best as possible.


just banging my head against the wall until i put a hole in it....
Re: zbuffer and background
Reply #4 - Jun 22nd, 2005, 5:40pm
 
oh wait!  if i change the line in my GL example from
Code:

gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

to

Code:

gl.glClear(GL.GL_DEPTH_BUFFER_BIT);


it does exactly what i want -- clears the z-buffer without clearing the color buffer.  still dunno how do this in P3D, but that's just trivia as far as i'm concerned, since i'm working in gl.

thanks again for the explanation v3ga, exactly what i needed!
-d
Re: zbuffer and background
Reply #5 - Jun 22nd, 2005, 6:37pm
 
to clear only the zbuffer with P3D, use the following:

Code:
PGraphics3 g3 = (PGraphics3) g;
for (int i = 0; i < width*height; i++) {
g3.zbuffer[i] = MAX_FLOAT;
}


when rev 92 comes along, you won't have to do the cast, so it'll just be:
Code:
for (int i = 0; i < width*height; i++) {
g.zbuffer[i] = MAX_FLOAT;
}


and you might save a little time by calculating width*height first rather than each time you go through the for loop Wink
Re: zbuffer and background
Reply #6 - Jun 22nd, 2005, 9:48pm
 
and, for what it's worth to whoever reads this in the future, even faster to use System.arraycopy --

Code:

PGraphics3 g3;
int numPx;

boolean firstframe = true;
boolean secondframe = false;

float[] clearZ;

void setup () {
size(400, 400, P3D);
noStroke();
g3 = (PGraphics3) g;

numPx = width*height;
clearZ = new float[numPx];
for (int i=0; i<numPx; i++) {
clearZ[i] = MAX_FLOAT;
}
}

void draw () {
System.arraycopy(clearZ, 0, g3.zbuffer, 0, numPx);

if (secondframe) {
translate(0, 0, 150);
fill(0, 255, 0);
rect(100, 100, 100, 100);
translate(0, 0, -150);
secondframe = false;
}

if (firstframe) {
background(0);
firstframe = false;
secondframe = true;
}

fill(255, 0, 0);
rect(100, 0, 100, height);
}
Page Index Toggle Pages: 1