projet-rendu/Fragment.java

151 lines
3.3 KiB
Java
Raw Normal View History

2022-04-12 10:08:58 +00:00
import java.awt.*;
import algebra.*;
/**
* The Fragment class represents an attributed 'pixel' as generated
* by a Rasterizer.
2022-04-19 19:59:20 +00:00
*
2022-04-12 10:08:58 +00:00
* @author cdehais
*/
public class Fragment {
private int x;
private int y;
private int numAttributes;
2022-04-19 19:59:20 +00:00
/*
* attributes placement:
* 0: depth
2022-04-12 10:08:58 +00:00
* 1-3: color
* 4-6: normal
* 7-8: (u,v) texture coordinates
2022-04-19 20:25:27 +00:00
* 9-11: unprojected vertice un 3D space
2022-04-12 10:08:58 +00:00
*/
double[] attributes;
2022-04-19 19:59:20 +00:00
public Fragment(int x, int y) { // int numAdditionalAttributes) {
2022-04-12 10:08:58 +00:00
this.x = x;
this.y = y;
2022-04-19 20:25:27 +00:00
numAttributes = 12;
2022-04-12 10:08:58 +00:00
attributes = new double[numAttributes];
}
2022-04-19 19:59:20 +00:00
public int getNumAttributes() {
2022-04-12 10:08:58 +00:00
return numAttributes;
}
/**
* Gets a scalar attribute at index.
*/
2022-04-19 19:59:20 +00:00
public double getAttribute(int index) {
2022-04-12 10:08:58 +00:00
return attributes[index];
}
/**
2022-04-19 19:59:20 +00:00
* Gets a vector attribute at the given starting location and with the given
* dimension.
2022-04-12 10:08:58 +00:00
*/
2022-04-19 19:59:20 +00:00
public double[] getAttribute(int index, int dimension) {
2022-04-12 10:08:58 +00:00
double[] attr = new double[dimension];
for (int i = 0; i < dimension; i++) {
2022-04-19 19:59:20 +00:00
attr[i] = attributes[index + i];
2022-04-12 10:08:58 +00:00
}
return attr;
}
2022-04-19 19:59:20 +00:00
public void setAttribute(int index, double value) {
2022-04-12 10:08:58 +00:00
attributes[index] = value;
}
/**
* Gets the x pixel coordinate of the Fragment
*/
2022-04-19 19:59:20 +00:00
public int getX() {
2022-04-12 10:08:58 +00:00
return x;
}
2022-04-19 19:59:20 +00:00
2022-04-12 10:08:58 +00:00
/**
* Gets the y pixel coordinate of the Fragment
*/
2022-04-19 19:59:20 +00:00
public int getY() {
2022-04-12 10:08:58 +00:00
return y;
}
/**
2022-04-19 19:59:20 +00:00
* Gets the pixel coordinates (x, y) of the Fragment as a size 2 array
2022-04-12 10:08:58 +00:00
*/
2022-04-19 19:59:20 +00:00
public int[] getPosition() {
2022-04-12 10:08:58 +00:00
int[] position = new int[2];
position[0] = x;
position[1] = y;
return position;
}
2022-04-19 19:59:20 +00:00
public void setPosition(int x, int y) {
2022-04-12 10:08:58 +00:00
this.x = x;
this.y = y;
}
2022-04-19 19:59:20 +00:00
public double getDepth() {
2022-04-12 10:08:58 +00:00
return attributes[0];
}
2022-04-19 19:59:20 +00:00
public void setDepth(double z) {
2022-04-12 10:08:58 +00:00
attributes[0] = z;
}
2022-04-19 19:59:20 +00:00
public Vector3 getNormal() {
return new Vector3(attributes[4], attributes[5], attributes[6]);
2022-04-12 10:08:58 +00:00
}
2022-04-19 19:59:20 +00:00
public void setNormal(Vector normal) {
attributes[4] = normal.get(0);
attributes[5] = normal.get(1);
attributes[6] = normal.get(2);
2022-04-12 10:08:58 +00:00
}
2022-04-19 19:59:20 +00:00
public void setNormal(double nx, double ny, double nz) {
2022-04-12 10:08:58 +00:00
attributes[4] = nx;
attributes[5] = ny;
attributes[6] = nz;
}
2022-04-19 19:59:20 +00:00
public Color getColor() {
int r = (int) Math.min(255, Math.max(255 * attributes[1], 0));
int g = (int) Math.min(255, Math.max(255 * attributes[2], 0));
int b = (int) Math.min(255, Math.max(255 * attributes[3], 0));
return new Color(r, g, b);
2022-04-12 10:08:58 +00:00
}
2022-04-19 19:59:20 +00:00
public void setColor(Color color) {
attributes[1] = color.getRed();
attributes[2] = color.getGreen();
attributes[3] = color.getBlue();
2022-04-12 10:08:58 +00:00
}
2022-04-19 19:59:20 +00:00
public void setColor(double r, double g, double b) {
2022-04-12 10:08:58 +00:00
attributes[1] = r;
attributes[2] = g;
attributes[3] = b;
}
2022-04-19 19:59:20 +00:00
public String toString() {
2022-04-12 10:08:58 +00:00
return "(" + x + "," + y + ")";
}
2022-04-19 20:25:27 +00:00
public void setVertice(Vector vector) {
attributes[9] = vector.get(0);
attributes[10] = vector.get(1);
attributes[11] = vector.get(2);
}
public Vector3 getVertice() {
return new Vector3(attributes[9], attributes[10], attributes[11]);
}
2022-04-12 10:08:58 +00:00
}