Add diffuseImage Material

This commit is contained in:
Petar Kapriš 2022-12-20 14:57:38 +01:00 committed by Петар Каприш
parent edf8d251ed
commit 110e85827e

View file

@ -1,10 +1,26 @@
package xyz.marsavic.gfxlab.graphics3d; package xyz.marsavic.gfxlab.graphics3d;
import xyz.marsavic.geometry.Vector;
import xyz.marsavic.gfxlab.Color; import xyz.marsavic.gfxlab.Color;
import java.awt.image.BufferedImage;
public record Material( public record Material(
Color diffuse 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 Material diffuse(Color diffuse) { return new Material(diffuse); }
public static final Material BLACK = new Material(Color.BLACK); public static final Material BLACK = new Material(Color.BLACK);