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);
	
	public static Material matte (Color  c) { return BLACK.diffuse(c); }
	public static Material matte (double k) { return matte(Color.gray(k)); }
	public static Material matte (        ) { return matte(Color.WHITE); }
	public static final Material MATTE = matte();
}
