public static Point convert(Point point, Camera camera) {
////////////////////////////////////////////////////////////////////////////
double pointMatrixD[][] = { { point.x },
{ point.y },
{ point.z } };
Matrix pointMatrix = new Matrix(pointMatrixD);
////////////////////////////////////////////////////////////////////////////
double camPointMatrixD[][] = { { camera.location.x },
{ camera.location.y },
{ camera.location.z } };
Matrix camPointMatrix = new Matrix(camPointMatrixD);
//////////////////////////////////////////////////////////////////////////// the x rotation matrix
double xRotationMatrixD[][] = { { 1, 0, 0 },
{ 0, Ops.cos(-camera.rotation.x), Ops.sin(-camera.rotation.x) },
{ 0, -Ops.sin(-camera.rotation.x), Ops.cos(-camera.rotation.x) } };
Matrix xRotationMatrix = new Matrix(xRotationMatrixD);
//////////////////////////////////////////////////////////////////////////// the y rotation matrix
double yRotationMatrixD[][] = { { Ops.cos(-camera.rotation.y), 0, -Ops.sin(-camera.rotation.y) },
{ 0, 1, 0 },
{ Ops.sin(-camera.rotation.y), 0, Ops.cos(-camera.rotation.y) } };
Matrix yRotationMatrix = new Matrix(yRotationMatrixD);
//////////////////////////////////////////////////////////////////////////// the z rotation matrix
double zRotationMatrixD[][] = { { Ops.cos(-camera.rotation.z), Ops.sin(-camera.rotation.z), 0 },
{ -Ops.sin(-camera.rotation.z), Ops.cos(-camera.rotation.z), 0 },
{ 0, 0, 1 } };
Matrix zRotationMatrix = new Matrix(zRotationMatrixD);
////////////////////////////////////////////////////////////////////////////
Matrix translatedPointMatrix = null;
try {
/* translates the point from its coordinate system into the cameras coordinate system */
translatedPointMatrix = xRotationMatrix.multiply(
yRotationMatrix.multiply(
zRotationMatrix.multiply(
pointMatrix.subtract(camPointMatrix)
)
)
);
} catch(Exception e) {
// System.out.println(e.getMessage());
}
/* simply translates the matrix to a point */
Point translatedPoint = new Point(translatedPointMatrix.get(0, 0),
translatedPointMatrix.get(1, 0),
translatedPointMatrix.get(2, 0));
/* calculates and returns the final projected point knowing the distance from the camera(camera.viewerPosition) */
return new Point( (translatedPoint.x - camera.viewerPosition.x) * (camera.viewerPosition.z / translatedPoint.z),
(translatedPoint.y - camera.viewerPosition.y) * (camera.viewerPosition.z / translatedPoint.z));
}
glitch in the matrix
I've just implemented a function(in java) which converts a point in 3d space into a 2d point to be projected on screen the problem is that every time the camera location goes beyond a point it displays a sort of mirror image of this point. heres the code I use:
the camera class just defines the location of the camera(x, y, z), the rotation(x, y, z) and the field of view(viewerPosition)(x, y, z) which is usally somthing like(0, 0, -1000). I know this probaly isn't the most efficient way of converting a point but it does work. any ideas why its displaying the inverted point if the camera is pointed in the opposite direction?
You probably get the inversion when the point is actually behind the camera from the cameras point of view. In that case the point should actually not be visible at all.
The normal way to transform a point by a camera or any other transformation is by a simple matrix multiplication.
p' = TransformationMatrix * p
where TransformationMatrix is a 4x4 matrix and p is a 4-vector with (x, y, z, w) w normally beeing 1. The resulting point lies in clip space where you would do the clipping to the view frustum by comparing x, y, z to w. The final step is the perspective division x/w, y/w, z/w. Then you only need to scale and offset the values to fit into your viewport.
The normal way to transform a point by a camera or any other transformation is by a simple matrix multiplication.
p' = TransformationMatrix * p
where TransformationMatrix is a 4x4 matrix and p is a 4-vector with (x, y, z, w) w normally beeing 1. The resulting point lies in clip space where you would do the clipping to the view frustum by comparing x, y, z to w. The final step is the perspective division x/w, y/w, z/w. Then you only need to scale and offset the values to fit into your viewport.
I think the transformation matrix you are taking about is just the result of:
but this is the first time i've worked with matrices/3d graphics programming so im not sure.
translatedPointMatrix = xRotationMatrix.multiply( yRotationMatrix.multiply( zRotationMatrix.multiply( pointMatrix.subtract(camPointMatrix) ) ) );
but this is the first time i've worked with matrices/3d graphics programming so im not sure.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement