Skip to content

Commit e08a393

Browse files
committed
Fix faces rendering bugs
1 parent ce810fd commit e08a393

4 files changed

Lines changed: 88 additions & 34 deletions

File tree

data/long.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[
2+
{
3+
"x": -4000,
4+
"y": -1000,
5+
"z": 3000,
6+
"xLen": 2000,
7+
"yLen": 2000,
8+
"zLen": 2000
9+
},
10+
11+
{
12+
"x": -4000,
13+
"y": -1000,
14+
"z": 6000,
15+
"xLen": 2000,
16+
"yLen": 2000,
17+
"zLen": 2000
18+
},
19+
20+
{
21+
"x": 7000,
22+
"y": -1000,
23+
"z": 3000,
24+
"xLen": 2000,
25+
"yLen": 2000,
26+
"zLen": 12000
27+
},
28+
29+
{
30+
"x": 4000,
31+
"y": -1000,
32+
"z": 6000,
33+
"xLen": 2000,
34+
"yLen": 2000,
35+
"zLen": 2000
36+
}
37+
]

src/pl/wydzials/virtualcamera/Sketch.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class Sketch extends PApplet {
3333
double zRotation = 0;
3434

3535
public void settings() {
36-
size(1400, 800);
36+
size(1700, 900);
3737
}
3838

3939
public void setup() {
@@ -42,10 +42,31 @@ public void setup() {
4242

4343
ModelCreator creator = new ModelCreator();
4444
creator.readCubesFromFile("data/model.json");
45-
// creator.generateRandomCubes(100);
45+
creator.generateRandomCubes(100);
4646
model = creator.getModel();
4747

4848
facesTree = BinarySearchPartitioning.buildTree(model.getFaces());
49+
50+
updatePointsFromTree(facesTree);
51+
}
52+
53+
private void updatePointsFromTree(TreeNode<Face> faces) {
54+
if (faces == null) {
55+
return;
56+
}
57+
Face face = faces.key;
58+
59+
updatePointsFromTree(faces.left);
60+
for (int i = 0; i < face.getPoints().length; i++) {
61+
Point point = face.getPoint(i);
62+
63+
if (!model.getPoints().containsKey(point.key())) {
64+
model.getPoints().put(point.key(), point);
65+
} else {
66+
face.setPoint(i, model.getPoints().get(point.key()));
67+
}
68+
}
69+
updatePointsFromTree(faces.right);
4970
}
5071

5172
public void draw() {
@@ -54,13 +75,9 @@ public void draw() {
5475

5576
updateSpeedFromFramerate();
5677
updateFocalLength();
57-
5878
updatePoints();
59-
projectLines();
6079

6180
renderFaces(facesTree);
62-
//renderLines();
63-
renderText();
6481
}
6582
}
6683

@@ -97,30 +114,20 @@ private Point projectPoint(Point point) {
97114
return new Point(x, y, z);
98115
}
99116

100-
private void projectLines() {
101-
projectedLines.clear();
117+
private void renderLines() {
118+
strokeCap(ROUND);
119+
stroke(0, 0, 0);
102120
for (Line line : model.getLines()) {
103121
if (areVisible(line.point1) && areVisible(line.point2)) {
104122
Point p1 = projectPoint(line.point1);
105123
Point p2 = projectPoint(line.point2);
106124

107-
projectedLines.add(new Line(p1, p2));
125+
strokeWeight(2);
126+
line((float) p1.x, (float) p1.y, (float) p2.x, (float) p2.y);
108127
}
109128
}
110129
}
111130

112-
private void renderLines() {
113-
strokeCap(ROUND);
114-
stroke(0, 0, 0);
115-
for (Line line : projectedLines) {
116-
Point p1 = line.point1;
117-
Point p2 = line.point2;
118-
119-
strokeWeight(2);
120-
line((float) p1.x, (float) p1.y, (float) p2.x, (float) p2.y);
121-
}
122-
}
123-
124131
private void renderFaces(TreeNode<Face> tree) {
125132
if (tree == null) {
126133
return;
@@ -161,8 +168,9 @@ private void renderFace(Face face) {
161168
quadPoints[2 * n + 1] = (float) projected.y;
162169
}
163170

164-
noStroke();
171+
stroke(face.getColor().getRed(), face.getColor().getBlue(), face.getColor().getGreen());
165172
fill(face.getColor().getRed(), face.getColor().getBlue(), face.getColor().getGreen());
173+
166174
quad(quadPoints[0], quadPoints[1], quadPoints[2], quadPoints[3],
167175
quadPoints[4], quadPoints[5], quadPoints[6], quadPoints[7]);
168176
}

src/pl/wydzials/virtualcamera/bsp/BinarySearchPartitioning.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ private static void recursiveBuild(TreeNode<Face> tree, Set<Face> faces) {
4747
secSidePoint[secCount++] = singlePoint;
4848
}
4949
Face[] dividedFace = Geometry.divideFace(firstSidePoint, secSidePoint, planeIndexes);
50+
dividedFace[0].setColor(face.getColor());
51+
dividedFace[1].setColor(face.getColor());
5052
right.add(dividedFace[0]);
5153
left.add(dividedFace[1]);
5254
}

src/pl/wydzials/virtualcamera/bsp/Geometry.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static void main(String[] args) {
1818
double[] planeIndexes = calcThePlane(pointArr);
1919
Point pointD = new Point(1, -5, 1);
2020
Point pointE = new Point(2, -5, 1);
21-
// Point pointE = new Point(-6.29,1.07,-3.2);
2221

2322
int sideDecision = areTwoPointsSameSide(pointD, pointE, planeIndexes);
2423
if (sideDecision == -1)
@@ -27,7 +26,6 @@ else if (sideDecision == 1)
2726
System.out.println("Są po tej samej stronie");
2827
else
2928
System.out.println("Jeden z punktów leży na płaszczyźnie");
30-
3129
}
3230

3331
public static double[] calcThePlane(List<Point> points) {
@@ -51,7 +49,7 @@ public static int areTwoPointsSameSide(Point firstPoint, Point secPoint, double[
5149
double firstValue = firstPoint.x * planeIndexes[0] + firstPoint.y * planeIndexes[1] + firstPoint.z * planeIndexes[2] + planeIndexes[3];
5250
double secValue = secPoint.x * planeIndexes[0] + secPoint.y * planeIndexes[1] + secPoint.z * planeIndexes[2] + planeIndexes[3];
5351

54-
if (java.lang.Math.abs(firstValue) < 10 || java.lang.Math.abs(secValue) < 10) {
52+
if (Math.abs(firstValue) < 10 || Math.abs(secValue) < 10) {
5553
return 0;
5654
}
5755
if (firstValue * secValue > 0) {
@@ -71,13 +69,13 @@ public static int areFaceAndPointSameSide(Face face, Point point, double[] plane
7169
long zeroCount = Arrays.stream(sameSides).filter(i -> i == 0).count();
7270

7371
if (zeroCount == 4) {
74-
return 0; // w plaszczyznie
72+
return 0;
7573
} else if (sameCount == 4 || (zeroCount > 0 && sameCount > oppositeCount)) {
7674
return 1;
7775
} else if (oppositeCount == 4 || (zeroCount > 0 && oppositeCount > sameCount)) {
7876
return -1;
7977
} else {
80-
return -10; // do podzialu
78+
return -10;
8179
}
8280
}
8381

@@ -86,12 +84,16 @@ public static Face[] divideFace(Point[] oneSidePoints, Point[] secSidePoints, do
8684
Point firstPoint = findPoint(orderedPoints[0], orderedPoints[1], planeIndexes);
8785
Point secPoint = findPoint(orderedPoints[2], orderedPoints[3], planeIndexes);
8886

89-
System.out.println("Pierwsza ściana: " + oneSidePoints[0] + " " + oneSidePoints[1] + " " + secPoint + " " + firstPoint);
90-
System.out.println("Druga ściana: " + secSidePoints[0] + " " + secSidePoints[1] + " " + secPoint + " " + firstPoint);
91-
System.out.println();
87+
Face firstFace = new Face(oneSidePoints[0], oneSidePoints[1], firstPoint, secPoint);
88+
Face secFace = new Face(secSidePoints[0], secSidePoints[1], firstPoint, secPoint);
89+
90+
if (pointsDistance(oneSidePoints[1], secPoint) < pointsDistance(oneSidePoints[1], firstPoint)) {
91+
firstFace = new Face(oneSidePoints[0], oneSidePoints[1], secPoint, firstPoint);
92+
}
93+
if (pointsDistance(secSidePoints[1], secPoint) < pointsDistance(secSidePoints[1], firstPoint)) {
94+
secFace = new Face(secSidePoints[0], secSidePoints[1], secPoint, firstPoint);
95+
}
9296

93-
Face firstFace = new Face(oneSidePoints[0], oneSidePoints[1], secPoint, firstPoint);
94-
Face secFace = new Face(secSidePoints[0], secSidePoints[1], secPoint, firstPoint);
9597
return new Face[]{firstFace, secFace};
9698
}
9799

@@ -107,8 +109,8 @@ private static Point findPoint(Point firstPoint, Point secPoint, double[] planeI
107109
private static Point[] determinePointPairs(Point[] oneSidePoints, Point[] secSidePoints) {
108110
double[] firstVector = calculateVector(oneSidePoints[0], secSidePoints[0]);
109111
double[] secVector = calculateVector(oneSidePoints[0], secSidePoints[1]);
110-
double firstVectorLength = java.lang.Math.sqrt(firstVector[0] * firstVector[0] + firstVector[1] * firstVector[1] + firstVector[2] * firstVector[2]);
111-
double secVectorLength = java.lang.Math.sqrt(secVector[0] * secVector[0] + secVector[1] * secVector[1] + secVector[2] * secVector[2]);
112+
double firstVectorLength = Math.sqrt(firstVector[0] * firstVector[0] + firstVector[1] * firstVector[1] + firstVector[2] * firstVector[2]);
113+
double secVectorLength = Math.sqrt(secVector[0] * secVector[0] + secVector[1] * secVector[1] + secVector[2] * secVector[2]);
112114

113115
if (firstVectorLength < secVectorLength) {
114116
return new Point[]{oneSidePoints[0], secSidePoints[0], oneSidePoints[1], secSidePoints[1]};
@@ -119,4 +121,9 @@ private static Point[] determinePointPairs(Point[] oneSidePoints, Point[] secSid
119121
private static double[] calculateVector(Point firstPoint, Point secPoint) {
120122
return new double[]{firstPoint.x - secPoint.x, firstPoint.y - secPoint.y, firstPoint.z - secPoint.z};
121123
}
124+
125+
private static double pointsDistance(Point a, Point b) {
126+
double[] vector = calculateVector(a, b);
127+
return Math.sqrt(Arrays.stream(vector).map(v -> v * v).sum());
128+
}
122129
}

0 commit comments

Comments
 (0)