Newer
Older
EPQ-3D-renderer / src / main / java / uk / org / floop / epq3d / Texture.java
@cory cory on 1 Feb 2023 1 KB Fix #14
package uk.org.floop.epq3d;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

public class Texture {
    protected final String name;
    private final String type;
    public int[][] img;
    public Color color;

    public Texture(String _name, Color _color) {
        name = _name;
        type = "solid";
        color = _color;
    }

    public Texture(String _name, String _imgPath) {
        name = _name;
        type = "image";

        // grab file and pack it into a list[][] int (gives better performance than a BufferedImage by a _lot_
        File imgFile = new File(_imgPath);
        try {
            BufferedImage _img = ImageIO.read(imgFile);
            for (int x = 0; x < _img.getWidth(); x += 1) {
                for (int y = 0; y < _img.getHeight(); y += 1) {
                    img[x][y] = _img.getRGB(x, y);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean isSolid() {
        if (Objects.equals(type, "solid")) {
            return true;
        } else {
            return false;
        }
    }

    public Color getColor(double ang) {
        double multiplier = 1 - ang/(2*Math.PI);
        int rgb = color.getRGB();
        int r = rgb & 0xff;
        int g = (rgb >> 8) & 0xff;
        int b = (rgb >> 16) & 0xff;
        return new Color((int) (r*multiplier), (int) (g*multiplier), (int) (b*multiplier));
    }
}