   const vec4 yAxis = vec4(0, 0.5, 0, 0);
   const vec4 zAxis = vec4(0, 0, 0.5, 0);
 
+  vec4 nearPlane = vec4(matrix[0][3] + matrix[0][2], matrix[1][3] + matrix[1][2],
+                    matrix[2][3] + matrix[2][2], matrix[3][3] + matrix[3][2]);
+
+  float magnitude = sqrt(dot(nearPlane.xyz, nearPlane.xyz));
+  nearPlane = nearPlane / magnitude;
   vec4 center = gl_in[0].gl_Position;
 
   // top
-  processSide(matrix, center, yAxis, xAxis, zAxis);
+  processSide(matrix, nearPlane, center, yAxis, xAxis, zAxis);
   // right
-  processSide(matrix, center, xAxis, yAxis, zAxis);
+  processSide(matrix, nearPlane, center, xAxis, yAxis, zAxis);
   // front
-  processSide(matrix, center, zAxis, xAxis, yAxis);
+  processSide(matrix, nearPlane, center, zAxis, xAxis, yAxis);
   // bottom
-  processSide(matrix, center, -yAxis, xAxis, zAxis);
+  processSide(matrix, nearPlane, center, -yAxis, xAxis, zAxis);
   // left
-  processSide(matrix, center, -xAxis, yAxis, zAxis);
+  processSide(matrix, nearPlane, center, -xAxis, yAxis, zAxis);
   // back
-  processSide(matrix, center, -zAxis, xAxis, yAxis);
+  processSide(matrix, nearPlane, center, -zAxis, xAxis, yAxis);
 
   fillHole(matrix);
 }

  cutPositions[cutPositionCount++] = newPos;
}

void processTriangle(const mat4 matrix, const vec4 triangle[3])
{
  const float cutOffZ = 0.5;
  int emittedVertexCount = 0;
  vec4 plane = vec4(matrix[0][3] + matrix[0][2], matrix[1][3] + matrix[1][2],
                    matrix[2][3] + matrix[2][2], matrix[3][3] + matrix[3][2]);

  float magnitude = sqrt(dot(plane.xyz, plane.xyz));
  plane = plane / magnitude;

  vec4 firstPosition;
  bool triangleSplittingNecessary = false;

  for (int i = 0; i < 3; ++i)
  {
    vec4 inPos = triangle[i];
    bool isPosInFOV = dot(inPos, nearPlane) > cutOffZ;
    if (isPosInFOV)
    {
      emittedVertexCount = emitWithPrimitiveHandling(
