2022-04-12 10:08:58 +00:00
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.image.*;
|
|
|
|
import java.io.*;
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 2D Texture class.
|
|
|
|
*/
|
|
|
|
public class Texture {
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
BufferedImage image;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new Texture with the content of the image at @path.
|
2022-04-19 08:25:47 +00:00
|
|
|
*/
|
|
|
|
public Texture(String path) throws Exception {
|
|
|
|
image = ImageIO.read(new File(path));
|
|
|
|
width = image.getWidth();
|
|
|
|
height = image.getHeight();
|
2022-04-12 10:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-19 08:25:47 +00:00
|
|
|
* Samples the texture at texture coordinates (u,v), using nearest neighboor
|
|
|
|
* interpolation
|
2022-04-12 10:08:58 +00:00
|
|
|
* u and v and wrapped around to [0,1].
|
2022-04-19 08:25:47 +00:00
|
|
|
*/
|
|
|
|
public Color sample(double u, double v) {
|
2022-04-12 10:08:58 +00:00
|
|
|
|
2022-04-19 09:02:07 +00:00
|
|
|
double x = u * width;
|
|
|
|
double y = v * height;
|
2022-04-12 10:08:58 +00:00
|
|
|
|
2022-04-19 09:02:07 +00:00
|
|
|
int px = (int) Math.floor(x);
|
|
|
|
int py = (int) Math.floor(y);
|
|
|
|
|
|
|
|
Color couleur = new Color(image.getRGB(px, py));
|
|
|
|
|
|
|
|
return couleur;
|
2022-04-12 10:08:58 +00:00
|
|
|
}
|
|
|
|
}
|