Example #1
0
static glm::mat4 Util_SetViewMatrix(EERIE_TRANSFORM &transform) {

	Vec3f vFrom(transform.pos.x, -transform.pos.y, transform.pos.z);
	Vec3f vTout(0.0f, 0.0f, 1.0f);

	Vec3f vView;
	rotPoint(&vTout, &vView, transform);
	
	Vec3f up(0.f, 1.f, 0.f);
	Vec3f vWorldUp;
	rotPoint(&up, &vWorldUp, transform);

	return Util_LookAt(vFrom, vView, vWorldUp);
}
Example #2
0
void Util_SetViewMatrix(glm::mat4x4 &mat, EERIE_TRANSFORM &transform) {

	Vec3f vFrom(transform.pos.x, -transform.pos.y, transform.pos.z);
	Vec3f vTout(0.0f, 0.0f, 1.0f);

	Vec3f vView;
	vView.y = -(vTout.z * transform.xsin);
	vView.z = -(vTout.z * transform.xcos);
	vView.x =  (vView.z * transform.ysin);
	vView.z = -(vView.z * transform.ycos);

	Vec3f vWorldUp(0.f, 1.f, 0.f);

	// Normalize the z basis vector
	float fLength = glm::length(vView);
	if (fLength < 1e-6f)
		return;

	// Get the dot product, and calculate the projection of the z basis
	// vector onto the up vector. The projection is the y basis vector.
	float fDotProduct = glm::dot(vWorldUp, vView);

	Vec3f vUp = vWorldUp - vView * fDotProduct;

	// If this vector has near-zero length because the input specified a
	// bogus up vector, let's try a default up vector
	if(1e-6f > (fLength = glm::length(vUp)))
	{
		vUp = Vec3f_Y_AXIS - vView * vView.y;

		// If we still have near-zero length, resort to a different axis.
		if(1e-6f > (fLength = glm::length(vUp)))
		{
			vUp = Vec3f_Z_AXIS - vView * vView.z;

			if(1e-6f > (fLength = glm::length(vUp)))
				return;
		}
	}

	// Normalize the y basis vector
	vUp /= fLength;

	// The x basis vector is found simply with the cross product of the y
	// and z basis vectors
	Vec3f vRight = glm::cross(vUp, vView);

	// Start building the matrix. The first three rows contains the basis
	// vectors used to rotate the view to point at the lookat point
	mat[0][0] = vRight.x;
	mat[0][1] = vUp.x;
	mat[0][2] = vView.x;
	mat[1][0] = vRight.y;
	mat[1][1] = vUp.y;
	mat[1][2] = vView.y;
	mat[2][0] = vRight.z;
	mat[2][1] = vUp.z;
	mat[2][2] = vView.z;

	// Do the translation values (rotations are still about the eyepoint)
	mat[3][0] = -glm::dot(vFrom, vRight);
	mat[3][1] = -glm::dot(vFrom, vUp);
	mat[3][2] = -glm::dot(vFrom, vView);
	mat[3][3] = 1.0f;
}