|
|
261920 |
|
|
|
261920 |
typedef struct {
|
|
|
261920 |
union {
|
|
|
261920 |
struct { double x, y, z; };
|
|
|
261920 |
struct { double a[3]; };
|
|
|
261920 |
};
|
|
|
261920 |
} Vector3;
|
|
|
261920 |
|
|
|
261920 |
typedef struct {
|
|
|
261920 |
union {
|
|
|
261920 |
struct { Vector3 xyz; };
|
|
|
261920 |
struct { double x, y, z, w; };
|
|
|
261920 |
struct { double a[4]; };
|
|
|
261920 |
};
|
|
|
261920 |
} Vector4;
|
|
|
261920 |
|
|
|
261920 |
typedef struct {
|
|
|
261920 |
union {
|
|
|
261920 |
struct { double m[4][4]; };
|
|
|
261920 |
struct { double m00, m01, m02, m03,
|
|
|
261920 |
m10, m11, m12, m13,
|
|
|
261920 |
m20, m21, m22, m23,
|
|
|
261920 |
m30, m31, m32, m33; };
|
|
|
261920 |
struct { Vector4 ox, oy, oz, ow; };
|
|
|
261920 |
struct { double a[16]; };
|
|
|
261920 |
};
|
|
|
261920 |
} Matrix;
|
|
|
261920 |
|
|
|
261920 |
|
|
|
261920 |
void vector3Set(Vector3 *vector, double x, double y, double z)
|
|
|
261920 |
{ vector->x = x; vector->y = y; vector->z = z; }
|
|
|
261920 |
|
|
|
261920 |
void vector3Copy(Vector3 *vector, const Vector3 *a)
|
|
|
261920 |
{ vector3Set(vector, a->x, a->y, a->z); }
|
|
|
261920 |
|
|
|
261920 |
void vector3Zero(Vector3 *vector)
|
|
|
261920 |
{ vector3Set(vector, 0, 0, 0); }
|
|
|
261920 |
|
|
|
261920 |
double vector3Dot(Vector3 *a, Vector3 *b)
|
|
|
261920 |
{ return a->x*b->x + a->y*b->y + a->z*b->z; }
|
|
|
261920 |
|
|
|
261920 |
|
|
|
261920 |
void vector4Set(Vector4 *vector, double x, double y, double z, double w)
|
|
|
261920 |
{ vector->x = x; vector->y = y; vector->z = z; vector->w = w; }
|
|
|
261920 |
|
|
|
261920 |
void vector4Copy(Vector4 *vector, const Vector4 *a)
|
|
|
261920 |
{ vector4Set(vector, a->x, a->y, a->z, a->w); }
|
|
|
261920 |
|
|
|
261920 |
void vector4Zero(Vector4 *vector)
|
|
|
261920 |
{ vector4Set(vector, 0, 0, 0, 0); }
|
|
|
261920 |
|
|
|
261920 |
void vector4PespectiveDivide(Vector4 *vector, const Vector4 *a)
|
|
|
261920 |
{ double k = 1/a->w; vector4Set(vector, a->x*k, a->y*k, a->z*k, 1); }
|
|
|
261920 |
|
|
|
261920 |
void vector4MultMatrix(Vector4 *vector, const Vector4 *a, const Matrix *b) {
|
|
|
261920 |
vector4Set(vector, b->ox.x*a->x + b->oy.x*a->y + b->oz.x*a->z + b->ow.x*a->w,
|
|
|
261920 |
b->ox.y*a->x + b->oy.y*a->y + b->oz.y*a->z + b->ow.y*a->w,
|
|
|
261920 |
b->ox.z*a->x + b->oy.z*a->y + b->oz.z*a->z + b->ow.z*a->w,
|
|
|
261920 |
b->ox.w*a->x + b->oy.w*a->y + b->oz.w*a->z + b->ow.w*a->w );
|
|
|
261920 |
}
|
|
|
261920 |
|
|
|
261920 |
|
|
|
261920 |
void matrixCopy(Matrix *matrix, const Matrix *a) {
|
|
|
261920 |
vector4Copy(&matrix->ox, &a->ox);
|
|
|
261920 |
vector4Copy(&matrix->oy, &a->oy);
|
|
|
261920 |
vector4Copy(&matrix->oz, &a->oz);
|
|
|
261920 |
vector4Copy(&matrix->ow, &a->ow);
|
|
|
261920 |
}
|
|
|
261920 |
|
|
|
261920 |
void matrixMultMatrix(Matrix *matrix, const Matrix *a, const Matrix *b) {
|
|
|
261920 |
if (matrix == a || matrix == b) {
|
|
|
261920 |
Matrix m;
|
|
|
261920 |
matrixMultMatrix(&m, a, b);
|
|
|
261920 |
matrixCopy(matrix, &m);
|
|
|
261920 |
return;
|
|
|
261920 |
}
|
|
|
261920 |
vector4MultMatrix(&matrix->ox, &a->ox, b);
|
|
|
261920 |
vector4MultMatrix(&matrix->oy, &a->oy, b);
|
|
|
261920 |
vector4MultMatrix(&matrix->oz, &a->oz, b);
|
|
|
261920 |
vector4MultMatrix(&matrix->ow, &a->ow, b);
|
|
|
261920 |
}
|
|
|
261920 |
|
|
|
261920 |
void matrixZero(Matrix *matrix) {
|
|
|
261920 |
vector4Zero( &matrix->ox );
|
|
|
261920 |
vector4Zero( &matrix->oy );
|
|
|
261920 |
vector4Zero( &matrix->oz );
|
|
|
261920 |
vector4Zero( &matrix->ow );
|
|
|
261920 |
}
|
|
|
261920 |
|
|
|
261920 |
void matrixSetIdentity(Matrix *matrix) {
|
|
|
261920 |
vector4Set( &matrix->ox, 1, 0, 0, 0 );
|
|
|
261920 |
vector4Set( &matrix->oy, 0, 1, 0, 0 );
|
|
|
261920 |
vector4Set( &matrix->oz, 0, 0, 1, 0 );
|
|
|
261920 |
vector4Set( &matrix->ow, 0, 0, 0, 1 );
|
|
|
261920 |
}
|
|
|
261920 |
|
|
|
261920 |
void matrixSetRotation(Matrix *matrix, double angle, double x, double y, double z) {
|
|
|
261920 |
double s = sin(angle/180.0*PI);
|
|
|
261920 |
double c = cos(angle/180.0*PI);
|
|
|
261920 |
double cc = 1 - c, cx = x*cc, cy = y*cc, cz=z*cc;
|
|
|
261920 |
vector4Set( &matrix->ox, x*cx + c , y*cx + z*s, z*cx - y*s, 0 );
|
|
|
261920 |
vector4Set( &matrix->oy, x*cy - z*s, y*cy + c , z*cy + x*s, 0 );
|
|
|
261920 |
vector4Set( &matrix->oz, x*cz + y*s, y*cz - x*s, z*cz + c , 0 );
|
|
|
261920 |
vector4Set( &matrix->ow, 0 , 0 , 0 , 1 );
|
|
|
261920 |
}
|
|
|
261920 |
|
|
|
261920 |
void matrixSetPerspective(Matrix *matrix, double fovy, double aspect, double zNear, double zFar) {
|
|
|
261920 |
double f = 1/tan(0.5*fovy/180.0*PI);
|
|
|
261920 |
double d = 1/(zNear - zFar);
|
|
|
261920 |
vector4Set( &matrix->ox, f/aspect, 0, 0 , 0 );
|
|
|
261920 |
vector4Set( &matrix->oy, 0 , f, 0 , 0 );
|
|
|
261920 |
vector4Set( &matrix->oz, 0 , 0, (zNear + zFar)*d, -1 );
|
|
|
261920 |
vector4Set( &matrix->ow, 0 , 0, 2*zNear*zFar*d , 0 );
|
|
|
261920 |
}
|
|
|
261920 |
|
|
|
261920 |
void matrixInvert(Matrix *matrix, const Matrix *a) {
|
|
|
261920 |
if (matrix == a) {
|
|
|
261920 |
Matrix m;
|
|
|
261920 |
matrixInvert(&m, a);
|
|
|
261920 |
matrixCopy(matrix, &m);
|
|
|
261920 |
return;
|
|
|
261920 |
}
|
|
|
261920 |
|
|
|
261920 |
matrix->m00 = a->m11*(a->m22*a->m33 - a->m23*a->m32) + a->m12*(a->m23*a->m31 - a->m21*a->m33) + a->m13*(a->m21*a->m32 - a->m22*a->m31);
|
|
|
261920 |
matrix->m10 = a->m10*(a->m23*a->m32 - a->m22*a->m33) + a->m12*(a->m20*a->m33 - a->m23*a->m30) + a->m13*(a->m22*a->m30 - a->m20*a->m32);
|
|
|
261920 |
matrix->m20 = a->m10*(a->m21*a->m33 - a->m23*a->m31) + a->m11*(a->m23*a->m30 - a->m20*a->m33) + a->m13*(a->m20*a->m31 - a->m21*a->m30);
|
|
|
261920 |
matrix->m30 = a->m10*(a->m22*a->m31 - a->m21*a->m32) + a->m11*(a->m20*a->m32 - a->m22*a->m30) + a->m12*(a->m21*a->m30 - a->m20*a->m31);
|
|
|
261920 |
|
|
|
261920 |
double det = a->m00*matrix->m00 + a->m01*matrix->m10 + a->m02*matrix->m20 + a->m03*matrix->m30;
|
|
|
261920 |
if (fabs(det) <= 1e-10)
|
|
|
261920 |
{ matrixZero(matrix); return; }
|
|
|
261920 |
det = 1/det;
|
|
|
261920 |
matrix->m00 *= det;
|
|
|
261920 |
matrix->m10 *= det;
|
|
|
261920 |
matrix->m20 *= det;
|
|
|
261920 |
matrix->m30 *= det;
|
|
|
261920 |
|
|
|
261920 |
matrix->m01 = det*(a->m01*(a->m23*a->m32 - a->m22*a->m33) + a->m02*(a->m21*a->m33 - a->m23*a->m31) + a->m03*(a->m22*a->m31 - a->m21*a->m32));
|
|
|
261920 |
matrix->m11 = det*(a->m00*(a->m22*a->m33 - a->m23*a->m32) + a->m02*(a->m23*a->m30 - a->m20*a->m33) + a->m03*(a->m20*a->m32 - a->m22*a->m30));
|
|
|
261920 |
matrix->m21 = det*(a->m00*(a->m23*a->m31 - a->m21*a->m33) + a->m01*(a->m20*a->m33 - a->m23*a->m30) + a->m03*(a->m21*a->m30 - a->m20*a->m31));
|
|
|
261920 |
matrix->m31 = det*(a->m00*(a->m21*a->m32 - a->m22*a->m31) + a->m01*(a->m22*a->m30 - a->m20*a->m32) + a->m02*(a->m20*a->m31 - a->m21*a->m30));
|
|
|
261920 |
matrix->m02 = det*(a->m01*(a->m12*a->m33 - a->m13*a->m32) + a->m02*(a->m13*a->m31 - a->m11*a->m33) + a->m03*(a->m11*a->m32 - a->m12*a->m31));
|
|
|
261920 |
matrix->m12 = det*(a->m00*(a->m13*a->m32 - a->m12*a->m33) + a->m02*(a->m10*a->m33 - a->m13*a->m30) + a->m03*(a->m12*a->m30 - a->m10*a->m32));
|
|
|
261920 |
matrix->m22 = det*(a->m00*(a->m11*a->m33 - a->m13*a->m31) + a->m01*(a->m13*a->m30 - a->m10*a->m33) + a->m03*(a->m10*a->m31 - a->m11*a->m30));
|
|
|
261920 |
matrix->m32 = det*(a->m00*(a->m12*a->m31 - a->m11*a->m32) + a->m01*(a->m10*a->m32 - a->m12*a->m30) + a->m02*(a->m11*a->m30 - a->m10*a->m31));
|
|
|
261920 |
matrix->m03 = det*(a->m01*(a->m13*a->m22 - a->m12*a->m23) + a->m02*(a->m11*a->m23 - a->m13*a->m21) + a->m03*(a->m12*a->m21 - a->m11*a->m22));
|
|
|
261920 |
matrix->m13 = det*(a->m00*(a->m12*a->m23 - a->m13*a->m22) + a->m02*(a->m13*a->m20 - a->m10*a->m23) + a->m03*(a->m10*a->m22 - a->m12*a->m20));
|
|
|
261920 |
matrix->m23 = det*(a->m00*(a->m13*a->m21 - a->m11*a->m23) + a->m01*(a->m10*a->m23 - a->m13*a->m20) + a->m03*(a->m11*a->m20 - a->m10*a->m21));
|
|
|
261920 |
matrix->m33 = det*(a->m00*(a->m11*a->m22 - a->m12*a->m21) + a->m01*(a->m12*a->m20 - a->m10*a->m22) + a->m02*(a->m10*a->m21 - a->m11*a->m20));
|
|
|
261920 |
}
|