Add diffuseImage Material
This commit is contained in:
parent
edf8d251ed
commit
110e85827e
|
@ -1,10 +1,26 @@
|
|||
package xyz.marsavic.gfxlab.graphics3d;
|
||||
|
||||
import xyz.marsavic.geometry.Vector;
|
||||
import xyz.marsavic.gfxlab.Color;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public record Material(
|
||||
Color diffuse
|
||||
) {
|
||||
public static Material diffuseImage(BufferedImage img, Vector uv) {
|
||||
int x = (int) (uv.x()*img.getWidth()) % img.getWidth();
|
||||
if (x < 0) {
|
||||
x += img.getWidth(); // adding in case of negative modulo
|
||||
}
|
||||
|
||||
int y = (int) (uv.y()*img.getWidth()) % img.getWidth();
|
||||
if (y < 0) {
|
||||
y += img.getHeight();
|
||||
}
|
||||
y = img.getHeight()-y-1; // inverting y, BufferedImage is encoded from top to bottom, and uv mapping is bottom to top
|
||||
return new Material(Color.code(img.getRGB(x, y)));
|
||||
}
|
||||
public Material diffuse(Color diffuse) { return new Material(diffuse); }
|
||||
|
||||
public static final Material BLACK = new Material(Color.BLACK);
|
||||
|
|
Loading…
Reference in a new issue