Newer
Older
EPQ-3D-renderer / src / main / java / uk / org / floop / epq3d / Texture.java
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, Color _color, String _imgPath) {
        name = _name;
        type = "image";
        color = _color;
        // grab file and pack it into a list[][] int (gives better performance than a BufferedImage by a _lot_
        String path = System.getProperty("user.dir") + "/" + _imgPath;
        File imgFile = new File(path);
        try {
            BufferedImage _img = ImageIO.read(imgFile);
            img = new int[_img.getWidth()][_img.getHeight()];
            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.5 - ang/(Math.PI);
        int rgb = color.getRGB();
        int r = (rgb >> 16) & 0xff;
        int g = (rgb >> 8) & 0xff;
        int b = rgb & 0xff;
            return new Color(
                    (int) (Math.min(255, (r + 255*(multiplier-1)) * multiplier - 255*(multiplier-1))),
                    (int) (Math.min(255, (g + 255*(multiplier-1)) * multiplier - 255*(multiplier-1))),
                    (int) (Math.min(255, (b + 255*(multiplier-1)) * multiplier - 255*(multiplier-1))));
    }
    public Color getColor(double ang, int x, int y) {
        double multiplier = 1.5 - ang/(Math.PI);
        int rgb = img[x][y];
        int r = (rgb >> 16) & 0xff;
        int g = (rgb >> 8) & 0xff;
        int b = rgb & 0xff;
        return new Color(
                (int) (Math.min(255, (r + 255*(multiplier-1)) * multiplier - 255*(multiplier-1))),
                (int) (Math.min(255, (g + 255*(multiplier-1)) * multiplier - 255*(multiplier-1))),
                (int) (Math.min(255, (b + 255*(multiplier-1)) * multiplier - 255*(multiplier-1))));
    }
}