import java.lang.Double; /** * The DepthBuffer class implements a DepthBuffer and its pass test. */ public class DepthBuffer { private double[] buffer; int width; int height; /** * Constructs a DepthBuffer of size width x height. * The buffer is initially cleared. */ public DepthBuffer(int width, int height) { buffer = new double[width * height]; this.width = width; this.height = height; clear(); } /** * Clears the buffer to infinite depth for all fragments. */ public void clear() { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { buffer[i * width + j] = Double.POSITIVE_INFINITY; // buffer[i * width + j] = 0; } } } /** * Test if a fragment passes the DepthBuffer test, i.e. is the fragment the * closest at its position. */ public boolean testFragment(Fragment fragment) { if ((fragment.getX() >= 0) && (fragment.getX() < width) && (fragment.getY() >= 0) && (fragment.getY() < height)) { if (fragment.getDepth() < this.buffer[fragment.getY() * width + fragment.getX()]) { return true; } // if (fragment.getDepth() > this.buffer[fragment.getY() * width + // fragment.getX()]) { // return true; // } return false; } else { return false; } } /** * Writes the fragment depth to the buffer */ public void writeFragment(Fragment fragment) { if (testFragment(fragment)) { this.buffer[fragment.getY() * width + fragment.getX()] = fragment.getDepth(); } } }