diff -rupN ./opensg_org/Source/Base/Base/OSGMatrix.h ./opensg_math/Source/Base/Base/OSGMatrix.h --- ./opensg_org/Source/Base/Base/OSGMatrix.h 2017-01-16 11:34:53.259335300 +0100 +++ ./opensg_math/Source/Base/Base/OSGMatrix.h 2016-09-26 11:20:43.245588800 +0200 @@ -171,6 +171,11 @@ class TransformationMatrix const VectorType3f &vector3, const VectorType3f &vector4 ); + void setValue (const VectorType &vector1, + const VectorType &vector2, + const VectorType &vector3, + const VectorType &vector4 ); + void setValue (const ValueTypeT rVal00, const ValueTypeT rVal10, const ValueTypeT rVal20, diff -rupN ./opensg_org/Source/Base/Base/OSGMatrix.inl ./opensg_math/Source/Base/Base/OSGMatrix.inl --- ./opensg_org/Source/Base/Base/OSGMatrix.inl 2017-01-16 11:34:53.259335300 +0100 +++ ./opensg_math/Source/Base/Base/OSGMatrix.inl 2016-09-26 11:21:16.962447700 +0200 @@ -243,6 +243,18 @@ void TransformationMatrix::s } template inline +void TransformationMatrix::setValue(const VectorType &vector1, + const VectorType &vector2, + const VectorType &vector3, + const VectorType &vector4) +{ + _matrix[0].setValue(vector1); + _matrix[1].setValue(vector2); + _matrix[2].setValue(vector3); + _matrix[3].setValue(vector4); +} + +template inline void TransformationMatrix::setValue(const ValueTypeT rVal00, const ValueTypeT rVal10, const ValueTypeT rVal20, diff -rupN ./opensg_org/Source/Base/Base/OSGMatrixUtility.cpp ./opensg_math/Source/Base/Base/OSGMatrixUtility.cpp --- ./opensg_org/Source/Base/Base/OSGMatrixUtility.cpp 2017-01-16 11:34:53.259335300 +0100 +++ ./opensg_math/Source/Base/Base/OSGMatrixUtility.cpp 2017-01-18 09:03:37.145968500 +0100 @@ -108,11 +108,182 @@ OSG_BASE_DLLMAPPING bool MatrixOrthogona -(rRight + rLeft ) / (rRight - rLeft ), -(rTop + rBottom) / (rTop - rBottom), -(rFar + rNear ) / (rFar - rNear ), - 1.); + 1.f); return false; } +OSG_BASE_DLLMAPPING bool MatrixOrthogonal(OSG::Matrix &result, + OSG::Real32 rWidth, + OSG::Real32 rHeight, + OSG::Real32 rNear, + OSG::Real32 rFar) +{ + bool error = false; + + if(rWidth < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixOrthogonal: width " << rWidth << " very small " + << "!\n" << std::endl; + + error = true; + } + + if(rHeight < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixOrthogonal: height " << rHeight << " very small " + << "!\n" << std::endl; + + error = true; + } + + if(osgAbs(rFar - rNear) < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixOrthogonal: near " << rNear << " ~= far " << rFar + << "!\n" << std::endl; + + error = true; + } + + + if(error == true) + { + result.setIdentity(); + return true; + } + + result.setValueTransposed( + + 2.f / rWidth, + 0.f, + 0.f, + 0.f, + + 0.f, + 2.f / rHeight, + 0.f, + 0.f, + + 0.f, + 0.f, + -2.f / (rFar - rNear), + 0.f, + + 0.f, + 0.f, + -(rFar + rNear) / (rFar - rNear), + 1.f); + + return false; +} + +OSG_BASE_DLLMAPPING bool MatrixOrthogonal2D(OSG::Matrix &result, + OSG::Real32 rLeft, + OSG::Real32 rRight, + OSG::Real32 rBottom, + OSG::Real32 rTop) +{ + bool error = false; + + if(osgAbs(rRight - rLeft) < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixOrthogonal2D: right " << rRight << " ~= left " + << rLeft << "!\n" << std::endl; + + error = true; + } + + if(osgAbs(rTop - rBottom) < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixOrthogonal2D: top " << rTop << " ~= bottom " + << rBottom << "!\n" << std::endl; + + error = true; + } + + if(error == true) + { + result.setIdentity(); + return true; + } + + result.setValueTransposed( + + 2.f / (rRight - rLeft), + 0.f, + 0.f, + 0.f, + + 0.f, + 2.f / (rTop - rBottom), + 0.f, + 0.f, + + 0.f, + 0.f, + -1.f, + 0.f, + + -(rRight + rLeft ) / (rRight - rLeft ), + -(rTop + rBottom) / (rTop - rBottom), + 0.f, + 1.f); + + return false; +} + +OSG_BASE_DLLMAPPING bool MatrixOrthogonal2D(OSG::Matrix &result, + OSG::Real32 rWidth, + OSG::Real32 rHeight) +{ + bool error = false; + + if(rWidth < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixOrthogonal2D: width " << rWidth << " very small " + << "!\n" << std::endl; + + error = true; + } + + if(rHeight < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixOrthogonal2D: height " << rHeight << " very small " + << "!\n" << std::endl; + + error = true; + } + + if(error == true) + { + result.setIdentity(); + return true; + } + + result.setValueTransposed( + + 2.f / rWidth, + 0.f, + 0.f, + 0.f, + + 0.f, + 2.f / rHeight, + 0.f, + 0.f, + + 0.f, + 0.f, + -1.f, + 0.f, + + 0.f, + 0.f, + 0.f, + 1.f); + + return false; +} OSG_BASE_DLLMAPPING bool MatrixFrustum(OSG::Matrix &result, OSG::Real32 rLeft, @@ -122,9 +293,9 @@ OSG_BASE_DLLMAPPING bool MatrixFrustum(O OSG::Real32 rNear, OSG::Real32 rFar) { - Real32 dz = rFar - rNear; Real32 dx = rRight - rLeft; Real32 dy = rTop - rBottom; + Real32 dz = rFar - rNear; Real32 n2 = 2.f * rNear; result.setValueTransposed( @@ -158,7 +329,6 @@ OSG_BASE_DLLMAPPING bool MatrixPerspecti OSG::Real32 rNear, OSG::Real32 rFar) { - Real32 ct = osgTan(rFovy); bool error = false; if(rNear > rFar) @@ -199,6 +369,8 @@ OSG_BASE_DLLMAPPING bool MatrixPerspecti return true; } + Real32 ct = osgTan(rFovy); + MatrixFrustum( result, -rNear * ct * rAspect, rNear * ct * rAspect, @@ -210,6 +382,74 @@ OSG_BASE_DLLMAPPING bool MatrixPerspecti return false; } +OSG_BASE_DLLMAPPING bool MatrixPerspective(OSG::Matrix &result, + OSG::Real32 rFovy, + OSG::Real32 rWidth, + OSG::Real32 rHeight, + OSG::Real32 rNear, + OSG::Real32 rFar) +{ + bool error = false; + + if(rNear > rFar) + { + SWARNING << "MatrixPerspective: near " << rNear << " > far " << rFar + << "!\n" << std::endl; + + error = true; + } + + if(rFovy <= TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixPerspective: fovy " << rFovy << " very small!\n" + << std::endl; + + error = true; + } + + if(osgAbs(rNear - rFar) < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixPerspective: near " << rNear << " ~= far " << rFar + << "!\n" << std::endl; + + error = true; + } + + if(rWidth < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixPerspective: width " << rWidth << " very small!\n" + << std::endl; + + error = true; + } + + if(rHeight < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixPerspective: height " << rHeight << " very small!\n" + << std::endl; + + error = true; + } + + if(error) + { + result.setIdentity(); + return true; + } + + Real32 rAspect = rWidth / rHeight; + Real32 ct = osgTan(rFovy); + + MatrixFrustum( result, + -rNear * ct * rAspect, + rNear * ct * rAspect, + -rNear * ct, + rNear * ct, + rNear, + rFar ); + + return false; +} OSG_BASE_DLLMAPPING bool MatrixStereoPerspective(OSG::Matrix &projection, OSG::Matrix &projtrans, @@ -306,6 +546,133 @@ OSG_BASE_DLLMAPPING bool MatrixStereoPer return false; } +OSG_BASE_DLLMAPPING bool MatrixInfinitePerspective(OSG::Matrix &result, + OSG::Real32 rFovy, + OSG::Real32 rAspect, + OSG::Real32 rNear) +{ + // see http://www.terathon.com/gdc07_lengyel.pdf + + bool error = false; + + if(rFovy <= TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixInfinitePerspective: fovy " << rFovy << " very small!\n" + << std::endl; + + error = true; + } + + if(rAspect < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixInfinitePerspective: aspect ratio " << rAspect + << " very small!\n" << std::endl; + + error = true; + } + + if(error) + { + result.setIdentity(); + return true; + } + + Real32 r = osgTan(rFovy) * rNear; + Real32 dx = 2.f * r * rAspect; + Real32 dy = 2.f * r; + Real32 n2 = 2.f * rNear; + + result.setValueTransposed( + n2 / dx, + 0.f, + 0.f, + 0.f, + + 0.f, + n2 / dy, + 0.f, + 0.f, + + 0.f, + 0.f, + -1.f, + -1.f, + + 0.f, + 0.f, + -n2, + 0.f); + + return false; +} + +OSG_BASE_DLLMAPPING bool MatrixEpsInfinitePerspective(OSG::Matrix &result, + OSG::Real32 rFovy, + OSG::Real32 rAspect, + OSG::Real32 rNear, + OSG::Real32 rEps) +{ + // see http://www.terathon.com/gdc07_lengyel.pdf + + bool error = false; + + if(rFovy <= TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixEpsInfinitePerspective: fovy " << rFovy << " very small!\n" + << std::endl; + + error = true; + } + + if(rAspect < TypeTraits::getDefaultEps()) + { + SWARNING << "MatrixEpsInfinitePerspective: aspect ratio " << rAspect + << " very small!\n" << std::endl; + + error = true; + } + + if(rEps <= 2.4E-7) + { + SWARNING << "MatrixEpsInfinitePerspective: epsilon " << rEps << " to small!\n" + << std::endl; + } + + if(error) + { + result.setIdentity(); + return true; + } + + Real32 r = osgTan(rFovy) * rNear; + Real32 dx = 2.f * r * rAspect; + Real32 dy = 2.f * r; + Real32 n2 = 2.f * rNear; + + result.setValueTransposed( + n2 / dx, + 0.f, + 0.f, + 0.f, + + 0.f, + n2 / dy, + 0.f, + 0.f, + + 0.f, + 0.f, + rEps - 1.f, + -1.f, + + 0.f, + 0.f, + (rEps - 2.f) * rNear, + 0.f); + + return false; +} + /*! \warning This matrix is meant to used for setting up the beacon transformation of a OSG::Camera! They are inverted compared to the similarly named OpenGl function! @@ -396,6 +763,108 @@ OSG_BASE_DLLMAPPING bool MatrixLookAt(OS return false; } +/*! \warning This matrix is the classical OpenGL lookAt function. + For setting up the beacon transformation of a OSG::Camera you + should go for the MatrixLookAt(...) functions. +*/ +OSG_BASE_DLLMAPPING bool MatrixLookAtGL(OSG::Matrix &result, + OSG::Real32 fromx, + OSG::Real32 fromy, + OSG::Real32 fromz, + OSG::Real32 atx, + OSG::Real32 aty, + OSG::Real32 atz, + OSG::Real32 upx, + OSG::Real32 upy, + OSG::Real32 upz) +{ + Vec3f view; + Vec3f right; + Vec3f newup; + Vec3f up; + + view.setValues(fromx - atx , fromy - aty, fromz - atz); + view.normalize(); + + up.setValues(upx, upy, upz); + + right = up.cross(view); + + if(right.dot(right) < TypeTraits::getDefaultEps()) + { + return true; + } + + right.normalize(); + + newup = view.cross(right); + + Pnt3f from(fromx, fromy, fromz); + + result.setIdentity (); + result.setTranslate(-right.dot(from), -newup.dot(from), -view.dot(from)); + + Matrix tmpm; + + tmpm.setValue(right, newup, view); + + result.mult(tmpm); + result.transpose(); + + return false; +} + +/*! \warning This matrix is the classical OpenGL lookAt function. + For setting up the beacon transformation of a OSG::Camera you + should go for the MatrixLookAt(...) functions. +*/ +OSG_BASE_DLLMAPPING bool MatrixLookAtGL(OSG::Matrix &result, + OSG::Pnt3f from, + OSG::Pnt3f at, + OSG::Vec3f up ) +{ + Vec3f view; + Vec3f right; + Vec3f newup; + Vec3f tmp; + + view = from - at; + view.normalize(); + + right = up.cross(view); + + if(right.dot(right) < TypeTraits::getDefaultEps()) + { + return true; + } + + right.normalize(); + + newup = view.cross(right); + + result.setValue( + right[0], + right[1], + right[2], + -right.dot(from), + + newup[0], + newup[1], + newup[2], + -newup.dot(from), + + view[0], + view[1], + view[2], + -view.dot(from), + + 0.f, + 0.f, + 0.f, + 1.f); + + return false; +} OSG_BASE_DLLMAPPING bool MatrixProjection(OSG::Matrix &OSG_CHECK_ARG(result), OSG::Real32 OSG_CHECK_ARG(rLeft), @@ -438,8 +907,8 @@ void makeViewFromUVND( Matrix &Resu Result[3][2] = D.z(); //Clear up the rest of the matrix - Result[3][3] = 1.0; - Result[0][3] = Result[1][3] = Result[2][3] = 0.0; + Result[3][3] = 1.f; + Result[0][3] = Result[1][3] = Result[2][3] = 0.f; } void getUVNDFromMat(const Matrix &Mat, diff -rupN ./opensg_org/Source/Base/Base/OSGMatrixUtility.h ./opensg_math/Source/Base/Base/OSGMatrixUtility.h --- ./opensg_org/Source/Base/Base/OSGMatrixUtility.h 2017-01-16 11:34:53.259335300 +0100 +++ ./opensg_math/Source/Base/Base/OSGMatrixUtility.h 2016-06-21 12:54:23.102067500 +0200 @@ -52,6 +52,15 @@ OSG_BEGIN_NAMESPACE Matrix utility functions to create and analyze special kinds of matrices. + Remarks: 1. fovy parameters are measured from the y-axis to the + border of the view, i.e. the half of the full fovy + value must be given to the functions below. + + 2. All projection matrices are right handed systems. + If you need a left hand system instead, multiply by + a scale matrix S(1, 1,-1, 1) in the diagonal. + + 3. All projection matrices map to OpenGL NDC cube [-1,1]^3 \{ */ @@ -64,7 +73,32 @@ OSG_BASE_DLLMAPPING bool MatrixOrthogona OSG::Real32 rBottom, OSG::Real32 rTop, OSG::Real32 rNear, - OSG::Real32 rFar ); + OSG::Real32 rFar ); + +/*! \ingroup GrpBaseBaseMathMatrixFn + \relatesalso TransformationMatrix + */ +OSG_BASE_DLLMAPPING bool MatrixOrthogonal (OSG::Matrix &result, + OSG::Real32 rWidth, + OSG::Real32 rHeight, + OSG::Real32 rNear, + OSG::Real32 rFar ); + +/*! \ingroup GrpBaseBaseMathMatrixFn + \relatesalso TransformationMatrix + */ +OSG_BASE_DLLMAPPING bool MatrixOrthogonal2D (OSG::Matrix &result, + OSG::Real32 rLeft, + OSG::Real32 rRight, + OSG::Real32 rBottom, + OSG::Real32 rTop ); + +/*! \ingroup GrpBaseBaseMathMatrixFn + \relatesalso TransformationMatrix + */ +OSG_BASE_DLLMAPPING bool MatrixOrthogonal2D (OSG::Matrix &result, + OSG::Real32 rWidth, + OSG::Real32 rHeight ); /*! \ingroup GrpBaseBaseMathMatrixFn \relatesalso TransformationMatrix @@ -89,6 +123,16 @@ OSG_BASE_DLLMAPPING bool MatrixPerspecti /*! \ingroup GrpBaseBaseMathMatrixFn \relatesalso TransformationMatrix */ +OSG_BASE_DLLMAPPING bool MatrixPerspective (OSG::Matrix &result, + OSG::Real32 rFovy, + OSG::Real32 rWidth, + OSG::Real32 rHeight, + OSG::Real32 rNear, + OSG::Real32 rFar ); + +/*! \ingroup GrpBaseBaseMathMatrixFn + \relatesalso TransformationMatrix + */ OSG_BASE_DLLMAPPING bool MatrixStereoPerspective(OSG::Matrix &projection, OSG::Matrix &projtrans, OSG::Real32 rFovy, @@ -102,6 +146,26 @@ OSG_BASE_DLLMAPPING bool MatrixStereoPer /*! \ingroup GrpBaseBaseMathMatrixFn \relatesalso TransformationMatrix + + see http://www.terathon.com/gdc07_lengyel.pdf + */ +OSG_BASE_DLLMAPPING bool MatrixInfinitePerspective(OSG::Matrix &result, + OSG::Real32 rFovy, + OSG::Real32 rAspect, + OSG::Real32 rNear ); + +/*! \ingroup GrpBaseBaseMathMatrixFn + \relatesalso TransformationMatrix + + see http://www.terathon.com/gdc07_lengyel.pdf + */ +OSG_BASE_DLLMAPPING bool MatrixEpsInfinitePerspective(OSG::Matrix &result, + OSG::Real32 rFovy, + OSG::Real32 rAspect, + OSG::Real32 rNear, + OSG::Real32 rEps = OSG::TypeTraits::getDefaultEps() ); +/*! \ingroup GrpBaseBaseMathMatrixFn + \relatesalso TransformationMatrix */ OSG_BASE_DLLMAPPING bool MatrixLookAt (OSG::Matrix &result, OSG::Real32 fromx, @@ -121,6 +185,28 @@ OSG_BASE_DLLMAPPING bool MatrixLookAt OSG::Pnt3f from, OSG::Pnt3f at, OSG::Vec3f up ); + +/*! \ingroup GrpBaseBaseMathMatrixFn + \relatesalso TransformationMatrix + */ +OSG_BASE_DLLMAPPING bool MatrixLookAtGL (OSG::Matrix &result, + OSG::Real32 fromx, + OSG::Real32 fromy, + OSG::Real32 fromz, + OSG::Real32 atx, + OSG::Real32 aty, + OSG::Real32 atz, + OSG::Real32 upx, + OSG::Real32 upy, + OSG::Real32 upz ); + +/*! \ingroup GrpBaseBaseMathMatrixFn + \relatesalso TransformationMatrix + */ +OSG_BASE_DLLMAPPING bool MatrixLookAtGL (OSG::Matrix &result, + OSG::Pnt3f from, + OSG::Pnt3f at, + OSG::Vec3f up ); /*! \ingroup GrpBaseBaseMathMatrixFn