void LXTransform3DSetIdentity(LXTransform3DRef r) { if ( !r) return; LXTransform3DImpl *imp = (LXTransform3DImpl *)r; setIdentityMatrix(&(imp->mat)); }
// Defines a transformation matrix mat with a scale void setScaleMatrix(float *mat, float sx, float sy, float sz) { setIdentityMatrix(mat,4); mat[0] = sx; mat[5] = sy; mat[10] = sz; }
// Defines a transformation matrix mat with a translation void setTranslationMatrix(float *mat, float x, float y, float z) { setIdentityMatrix(mat,4); mat[12] = x; mat[13] = y; mat[14] = z; }
mat4& mat4::setScalingMatrix(const vec3& scaling) { setIdentityMatrix(); setElement(0, 0, scaling.x); setElement(1, 1, scaling.y); setElement(2, 2, scaling.z); return *this; }
mat4& mat4::setTranslationMatrix(const vec3& translation) { setIdentityMatrix(); setElement(3, 0, translation.x); setElement(3, 1, translation.y); setElement(3, 2, translation.z); return *this; }
// glLoadIdentity implementation void VSML::loadIdentity(MatrixTypes aType) { setIdentityMatrix(mMatrix[aType]); #ifdef VSML_ALWAYS_SEND_TO_OPENGL matrixToGL(aType); #endif }
void setRotateMatrixZ(ofMatrix4x4 & _m, double angle) { double sine = sin(angle); double cosine = cos(angle); setIdentityMatrix(_m); _m(0,0) = cosine; _m(0,1) = -sine; _m(1,0) = sine; _m(1,1) = cosine; };
void doTranslationMatrix(float x, float y, float z, float* m) { memset(m, 0, 16 *sizeof(*m)); setIdentityMatrix(m, 4); m[3] = x; m[7] = y; m[11] = z; }
void Matrix::translate(const float x, const float y, const float z) { float mat[16]; setIdentityMatrix(mat); mat[12] = x; mat[13] = y; mat[14] = z; multMatrix(mat); }
void doRotationMatrixX(float degree, float *m) { memset(m, 0, 16 *sizeof(*m)); setIdentityMatrix(m, 4); m[5] = cos(degree); m[6] = -sin(degree); m[9] = sin(degree); m[10] = cos(degree); }
void Matrix::ortho(const float left, const float right, const float bottom, const float top, const float nearp, const float farp) { setIdentityMatrix(entries_); entries_[0] = 2 / (right - left); entries_[5] = 2 / (top - bottom); entries_[10] = -2 / (farp - nearp); entries_[12] = -(right + left) / (right - left); entries_[13] = -(top + bottom) / (top - bottom); entries_[14] = -(farp + nearp) / (farp - nearp); }
mat4& mat4::setLookAtMatrix(const vec3& forward, const vec3& up, const vec3& right) { setIdentityMatrix(); setElement(0, 0, right.x); setElement(1, 0, right.y); setElement(2, 0, right.z); setElement(0, 1, up.x); setElement(1, 1, up.y); setElement(2, 1, up.z); setElement(0, 2, forward.x); setElement(1, 2, forward.y); setElement(2, 2, forward.z); return *this; }
void Matrix::scale(const float x, const float y, const float z) { float mat[16]; setIdentityMatrix(mat); mat[0] = x; mat[5] = y; mat[10] = z; multMatrix(mat); }
// gluLookAt implementation void VSML::lookAt(float xPos, float yPos, float zPos, float xLook, float yLook, float zLook, float xUp, float yUp, float zUp) { float dir[3], right[3], up[3]; up[0] = xUp; up[1] = yUp; up[2] = zUp; dir[0] = (xLook - xPos); dir[1] = (yLook - yPos); dir[2] = (zLook - zPos); normalize(dir); crossProduct(dir,up,right); normalize(right); crossProduct(right,dir,up); normalize(up); float m1[16],m2[16]; m1[0] = right[0]; m1[4] = right[1]; m1[8] = right[2]; m1[12] = 0.0f; m1[1] = up[0]; m1[5] = up[1]; m1[9] = up[2]; m1[13] = 0.0f; m1[2] = -dir[0]; m1[6] = -dir[1]; m1[10] = -dir[2]; m1[14] = 0.0f; m1[3] = 0.0f; m1[7] = 0.0f; m1[11] = 0.0f; m1[15] = 1.0f; setIdentityMatrix(m2,4); m2[12] = -xPos; m2[13] = -yPos; m2[14] = -zPos; multMatrix(MODELVIEW, m1); multMatrix(MODELVIEW, m2); #ifdef VSML_ALWAYS_SEND_TO_OPENGL matrixToGL(MODELVIEW); #endif }
// glScale implementation with matrix selection void scale(MatrixTypes aType, float x, float y, float z) { float mat[16]; setIdentityMatrix(mat,4); mat[0] = x; mat[5] = y; mat[10] = z; multMatrix(aType,mat); }
void buildProjectionMatrix(float fov, float ratio, float nearP, float farP) { float f = 1.0f / tan(fov * PI / 360.0f); setIdentityMatrix(projMatrix,4); projMatrix[0] = f / ratio; projMatrix[1 * 4 + 1] = f; projMatrix[2 * 4 + 2] = -(nearP+farP) / (nearP - farP); projMatrix[3 * 4 + 2] = (2*farP*nearP) / (nearP - farP); projMatrix[2 * 4 + 3] = 1.0f; projMatrix[3 * 4 + 3] = 0.0f; }
// glTranslate implementation with matrix selection void translate(MatrixTypes aType, float x, float y, float z) { float mat[16]; setIdentityMatrix(mat); mat[12] = x; mat[13] = y; mat[14] = z; multMatrix(aType,mat); }
// gl Translate implementation with matrix selection void VSMatrix::translate(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z) { FLOATTYPE mat[16]; setIdentityMatrix(mat); mat[12] = x; mat[13] = y; mat[14] = z; multMatrix(mat); }
LXTransform3DRef LXTransform3DCreateWithMatrix(LX4x4Matrix *matrix) { LXTransform3DRef r = LXTransform3DCreateIdentity(); LXTransform3DImpl *imp = (LXTransform3DImpl *)r; if (matrix) imp->mat = *matrix; else setIdentityMatrix(&(imp->mat)); return r; }
void Matrix::frustum(const float left, const float right, const float bottom, const float top, const float nearp, const float farp) { setIdentityMatrix(entries_); entries_[0] = 2 * nearp / (right - left); entries_[5] = 2 * nearp / (top - bottom); entries_[8] = (right + left) / (right - left); entries_[9] = (top + bottom) / (top - bottom); entries_[10] = (farp + nearp) / (farp - nearp); entries_[11] = -1.0f; entries_[12] = 2 * farp * nearp / (farp - nearp); entries_[15] = 0.0f; }
// gl Scale implementation with matrix selection void VSMatrix::scale(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z) { FLOATTYPE mat[16]; setIdentityMatrix(mat,4); mat[0] = x; mat[5] = y; mat[10] = z; multMatrix(mat); }
void buildProjectionMatrix(float fov, float ratio, float nr, float fr) { double f = 1.0f / tan (fov * (TwoPi / 720.0)); setIdentityMatrix(projMatrix,4); projMatrix[0] = float(f) / ratio; projMatrix[1 * 4 + 1] = float(f); projMatrix[2 * 4 + 2] = (fr + nr) / (nr - fr); projMatrix[3 * 4 + 2] = (2.0f * fr * nr) / (nr - fr); projMatrix[2 * 4 + 3] = -1.0f; projMatrix[3 * 4 + 3] = 0.0f; }
void Matrix::perspective(const float fov, const float ratio, const float nearp, const float farp) { const float f = 1.0f / tan(fov * (M_PI / 360.f)); setIdentityMatrix(entries_); entries_[0] = f / ratio; entries_[5] = f; entries_[10] = (farp + nearp) / (nearp - farp); entries_[14] = (2.0f * farp * nearp) / (nearp - farp); entries_[11] = -1.0f; entries_[15] = 0.0f; }
void LXTransform3DTranslate(LXTransform3DRef r, LXFloat tx, LXFloat ty, LXFloat tz) { if ( !r) return; LX4x4Matrix mm; setIdentityMatrix(&mm); mm.m14 = tx; mm.m24 = ty; mm.m34 = tz; mm.m44 = 1.0; LXTransform3DConcatMatrix(r, &mm); }
void LXTransform3DScale(LXTransform3DRef r, LXFloat sx, LXFloat sy, LXFloat sz) { if ( !r) return; LX4x4Matrix mm; setIdentityMatrix(&mm); mm.m11 = sx; mm.m22 = sy; mm.m33 = sz; mm.m44 = 1.0; LXTransform3DConcatMatrix(r, &mm); }
LXTransform3DRef LXTransform3DCreateIdentity() { // LX4x4Matrix is a struct, but we want its size to be equal to a 4x4 array of floats -- // check that the compiler doesn't do any evil padding assert(sizeof(LX4x4Matrix) == 4*4*sizeof(LXFloat)); LXTransform3DImpl *imp = (LXTransform3DImpl *) _lx_calloc(sizeof(LXTransform3DImpl), 1); LXREF_INIT(imp, LXTransform3DTypeID(), LXTransform3DRetain, LXTransform3DRelease); setIdentityMatrix(&(imp->mat)); return (LXTransform3DRef)imp; }
// gluLookAt implementation void VSMatrix::lookAt(FLOATTYPE xPos, FLOATTYPE yPos, FLOATTYPE zPos, FLOATTYPE xLook, FLOATTYPE yLook, FLOATTYPE zLook, FLOATTYPE xUp, FLOATTYPE yUp, FLOATTYPE zUp) { FLOATTYPE dir[3], right[3], up[3]; up[0] = xUp; up[1] = yUp; up[2] = zUp; dir[0] = (xLook - xPos); dir[1] = (yLook - yPos); dir[2] = (zLook - zPos); normalize(dir); crossProduct(dir,up,right); normalize(right); crossProduct(right,dir,up); normalize(up); FLOATTYPE m1[16],m2[16]; m1[0] = right[0]; m1[4] = right[1]; m1[8] = right[2]; m1[12] = 0.0f; m1[1] = up[0]; m1[5] = up[1]; m1[9] = up[2]; m1[13] = 0.0f; m1[2] = -dir[0]; m1[6] = -dir[1]; m1[10] = -dir[2]; m1[14] = 0.0f; m1[3] = 0.0f; m1[7] = 0.0f; m1[11] = 0.0f; m1[15] = 1.0f; setIdentityMatrix(m2,4); m2[12] = -xPos; m2[13] = -yPos; m2[14] = -zPos; multMatrix(m1); multMatrix(m2); }
// gluLookAt implementation void lookAt(float xPos, float yPos, float zPos, float xLook, float yLook, float zLook, float xUp, float yUp, float zUp) { float dir[3], right[3], up[3]; up[0] = xUp; up[1] = yUp; up[2] = zUp; dir[0] = (xLook - xPos); dir[1] = (yLook - yPos); dir[2] = (zLook - zPos); normalize(dir); crossProduct(dir,up,right); normalize(right); crossProduct(right,dir,up); normalize(up); float m1[16],m2[16]; m1[0] = right[0]; m1[4] = right[1]; m1[8] = right[2]; m1[12] = 0.0f; m1[1] = up[0]; m1[5] = up[1]; m1[9] = up[2]; m1[13] = 0.0f; m1[2] = -dir[0]; m1[6] = -dir[1]; m1[10] = -dir[2]; m1[14] = 0.0f; m1[3] = 0.0f; m1[7] = 0.0f; m1[11] = 0.0f; m1[15] = 1.0f; setIdentityMatrix(m2,4); m2[12] = -xPos; m2[13] = -yPos; m2[14] = -zPos; multMatrix(VIEW, m1); multMatrix(VIEW, m2); }
// glTranslate implementation with matrix selection void VSML::translate(MatrixTypes aType, float x, float y, float z) { float mat[16]; setIdentityMatrix(mat); mat[12] = x; mat[13] = y; mat[14] = z; multMatrix(aType,mat); #ifdef VSML_ALWAYS_SEND_TO_OPENGL matrixToGL(aType); #endif }
// glScale implementation with matrix selection void VSML::scale(MatrixTypes aType, float x, float y, float z) { float mat[16]; setIdentityMatrix(mat,4); mat[0] = x; mat[5] = y; mat[10] = z; multMatrix(aType,mat); #ifdef VSML_ALWAYS_SEND_TO_OPENGL matrixToGL(aType); #endif }