function crossproduct(vector1,vector2) {
	var returnVector = new Array(4);
	returnVector[0] = vector1[1]*vector2[2]-vector1[2]*vector2[1];
	returnVector[1] = vector1[2]*vector2[0]-vector1[0]*vector2[2];
	returnVector[2] = vector1[0]*vector2[1]-vector1[1]*vector2[0];
	returnVector[3] = 1;
	return returnVector;
}

function axismatrix(vertex,theta) {
	var distancesq = vertex[0]*vertex[0]+vertex[1]*vertex[1]+vertex[2]*vertex[2];
	var normalized = Array(vertex[0]*vertex[0]/distancesq,vertex[1]*vertex[1]/distancesq,vertex[2]*vertex[2]/distancesq,1);
	if(vertex[0] > 0) normalized[0] *= -1;
	if(vertex[1] > 0) normalized[1] *= -1;
	if(vertex[2] > 0) normalized[2] *= -1;
	var newrotateMatrix = matrixMultiply(matrixMultiply(rotatexmatrix(-theta*normalized[0]),rotateymatrix(-theta*normalized[1])),rotatezmatrix(theta*normalized[2]));
	testrotateMatrix = matrixMultiply(newrotateMatrix,oldcamrotateMatrix);
	if (testrotateMatrix[2][2] < -0.01) {
		camrotateMatrix = matrixMultiply(newrotateMatrix,oldcamrotateMatrix);
		return true;
	}
	else {
		oldcamrotateMatrix = camrotateMatrix;
		return false;
	}
}

function matrixMultiply(matrix1,matrix2) {
	var returnMatrix = new Array(4);
	for (var s=0;s<4;s++) {
		var rowMatrix = Array(0,0,0,0);
		for (var t=0;t<4;t++) {
			for (u=0;u<4;u++) {
				rowMatrix[t] += matrix1[s][u]*matrix2[u][t];
			}
		}
		returnMatrix[s] = rowMatrix;
	}
	return returnMatrix;
}

function vertexMultiply(matrix1,matrix2) {
	var returnMatrix = Array(0,0,0,0);
	for (var x=0;x<4;x++) {
		for (var y=0;y<4;y++) {
			returnMatrix[x] += matrix1[x][y]*matrix2[y];
		}
	}
	return returnMatrix;
}

function rotatexmatrix(theta) { return Array(Array(1,0,0,0),Array(0,Math.cos(theta),0 - Math.sin(theta),0),Array(0,Math.sin(theta),Math.cos(theta),0),Array(0,0,0,1)); }

function rotateymatrix(theta) { return Array(Array(Math.cos(theta),0,Math.sin(theta),0),Array(0,1,0,0),Array(0 - Math.sin(theta),0,Math.cos(theta),0),Array(0,0,0,1)); }

function rotatezmatrix(theta) {	return Array(Array(Math.cos(theta),0 - Math.sin(theta),0,0),Array(Math.sin(theta),Math.cos(theta),0,0),Array(0,0,1,0),Array(0,0,0,1)); }

function translatematrix(dx,dy,dz) { return Array(Array(1,0,0,dx),Array(0,1,0,dy),Array(0,0,1,dz),Array(0,0,0,1)); }

function scalematrix(factor) { return Array(Array(factor,0,0,0),Array(0,factor,0,0),Array(0,0,factor,0),Array(0,0,0,1)); }