diff --git a/.idea/libraries/Maven__com_github_sarxos_webcam_capture_0_3_12.xml b/.idea/libraries/Maven__com_github_sarxos_webcam_capture_0_3_12.xml
new file mode 100644
index 0000000..bf31b20
--- /dev/null
+++ b/.idea/libraries/Maven__com_github_sarxos_webcam_capture_0_3_12.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/Maven__com_nativelibs4java_bridj_0_7_0.xml b/.idea/libraries/Maven__com_nativelibs4java_bridj_0_7_0.xml
new file mode 100644
index 0000000..4943dee
--- /dev/null
+++ b/.idea/libraries/Maven__com_nativelibs4java_bridj_0_7_0.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_2.xml b/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_2.xml
new file mode 100644
index 0000000..7d29533
--- /dev/null
+++ b/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_2.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/EPQ 3D renderer.iml b/EPQ 3D renderer.iml
index 50eaffd..178f8b2 100644
--- a/EPQ 3D renderer.iml
+++ b/EPQ 3D renderer.iml
@@ -13,6 +13,8 @@
-
+
+
+
\ No newline at end of file
diff --git a/jsonTextures.json b/jsonTextures.json
index ca60de7..679afc7 100644
--- a/jsonTextures.json
+++ b/jsonTextures.json
@@ -1,3 +1,3 @@
{"textures": [{"name": "Dots Stroke", "type": "solid", "color": "cccccc", "imgPath": "/"}, {"name": "Material", "type": "solid", "color": "cccccc", "imgPath": "/"},
-{"name": "Material.002", "type": "image", "color": "cccccc", "imgPath": "Grass block.png"},
+{"name": "Material.002", "type": "camera", "color": "cccccc", "imgPath": "Grass block.png"},
{"name": "Material.001", "type": "image", "color": "cccccc", "imgPath": "Grass block.png"}]}
diff --git a/pom.xml b/pom.xml
index f144601..96ba067 100644
--- a/pom.xml
+++ b/pom.xml
@@ -24,5 +24,10 @@
json-simple
1.1
+
+ com.github.sarxos
+ webcam-capture
+ 0.3.12
+
diff --git a/src/main/java/uk/org/floop/epq3d/JsonReader.java b/src/main/java/uk/org/floop/epq3d/JsonReader.java
index 8ef4d7c..49566e8 100644
--- a/src/main/java/uk/org/floop/epq3d/JsonReader.java
+++ b/src/main/java/uk/org/floop/epq3d/JsonReader.java
@@ -111,6 +111,8 @@
result[i] = new Texture(JsonTexture.get("name").toString(), Color.decode('#' + (String) JsonTexture.get("color")));
} else if(Objects.equals(JsonTexture.get("type"), "image")){
result[i] = new Texture(JsonTexture.get("name").toString(), Color.decode('#' + (String) JsonTexture.get("color")), (String) JsonTexture.get("imgPath"));
+ } else if(Objects.equals(JsonTexture.get("type"), "camera")){
+ result[i] = new Texture(JsonTexture.get("name").toString());
}
}
return result;
diff --git a/src/main/java/uk/org/floop/epq3d/Object3d.java b/src/main/java/uk/org/floop/epq3d/Object3d.java
index 0d8b1c2..417aecd 100644
--- a/src/main/java/uk/org/floop/epq3d/Object3d.java
+++ b/src/main/java/uk/org/floop/epq3d/Object3d.java
@@ -31,6 +31,10 @@
faces) {
face.invalidate();
}
+ for (Texture texture:
+ textures){
+ texture.invalidate();
+ }
}
public void draw(drawData drawData, ArrayList frustumInfo) {
diff --git a/src/main/java/uk/org/floop/epq3d/ObjectCollection.java b/src/main/java/uk/org/floop/epq3d/ObjectCollection.java
index 21b8e7c..67c24c2 100644
--- a/src/main/java/uk/org/floop/epq3d/ObjectCollection.java
+++ b/src/main/java/uk/org/floop/epq3d/ObjectCollection.java
@@ -160,4 +160,4 @@
}
}
}
-}
+}
\ No newline at end of file
diff --git a/src/main/java/uk/org/floop/epq3d/Screen.java b/src/main/java/uk/org/floop/epq3d/Screen.java
index 87b6ec3..9b581df 100644
--- a/src/main/java/uk/org/floop/epq3d/Screen.java
+++ b/src/main/java/uk/org/floop/epq3d/Screen.java
@@ -141,13 +141,14 @@
drawData.lastFrameTime = System.currentTimeMillis()- lastTime;
lastTime = System.currentTimeMillis();
mainCollection.invalidate(true);
+
ArrayList FrustumInfo = new ArrayList<>();
for(int i = 0; i<6; i += 1){FrustumInfo.add(i);}
mainCollection.draw(drawData, FrustumInfo);
// DEBUG DRAWING
{
//debugImg.getGraphics().drawString(Math.round(1000 / (float) (System.currentTimeMillis() - lastTime)) + " fps", 10, 10);
- drawData.debugImg.getGraphics().drawString("FrameTime: " + (System.currentTimeMillis() - lastTime) + " millis", 10, 10);
+ drawData.debugImg.getGraphics().drawString("FrameTime: " + (drawData.lastFrameTime) + " millis", 10, 10);
if(drawData.debugHud) {
drawData.debugImg.getGraphics().drawString("fpPos: " +
String.format("%.2f", player.FPWorldPos.x) + " " +
@@ -182,6 +183,7 @@
String.format("%.2f", drawData.frustumPlanes[i].normalVector.z), 10, 170 + 20 * i);
}
}
+ long lastTime1 = System.currentTimeMillis();
// write debug overlay
for(int x = 0; x < drawData.scrX; x +=1){
for(int y = 0; y < drawData.scrY; y += 1){
@@ -193,33 +195,17 @@
}
}
g.drawImage(drawData.bufImg, 0, 0, this);
- g.drawImage(drawData.debugImg, 0, 0, this);
- for(int x = 0; x < drawData.scrX; x +=1){
- for(int y = 0; y < drawData.scrY; y += 1){
- drawData.debugImg.setRGB(x, y, 0);
- }
- }
+// g.drawImage(drawData.debugImg, 0, 0, this);
+// for(int x = 0; x < drawData.scrX; x +=1){
+// for(int y = 0; y < drawData.scrY; y += 1){
+// drawData.debugImg.setRGB(x, y, 0);
+// }
+// }
+ System.out.println(System.currentTimeMillis() - lastTime1);
}
//System.out.println(drawData.timeSpentChoppingFaces + ", " + drawData.timeSpentDrawingNormalFaces);
drawData.timeSpentChoppingFaces = 0;
drawData.timeSpentDrawingNormalFaces = 0;
-
-// HTTPPost post = new HTTPPost();
-// // json WRITING
-// json = new JsonWriter();
-// Point3D[] vertexList;
-// int[][] faceList;
-//
-// // camera object
-// vertexList = new Point3D[5];
-// faceList = new int[][]{{0, 1, 2, 3}};
-// for (int i = 0; i < 4; i += 1) {
-// vertexList[i] = player.ScreenCornerPosS[i].getRotatedPoint();
-// }
-// vertexList[4] = player.FPWorldPos;
-// json.addBlenderObject(vertexList, faceList);
-//
-// post.invokePost(json);
}
@Override
diff --git a/src/main/java/uk/org/floop/epq3d/Texture.java b/src/main/java/uk/org/floop/epq3d/Texture.java
index 5d256f9..62ceadd 100644
--- a/src/main/java/uk/org/floop/epq3d/Texture.java
+++ b/src/main/java/uk/org/floop/epq3d/Texture.java
@@ -1,5 +1,7 @@
package uk.org.floop.epq3d;
+import com.github.sarxos.webcam.Webcam;
+
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
@@ -8,11 +10,13 @@
import java.util.Objects;
public class Texture {
+ boolean valid = false;
+ int index = 0;
protected final String name;
private final String type;
- public int[][] img;
+ public simpleImage image;
public Color color;
-
+ public Webcam camera;
public Texture(String _name, Color _color) {
name = _name;
type = "solid";
@@ -27,18 +31,35 @@
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);
- }
- }
+ 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;
@@ -59,14 +80,21 @@
(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;
+// 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, (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))));
+ (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))));
}
+
}
diff --git a/src/main/java/uk/org/floop/epq3d/Triangle.java b/src/main/java/uk/org/floop/epq3d/Triangle.java
index f05ae28..0ede463 100644
--- a/src/main/java/uk/org/floop/epq3d/Triangle.java
+++ b/src/main/java/uk/org/floop/epq3d/Triangle.java
@@ -338,11 +338,6 @@
(x - startPoint.x)* xzGradient +
(y - startPoint.y)* yzGradient));
int newZ = (int)(2147483647d/(zVal + 1));
-// if (newZ == 2147483647){
-// System.out.println((1/startPoint.z +
-// (x - startPoint.x)*xzGradient +
-// (y - startPoint.y)*yzGradient));
-// }
// if the new Z value is greater than the existing Z value on the buffer, the new pixel is calculated and drawn
if(drawData.zBuf[x][y] == 0 ||
newZ > drawData.zBuf[x][y]){ //newZ > zBuf.getRGB(x, y) ||
@@ -352,8 +347,8 @@
if(isTextured){
double[] pos = textureMappingMatrix.multiplyPoint2raw(x, y);
pixColor = texture.getColor(ang,
- Math.floorMod((int)(pos[0]*texture.img.length), texture.img.length),
- Math.floorMod(-1-(int)(pos[1]*texture.img[0].length), texture.img[0].length)
+ Math.floorMod((int)(pos[0]*texture.image.getWidth()), texture.image.getWidth()),
+ Math.floorMod(-1-(int)(pos[1]*texture.image.getHeight()), texture.image.getHeight())
);
} else {
pixColor = texture.getColor(ang);
diff --git a/src/main/java/uk/org/floop/epq3d/drawData.java b/src/main/java/uk/org/floop/epq3d/drawData.java
index f3b618c..e718921 100644
--- a/src/main/java/uk/org/floop/epq3d/drawData.java
+++ b/src/main/java/uk/org/floop/epq3d/drawData.java
@@ -31,6 +31,7 @@
// player
public double playerSpeed;
public double playerSprintModifier;
+ public simpleImage simImg;
// draw onto
BufferedImage bufImg;
public int[][] drawImg;
diff --git a/src/main/java/uk/org/floop/epq3d/simpleImage.java b/src/main/java/uk/org/floop/epq3d/simpleImage.java
index ec5d854..0d70f6e 100644
--- a/src/main/java/uk/org/floop/epq3d/simpleImage.java
+++ b/src/main/java/uk/org/floop/epq3d/simpleImage.java
@@ -1,20 +1,41 @@
package uk.org.floop.epq3d;
import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.awt.image.DataBufferByte;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
public class simpleImage extends Image {
+ private int width;
+ private int height;
+ private byte[] data;
+ public simpleImage(BufferedImage img){
+ //System.out.println(img.getType());
+ width = img.getWidth();
+ height = img.getHeight();
+ data = ((DataBufferByte)img.getRaster().getDataBuffer()).getData() ;
+ }
+ public simpleImage(int x, int y){
+ //System.out.println(img.getType());
+ width = x;
+ height = y;
+ data = new byte[x*y*3];
+ }
@Override
public int getWidth(ImageObserver observer) {
- return 0;
+ return width;
}
-
+ public int getWidth(){
+ return width;
+ }
@Override
public int getHeight(ImageObserver observer) {
- return 0;
+ return height;
}
-
+ public int getHeight(){
+ return height;
+ }
@Override
public ImageProducer getSource() {
return null;
@@ -29,4 +50,44 @@
public Object getProperty(String name, ImageObserver observer) {
return null;
}
+ public int[] getPixel(int x, int y){
+ return new int[]{
+ Byte.toUnsignedInt(data[3*(y*width + x)]),
+ Byte.toUnsignedInt(data[3*(y*width + x)+1]),
+ Byte.toUnsignedInt(data[3*(y*width + x)+2])};
+ }
+ public int getRGB(int x, int y){
+ int result =
+ Byte.toUnsignedInt(data[(3 * y * width + x)])|
+ Byte.toUnsignedInt(data[(3 * y * width + x)+1]) << 8|
+ Byte.toUnsignedInt(data[(3 * y * width + x)+2]) << 16;
+ if(result != 0){
+ int debug = 1;
+ }
+ return result;
+ }
+
+ public void setRGB(int x, int y, int i) {
+ data[(3 * y * width + x)] = (byte) (i & 0xff);
+ data[(3*y*width + x)+1] = (byte) ((i >> 8) & 0xff);
+ data[(3*y*width + x)+2] = (byte) ((i >> 16) & 0xff);
+ }
+ public BufferedImage getBufferedImage(){
+// try {
+// BufferedImage result = ImageIO.read(new ByteArrayInputStream(data));
+// return result;
+// } catch (IOException e) {
+// throw new RuntimeException(e);
+// }
+ BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+ for(int x = 0; x < width; x += 1){
+ for(int y = 0; y < height; y += 1){
+ result.setRGB(x, y,
+ data[3*(y*width + x)]+
+ data[3*(y*width + x)+1]<<8+
+ data[3*(y*width + x)+2]<<16);
+ }
+ }
+ return result;
+ }
}
diff --git a/src/main/java/uk/org/floop/epq3d/testing.java b/src/main/java/uk/org/floop/epq3d/testing.java
index 8066e9a..2a30ed4 100644
--- a/src/main/java/uk/org/floop/epq3d/testing.java
+++ b/src/main/java/uk/org/floop/epq3d/testing.java
@@ -1,7 +1,16 @@
package uk.org.floop.epq3d;
+
+import com.github.sarxos.webcam.Webcam;
+
+import java.awt.image.BufferedImage;
+
class testing {
public static void main(String[] args) {
- System.out.println(System.getProperty("user.dir"));
+ Webcam webcam = Webcam.getDefault();
+ webcam.open();
+
+ BufferedImage image = webcam.getImage();
+ int x = 0;
}
}