Reputation: 1
I have had this bug for a while, and I tracked it down to the way I am trying to rotate and then center a mesh in my raytracer. The way I figured it out is the meshes need to be rotated first, and then centered with the bottom center point of the mesh at 0, 0, 0. Here is my code:
for(int i = 0; i < triangleList.size(); i++) {
for(int a = 0; a < 3; a++) {
double x = triangleList.get(i)[a][0];
double y = triangleList.get(i)[a][1];
double z = triangleList.get(i)[a][2];
double X = rx(x, z, Math.toRadians(xDir));
double Y = y;
double Z = rz(x, z, Math.toRadians(xDir));
x = X;
y = rx(Y, Z, Math.toRadians(yDir));
z = rz(Y, Z, Math.toRadians(yDir));
triangleList.get(i)[a][0] = x;
triangleList.get(i)[a][1] = y;
triangleList.get(i)[a][2] = z;
}
}
double leastX = Double.MAX_VALUE;
double mostX = Double.MIN_VALUE;
double leastZ = Double.MAX_VALUE;
double mostZ = Double.MIN_VALUE;
double leastY = Double.MAX_VALUE;
for(int i = 0; i < triangleList.size(); i++) {
for(int a = 0; a < 3; a++) {
leastX = Math.min(leastX, triangleList.get(i)[a][0]);
mostX = Math.max(mostX, triangleList.get(i)[a][0]);
leastY = Math.min(leastY, triangleList.get(i)[a][1]);
leastZ = Math.min(leastZ, triangleList.get(i)[a][2]);
mostZ = Math.max(mostZ, triangleList.get(i)[a][2]);
}
}
mostX = (leastX + mostX) / 2;
mostZ = (leastZ + mostZ) / 2;
for(int i = 0; i < triangleList.size(); i++) {
for(int a = 0; a < 3; a++) {
double x = triangleList.get(i)[a][0];
double y = triangleList.get(i)[a][1];
double z = triangleList.get(i)[a][2];
triangleList.get(i)[a][0] = x - mostX;
triangleList.get(i)[a][1] = y - leastY;
triangleList.get(i)[a][2] = z - mostZ;
}
}
double[][][] newMesh = triangleList.toArray(new double[0][3][3]);
I tried first centering then rotating, but that did not fix the problem.
Upvotes: 0
Views: 18