diff --git a/config.json b/config.json index c5d5b6a..108103e 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,7 @@ { "__comment":"Values in pixels", -"screenWidth":900, -"screenHeight":600, +"screenWidth":1920, +"screenHeight":1080, "maxFPS":60, "FOV":100, "playerSpeed":2.0, diff --git a/jsonTextures.json b/jsonTextures.json index 24b98b7..a932656 100644 --- a/jsonTextures.json +++ b/jsonTextures.json @@ -7,13 +7,13 @@ {"name": "Dots Stroke", "type": "solid", "color": "cccccc", "imgPath": "/", "randomValue": 0.1}, {"name": "Grass03", "type": "solid", "color": "49cc07", "imgPath": "/", "randomValue": 0.1}, {"name": "Ground Leaves 01", "type": "solid", "color": "09570b", "imgPath": "/", "randomValue": 0.1}, -{"name": "Material", "type": "solid", "color": "0a7c7c", "imgPath": "/", "randomValue": 0.1}, -{"name": "Material.001", "type": "solid", "color": "cccccc", "imgPath": "/", "randomValue": 0.1}, +{"name": "Material", "type": "solid", "color": "0a7c7c", "imgPath": "/", "randomValue": 0.0}, +{"name": "Material.001", "type": "solid", "color": "cccccc", "imgPath": "/", "randomValue": 0.0}, {"name": "Trunk", "type": "solid", "color": "330a01", "imgPath": "/", "randomValue": 0.1}, {"name": "twig.008", "type": "solid", "color": "cc9248", "imgPath": "/", "randomValue": 0.1}, {"name": "Water", "type": "solid", "color": "1027cc", "imgPath": "/", "randomValue": 0.1}, {"name": "White", "type": "solid", "color": "cccccc", "imgPath": "/", "randomValue": 0.0}, -{"name": "Window", "type": "solid", "color": "2c2c2c", "imgPath": "/", "randomValue": 0.1}, +{"name": "Window", "type": "solid", "color": "2c2c2c", "imgPath": "/", "randomValue": 0.0}, {"name": "Wood Planks with Nails", "type": "solid", "color": "5f1806", "imgPath": "/", "randomValue": 0.1}, {"name": "Wood Planks with Nails.002", "type": "solid", "color": "9c3007", "imgPath": "/", "randomValue": 0.1}, {"name": "Wood Planks with Nails.003", "type": "solid", "color": "771c06", "imgPath": "/", "randomValue": 0.1}]} diff --git a/src/main/java/uk/org/floop/epq3d/Face.java b/src/main/java/uk/org/floop/epq3d/Face.java index 8d99399..dd1b2cb 100644 --- a/src/main/java/uk/org/floop/epq3d/Face.java +++ b/src/main/java/uk/org/floop/epq3d/Face.java @@ -116,7 +116,6 @@ // project all points in triangle tris[tri_i].draw(drawData, ang, randomValue); } else if (numOfInvalidPoints == 1) {// if one point is invalid, interpolate 2 new points and draw 2 new triangles: - // find which point is the invalid one TODO could optimise and find this beforehand int invalidIndex = -1; for (int i = 0; i < 3; i += 1) { if (points[trisFaceList[tri_i][i]].getRotatedPoint().z < 0.001) { @@ -136,7 +135,8 @@ // interpolate between oldPoint1 and invalidPoint newPoint1 = interpolate(drawData, frustumInfo, oldPoint1, invalidPoint); newPoint1.setProjectedPoint(drawData); - // interpolate between oldPoint2 and invalid point + + // interpolate between oldPoint2 and invalid point newPoint2 = interpolate(drawData, frustumInfo, oldPoint2, invalidPoint); newPoint2.setProjectedPoint(drawData); @@ -168,10 +168,12 @@ // interpolate for z = 0.001 between invalid1 and oldPoint newPoint1 = interpolate(drawData, frustumInfo, oldPoint, invalidPoint1); newPoint1.setProjectedPoint(drawData); + // interpolate for z = 0.001 between invalid2 and oldPoint newPoint2 = interpolate(drawData, frustumInfo, oldPoint, invalidPoint2); newPoint2.setProjectedPoint(drawData); + // project points, set their z values, and then draw triangles { // create and draw new triangle @@ -188,27 +190,28 @@ } private PointComp interpolate(drawData drawData, ArrayList frustumInfo, PointComp oldPoint, PointComp invalidPoint){ // // calculate distance to each of the frustum planes. Then store the minimum distance from each of -// double minDis = 0; -// int planeMin = 0; -// for (int planeId: -// frustumInfo) { -// // don't check the near plane -// if(planeId != 0) { -// double dist = drawData.frustumPlanes[planeId].getDistance(invalidPoint.getRotatedPoint()); -// if (minDis > dist) { -// minDis = dist; -// planeMin = planeId; -// } -// } -// } + double minDis = 0; + int planeMin = 0; + for (int planeId: + frustumInfo) { + // don't check the near plane + if(planeId != 0) { + double dist = drawData.frustumPlanes[planeId].getDistance(invalidPoint.getRotatedPoint()); + if (minDis > dist) { + minDis = dist; + planeMin = planeId; + } + } + } double interpolateZVal = 0.1; -// if(minDis < 0){ + if(minDis < -1){ + interpolateZVal = 0.1; // Vector3D oldNew = new Vector3D(oldPoint.getRotatedPoint(), invalidPoint.getRotatedPoint()); // double pointDis = oldNew.getLength(); -// double fraction = -minDis / pointDis; +// x double fraction = -minDis / pointDis; // double newZ = (oldPoint.getRotatedPoint().z - invalidPoint.getRotatedPoint().z)*fraction - invalidPoint.getRotatedPoint().z; // interpolateZVal = Math.max(interpolateZVal, newZ); -// } + } double gradX = (oldPoint.getRotatedPoint().z - invalidPoint.getRotatedPoint().z) / (oldPoint.getRotatedPoint().x - invalidPoint.getRotatedPoint().x); double gradY = (oldPoint.getRotatedPoint().z - invalidPoint.getRotatedPoint().z) / diff --git a/src/main/java/uk/org/floop/epq3d/Screen.java b/src/main/java/uk/org/floop/epq3d/Screen.java index e543406..16134c7 100644 --- a/src/main/java/uk/org/floop/epq3d/Screen.java +++ b/src/main/java/uk/org/floop/epq3d/Screen.java @@ -127,20 +127,26 @@ } } long drawTime = System.nanoTime(); + + //DataBuffer db = drawData.bufImg.getRaster().getDataBuffer(); for(@Parallel int x = 0; x < drawData.scrX; x +=1){ + //drawData.bufImg.setRGB(x, 0, 1, drawData.scrY, drawData.drawImg[x], 0, 1); for(@Parallel int y = 0; y < drawData.scrY; y += 1){ - drawData.bufImg.setRGB(x, y, drawData.drawImg[x][y]); - drawData.drawImg[x][y] = 0; + //db.setElem(y* drawData.scrX + x, drawData.drawImg[x][y]); + //drawData.bufImg.setRGB(x, y, drawData.drawImg[x][y]); + //drawData.drawImg[x][y] = 0; drawData.zBuf[x][y] = 0; } } g.drawImage(drawData.bufImg, 0, 0, this); + drawData.bufImg.getGraphics().fillRect(0,0,drawData.scrX, drawData.scrY); + drawTime = System.nanoTime() - drawTime; long current = System.nanoTime(); System.out.println("Frame: " + "total: " + (current - lastTime)/1000000d - + " drawing: " + (current - drawTime)/1000000d + + " drawing: " + (drawTime)/1000000d + " chopping: " + drawData.timeSpentChoppingFaces - + " tris : " + drawData.timeSpentDrawingTris + + " tris : " + drawData.timeSpentDrawingNormalFaces + " pixels : " + drawData.timeSpentGettingPixels/1000000d ); System.out.print("TriBreakdown: "); diff --git a/src/main/java/uk/org/floop/epq3d/Texture.java b/src/main/java/uk/org/floop/epq3d/Texture.java index 6c25809..0df48ee 100644 --- a/src/main/java/uk/org/floop/epq3d/Texture.java +++ b/src/main/java/uk/org/floop/epq3d/Texture.java @@ -15,20 +15,20 @@ protected final String name; private final String type; public simpleImage image; - public Color color; + 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; + color = _color.getRGB(); randomValue = _randomValue; } public Texture(String _name, Color _color, String _imgPath) { name = _name; type = "image"; - color = _color; + 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); @@ -61,7 +61,6 @@ if(newImg != null){ image = new simpleImage(newImg); } - } public boolean isSolid() { if (Objects.equals(type, "solid")) { @@ -71,19 +70,19 @@ } } - public Color getColor(double ang, double random) { + 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 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)))); + 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 Color getColor(double ang, int x, int y) { + public int getColor(double ang, int x, int y) { // if(!valid && Objects.equals(type, "camera")){ // nextFrame(); // valid = true; @@ -91,14 +90,11 @@ 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)))); + 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; } } diff --git a/src/main/java/uk/org/floop/epq3d/Triangle.java b/src/main/java/uk/org/floop/epq3d/Triangle.java index 6159b40..2c58db6 100644 --- a/src/main/java/uk/org/floop/epq3d/Triangle.java +++ b/src/main/java/uk/org/floop/epq3d/Triangle.java @@ -114,7 +114,7 @@ while(x-1 == point1[0]) { if((LineLong.isDrawn || drawData.drawLines) && // draw line pixels if needed, and on screen point1[0] > 0 && point1[1] > 0 && point1[0] < drawData.scrX && point1[1] < drawData.scrY){ - drawData.drawImg[point1[0]][point1[1]] = Color.HSBtoRGB(0, 1, 1); + drawData.drawImg.setElem(point1[0] + point1[1]*drawData.scrX, Color.HSBtoRGB(0, 1, 1)); } try { point1 = LineLong.nextPix(); @@ -128,7 +128,7 @@ try{ if((LineA.isDrawn || drawData.drawLines) && // draw line pixels if needed, and on screen point2[0] > 0 && point2[1] > 0 && point2[0] < drawData.scrX && point2[1] < drawData.scrY){ - drawData.drawImg[point2[0]][point2[1]] = Color.HSBtoRGB(0, 1, 1); + drawData.drawImg.setElem(point2[0] + point2[1]*drawData.scrX, Color.HSBtoRGB(0, 1, 1)); } point2 = LineA.nextPix(); } @@ -139,7 +139,7 @@ else { if((LineB.isDrawn || drawData.drawLines) && // draw line pixels if needed, and on screen point2[0] > 0 && point2[1] > 0 && point2[0] < drawData.scrX && point2[1] < drawData.scrY){ - drawData.drawImg[point2[0]][point2[1]] = Color.HSBtoRGB(0, 1, 1); + drawData.drawImg.setElem(point2[0] + point2[1]*drawData.scrX, Color.HSBtoRGB(0, 1, 1)); } point2 = LineB.nextPix(); } @@ -335,36 +335,39 @@ return true; } private void drawPix(drawData drawData, int x, int y, double ang, double random){ - ///long nanotime = System.nanoTime(); + //long nanotime = System.nanoTime(); // a value of 0 represents a value which is on the camera, at z=0. // not the best mapping but it's probably good enough - double zVal = 1/((1/startPoint.z + + double zVal = ((1/startPoint.z + (x - startPoint.x)* xzGradient + (y - startPoint.y)* yzGradient)); - int newZ = (int)(2147483647d/(zVal + 1)); + //int newZ = (int)(2147483647d/(zVal + 1)); + int newZ = (int)(zVal * 16384); // 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] || drawData.zBufferDisabled){ drawData.zBuf[x][y] = newZ; // project result - Color pixColor; + int pixColor; if(isTextured){ double[] pos = textureMappingMatrix.multiplyPoint2raw(x, y); - long nanoTime = System.nanoTime(); - pixColor = texture.getColor(ang, - Math.floorMod((int)(pos[0]*texture.image.getWidth()), texture.image.getWidth()), - Math.floorMod(-1-(int)(pos[1]*texture.image.getHeight()), texture.image.getHeight()) - ); - drawData.timeSpentGettingPixels += System.nanoTime()-nanoTime; + //pixColor = texture.getColor(ang, 0, 0); + pixColor = texture.getColor(ang, (int)(pos[0]*1920), 1080-(int)(pos[1]*1080)); +// Math.floorMod((int)(pos[0]*texture.image.getWidth()), texture.image.getWidth()), +// Math.floorMod(-1-(int)(pos[1]*texture.image.getHeight()), texture.image.getHeight()) + //); + //drawData.timeSpentGettingPixels += System.nanoTime()-nanoTime; } else { pixColor = texture.getColor(ang, random); } if(drawData.drawZBuffer){ - drawData.drawImg[x][y] = Color.getHSBColor(0, 0, (float)newZ/2147483648f + 0.5f).getRGB(); + drawData.drawImg.setElem(x + y*drawData.scrX, Color.getHSBColor(0, 0, (float)newZ/2147483648f + 0.5f).getRGB()); + //drawData.drawImg[x][y] = Color.getHSBColor(0, 0, (float)newZ/2147483648f + 0.5f).getRGB(); } else{ - drawData.drawImg[x][y] = pixColor.getRGB(); + drawData.drawImg.setElem(x + y*drawData.scrX, pixColor); + //drawData.drawImg[x][y] = pixColor; } } - //drawData.trisTimeList[3] += System.nanoTime() - nanotime; + //terdrawData.trisTimeList[3] += System.nanoTime() - nanotime; } } diff --git a/src/main/java/uk/org/floop/epq3d/drawData.java b/src/main/java/uk/org/floop/epq3d/drawData.java index 7b93638..7878749 100644 --- a/src/main/java/uk/org/floop/epq3d/drawData.java +++ b/src/main/java/uk/org/floop/epq3d/drawData.java @@ -6,6 +6,7 @@ import org.json.simple.parser.ParseException; import java.awt.image.BufferedImage; +import java.awt.image.DataBuffer; import java.io.FileReader; import java.io.IOException; import java.util.Objects; @@ -35,7 +36,7 @@ public simpleImage simImg; // draw onto BufferedImage bufImg; - public int[][] drawImg; + public DataBuffer drawImg; public int[][] zBuf; public BufferedImage debugImg; @@ -85,7 +86,7 @@ } // init varaibles bufImg = new BufferedImage(scrX, scrY, BufferedImage.TYPE_INT_ARGB); - drawImg = new int[scrX][scrY]; + drawImg = bufImg.getRaster().getDataBuffer();//new int[scrX][scrY]; zBuf = new int[scrX][scrY]; debugImg = new BufferedImage(scrX, scrY, BufferedImage.TYPE_INT_ARGB); debugImg.createGraphics(); diff --git a/src/main/java/uk/org/floop/epq3d/testing.java b/src/main/java/uk/org/floop/epq3d/testing.java index 2a30ed4..78e77f4 100644 --- a/src/main/java/uk/org/floop/epq3d/testing.java +++ b/src/main/java/uk/org/floop/epq3d/testing.java @@ -1,16 +1,31 @@ package uk.org.floop.epq3d; -import com.github.sarxos.webcam.Webcam; +//import static java.lang.FdLibm.Exp.compute; +// Java code for thread creation by extending +// the Thread class +class MultithreadingDemo extends Thread { + public void run() + { + try { + // Displaying the thread that is running + System.out.println( + "Thread " + Thread.currentThread().getId() + + " is running"); + } + catch (Exception ignored){} + } +} -import java.awt.image.BufferedImage; - -class testing { - public static void main(String[] args) { - Webcam webcam = Webcam.getDefault(); - webcam.open(); - - BufferedImage image = webcam.getImage(); - int x = 0; +// Main Class +public class testing { + public static void main(String[] args) + { + int n = 8; // Number of threads + for (int i = 0; i < n; i++) { + MultithreadingDemo object + = new MultithreadingDemo(); + object.start(); + } } }