projet-rendu/DepthBuffer.java

62 lines
1.5 KiB
Java
Raw Normal View History

2022-04-12 10:08:58 +00:00
import java.lang.Double;
/**
* The DepthBuffer class implements a DepthBuffer and its pass test.
*/
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.
*/
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;
clear();
2022-04-12 10:08:58 +00:00
}
/**
* Clears the buffer to infinite depth for all fragments.
*/
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.
*/
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()]) {
return true;
}
2022-04-12 10:08:58 +00:00
return false;
} else {
return false;
}
}
/**
* Writes the fragment depth to the buffer
2022-04-12 10:08:58 +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
}
}
}