Exemplo n.º 1
0
Matrix4x4 Matrix::Scale(float const& scale)
{
    return glm::scale(Matrix4x4(), Vector3f(scale));
}
Exemplo n.º 2
0
// @returns the translation matrix described by the vector
Matrix4x4 getTransMat(const Vector3 &v) {
   return Matrix4x4(1, 0, 0, v[0], 
                    0, 1, 0, v[1], 
                    0, 0, 1, v[2], 
                    0, 0, 0, 1);
}
Exemplo n.º 3
0
// scale
Matrix4x4 Matrix::Scale(Vector3f const& scale)
{
    return glm::scale(Matrix4x4(), scale);
}
Exemplo n.º 4
0
Matrix4x4 Matrix4x4::Transpose(const Matrix4x4& mat) {
	return Matrix4x4(mat.m[0][0], mat.m[1][0], mat.m[2][0], mat.m[3][0],
					 mat.m[0][1], mat.m[1][1], mat.m[2][1], mat.m[3][1],
					 mat.m[0][2], mat.m[1][2], mat.m[2][2], mat.m[3][2],
					 mat.m[0][3], mat.m[1][3], mat.m[2][3], mat.m[3][3]);
}
Exemplo n.º 5
0
#include "Transform.hpp"

Transform Transform::identity = Transform(Matrix4x4(), Matrix4x4());

BBox Transform::operator()(const BBox& b) const {
#if 1
	const Transform &M = *this;
	BBox ret(M(Point(b.pmin.x, b.pmin.y, b.pmin.z)));
	ret = combine(ret, M(Point(b.pmax.x, b.pmin.y, b.pmin.z)));
	ret = combine(ret, M(Point(b.pmin.x, b.pmax.y, b.pmin.z)));
	ret = combine(ret, M(Point(b.pmin.x, b.pmin.y, b.pmax.z)));
	ret = combine(ret, M(Point(b.pmin.x, b.pmax.y, b.pmax.z)));
	ret = combine(ret, M(Point(b.pmax.x, b.pmax.y, b.pmin.z)));
	ret = combine(ret, M(Point(b.pmax.x, b.pmin.y, b.pmax.z)));
	ret = combine(ret, M(Point(b.pmax.x, b.pmax.y, b.pmax.z)));
	return ret;
#else
	// note the fact that the eight corner points are linear combinations 
	//	of three axis-aligned basis vectors and a single corner point
	// PBRT p104 exercise 2.1
	const Transform & T = *this;
	auto d = b.pmax - b.pmin;
	auto x = T(Vec3(d.x, 0, 0));
	auto y = T(Vec3(0, d.y, 0));
	auto z = T(Vec3(0, 0, d.z));
	auto pmin = T(b.pmin);
	auto pmax = pmin;
#define NEGTIVE(v) ((v) < 0 ? (v) : 0)
#define POSITIVE(v) ((v) > 0 ? (v) : 0)
	pmin.x += NEGTIVE(x.x) + NEGTIVE(y.x) + NEGTIVE(z.x);
	pmin.y += NEGTIVE(x.y) + NEGTIVE(y.y) + NEGTIVE(z.y);
Exemplo n.º 6
0
Matrix4x4 Matrix::Translate(float const& x, float const& y, float const& z)
{
    return glm::translate(Matrix4x4(), Vector3f(x, y, z));
}
Exemplo n.º 7
0
 Matrix4x4 transpose() const
 {
   return Matrix4x4(getColumn(0), getColumn(1), 
                     getColumn(2), getColumn(3));
 }
Exemplo n.º 8
0
Matrix4x4 Matrix4x4::Identity( )
{
    return Matrix4x4( identity );
}
Exemplo n.º 9
0
Matrix4x4 Matrix4x4::Zero( )
{
    return Matrix4x4( zero );
}
Exemplo n.º 10
0
	/**
	*<Summary>
	* Returns translated passed position in calling GameObject Coorinate system
	*</Summary>
	*/
	Vector3D GameObject::getTranslatedPosition(Vector3D i_position)
	{
		return((getTranslationMatrix()* Matrix4x4(i_position)).getPositionFromMatrix4x4());
	}
Exemplo n.º 11
0
#include "Matrix4x4.h"
#include "Vector3.h"
#include <exception>
#include "MathHelper.h"

static const Matrix4x4& identity = Matrix4x4( 1, 1, 1, 1,
                                             1, 1, 1, 1,
                                             1, 1, 1, 1,
                                             1, 1, 1, 1, "identity" );

static const Matrix4x4& zero = Matrix4x4( 0, 0, 0, 0,
                                         0, 0, 0, 0,
                                         0, 0, 0, 0,
                                         0, 0, 0, 0, "zero" );

Matrix4x4::Matrix4x4( float m00, float m10, float m20, float m30, float m01, float m11, float m21, float m31, float m02, float m12, float m22, float m32, float m03, float m13, float m23, float m33, std::string id )
    : ID( id ), Data( new float[ 16 ]{ m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33 } )
{

}

Matrix4x4::Matrix4x4( const Matrix4x4& m )
{
    this->ID = m.ID + " copy";
    this->Data = new float[ m.Dimensions * m.Dimensions ];
    for ( int x = 0; x < this->Dimensions; x++ )
        for ( int y = 0; y < this->Dimensions; y++ )
            this->SetValue( x, y, m.GetValue( x, y ) );
}

Matrix4x4::~Matrix4x4( )
Exemplo n.º 12
0
 Matrix4x4 Matrix4x4::operator* (const Matrix4x4& m) const {
   return Matrix4x4();	       // TODO
 }
Exemplo n.º 13
0
 Matrix4x4 Matrix4x4::translate(csgjs_real x, csgjs_real y, csgjs_real z) {
   return Matrix4x4(1,0,0,0,
                    0,1,0,0,
                    0,0,1,0,
                    x,y,z,1);
 }
Exemplo n.º 14
0
/**
  operator *
  Matrix post-multiplication operator
  @param rhs The matrix to post multiply the matrix by
  @return A matrix containing the result of multiplying the two matrices
*/
Matrix4x4 Matrix4x4::operator * (const Matrix4x4& rhs)
{
  float tmp[16];
	// Post-Multiply Matrices
  // so that transformations go right to left

  tmp[0] = rhs._mat[0]  * _mat[0] +
           rhs._mat[4]  * _mat[1] +
           rhs._mat[8]  * _mat[2] +
           rhs._mat[12] * _mat[3];

  tmp[4] = rhs._mat[0]  * _mat[4] +
           rhs._mat[4]  * _mat[5] +
           rhs._mat[8]  * _mat[6] +
           rhs._mat[12] * _mat[7];

  tmp[8] = rhs._mat[0]  * _mat[8] +
           rhs._mat[4]  * _mat[9] +
           rhs._mat[8]  * _mat[10] +
           rhs._mat[12] * _mat[11];

  tmp[12]= rhs._mat[0]  * _mat[12] +
           rhs._mat[4]  * _mat[13] +
           rhs._mat[8]  * _mat[14] +
           rhs._mat[12] * _mat[15];


  tmp[1] = rhs._mat[1]  * _mat[0] +
           rhs._mat[5]  * _mat[1] +
           rhs._mat[9]  * _mat[2] +
           rhs._mat[13] * _mat[3];

  tmp[5] = rhs._mat[1]  * _mat[4] +
           rhs._mat[5]  * _mat[5] +
           rhs._mat[9]  * _mat[6] +
           rhs._mat[13] * _mat[7];

  tmp[9] = rhs._mat[1]  * _mat[8] +
           rhs._mat[5]  * _mat[9] +
           rhs._mat[9]  * _mat[10] +
           rhs._mat[13] * _mat[11];

  tmp[13]= rhs._mat[1]  * _mat[12] +
           rhs._mat[5]  * _mat[13] +
           rhs._mat[9]  * _mat[14] +
           rhs._mat[13] * _mat[15];


  tmp[2] = rhs._mat[2]  * _mat[0] +
           rhs._mat[6]  * _mat[1] +
           rhs._mat[10] * _mat[2] +
           rhs._mat[14] * _mat[3];

  tmp[6] = rhs._mat[2]  * _mat[4] +
           rhs._mat[6]  * _mat[5] +
           rhs._mat[10] * _mat[6] +
           rhs._mat[14] * _mat[7];

  tmp[10]= rhs._mat[2]  * _mat[8] +
           rhs._mat[6]  * _mat[9] +
           rhs._mat[10] * _mat[10] +
           rhs._mat[14] * _mat[11];

  tmp[14]= rhs._mat[2]  * _mat[12] +
           rhs._mat[6]  * _mat[13] +
           rhs._mat[10] * _mat[14] +
           rhs._mat[14] * _mat[15];


  tmp[3] = rhs._mat[3]  * _mat[0] +
           rhs._mat[7]  * _mat[1] +
           rhs._mat[11] * _mat[2] +
           rhs._mat[15] * _mat[3];

  tmp[7] = rhs._mat[3]  * _mat[4] +
           rhs._mat[7]  * _mat[5] +
           rhs._mat[11] * _mat[6] +
           rhs._mat[15] * _mat[7];

  tmp[11]= rhs._mat[3]  * _mat[8] +
           rhs._mat[7]  * _mat[9] +
           rhs._mat[11] * _mat[10] +
           rhs._mat[15] * _mat[11];

  tmp[15]= rhs._mat[3]  * _mat[12] +
           rhs._mat[7]  * _mat[13] +
           rhs._mat[11] * _mat[14] +
           rhs._mat[15] * _mat[15];

	return Matrix4x4(tmp[0], tmp[4],  tmp[8], tmp[12],
                   tmp[1], tmp[5],  tmp[9], tmp[13],
                   tmp[2], tmp[6], tmp[10], tmp[14],
                   tmp[3], tmp[7], tmp[11], tmp[15]);
}
Exemplo n.º 15
0
Matrix4x4 Matrix::Scale(float const& x, float const& y, float const& z)
{
    return glm::scale(Matrix4x4(), Vector3f(x, y, z));
}
Exemplo n.º 16
0
void Viewer::reset()
{
	if ( !m_initflag )
	{
		update_mode( MODELROTATE );
	}

	// Default mouse information
	m_button1 = false;
	m_button2 = false;
	m_button3 = false;
	m_ixpos   = 0.0;
	m_xpos    = 0.0;
	m_iypos   = 0.0;
	m_ypos    = 0.0;
	m_txpos   = 0.0;

	// Initialize viewport
	for ( int i = 0; i < 4; i += 1 )
	{
		m_viewport[i] = ( Point2D() );
	}

	// Default FOV of 30
	m_fov  = 30.0;

	// Set the default far and near plane values
	m_near = 2.0;
	m_far  = 20.0;

	// Initialize all the transformation matrices
	m_projection = Matrix4x4();
	m_modelling  = Matrix4x4();
	m_viewing    = Matrix4x4();
	m_scaling    = Matrix4x4();
	// Start off by pushing the cube back into the screen
	Vector4D row1, row2, row3, row4;
	row1      = Vector4D( 1,   0,  0, 0 );
	row2      = Vector4D( 0,   1,  0, 0 );
	row3      = Vector4D( 0,   0,  1, 8 );
	row4      = Vector4D( 0,   0,  0, 1 );
	m_viewing = Matrix4x4( row1, row2, row3, row4 );

	// Initialize the unit cubes
	m_unitCube[0] = ( Point3D( 1.0, -1.0, -1.0) );
	m_unitCube[1] = ( Point3D(-1.0, -1.0, -1.0) );
	m_unitCube[2] = ( Point3D(-1.0,  1.0, -1.0) );
	m_unitCube[3] = ( Point3D( 1.0,  1.0, -1.0) );
	m_unitCube[4] = ( Point3D(-1.0, -1.0,  1.0) );
	m_unitCube[5] = ( Point3D( 1.0, -1.0,  1.0) );
	m_unitCube[6] = ( Point3D( 1.0,  1.0,  1.0) );
	m_unitCube[7] = ( Point3D(-1.0,  1.0,  1.0) );
	for ( int i = 0; i < 8; i += 1 )
	{
		m_unitCubeTrans[i] = ( Point3D() );
	}

	// Initialize the gnomons
	m_gnomon[0] = ( Point3D(0.0, 0.0, 0.0) );
	m_gnomon[1] = ( Point3D(0.5, 0.0, 0.0) );
	m_gnomon[2] = ( Point3D(0.0, 0.5, 0.0) );
	m_gnomon[3] = ( Point3D(0.0, 0.0, 0.5) );
	for ( int i = 0; i < 4; i += 1 )
	{
		m_gnomonTrans[i] = ( Point3D() );
	}

	m_viewflag = false;
	m_initflag = false;

	// Initialize the perspective
	set_perspective( m_fov, 1, m_near, m_far );
}
Exemplo n.º 17
0
// translate
Matrix4x4 Matrix::Translate(Vector3f const& translateAmount)
{
    return glm::translate(Matrix4x4(), translateAmount);
}
Exemplo n.º 18
0
void
LayerManagerComposite::PopGroupForLayerEffects(RefPtr<CompositingRenderTarget> aPreviousTarget,
                                               IntRect aClipRect,
                                               bool aGrayscaleEffect,
                                               bool aInvertEffect,
                                               float aContrastEffect)
{
  MOZ_ASSERT(mTwoPassTmpTarget);

  // This is currently true, so just making sure that any new use of this
  // method is flagged for investigation
  MOZ_ASSERT(aInvertEffect || aGrayscaleEffect || aContrastEffect != 0.0);

  mCompositor->SetRenderTarget(aPreviousTarget);

  EffectChain effectChain(RootLayer());
  Matrix5x4 effectMatrix;
  if (aGrayscaleEffect) {
    // R' = G' = B' = luminance
    // R' = 0.2126*R + 0.7152*G + 0.0722*B
    // G' = 0.2126*R + 0.7152*G + 0.0722*B
    // B' = 0.2126*R + 0.7152*G + 0.0722*B
    Matrix5x4 grayscaleMatrix(0.2126f, 0.2126f, 0.2126f, 0,
                              0.7152f, 0.7152f, 0.7152f, 0,
                              0.0722f, 0.0722f, 0.0722f, 0,
                              0,       0,       0,       1,
                              0,       0,       0,       0);
    effectMatrix = grayscaleMatrix;
  }

  if (aInvertEffect) {
    // R' = 1 - R
    // G' = 1 - G
    // B' = 1 - B
    Matrix5x4 colorInvertMatrix(-1,  0,  0, 0,
                                 0, -1,  0, 0,
                                 0,  0, -1, 0,
                                 0,  0,  0, 1,
                                 1,  1,  1, 0);
    effectMatrix = effectMatrix * colorInvertMatrix;
  }

  if (aContrastEffect != 0.0) {
    // Multiplying with:
    // R' = (1 + c) * (R - 0.5) + 0.5
    // G' = (1 + c) * (G - 0.5) + 0.5
    // B' = (1 + c) * (B - 0.5) + 0.5
    float cP1 = aContrastEffect + 1;
    float hc = 0.5*aContrastEffect;
    Matrix5x4 contrastMatrix( cP1,   0,   0, 0,
                                0, cP1,   0, 0,
                                0,   0, cP1, 0,
                                0,   0,   0, 1,
                              -hc, -hc, -hc, 0);
    effectMatrix = effectMatrix * contrastMatrix;
  }

  effectChain.mPrimaryEffect = new EffectRenderTarget(mTwoPassTmpTarget);
  effectChain.mSecondaryEffects[EffectTypes::COLOR_MATRIX] = new EffectColorMatrix(effectMatrix);

  gfx::Rect clipRectF(aClipRect.x, aClipRect.y, aClipRect.width, aClipRect.height);
  mCompositor->DrawQuad(Rect(Point(0, 0), Size(mTwoPassTmpTarget->GetSize())), clipRectF, effectChain, 1.,
                        Matrix4x4());
}
Exemplo n.º 19
0
// rotate
Matrix4x4 Matrix::RotateAroundAxis(float const& angle, Vector3f const& rotateAxis)
{
    return glm::rotate(Matrix4x4(), angle, rotateAxis);
}
Exemplo n.º 20
0
void
BasicContainerLayer::ComputeEffectiveTransforms(const Matrix4x4& aTransformToSurface)
{
  // We push groups for container layers if we need to, which always
  // are aligned in device space, so it doesn't really matter how we snap
  // containers.
  Matrix residual;
  Matrix4x4 idealTransform = GetLocalTransform() * aTransformToSurface;
  if (!Extend3DContext() && !Is3DContextLeaf()) {
    // For 3D transform leaked from extended parent layer.
    idealTransform.ProjectTo2D();
  }

  if (!idealTransform.CanDraw2D()) {
    if (!Extend3DContext() ||
        (!idealTransform.Is2D() && Creates3DContextWithExtendingChildren())) {
      if (!Creates3DContextWithExtendingChildren()) {
        idealTransform.ProjectTo2D();
      }
      mEffectiveTransform = idealTransform;
      ComputeEffectiveTransformsForChildren(Matrix4x4());
      ComputeEffectiveTransformForMaskLayers(Matrix4x4());
      mUseIntermediateSurface = true;
      return;
    }

    mEffectiveTransform = idealTransform;
    ComputeEffectiveTransformsForChildren(idealTransform);
    ComputeEffectiveTransformForMaskLayers(idealTransform);
    mUseIntermediateSurface = false;
    return;
  }

  // With 2D transform or extended 3D context.

  Layer* child = GetFirstChild();
  bool hasSingleBlendingChild = false;
  if (!HasMultipleChildren() && child) {
    hasSingleBlendingChild = child->GetMixBlendMode() != CompositionOp::OP_OVER;
  }

  /* If we have a single childand it is not blending,, it can just inherit our opacity,
   * otherwise we need a PushGroup and we need to mark ourselves as using
   * an intermediate surface so our children don't inherit our opacity
   * via GetEffectiveOpacity.
   * Having a mask layer always forces our own push group
   * Having a blend mode also always forces our own push group
   */
  mUseIntermediateSurface =
    GetMaskLayer() ||
    GetForceIsolatedGroup() ||
    (GetMixBlendMode() != CompositionOp::OP_OVER && HasMultipleChildren()) ||
    (GetEffectiveOpacity() != 1.0 && (HasMultipleChildren() || hasSingleBlendingChild));

  if (!Extend3DContext()) {
    idealTransform.ProjectTo2D();
  }
  mEffectiveTransform =
    !mUseIntermediateSurface ?
    idealTransform : SnapTransformTranslation(idealTransform, &residual);
  Matrix4x4 childTransformToSurface =
    (!mUseIntermediateSurface ||
     (mUseIntermediateSurface && !Extend3DContext() /* 2D */)) ?
    idealTransform : Matrix4x4::From2D(residual);
  ComputeEffectiveTransformsForChildren(childTransformToSurface);

  ComputeEffectiveTransformForMaskLayers(aTransformToSurface);
}
Exemplo n.º 21
0
void CRenderingContext::SetupMaterial()
{
	CRenderContext& oContext = GetContext();

	if (!oContext.m_hMaterial.IsValid())
		return;

	if (!m_pShader)
		return;

	const tstring& sMaterialBlend = oContext.m_hMaterial->m_sBlend;
	if (sMaterialBlend == "alpha")
		SetBlend(BLEND_ALPHA);
	else if (sMaterialBlend == "additive")
		SetBlend(BLEND_ADDITIVE);
	else
	{
		TAssert(sMaterialBlend == "none" || !sMaterialBlend.length());
		SetBlend(BLEND_NONE);
	}

	for (auto it = m_pShader->m_asUniforms.begin(); it != m_pShader->m_asUniforms.end(); it++)
	{
		CShader::CUniform& pUniformName = it->second;
		CShader::CParameter::CUniform* pUniform = it->second.m_pDefault;

		if (pUniform)
		{
			if (pUniformName.m_sUniformType == "float")
				SetUniform(it->first.c_str(), pUniform->m_flValue);
			else if (pUniformName.m_sUniformType == "vec2")
				SetUniform(it->first.c_str(), pUniform->m_vec2Value);
			else if (pUniformName.m_sUniformType == "vec3")
				SetUniform(it->first.c_str(), pUniform->m_vecValue);
			else if (pUniformName.m_sUniformType == "vec4")
				SetUniform(it->first.c_str(), pUniform->m_vec4Value);
			else if (pUniformName.m_sUniformType == "int")
				SetUniform(it->first.c_str(), pUniform->m_iValue);
			else if (pUniformName.m_sUniformType == "bool")
				SetUniform(it->first.c_str(), pUniform->m_bValue);
			else if (pUniformName.m_sUniformType == "mat4")
			{
				TUnimplemented();
			}
			else if (pUniformName.m_sUniformType == "sampler2D")
			{
				TUnimplemented();
			}
			else
				TUnimplemented();
		}
		else
		{
			if (pUniformName.m_sUniformType == "float")
				SetUniform(it->first.c_str(), 0.0f);
			else if (pUniformName.m_sUniformType == "vec2")
				SetUniform(it->first.c_str(), Vector2D());
			else if (pUniformName.m_sUniformType == "vec3")
				SetUniform(it->first.c_str(), Vector());
			else if (pUniformName.m_sUniformType == "vec4")
				SetUniform(it->first.c_str(), Vector4D());
			else if (pUniformName.m_sUniformType == "int")
				SetUniform(it->first.c_str(), 0);
			else if (pUniformName.m_sUniformType == "bool")
				SetUniform(it->first.c_str(), false);
			else if (pUniformName.m_sUniformType == "mat4")
				SetUniform(it->first.c_str(), Matrix4x4());
			else if (pUniformName.m_sUniformType == "sampler2D")
				SetUniform(it->first.c_str(), 0);
			else
				TUnimplemented();
		}
	}

	for (size_t i = 0; i < oContext.m_hMaterial->m_aParameters.size(); i++)
	{
		auto& oParameter = oContext.m_hMaterial->m_aParameters[i];
		CShader::CParameter* pShaderParameter = oParameter.m_pShaderParameter;

		TAssert(pShaderParameter);
		if (!pShaderParameter)
			continue;

		for (size_t j = 0; j < pShaderParameter->m_aActions.size(); j++)
		{
			auto& oAction = pShaderParameter->m_aActions[j];
			tstring& sName = oAction.m_sName;
			tstring& sValue = oAction.m_sValue;
			tstring& sType = m_pShader->m_asUniforms[sName].m_sUniformType;
			if (sValue == "[value]")
			{
				if (sType == "float")
					SetUniform(sName.c_str(), oParameter.m_flValue);
				else if (sType == "vec2")
					SetUniform(sName.c_str(), oParameter.m_vec2Value);
				else if (sType == "vec3")
					SetUniform(sName.c_str(), oParameter.m_vecValue);
				else if (sType == "vec4")
					SetUniform(sName.c_str(), oParameter.m_vec4Value);
				else if (sType == "int")
					SetUniform(sName.c_str(), oParameter.m_iValue);
				else if (sType == "bool")
					SetUniform(sName.c_str(), oParameter.m_bValue);
				else if (sType == "mat4")
				{
					TUnimplemented();
				}
				else if (sType == "sampler2D")
				{
					// No op, handled below.
				}
				else
					TUnimplemented();
			}
			else
			{
				if (sType == "float")
					SetUniform(sName.c_str(), oAction.m_flValue);
				else if (sType == "vec2")
					SetUniform(sName.c_str(), oAction.m_vec2Value);
				else if (sType == "vec3")
					SetUniform(sName.c_str(), oAction.m_vecValue);
				else if (sType == "vec4")
					SetUniform(sName.c_str(), oAction.m_vec4Value);
				else if (sType == "int")
					SetUniform(sName.c_str(), oAction.m_iValue);
				else if (sType == "bool")
					SetUniform(sName.c_str(), oAction.m_bValue);
				else if (sType == "mat4")
				{
					TUnimplemented();
				}
				else if (sType == "sampler2D")
				{
					TUnimplemented();
					SetUniform(sName.c_str(), 0);
				}
				else
					TUnimplemented();
			}
		}	
	}

	for (size_t i = 0; i < m_pShader->m_asTextures.size(); i++)
	{
		if (!oContext.m_hMaterial->m_ahTextures[i].IsValid())
			continue;

		glActiveTexture(GL_TEXTURE0+i);
		glBindTexture(GL_TEXTURE_2D, (GLuint)oContext.m_hMaterial->m_ahTextures[i]->m_iGLID);
		SetUniform(m_pShader->m_asTextures[i].c_str(), (int)i);
	}
}
Exemplo n.º 22
0
Matrix4x4 Transpose(const Matrix4x4 &m) {
   return Matrix4x4(m.m[0][0], m.m[1][0], m.m[2][0], m.m[3][0],
                    m.m[0][1], m.m[1][1], m.m[2][1], m.m[3][1],
                    m.m[0][2], m.m[1][2], m.m[2][2], m.m[3][2],
                    m.m[0][3], m.m[1][3], m.m[2][3], m.m[3][3]);
}
Exemplo n.º 23
0
Matrix4x4 Matrix4x4::Inverse(const Matrix4x4& mat) {
	int indexC[4], indexR[4];
	int iPiv[4] = {0, 0, 0, 0};
	float mInv[4][4];
	
	memcpy(mInv, mat.m, 16 * sizeof(float));
	for (int i=0; i<4; i++) {
		int iRow = -1, iCol = -1;
		float big = 0;
		// Choose pivot
		for (int j = 0; j < 4; j++) {
			if (iPiv[j] != 1) {
				for (int k = 0; k < 4; k++) {
					if (iPiv[k] == 0) {
						if (fabsf(mInv[j][k]) >= big) {
							big = float(fabsf(mInv[j][k]));
							iRow = j;
							iCol = k;
						}
					}
					else if (iPiv[k] > 1) {
						//TODO
						//Error("Singular matrix in MatrixInvert");
						std::cout << "Singular matrix in MatrixInvert" << std::endl;
					}
				}
			}
		}
		++iPiv[iCol];
		// Swap rows _irow_ and _icol_ for pivot
		if (iRow != iCol) {
			for (int k = 0; k < 4; ++k)
				std::swap(mInv[iRow][k], mInv[iCol][k]);
		}
		indexR[i] = iRow;
		indexC[i] = iCol;
		if (mInv[iCol][iCol] == 0.) {
			//TODO
			//Error("Singular matrix in MatrixInvert");
			std::cout << "Singular matrix in MatrixInvert" << std::endl;
		}

		// Set $m[iCol][iCol]$ to one by scaling row _icol_ appropriately
		float pivinv = 1.f / mInv[iCol][iCol];
		mInv[iCol][iCol] = 1.f;
		for (int j = 0; j < 4; j++)
			mInv[iCol][j] *= pivinv;

		// Subtract this row from others to zero out their columns
		for (int j = 0; j < 4; j++) {
			if (j != iCol) {
				float save = mInv[j][iCol];
				mInv[j][iCol] = 0;
				for (int k = 0; k < 4; k++)
					mInv[j][k] -= mInv[iCol][k]*save;
			}
		}
	}
	// Swap columns to reflect permutation
	for (int j = 3; j >= 0; j--) {
		if (indexR[j] != indexC[j]) {
			for (int k = 0; k < 4; k++)
				std::swap(mInv[k][indexR[j]], mInv[k][indexC[j]]);
		}
	}
	return Matrix4x4(mInv);
}
Exemplo n.º 24
0
Matrix4x4 Matrix4x4::getInverse() const 
{
    int indxc[4], indxr[4];
    int ipiv[4] = { 0, 0, 0, 0 };
    
    float minv[4][4];
    memcpy(minv, m, sizeof(float[16]));
    
    for (int i = 0; i < 4; i++) {
        int irow = -1, icol = -1;
        float big = 0.f;
        
        // Choose pivot
        for (int j = 0; j < 4; j++) {
            if (ipiv[j] != 1) {
                for (int k = 0; k < 4; k++) {
                    if (ipiv[k] == 0) {
                        if (fabsf(minv[j][k]) >= big) {
                            big = float(fabsf(minv[j][k]));
                            irow = j;
                            icol = k;
                        }
                    }
                    else if (ipiv[k] > 1)
                        printf("Singular matrix in Matrix4x4::getInverse()\n");
                }
            }
        }

        assert(irow >= 0 && irow < 4);
        assert(icol >= 0 && icol < 4);
        
        ++ipiv[icol];
        // Swap rows _irow_ and _icol_ for pivot
        if (irow != icol) {
            for (int k = 0; k < 4; ++k)
                std::swap(minv[irow][k], minv[icol][k]);
        }
        
        indxr[i] = irow;
        indxc[i] = icol;
        if (minv[icol][icol] == 0.)
            printf("Singular matrix in Matrix4x4::getInverse()\n");
        
        // Set $m[icol][icol]$ to one by scaling row _icol_ appropriately
        float pivinv = 1.f / minv[icol][icol];
        minv[icol][icol] = 1.f;
        for (int j = 0; j < 4; j++)
            minv[icol][j] *= pivinv;
        
        // Subtract this row from others to zero out their columns
        for (int j = 0; j < 4; j++) {
            if (j != icol) {
                float save = minv[j][icol];
                minv[j][icol] = 0;
                for (int k = 0; k < 4; k++)
                    minv[j][k] -= minv[icol][k]*save;
            }
        }
    }
    
    // Swap columns to reflect permutation
    for (int j = 3; j >= 0; j--) {
        if (indxr[j] != indxc[j]) {
            for (int k = 0; k < 4; k++)
                std::swap(minv[k][indxr[j]], minv[k][indxc[j]]);
        }
    }
    
    return Matrix4x4(minv);
}
Exemplo n.º 25
0
Matrix4x4 Matrix4x4::operator*(const Matrix4x4 & rhs) const
{
	//Optimise for matrices in which bottom row is (0, 0, 0, 1) in both matrices
	if(	entries[3]==0.0f && entries[7]==0.0f && entries[11]==0.0f && entries[15]==1.0f	&&
		rhs.entries[3]==0.0f && rhs.entries[7]==0.0f &&
		rhs.entries[11]==0.0f && rhs.entries[15]==1.0f)
	{
		return Matrix4x4(	entries[0]*rhs.entries[0]+entries[4]*rhs.entries[1]+entries[8]*rhs.entries[2],
			entries[1]*rhs.entries[0]+entries[5]*rhs.entries[1]+entries[9]*rhs.entries[2],
			entries[2]*rhs.entries[0]+entries[6]*rhs.entries[1]+entries[10]*rhs.entries[2],
			0.0f,
			entries[0]*rhs.entries[4]+entries[4]*rhs.entries[5]+entries[8]*rhs.entries[6],
			entries[1]*rhs.entries[4]+entries[5]*rhs.entries[5]+entries[9]*rhs.entries[6],
			entries[2]*rhs.entries[4]+entries[6]*rhs.entries[5]+entries[10]*rhs.entries[6],
			0.0f,
			entries[0]*rhs.entries[8]+entries[4]*rhs.entries[9]+entries[8]*rhs.entries[10],
			entries[1]*rhs.entries[8]+entries[5]*rhs.entries[9]+entries[9]*rhs.entries[10],
			entries[2]*rhs.entries[8]+entries[6]*rhs.entries[9]+entries[10]*rhs.entries[10],
			0.0f,
			entries[0]*rhs.entries[12]+entries[4]*rhs.entries[13]+entries[8]*rhs.entries[14]+entries[12],
			entries[1]*rhs.entries[12]+entries[5]*rhs.entries[13]+entries[9]*rhs.entries[14]+entries[13],
			entries[2]*rhs.entries[12]+entries[6]*rhs.entries[13]+entries[10]*rhs.entries[14]+entries[14],
			1.0f);
	}

	//Optimise for when bottom row of 1st matrix is (0, 0, 0, 1)
	if(	entries[3]==0.0f && entries[7]==0.0f && entries[11]==0.0f && entries[15]==1.0f)
	{
		return Matrix4x4(	entries[0]*rhs.entries[0]+entries[4]*rhs.entries[1]+entries[8]*rhs.entries[2]+entries[12]*rhs.entries[3],
			entries[1]*rhs.entries[0]+entries[5]*rhs.entries[1]+entries[9]*rhs.entries[2]+entries[13]*rhs.entries[3],
			entries[2]*rhs.entries[0]+entries[6]*rhs.entries[1]+entries[10]*rhs.entries[2]+entries[14]*rhs.entries[3],
			rhs.entries[3],
			entries[0]*rhs.entries[4]+entries[4]*rhs.entries[5]+entries[8]*rhs.entries[6]+entries[12]*rhs.entries[7],
			entries[1]*rhs.entries[4]+entries[5]*rhs.entries[5]+entries[9]*rhs.entries[6]+entries[13]*rhs.entries[7],
			entries[2]*rhs.entries[4]+entries[6]*rhs.entries[5]+entries[10]*rhs.entries[6]+entries[14]*rhs.entries[7],
			rhs.entries[7],
			entries[0]*rhs.entries[8]+entries[4]*rhs.entries[9]+entries[8]*rhs.entries[10]+entries[12]*rhs.entries[11],
			entries[1]*rhs.entries[8]+entries[5]*rhs.entries[9]+entries[9]*rhs.entries[10]+entries[13]*rhs.entries[11],
			entries[2]*rhs.entries[8]+entries[6]*rhs.entries[9]+entries[10]*rhs.entries[10]+entries[14]*rhs.entries[11],
			rhs.entries[11],
			entries[0]*rhs.entries[12]+entries[4]*rhs.entries[13]+entries[8]*rhs.entries[14]+entries[12]*rhs.entries[15],
			entries[1]*rhs.entries[12]+entries[5]*rhs.entries[13]+entries[9]*rhs.entries[14]+entries[13]*rhs.entries[15],
			entries[2]*rhs.entries[12]+entries[6]*rhs.entries[13]+entries[10]*rhs.entries[14]+entries[14]*rhs.entries[15],
			rhs.entries[15]);
	}

	//Optimise for when bottom row of 2nd matrix is (0, 0, 0, 1)
	if(	rhs.entries[3]==0.0f && rhs.entries[7]==0.0f &&
		rhs.entries[11]==0.0f && rhs.entries[15]==1.0f)
	{
		return Matrix4x4(	entries[0]*rhs.entries[0]+entries[4]*rhs.entries[1]+entries[8]*rhs.entries[2],
			entries[1]*rhs.entries[0]+entries[5]*rhs.entries[1]+entries[9]*rhs.entries[2],
			entries[2]*rhs.entries[0]+entries[6]*rhs.entries[1]+entries[10]*rhs.entries[2],
			entries[3]*rhs.entries[0]+entries[7]*rhs.entries[1]+entries[11]*rhs.entries[2],
			entries[0]*rhs.entries[4]+entries[4]*rhs.entries[5]+entries[8]*rhs.entries[6],
			entries[1]*rhs.entries[4]+entries[5]*rhs.entries[5]+entries[9]*rhs.entries[6],
			entries[2]*rhs.entries[4]+entries[6]*rhs.entries[5]+entries[10]*rhs.entries[6],
			entries[3]*rhs.entries[4]+entries[7]*rhs.entries[5]+entries[11]*rhs.entries[6],
			entries[0]*rhs.entries[8]+entries[4]*rhs.entries[9]+entries[8]*rhs.entries[10],
			entries[1]*rhs.entries[8]+entries[5]*rhs.entries[9]+entries[9]*rhs.entries[10],
			entries[2]*rhs.entries[8]+entries[6]*rhs.entries[9]+entries[10]*rhs.entries[10],
			entries[3]*rhs.entries[8]+entries[7]*rhs.entries[9]+entries[11]*rhs.entries[10],
			entries[0]*rhs.entries[12]+entries[4]*rhs.entries[13]+entries[8]*rhs.entries[14]+entries[12],
			entries[1]*rhs.entries[12]+entries[5]*rhs.entries[13]+entries[9]*rhs.entries[14]+entries[13],
			entries[2]*rhs.entries[12]+entries[6]*rhs.entries[13]+entries[10]*rhs.entries[14]+entries[14],
			entries[3]*rhs.entries[12]+entries[7]*rhs.entries[13]+entries[11]*rhs.entries[14]+entries[15]);
	}	

	return Matrix4x4(	entries[0]*rhs.entries[0]+entries[4]*rhs.entries[1]+entries[8]*rhs.entries[2]+entries[12]*rhs.entries[3],
		entries[1]*rhs.entries[0]+entries[5]*rhs.entries[1]+entries[9]*rhs.entries[2]+entries[13]*rhs.entries[3],
		entries[2]*rhs.entries[0]+entries[6]*rhs.entries[1]+entries[10]*rhs.entries[2]+entries[14]*rhs.entries[3],
		entries[3]*rhs.entries[0]+entries[7]*rhs.entries[1]+entries[11]*rhs.entries[2]+entries[15]*rhs.entries[3],
		entries[0]*rhs.entries[4]+entries[4]*rhs.entries[5]+entries[8]*rhs.entries[6]+entries[12]*rhs.entries[7],
		entries[1]*rhs.entries[4]+entries[5]*rhs.entries[5]+entries[9]*rhs.entries[6]+entries[13]*rhs.entries[7],
		entries[2]*rhs.entries[4]+entries[6]*rhs.entries[5]+entries[10]*rhs.entries[6]+entries[14]*rhs.entries[7],
		entries[3]*rhs.entries[4]+entries[7]*rhs.entries[5]+entries[11]*rhs.entries[6]+entries[15]*rhs.entries[7],
		entries[0]*rhs.entries[8]+entries[4]*rhs.entries[9]+entries[8]*rhs.entries[10]+entries[12]*rhs.entries[11],
		entries[1]*rhs.entries[8]+entries[5]*rhs.entries[9]+entries[9]*rhs.entries[10]+entries[13]*rhs.entries[11],
		entries[2]*rhs.entries[8]+entries[6]*rhs.entries[9]+entries[10]*rhs.entries[10]+entries[14]*rhs.entries[11],
		entries[3]*rhs.entries[8]+entries[7]*rhs.entries[9]+entries[11]*rhs.entries[10]+entries[15]*rhs.entries[11],
		entries[0]*rhs.entries[12]+entries[4]*rhs.entries[13]+entries[8]*rhs.entries[14]+entries[12]*rhs.entries[15],
		entries[1]*rhs.entries[12]+entries[5]*rhs.entries[13]+entries[9]*rhs.entries[14]+entries[13]*rhs.entries[15],
		entries[2]*rhs.entries[12]+entries[6]*rhs.entries[13]+entries[10]*rhs.entries[14]+entries[14]*rhs.entries[15],
		entries[3]*rhs.entries[12]+entries[7]*rhs.entries[13]+entries[11]*rhs.entries[14]+entries[15]*rhs.entries[15]);
}
Exemplo n.º 26
0
// @returns the scale matrix described by the vector
Matrix4x4 getScaleMat(const Vector3 &v) {
   return Matrix4x4(v[0], 0, 0, 0, 
                    0, v[1], 0, 0, 
                    0, 0, v[2], 0, 
                    0, 0, 0, 1);
}