Example #1
0
void Magic3D::Object::applyMatrix(Matrix4 matrix)
{
    Vector3 tmpPos = matrix.getTranslation();
    Matrix3 mRot = matrix.getUpper3x3();
    Vector3 tmpScale  = Vector3(length(mRot.getCol0()), length(mRot.getCol1()), length(mRot.getCol2()));

    mRot.setCol0(normalize(mRot.getCol0()));
    mRot.setCol1(normalize(mRot.getCol1()));
    mRot.setCol2(normalize(mRot.getCol2()));

    setPosition(tmpPos);
    setRotation(Quaternion(mRot));
    setScale(tmpScale);
}
Example #2
0
Magic3D::Matrix4 Magic3D::Object::getMatrixFromParent()
{
    Matrix4 m = getMatrix();

    if (getParent())
    {
        m = getParent()->getMatrixFromParent();

        if (getParentBone())
        {
            m = m * getParentBone()->getMatrixFromParent();
        }

        if (isParentPosition() && isParentRotation() && isParentScale())
        {
            m = m * getMatrix();
        }
        else
        {
            Vector3 tmpPos = m.getTranslation();
            Matrix3 mRot = m.getUpper3x3();
            Vector3 tmpScale  = Vector3(length(mRot.getCol0()), length(mRot.getCol1()), length(mRot.getCol2()));

            mRot.setCol0(normalize(mRot.getCol0()));
            mRot.setCol1(normalize(mRot.getCol1()));
            mRot.setCol2(normalize(mRot.getCol2()));

            m = Matrix4::identity();

            if (isParentRotation())
            {
                m = m * Matrix4(mRot, Vector3(0.0f, 0.0f, 0.0f));
            }

            if (isParentPosition())
            {
                m.setTranslation(tmpPos);
            }

            if (isParentScale())
            {
                m = appendScale(m, tmpScale); // Faster than creating and multiplying a scale transformation matrix.
            }

            m = m * getMatrix();
        }
    }

    if (billboard != eBILLBOARD_NONE)
    {
        Camera* camera = Renderer::getInstance()->getCurrentViewPort()->getPerspective();
        Matrix3 view = camera->getView().getUpper3x3();
        Matrix3 bb = Matrix3::identity();

        switch (billboard)
        {
            case eBILLBOARD_HORIZONTAL:
            {
                Vector3 v1 = Vector3(1.0f, 0.0f, 0.0f);
                Vector3 v2 = view.getCol0();

                float angle = Math::angle(v1, v2, Vector3(0.0f, 1.0f, 0.0f));

                bb = Matrix3::rotationY(angle);
                break;
            }
            case eBILLBOARD_VERTICAL:
            {
                Vector3 v1 = Vector3(0.0f, 1.0f, 0.0f);
                Vector3 v2 = view.getCol1();

                float angle = Math::angle(v1, v2, Vector3(1.0f, 0.0f, 0.0f));

                bb = Matrix3::rotationX(angle);
                break;
            }
            default:
            {
                bb = view;
                break;
            }            
        }

        bb = inverse(m.getUpper3x3()) * inverse(bb);

        m = m * Matrix4(bb, Vector3(0.0f, 0.0f, 0.0f));
    }

    return m;
}