Newer
Older
EPQ-3D-renderer / src / main / java / uk / org / floop / epq3d / Texture.java
package uk.org.floop.epq3d;

import com.github.sarxos.webcam.Webcam;

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 {
    boolean valid = false;
    int index = 0;
    protected final String name;
    private final String type;
    public simpleImage image;
    public Color color;
    public Webcam camera;
    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 {
            image = new simpleImage(ImageIO.read(imgFile));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public Texture(String _name){
        name = _name;
        type = "camera";
        camera = Webcam.getDefault();
        camera.open(true);
    }
    public void invalidate(){
        index += 1;
        if(index == 10){
            nextFrame();
            index = 0;
        }
//        if(index < 0) {
//        }
        valid = true;

    }
    public void nextFrame(){
        BufferedImage newImg = camera.getImage();
        if(newImg != null){
            image = new simpleImage(newImg);
        }

    }
    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) {
//        if(!valid && Objects.equals(type, "camera")){
//            nextFrame();
//            valid = true;
//        }
        double multiplier = 1.5 - ang / (Math.PI);
        int[] rgb;
        rgb = image.getPixel(x, y);
        ;
//        int r = (rgb >> 16) & 0xff;
//        int g = (rgb >> 8) & 0xff;
//        int b = rgb & 0xff;
        return new Color(
                (int) (Math.min(255, (rgb[0] + 255 * (multiplier - 1)) * multiplier - 255 * (multiplier - 1))),
                (int) (Math.min(255, (rgb[1] + 255 * (multiplier - 1)) * multiplier - 255 * (multiplier - 1))),
                (int) (Math.min(255, (rgb[2] + 255 * (multiplier - 1)) * multiplier - 255 * (multiplier - 1))));
    }

}