# 3D software-based engine projection issues

I have been writing a 3D soft-engine for a little while and I have gotten it a reasonable position but recently noticed a bug that I can't seem to solve. When I am rendering a few cubes as a test, if a vertex of the cube moves beyond a certain point outside of the field of view, the coordinate value of that vertex is extended much further than it should be.

When glitching: I printed the coordinates before the screen space conversion and got:

point, x: -6.493128495525562 y: 3.074185544504503


so no surprise when the output of a debug line after screen space conversion was:

point a, x: -18294.57930136702 y: -3975.1260633549537
point b, x: 2637.13493945939 y: 745.4028905130891
point c, x: 1376.2540951732117 y: 643.1113243439205


point b and point c's coordinates are as expected but point a's coordinates are not.

Please note that those numbers were from different tests so they may not marry together perfectly but the principle is still the same.

Hopefully that adds some context.

I'll attach the methods that I think are the main issues, if there are any that I have excluded please let me know.

The important methods from the rendering class:

public Vector3 transform(Vector3 coord, double[][] transMat) {
// transforming the coordinates
Vector3 point = Matrix.vectorMatrixMultiply(coord, transMat);

//converting the coordinates from 0,0 at the centre to 0,0 at the top left
double x =  point.x * width + width / 2;
double y = -point.y * height + height / 2;
return (new Vector3(x, y, point.z));
}

// DrawPoint places the point into a back buffer ready to be drawn and tests if it is visible
public void drawPoint(Vector3 point, Color color) {
// Clipping what's visible on screen
int index = (int) (point.x + point.y * width);
if ((point.x >= 0) && (point.y >= 0) && (point.x < width) && (point.y < height)) {
// Drawing a pixel
if (point.z <= depthBuffer[index]) {
depthBuffer[index] = point.z;
backBuffer[index] = color.getRGB();
}
}
}

public void render(Camera camera, Vector<Mesh> meshes) {
//Transmat  = world*view*projection
double[][] viewMatrix = Matrix.fpsViewRH(camera.position, camera.target.y, camera.target.x);
double[][] projectionMatrix = Matrix.perspectiveProjection(90.35, 90, 0.001, 100);

Arrays.fill(depthBuffer,Double.MAX_VALUE);//reset the depth buffer for the next frame
Arrays.fill(backBuffer, 0);//reset the back buffer for the next frame

meshes.parallelStream().forEach(mesh ->{
// Apply rotation before translation
double[][] worldMatrix = Matrix.matrixMultiply(Matrix.rotationYawPitchRoll(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z), Matrix.setTranslationMatrix(mesh.position.x, mesh.position.y, mesh.position.z));
double[][] transformMatrix = Matrix.matrixMultiply(Matrix.matrixMultiply(worldMatrix, viewMatrix), projectionMatrix);
mesh.faces.parallelStream().forEach(face->{

//get the vertices of each face
Vector3 vertexA = mesh.vertices[face.a];
Vector3 vertexB = mesh.vertices[face.b];
Vector3 vertexC = mesh.vertices[face.c];

//transform the coordinates to be projected properly
Vector3 pixelA = transform(vertexA, transformMatrix);
Vector3 pixelB = transform(vertexB, transformMatrix);
Vector3 pixelC = transform(vertexC, transformMatrix);

if(mesh.drawWireFrame){
//draw the lines of the bounding box
drawLine(pixelA, pixelB, face.colour);
drawLine(pixelB, pixelC, face.colour);
drawLine(pixelC, pixelA, face.colour);

}else{
//draw a triangle based off the transformed coordinates
drawRasterTriangle(pixelA, pixelB, pixelC, face.colour);
}
System.out.println("point a, x: " + pixelA.x+" y: "+pixelA.y);
System.out.println("point b, x: " + pixelB.x+" y: "+pixelB.y);
System.out.println("point c, x: " + pixelC.x+" y: "+pixelC.y);
});

});
System.arraycopy(backBuffer, 0, ((DataBufferInt) bi.getRaster().getDataBuffer()).getData(), 0, backBuffer.length);
paintImmediately(0,0,width, height);
}


The used methods from the Matrix class:

 public static double[][] perspectiveProjection(double fovx, double fovy, double near, double far){
double[][] m = new double;
for (double[] row: m){
Arrays.fill(row, 0);
}
m= 1/(Math.tan(fovx/2));
m= 1/(Math.tan(fovy/2));
m= -(far+near)/(far-near);
m=-(2*(near*far))/(far-near);
m=-1;
return m;
}

//set the translation matrix
public static double[][] setTranslationMatrix(double tx, double ty, double tz) {
double[][] m = new double;
for (double[] row: m){
Arrays.fill(row, 0);
}
m = 1;
m = tx;
m = 1;
m = ty;
m = 1;
m = tz;
m = 1;
return m;
}

//set the x rotation matrix
public static double[][] setXRotationMatrix(double angle) {
double[][] m = new double;
for (double[] row: m){
Arrays.fill(row, 0);
}
m = 1;
m = Math.cos(angle);
m = -Math.sin(angle);
m = Math.sin(angle);
m = Math.cos(angle);
m = 1;
return m;
}

//set the y rotaion matrix
public static double[][] setYRotationMatrix(double angle) {
double[][] m = new double;
for (double[] row: m){
Arrays.fill(row, 0);
}
m = (double) Math.cos(angle);
m = (double) Math.sin(angle);
m = 1;
m = (double) -Math.sin(angle);
m = (double) Math.cos(angle);
m = 1;
return m;
}

//set the z rotation matrix
public static double[][] setZRotationMatrix(double angle) {
double[][] m = new double;
for (double[] row: m){
Arrays.fill(row, 0);
}
m = (double) Math.cos(angle);
m = (double) -Math.sin(angle);
m = (double) Math.sin(angle);
m = (double) Math.cos(angle);
m=1;
m =1;
return m;
}

public static Vector3 vectorMatrixMultiply(Vector3 point, double[][] m) {
// maths to project it to a perspective view
Vector3 out = new Vector3();
out.x = (point.x * m) + (point.y * m) + (point.z * m) + m;
out.y = (point.x * m) + (point.y * m) + (point.z * m) + m;
out.z = (point.x * m) + (point.y * m) + (point.z * m) + m;
double w = (point.x * m) + (point.y * m) + (point.z * m) + m;
if (w != 1) {
out.x = out.x/ w;
out.y = out.y/w;
out.z = out.z/w;
}
return out;
}

public static double[][] matrixMultiply(double[][] a, double[][] b){
double[][] temp = new double;
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
temp = (a * b) + (a * b) + (a * b) + (a * b);
return temp;
}

public static double[][] rotationYawPitchRoll(double x, double y, double z) {
double[][] m = new double;
m=matrixMultiply(setXRotationMatrix(x), setYRotationMatrix(y));
m=matrixMultiply(m, setZRotationMatrix(z));
return m;
}

//the view matrix that changes the eye position, pitch and yaw into a view matrix for a right-handed system
// should be in the range of [0 ... 360] degrees.
public static double[][] fpsViewRH(Vector3 eye, double pitch, double yaw ){
//the pitch and yaw values need to have been converted to radians
double cosPitch = Math.cos(pitch);
double sinPitch = Math.sin(pitch);
double cosYaw = Math.cos(yaw);
double sinYaw = Math.sin(yaw);

Vector3 xaxis = new Vector3(cosYaw, 0, -sinYaw);
Vector3 yaxis = new Vector3( sinYaw * sinPitch, cosPitch, cosYaw * sinPitch);
Vector3 zaxis = new Vector3(sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw);

// Create a 4x4 view matrix from the right, up, forward and eye position vectors
double[][] viewMatrix = new double;

viewMatrix=xaxis.x; viewMatrix=yaxis.x; viewMatrix=zaxis.x; viewMatrix=0;
viewMatrix=xaxis.y; viewMatrix=yaxis.y; viewMatrix=zaxis.y; viewMatrix=0;
viewMatrix=xaxis.z; viewMatrix=yaxis.z; viewMatrix=zaxis.z; viewMatrix=0;
viewMatrix=-xaxis.dot(eye); viewMatrix= -yaxis.dot(eye); viewMatrix= -zaxis.dot(eye); viewMatrix= 1;

return viewMatrix;
}


The values entered into the view matrix are confined within 0 and 360 and converted to radians so I know that's not an issue as well. Please excuse the crudity of the code, it is not finished yet. Any help would be greatly appreciated.

• It looks like you're not converting from clip space to screen space properly because of missing brackets. e.g. point.x * width + width / 2 should be (point.x * width + width) / 2 – Ryan Oct 31 '16 at 13:58
• Thanks for the reply Ryan, I just tried changing those lines around to what you said and the issue is still there. – Will D Oct 31 '16 at 21:17
• Are you sure that point doesn't get behind the camera? – Bálint May 4 '17 at 21:52
• I forgot to mention I resolved this, mostly and yes that was the problem so I just added a clipping plane in front of the camera – Will D May 4 '17 at 21:54