diff --git a/blenderJSON.py b/blenderJSON.py index d3969ef..b586b28 100644 --- a/blenderJSON.py +++ b/blenderJSON.py @@ -28,24 +28,33 @@ return[vec[0], vec[1], vec[2]] def vec2ToList(vec): return[vec[0], vec[1]] -jasonDict = { +jsonCollections = { "collections":{}, +} +jsonTextures = { "textures":{}, } - MainCollection = 0 collections = bpy.data.collections for collection in collections: if collection.name == "MainCollection": MainCollection = collection break -jasonDict["collections"] = [separateCollection(MainCollection)] -jasonDict["textures"] = [{ +jsonCollections["collections"] = [separateCollection(MainCollection)] +jsonTextures["textures"] = [{ "name":material.name, "type":"solid", -"color":"5555ff" -} for material in bpy.data.materials] -file = open("/home/cory/IdeaProjects/testing/testingJSON.json", "w") -file.write(json.dumps(jasonDict)) +"color": + f'{(int)(255*material.diffuse_color[0]):02x}'+ + f'{(int)(255*material.diffuse_color[1]):02x}'+ + f'{(int)(255*material.diffuse_color[2]):02x}', #[material.diffuse_color[0], material.diffuse_color[1], material.diffuse_color[2], material.diffuse_color[3]], +"imgPath":"/" +} + for material in bpy.data.materials] +file = open("/home/cory/IdeaProjects/testing/jsonCollections.json", "w") +file.write(json.dumps(jsonCollections)) +file.close() +file = open("/home/cory/IdeaProjects/testing/jsonTextures.json", "w") +file.write(json.dumps(jsonTextures)) file.close() diff --git a/config.json b/config.json index 28dae13..3a39d53 100644 --- a/config.json +++ b/config.json @@ -13,6 +13,6 @@ "drawLines":"disabled", "overrideBackFaceCulling":"disabled", "frustumCullingOverridePercent":0, - "drawZBuffer":"enabled" + "drawZBuffer":"disabled" } } diff --git a/src/main/java/uk/org/floop/epq3d/Face.java b/src/main/java/uk/org/floop/epq3d/Face.java index e9f1a3a..ea4214c 100644 --- a/src/main/java/uk/org/floop/epq3d/Face.java +++ b/src/main/java/uk/org/floop/epq3d/Face.java @@ -130,9 +130,6 @@ // interpolate between oldPoint1 and invalidPoint newPoint1 = interpolate(drawData, frustumInfo, oldPoint1, invalidPoint); newPoint1.setProjectedPoint(drawData); - if (newPoint1.getProjectedPoint().x == 0){ - int p = 0; - } // interpolate between oldPoint2 and invalid point newPoint2 = interpolate(drawData, frustumInfo, oldPoint2, invalidPoint); newPoint2.setProjectedPoint(drawData); @@ -187,20 +184,32 @@ } 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 interpolateZval = 0.001; + 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; + } + } + } + /* if the minimum distance is less than zero: + Find the z difference between oldPoint and invalidPoint + Find the distance from the oldPoint to the plane with that value + Add this distance to the minDis we found earlier + divide minDis by this number + multiply by the Z difference between oldPoint and invalidPoint + */ + double interpolateZval = 0.01; + if(minDis < 0 && false){ + interpolateZval = Math.max(interpolateZval, + (oldPoint.getRotatedPoint().z - invalidPoint.getRotatedPoint().z)*minDis/ + (drawData.frustumPlanes[planeMin].getDistance(oldPoint.getRotatedPoint()) + minDis)); + } double gradX = (oldPoint.getRotatedPoint().z - invalidPoint.getRotatedPoint().z) / (oldPoint.getRotatedPoint().x - invalidPoint.getRotatedPoint().x); double gradY = (oldPoint.getRotatedPoint().z - invalidPoint.getRotatedPoint().z) / @@ -504,29 +513,9 @@ perspectiveMappingMatrices[i][0] = uvTo00.getInverse().multiplyGetResult(faceTo00); i += 1; } - /* - System.out.println("UVPoints:###########################"); - //DEBUG - printout the positions of old uv coords and new uv coords - Point3D newPoint = new Point3D(); - for(int i = 0; i < 3; i+=1){ - newPoint.set(uvTo00.multiplyPoint3raw(UVPoints[tri[i]].x, UVPoints[tri[i]].y, 0)); - System.out.println(i + ": " + "xyzOld: " + - UVPoints[tri[i]].x + ", " + UVPoints[tri[i]].y + ", " + 0 + ", new:" + - newPoint.x + ", " + newPoint.y + ", " + newPoint.z); - } - System.out.println("FacePoints:###########################"); - //DEBUG - printout the positions of old face coords and new face coords - Point3D newPoint = new Point3D(); - for (int i = 0; i < 3; i += 1) { - newPoint.set(faceTo00.multiplyPoint3raw(points[tri[i]].point.x, points[tri[i]].point.y, points[tri[i]].point.z)); - System.out.println(i + ": " + "xyzOld: " + - points[tri[i]].point.x + ", " + points[tri[i]].point.y + ", " + points[tri[i]].point.z + ", new:" + - newPoint.x + ", " + newPoint.y + ", " + newPoint.z); - } - */ } // OPTIMIZATION - remove the z row from the matrix, since z output should always be zero. - // should decrease processing time of each pixel by a lot + // should decrease processing time of each pixel } private void bakePerspectiveMatrices(drawData drawData) { diff --git a/src/main/java/uk/org/floop/epq3d/Screen.java b/src/main/java/uk/org/floop/epq3d/Screen.java index 373a5f6..6d3d851 100644 --- a/src/main/java/uk/org/floop/epq3d/Screen.java +++ b/src/main/java/uk/org/floop/epq3d/Screen.java @@ -185,11 +185,9 @@ // write debug overlay for(int x = 0; x < drawData.scrX; x +=1){ for(int y = 0; y < drawData.scrY; y += 1){ - if(drawData.drawZBuffer){ - drawData.bufImg.setRGB(x, y, Color.getHSBColor(0, 0, (float)drawData.zBuf[x][y]/2147483648f).getRGB()); - } else { - drawData.bufImg.setRGB(x, y, drawData.drawImg[x][y]); - } + //drawData.bufImg.setRGB(x, y, Color.getHSBColor(0, 0, (float)drawData.zBuf[x][y]/2147483648f).getRGB()); + //drawData.bufImg.setRGB(x, y, Color.getHSBColor((float)drawData.zBuf[x][y]/2147483648f, 1, 1).getRGB()); + drawData.bufImg.setRGB(x, y, drawData.drawImg[x][y]); drawData.drawImg[x][y] = 0; drawData.zBuf[x][y] = 0; } diff --git a/src/main/java/uk/org/floop/epq3d/Triangle.java b/src/main/java/uk/org/floop/epq3d/Triangle.java index a47b43e..1cd3ef2 100644 --- a/src/main/java/uk/org/floop/epq3d/Triangle.java +++ b/src/main/java/uk/org/floop/epq3d/Triangle.java @@ -21,9 +21,11 @@ private boolean is_initialised = false; private Point2D min; private Point2D max; - // progress variables - private Point3D result = new Point3D(0,0,0); - private Point2D result2 = new Point2D(0,0); + private Point2D startPoint; + private double xGradient; + private double yGradient; + + private final Point2D result2 = new Point2D(0,0); public void invalidate() { is_initialised = false; } @@ -51,7 +53,7 @@ point2 = LineA.nextPix(); for(int x = min.x+1; x <= max.x; x += 1) { while(x-1 == point1[0]) { - if(LineLong.isDrawn && // draw line pixels if needed, and on screen + 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); } @@ -65,7 +67,7 @@ while(x-1 == point2[0]) { if (currentLine == 'A') { try{ - if(LineA.isDrawn && // draw line pixels if needed, and on screen + 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); } @@ -76,7 +78,7 @@ } } else { - if(LineB.isDrawn && // draw line pixels if needed, and on screen + 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); } @@ -96,8 +98,7 @@ double gradient = -(z2 - z1) / (point2[1]-point1[1]); for (int y = Math.max(point1[1], 0); y <= Math.min(point2[1], drawData.scrY - 1); y += 1) { // function only exists so I don't have to copy paste code everywhere. - double zVal = z1 + gradient*(point1[1]-y); - drawPix(drawData, x, y, zVal, ang); + drawPix(drawData, x, y, ang); } } else { double z2 = LineLong.getZVal(x); double z1; @@ -108,8 +109,7 @@ } double gradient = -(z2 - z1) / (point1[1]-point2[1]); for (int y = Math.max(point2[1], 0); y <= Math.min(point1[1], drawData.scrY - 1); y += 1) { - double zVal = z1 + gradient*(point2[1]-y); - drawPix(drawData, x, y, zVal, ang); + drawPix(drawData, x, y, ang); } } } @@ -175,23 +175,64 @@ } } // find z calculation constants - // they are always relative to lineLong point1 - PointComp refPoint = LineLong.point1; - Vector3D vec1 = new Vector3D(refPoint.getRotatedPoint(), LineLong.point2.getRotatedPoint()); + // they are always relative to lineLong point1. This is an arbitrary decision but it must be consistent + startPoint = LineLong.point1.getProjectedPoint(); + + // find two vectors in screen coordinates from the point + Vector3D vec1 = new Vector3D( + LineLong.point2.getProjectedPoint().x - startPoint.x, + LineLong.point2.getProjectedPoint().y - startPoint.y, + 1/LineLong.point2.getRotatedPoint().z - 1/startPoint.z + ); + // in theory, projected point and rotated point Z values are the same. However, at some point i'd like to remove z values from 2d points + Vector3D vec2 = new Vector3D( + LineA.point2.getProjectedPoint().x - startPoint.x, + LineA.point2.getProjectedPoint().y - startPoint.y, + 1/LineA.point2.getRotatedPoint().z - 1/startPoint.z + ); + // calculate the cross product of these two vectors in order to obtain a normal vector + Vector3D cross = vec1.cross(vec2); + // find xGradient and yGradient for the triangle, in terms of z. We take the negative reciprocal of these gradients, because of the normal vector. + xGradient = -cross.x / cross.z; + yGradient = -cross.y / cross.z; + if(!Double.isFinite(xGradient)){ + xGradient = 0; + } + if(!Double.isFinite(yGradient)){ + yGradient = 0; + } + +// double testZ2 = LineLong.point2.getProjectedPoint().z; +// double testZ = refPoint.getProjectedPoint().z + +// xGradient * (LineLong.point2.getProjectedPoint().x-refPoint.getProjectedPoint().x) + +// yGradient * (LineLong.point2.getProjectedPoint().y-refPoint.getProjectedPoint().y); + + double testZ2 = LineA.point2.getProjectedPoint().z; + + double testZ = 1/(1/startPoint.z + + xGradient * (LineA.point2.getProjectedPoint().x-startPoint.x) + + yGradient * (LineA.point2.getProjectedPoint().y-startPoint.y)); + //System.out.println(testZ - testZ2); // assign points to lines is_initialised = true; return true; } - private void drawPix(drawData drawData, int x, int y, double ZVal, double ang){ + private void drawPix(drawData drawData, int x, int y, double ang){ // 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 - int newZ = (int)((2147483648L/(ZVal+1))); + double zVal = 1/((1/startPoint.z + + (x - startPoint.x)*xGradient + + (y - startPoint.y)*yGradient)); + int newZ = (int)(2147483647d/(zVal + 1)); +// if (newZ == 2147483647){ +// System.out.println((1/startPoint.z + +// (x - startPoint.x)*xGradient + +// (y - startPoint.y)*yGradient)); +// } // 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) || drawData.zBuf[x][y] = newZ; - result = new Point3D(0,0,0); - //perspectiveMappingMatrix.multiplyPoint3to(new Point3D(x, y, 0), result); // project result Color pixColor; if(texture.isSolid()){ @@ -199,12 +240,11 @@ } else { throw new RuntimeException("Textures are not supported"); } - drawData.drawImg[x][y] = pixColor.getRGB(); -// result.projectOn(FPDis, scrX, scrY, result2); -// int colour = texture.img.getRGB( -// Math.floorMod((result2.x), texture.img.getWidth()), -// Math.floorMod((result2.y), texture.img.getHeight())); -// img.setRGB(x, y, (colour - 10*(result2.x/ texture.img.getWidth()) - 10*(result2.y/ texture.img.getHeight()))); + if(drawData.drawZBuffer){ + drawData.drawImg[x][y] = Color.getHSBColor((float)newZ/2147483648f + 0.5f, 1, 1).getRGB(); + } else{ + drawData.drawImg[x][y] = pixColor.getRGB(); + } } } } diff --git a/src/main/java/uk/org/floop/epq3d/drawData.java b/src/main/java/uk/org/floop/epq3d/drawData.java index c53e16a..ceedde1 100644 --- a/src/main/java/uk/org/floop/epq3d/drawData.java +++ b/src/main/java/uk/org/floop/epq3d/drawData.java @@ -27,8 +27,7 @@ public boolean backfaceCullingDisabled; public boolean drawZBuffer; public int frustumCullingOverridePercent; - - + public boolean drawLines; // player public double playerSpeed; public double playerSprintModifier; @@ -71,6 +70,7 @@ backfaceCullingDisabled = toBoolean(debugFile.get("overrideBackFaceCulling")); frustumCullingOverridePercent = (int)(long)debugFile.get("frustumCullingOverridePercent"); drawZBuffer = toBoolean(debugFile.get("drawZBuffer")); + drawLines = toBoolean(debugFile.get("drawLines")); } bufImg = new BufferedImage(scrX, scrY, BufferedImage.TYPE_INT_ARGB);