UfnCod3r
پنج شنبه 02 خرداد 1392, 10:48 صبح
سلام من می خوام چرخش رو ب صورت Quaternion, EulerAngle بگیرم .
هر کدی تاحالا پیدا کردم همش مشکل داره . :گریه:
مثلا این نمی دونم چشه .
void XMat4x4::getRotationQuat(XQuat& outRotation) const
{
// This is simply the length of each axis (row/column) in the matrix.
XVec3 xaxis(m[0], m[1], m[2]);
float scaleX = xaxis.length();
XVec3 yaxis(m[4], m[5], m[6]);
float scaleY = yaxis.length();
XVec3 zaxis(m[8], m[9], m[10]);
float scaleZ = zaxis.length();
// Determine if we have a negative scale (true if determinant is less than zero).
// In this case, we simply negate a single axis of the scale.
float det = determinant();
if (det < 0.0f)
scaleZ = -scaleZ;
// Scale too close to zero, can't decompose rotation.
if (scaleX < 0.000001f || scaleY < 0.000001f || fabs(scaleZ) < 0.000001f)
{
outRotation.setIdentity(); //return false;
return;
}
flt rn;
// Factor the scale out of the matrix axes.
rn = 1.0 / scaleX;
xaxis.x *= rn;
xaxis.y *= rn;
xaxis.z *= rn;
rn = 1.0 / scaleY;
yaxis.x *= rn;
yaxis.y *= rn;
yaxis.z *= rn;
rn = 1.0 / scaleZ;
zaxis.x *= rn;
zaxis.y *= rn;
zaxis.z *= rn;
// Now calculate the rotation from the resulting matrix (axes).
float trace = xaxis.x + yaxis.y + zaxis.z + 1.0f;
if (trace > 0.000001f)
{
float s = 0.5f / XSqrt(trace);
outRotation.w = 0.25f / s;
outRotation.x = (yaxis.z - zaxis.y) * s;
outRotation.y = (zaxis.x - xaxis.z) * s;
outRotation.z = (xaxis.y - yaxis.x) * s;
}
else
{
// Note: since xaxis, yaxis, and zaxis are normalized,
// we will never divide by zero in the code below.
if (xaxis.x > yaxis.y && xaxis.x > zaxis.z)
{
float s = 0.5f / XSqrt(1.0f + xaxis.x - yaxis.y - zaxis.z);
outRotation.w = (yaxis.z - zaxis.y) * s;
outRotation.x = 0.25f / s;
outRotation.y = (yaxis.x + xaxis.y) * s;
outRotation.z = (zaxis.x + xaxis.z) * s;
}
else if (yaxis.y > zaxis.z)
{
float s = 0.5f / XSqrt(1.0f + yaxis.y - xaxis.x - zaxis.z);
outRotation.w = (zaxis.x - xaxis.z) * s;
outRotation.x = (yaxis.x + xaxis.y) * s;
outRotation.y = 0.25f / s;
outRotation.z = (zaxis.y + yaxis.z) * s;
}
else
{
float s = 0.5f / XSqrt(1.0f + zaxis.z - xaxis.x - yaxis.y );
outRotation.w = (xaxis.y - yaxis.x ) * s;
outRotation.x = (zaxis.x + xaxis.z ) * s;
outRotation.y = (zaxis.y + yaxis.z ) * s;
outRotation.z = 0.25f / s;
}
}
}
این ک کلا خیلی وقتا اشتباه بر می گردونه :متفکر:
void XMat4x4::getRotationXYZ(XVec3& outAngle) const
{
float cy = XSqrt(m[0] * m[0] + m[1] * m[1]);
if(cy > 0.000001f)
{
XVec3 euler1;
XVec3 euler2;
euler1.x = atan2f(m[6], m[10]);
euler1.y = atan2f(- m[2], cy);
euler1.z = atan2f(m[1], m[0]);
euler2.x = atan2f(- m[6], - m[10]);
euler2.y = atan2f(- m[2], - cy);
euler2.z = atan2f(- m[1], - m[0]);
if( (fabs(euler1.x) + fabs(euler1.y) + fabs(euler1.z)) >
(fabs(euler2.x) + fabs(euler2.y) + fabs(euler2.z)))
{
outAngle = euler2 * X_RAD2DEG;
}
else
{
outAngle = euler1 * X_RAD2DEG;
}
}
else
{
outAngle.x = atan2f(- m[9], m[5]) * X_RAD2DEG;
outAngle.y = atan2f(- m[2], cy) * X_RAD2DEG;
outAngle.z = 0.0f;
}
}
اینم بعضی وقتا مشکل داره یهو تو ی نقطه ای می مونه و مدل دیگه نمی چرخه حی زاویه رو زیاد می کنم باز کم مشه:متفکر:
در ضمن سیستم مختصاتم راست دسته + ماتریسم ستونیه .:لبخندساده:
اگه کسی ی کتاب خونه داره که ماتری و .. برای OpenGL داره بده . کامل هم باشه ن فقط ضرب و ..
البته ب غیر از GLM اشغال
اخه چرا اپن ج ال اینقدر بد بخته حتی ی API درست درمون هم نداره ک باهاش کار کرد :قهقهه:
هر کدی تاحالا پیدا کردم همش مشکل داره . :گریه:
مثلا این نمی دونم چشه .
void XMat4x4::getRotationQuat(XQuat& outRotation) const
{
// This is simply the length of each axis (row/column) in the matrix.
XVec3 xaxis(m[0], m[1], m[2]);
float scaleX = xaxis.length();
XVec3 yaxis(m[4], m[5], m[6]);
float scaleY = yaxis.length();
XVec3 zaxis(m[8], m[9], m[10]);
float scaleZ = zaxis.length();
// Determine if we have a negative scale (true if determinant is less than zero).
// In this case, we simply negate a single axis of the scale.
float det = determinant();
if (det < 0.0f)
scaleZ = -scaleZ;
// Scale too close to zero, can't decompose rotation.
if (scaleX < 0.000001f || scaleY < 0.000001f || fabs(scaleZ) < 0.000001f)
{
outRotation.setIdentity(); //return false;
return;
}
flt rn;
// Factor the scale out of the matrix axes.
rn = 1.0 / scaleX;
xaxis.x *= rn;
xaxis.y *= rn;
xaxis.z *= rn;
rn = 1.0 / scaleY;
yaxis.x *= rn;
yaxis.y *= rn;
yaxis.z *= rn;
rn = 1.0 / scaleZ;
zaxis.x *= rn;
zaxis.y *= rn;
zaxis.z *= rn;
// Now calculate the rotation from the resulting matrix (axes).
float trace = xaxis.x + yaxis.y + zaxis.z + 1.0f;
if (trace > 0.000001f)
{
float s = 0.5f / XSqrt(trace);
outRotation.w = 0.25f / s;
outRotation.x = (yaxis.z - zaxis.y) * s;
outRotation.y = (zaxis.x - xaxis.z) * s;
outRotation.z = (xaxis.y - yaxis.x) * s;
}
else
{
// Note: since xaxis, yaxis, and zaxis are normalized,
// we will never divide by zero in the code below.
if (xaxis.x > yaxis.y && xaxis.x > zaxis.z)
{
float s = 0.5f / XSqrt(1.0f + xaxis.x - yaxis.y - zaxis.z);
outRotation.w = (yaxis.z - zaxis.y) * s;
outRotation.x = 0.25f / s;
outRotation.y = (yaxis.x + xaxis.y) * s;
outRotation.z = (zaxis.x + xaxis.z) * s;
}
else if (yaxis.y > zaxis.z)
{
float s = 0.5f / XSqrt(1.0f + yaxis.y - xaxis.x - zaxis.z);
outRotation.w = (zaxis.x - xaxis.z) * s;
outRotation.x = (yaxis.x + xaxis.y) * s;
outRotation.y = 0.25f / s;
outRotation.z = (zaxis.y + yaxis.z) * s;
}
else
{
float s = 0.5f / XSqrt(1.0f + zaxis.z - xaxis.x - yaxis.y );
outRotation.w = (xaxis.y - yaxis.x ) * s;
outRotation.x = (zaxis.x + xaxis.z ) * s;
outRotation.y = (zaxis.y + yaxis.z ) * s;
outRotation.z = 0.25f / s;
}
}
}
این ک کلا خیلی وقتا اشتباه بر می گردونه :متفکر:
void XMat4x4::getRotationXYZ(XVec3& outAngle) const
{
float cy = XSqrt(m[0] * m[0] + m[1] * m[1]);
if(cy > 0.000001f)
{
XVec3 euler1;
XVec3 euler2;
euler1.x = atan2f(m[6], m[10]);
euler1.y = atan2f(- m[2], cy);
euler1.z = atan2f(m[1], m[0]);
euler2.x = atan2f(- m[6], - m[10]);
euler2.y = atan2f(- m[2], - cy);
euler2.z = atan2f(- m[1], - m[0]);
if( (fabs(euler1.x) + fabs(euler1.y) + fabs(euler1.z)) >
(fabs(euler2.x) + fabs(euler2.y) + fabs(euler2.z)))
{
outAngle = euler2 * X_RAD2DEG;
}
else
{
outAngle = euler1 * X_RAD2DEG;
}
}
else
{
outAngle.x = atan2f(- m[9], m[5]) * X_RAD2DEG;
outAngle.y = atan2f(- m[2], cy) * X_RAD2DEG;
outAngle.z = 0.0f;
}
}
اینم بعضی وقتا مشکل داره یهو تو ی نقطه ای می مونه و مدل دیگه نمی چرخه حی زاویه رو زیاد می کنم باز کم مشه:متفکر:
در ضمن سیستم مختصاتم راست دسته + ماتریسم ستونیه .:لبخندساده:
اگه کسی ی کتاب خونه داره که ماتری و .. برای OpenGL داره بده . کامل هم باشه ن فقط ضرب و ..
البته ب غیر از GLM اشغال
اخه چرا اپن ج ال اینقدر بد بخته حتی ی API درست درمون هم نداره ک باهاش کار کرد :قهقهه: