matrix.c revision e0e0c19eefceaf5d4ec40f9466b58a771f50e799
#include <math.h>
#include <stdio.h>
#include "cr_matrix.h"
#include "cr_mem.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
static const CRmatrix identity_matrix = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
/*
* Initialize the given matrix to the identity.
*/
void
crMatrixInit(CRmatrix *m)
{
*m = identity_matrix;
}
/*
* Parse a string of 16 floats to initialize a matrix (row major order).
* If there's a parsing error, initialize the matrix to the identity.
*/
void
crMatrixInitFromString(CRmatrix *m, const char *s)
{
const char *fmt = "%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f";
const char *fmtb = "[ %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f ]";
if (n != 16) {
/* insufficient parameters */
crMatrixInit(m);
}
}
/*
* Initialize a matrix from an array of 16 values.
*/
void
crMatrixInitFromFloats(CRmatrix *m, const float *v)
{
m->m00 = v[0];
m->m01 = v[1];
m->m02 = v[2];
m->m03 = v[3];
m->m10 = v[4];
m->m11 = v[5];
m->m12 = v[6];
m->m13 = v[7];
m->m20 = v[8];
m->m21 = v[9];
m->m22 = v[10];
m->m23 = v[11];
m->m30 = v[12];
m->m31 = v[13];
m->m32 = v[14];
m->m33 = v[15];
}
void
crMatrixInitFromDoubles(CRmatrix *m, const double *v)
{
m->m00 = (float) v[0];
m->m01 = (float) v[1];
m->m02 = (float) v[2];
m->m03 = (float) v[3];
m->m10 = (float) v[4];
m->m11 = (float) v[5];
m->m12 = (float) v[6];
m->m13 = (float) v[7];
m->m20 = (float) v[8];
m->m21 = (float) v[9];
m->m22 = (float) v[10];
m->m23 = (float) v[11];
m->m30 = (float) v[12];
m->m31 = (float) v[13];
m->m32 = (float) v[14];
m->m33 = (float) v[15];
}
/* useful for debugging */
void
{
}
void
{
}
/* Return 1 if the matrices are equal, return 0 otherwise.
*/
int
{
}
/*
* Test if matrix is identity
*/
int
crMatrixIsIdentity(const CRmatrix *m)
{
}
/*
* Test if matrix is orthographic projection matrix.
*/
int
crMatrixIsOrthographic(const CRmatrix *m)
{
return m->m33 != 0.0;
}
void
{
}
/*
* Compute p = a * b
*/
void
{
CRmatrix t; /* temporary result, in case p = a or p = b */
*p = t;
}
void
{
float x = p->x;
float y = p->y;
float z = p->z;
float w = p->w;
}
void
{
double x = p->x;
double y = p->y;
double z = p->z;
double w = p->w;
}
void
{
/* Taken from Pomegranate code, trans.c.
* Note: We have our data structures reversed
*/
/* Perform transpose in asignment */
}
void
{
CRmatrix c;
*t = c;
}
/*
* Apply a translation to the given matrix.
*/
void
crMatrixTranslate(CRmatrix *m, float x, float y, float z)
{
}
/*
* Apply a rotation to the given matrix.
*/
void
{
const float one_minus_c = 1.0f - c;
float x_one_minus_c;
float y_one_minus_c;
float z_one_minus_c;
if (v_len == 0.0f)
return;
/* Normalize the vector */
if (v_len != 1.0f) {
x /= v_len;
y /= v_len;
z /= v_len;
}
/* compute some common values */
x_one_minus_c = x * one_minus_c;
y_one_minus_c = y * one_minus_c;
z_one_minus_c = z * one_minus_c;
/* Generate the terms of the rotation matrix
** from pg 325 OGL 1.1 Blue Book.
*/
crMatrixMultiply(m, m, &rot);
}
/*
* Apply a scale to the given matrix.
*/
void
crMatrixScale(CRmatrix *m, float x, float y, float z)
{
m->m00 *= x;
m->m01 *= x;
m->m02 *= x;
m->m03 *= x;
m->m10 *= y;
m->m11 *= y;
m->m12 *= y;
m->m13 *= y;
m->m20 *= z;
m->m21 *= z;
m->m22 *= z;
m->m23 *= z;
}
/*
* Make a projection matrix from frustum parameters.
*/
void
{
CRmatrix f;
f.m01 = 0.0;
f.m02 = 0.0;
f.m03 = 0.0;
f.m10 = 0.0;
f.m12 = 0.0;
f.m13 = 0.0;
f.m23 = -1.0;
f.m30 = 0.0;
f.m31 = 0.0;
f.m33 = 0.0;
crMatrixMultiply(m, m, &f);
}
void
{
crMatrixMultiply(m, m, &ortho);
}