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 int color; public double randomValue; // value which randomises the value (brightness) of the colour for each face. public Webcam camera; public Texture(String _name, Color _color, double _randomValue) { name = _name; type = "solid"; color = _color.getRGB(); randomValue = _randomValue; } public Texture(String _name, Color _color, String _imgPath) { name = _name; type = "image"; color = _color.getRGB(); // 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(drawData drawData){ if(Objects.equals(type, "camera") && !drawData.texturesDisabled){ index += 1; if(index == 10){ nextFrame(); 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 int getColor(double ang, double random) { // the multiplier changes the brightness of the face. It is dependent on the angle of the face, and a random value, which is applied to individual faces double multiplier = (1.5 - ang/(Math.PI))+random*randomValue; int r = (color >> 16) & 0xff; int g = (color >> 8) & 0xff; int b = color & 0xff; int result = 255 << 24; result = result | (int) (Math.min(255, (r + 255*(multiplier-1)) * multiplier - 255*(multiplier-1)))<<16; result = result | (int) (Math.min(255, (g + 255*(multiplier-1)) * multiplier - 255*(multiplier-1)))<<8; result = result | (int) (Math.min(255, (b + 255*(multiplier-1)) * multiplier - 255*(multiplier-1))); return result; } public int 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 result = 255 << 24; result = result | (int) (Math.min(255, (rgb[0] + 255*(multiplier-1)) * multiplier - 255*(multiplier-1)))<<16; result = result | (int) (Math.min(255, (rgb[1] + 255*(multiplier-1)) * multiplier - 255*(multiplier-1)))<<8; result = result | (int) (Math.min(255, (rgb[2] + 255*(multiplier-1)) * multiplier - 255*(multiplier-1))); return result; } }