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