void fzMath_mat4PerspectiveProjection(fzFloat fovY, fzFloat aspect, fzFloat zNear, fzFloat zFar, float *output) { FZ_ASSERT(output != NULL, "Output matrix cannot be NULL."); fzFloat r = FZ_DEGREES_TO_RADIANS(fovY / 2); fzFloat deltaZ = zFar - zNear; fzFloat s = fzMath_sin(r); fzFloat cotangent = 0; if (deltaZ == 0 || s == 0 || aspect == 0) { FZLOGERROR("Perpertive impossible."); return; } cotangent = fzMath_cos(r) / s; fzMath_mat4Identity(output); output[0] = cotangent / aspect; output[5] = cotangent; output[10] = -(zFar + zNear) / deltaZ; output[11] = -1; output[14] = -2 * zNear * zFar / deltaZ; output[15] = 0; }
void fzMath_mat4LookAt(const fzPoint3& eye, const fzPoint3& center, const fzPoint3& up, float *output) { FZ_ASSERT(output != NULL, "Output pointer cannot be NULL."); fzPoint3 f(center); f.x -= eye.x; f.y -= eye.y; f.z -= eye.z; f.normalize(); fzPoint3 up2(up); up2.normalize(); fzPoint3 s(f.getCrossed(up2)); s.normalize(); fzPoint3 u(s.getCrossed(f)); //s.normalize(); u.normalize(); fzMath_mat4Identity(output); output[0] = s.x; output[4] = s.y; output[8] = s.z; output[1] = u.x; output[5] = u.y; output[9] = u.z; output[2] = -f.x; output[6] = -f.y; output[10] = -f.z; /* fzMat4 translationMatrix; fzAffineTransform translate(FZAffineTransformIdentity); translate.translate(-eye.x, -eye.y); translate.setZ(-eye.y); */ /* fzMath_mat4Multiply(<#const fzFloat *mIn1#>, <#const fzFloat *mIn2#>, <#fzFloat *mOut#>) fzMath_mat4Multiply(output, translate, output); */ // kmMat4 translate; // kmMat4Translation(&translate, -pEye->x, -pEye->y, -pEye->z); // kmMat4Multiply(pOut, pOut, &translate); }
void fzMath_mat4LookAt(const fzPoint3& eye, const fzPoint3& center, const fzPoint3& up, float *output) { FZ_ASSERT(output != NULL, "Output pointer cannot be NULL."); fzPoint3 f(center); f -= eye; f.normalize(); fzPoint3 up2(up); up2.normalize(); fzPoint3 s = f.getCrossed(up2); s.normalize(); fzPoint3 u(s.getCrossed(f)); // u.normalize(); //s.normalize(); fzMath_mat4Identity(output); output[0] = s.x; //1 output[4] = s.y; output[8] = s.z; output[1] = u.x; output[5] = u.y; // 1 output[9] = u.z; output[2] = -f.x; output[6] = -f.y; output[10] = -f.z; // 1 float x = -eye.x; //mat[12] float y = -eye.y; //mat[13] float z = -eye.z; //mat[14] float mat[4]; mat[0] = output[0] * x + output[4] * y + output[8] * z + output[12]; mat[1] = output[1] * x + output[5] * y + output[9] * z + output[13]; mat[2] = output[2] * x + output[6] * y + output[10] * z + output[14]; mat[3] = output[3] * x + output[7] * y + output[11] * z + output[15]; memcpy(&output[12], mat, sizeof(float)*4); }
void fzMath_mat4OrthoProjection(fzFloat left, fzFloat right, fzFloat bottom, fzFloat top, fzFloat nearVal, fzFloat farVal, float* output ) { FZ_ASSERT(output != NULL, "Output pointer cannot be NULL."); fzFloat tx = -((right + left) / (right - left)); fzFloat ty = -((top + bottom) / (top - bottom)); fzFloat tz = -((farVal + nearVal) / (farVal - nearVal)); fzMath_mat4Identity(output); output[0] = 2 / (right - left); output[5] = 2 / (top - bottom); output[10] = -2 / (farVal - nearVal); output[12] = tx; output[13] = ty; output[14] = tz; }