1 #include "Common/Math/lin/matrix4x4.h"
2 
3 #include <cstdio>
4 
5 #include "Common/Math/lin/vec3.h"
6 #include "Common/Math/fast/fast_matrix.h"
7 
8 #ifdef _WIN32
9 #undef far
10 #undef near
11 #endif
12 
13 // See http://code.google.com/p/oolongengine/source/browse/trunk/Oolong+Engine2/Math/neonmath/neon_matrix_impl.cpp?spec=svn143&r=143	when we need speed
14 // no wait. http://code.google.com/p/math-neon/
15 
16 namespace Lin {
17 
transpose() const18 Matrix4x4 Matrix4x4::transpose() const
19 {
20 	Matrix4x4 out;
21 	out.xx = xx;out.xy = yx;out.xz = zx;out.xw = wx;
22 	out.yx = xy;out.yy = yy;out.yz = zy;out.yw = wy;
23 	out.zx = xz;out.zy = yz;out.zz = zz;out.zw = wz;
24 	out.wx = xw;out.wy = yw;out.wz = zw;out.ww = ww;
25 	return out;
26 }
27 
operator *(const Matrix4x4 & other) const28 Matrix4x4 Matrix4x4::operator * (const Matrix4x4 &other) const
29 {
30 	Matrix4x4 temp;
31 	fast_matrix_mul_4x4(temp.m, other.m, this->m);
32 	return temp;
33 }
34 
setViewFrame(const Vec3 & pos,const Vec3 & vRight,const Vec3 & vView,const Vec3 & vUp)35 void Matrix4x4::setViewFrame(const Vec3 &pos, const Vec3 &vRight, const Vec3 &vView, const Vec3 &vUp) {
36 	xx = vRight.x; xy = vUp.x; xz=vView.x; xw = 0.0f;
37 	yx = vRight.y; yy = vUp.y; yz=vView.y; yw = 0.0f;
38 	zx = vRight.z; zy = vUp.z; zz=vView.z; zw = 0.0f;
39 
40 	wx = -pos * vRight;
41 	wy = -pos * vUp;
42 	wz = -pos * vView;
43 	ww = 1.0f;
44 }
45 
setOrtho(float left,float right,float bottom,float top,float near,float far)46 void Matrix4x4::setOrtho(float left, float right, float bottom, float top, float near, float far) {
47 	empty();
48 	xx = 2.0f / (right - left);
49 	yy = 2.0f / (top - bottom);
50 	zz = 2.0f / (far - near);
51 	wx = -(right + left) / (right - left);
52 	wy = -(top + bottom) / (top - bottom);
53 	wz = -(far + near) / (far - near);
54 	ww = 1.0f;
55 }
56 
setOrthoD3D(float left,float right,float bottom,float top,float near,float far)57 void Matrix4x4::setOrthoD3D(float left, float right, float bottom, float top, float near, float far) {
58 	empty();
59 	xx = 2.0f / (right - left);
60 	yy = 2.0f / (top - bottom);
61 	zz = 1.0f / (far - near);
62 	wx = -(right + left) / (right - left);
63 	wy = -(top + bottom) / (top - bottom);
64 	wz = -near / (far - near);
65 	ww = 1.0f;
66 }
67 
setOrthoVulkan(float left,float right,float top,float bottom,float near,float far)68 void Matrix4x4::setOrthoVulkan(float left, float right, float top, float bottom, float near, float far) {
69 	empty();
70 	xx = 2.0f / (right - left);
71 	yy = 2.0f / (bottom - top);
72 	zz = 1.0f / (far - near);
73 	wx = -(right + left) / (right - left);
74 	wy = -(top + bottom) / (bottom - top);
75 	wz = -near / (far - near);
76 	ww = 1.0f;
77 }
78 
toText(char * buffer,int len) const79 void Matrix4x4::toText(char *buffer, int len) const {
80 	snprintf(buffer, len, "%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n",
81 		xx,xy,xz,xw,
82 		yx,yy,yz,yw,
83 		zx,zy,zz,zw,
84 		wx,wy,wz,ww);
85 	buffer[len - 1] = '\0';
86 }
87 
print() const88 void Matrix4x4::print() const {
89 	char buffer[256];
90 	toText(buffer, 256);
91 	puts(buffer);
92 }
93 
94 }
95