improving the evalFromRawMatrix decomposition

This commit is contained in:
Sam Gateau 2014-11-07 14:27:22 -08:00
parent dc748a85a3
commit 9d146e7ea3
4 changed files with 38 additions and 32 deletions

View file

@ -59,20 +59,31 @@ Transform::Mat4& Transform::evalRelativeTransform( Mat4& result, const Vec3& ori
return result; return result;
} }
void Transform::evalRotationScale(const Mat3& rotationScaleMatrix) {
Vec3 scale(glm::length(rotationScaleMatrix[0]), glm::length(rotationScaleMatrix[1]), glm::length(rotationScaleMatrix[2]));
if (scale.x < 0.00001f) scale.x = 0.00001f;
if (scale.y < 0.00001f) scale.y = 0.00001f;
if (scale.z < 0.00001f) scale.z = 0.00001f;
Mat3 matRotScale(
rotationScaleMatrix[0] / scale.x,
rotationScaleMatrix[1] / scale.y,
rotationScaleMatrix[2] / scale.z);
setRotation(glm::quat_cast(matRotScale));
float determinant = glm::determinant(matRotScale);
if (determinant < 0.f) {
scale.x = -scale.x;
}
setScale(scale);
}
void Transform::evalFromRawMatrix(const Mat4& matrix) { void Transform::evalFromRawMatrix(const Mat4& matrix) {
if ((matrix[0][3] == 0) && (matrix[1][3] == 0) && (matrix[2][3] == 0) && (matrix[3][3] == 1.f)) { if ((matrix[0][3] == 0) && (matrix[1][3] == 0) && (matrix[2][3] == 0) && (matrix[3][3] == 1.f)) {
setTranslation(Vec3(matrix[3])); setTranslation(Vec3(matrix[3]));
Mat3 matRS = Mat3(matrix); evalRotationScale(Mat3(matrix));
Vec3 scale(glm::length(matRS[0]), glm::length(matRS[1]), glm::length(matRS[2]));
matRS[0] *= 1/scale.x;
matRS[1] *= 1/scale.y;
matRS[2] *= 1/scale.z;
setRotation(glm::quat_cast(matRS));
setScale(scale);
} }
} }
@ -91,18 +102,9 @@ Transform& Transform::mult( Transform& result, const Transform& left, const Tran
left.updateCache(); left.updateCache();
result.setTranslation(Vec3(left.getMatrix() * Vec4(right.getTranslation(), 1.f))); result.setTranslation(Vec3(left.getMatrix() * Vec4(right.getTranslation(), 1.f)));
Mat4 mat = left.getMatrix() * right.getMatrix(); Mat4 mat = left.getMatrix() * right.getMatrix();
Mat3 matRS = Mat3(mat); result.evalRotationScale(Mat3(mat));
Vec3 scale(glm::length(matRS[0]), glm::length(matRS[1]), glm::length(matRS[2]));
matRS[0] *= 1/scale.x;
matRS[1] *= 1/scale.y;
matRS[2] *= 1/scale.z;
result.setRotation(glm::quat_cast(matRS));
result.setScale(scale);
return result; return result;
} }

View file

@ -60,6 +60,7 @@ public:
Transform& evalInverseTranspose(Transform& result); Transform& evalInverseTranspose(Transform& result);
void evalFromRawMatrix(const Mat4& matrix); void evalFromRawMatrix(const Mat4& matrix);
void evalRotationScale(const Mat3& rotationScalematrix);
static Transform& mult( Transform& result, const Transform& left, const Transform& right); static Transform& mult( Transform& result, const Transform& left, const Transform& right);
@ -110,6 +111,7 @@ protected:
}; };
typedef QSharedPointer< Transform > TransformPointer; typedef QSharedPointer< Transform > TransformPointer;
typedef std::vector< TransformPointer > Transforms;
}; };

View file

@ -554,14 +554,18 @@ bool Model::render(float alpha, RenderMode mode, RenderArgs* args) {
} }
// Let's introduce a gpu::Batch to capture all the calls to the graphics api // Let's introduce a gpu::Batch to capture all the calls to the graphics api
gpu::Batch batch; _renderBatch.clear();
gpu::Batch& batch = _renderBatch;
GLBATCH(glPushMatrix)(); GLBATCH(glPushMatrix)();
// Capture the view matrix once for the rendering of this model // Capture the view matrix once for the rendering of this model
gpu::TransformPointer viewTransform(new gpu::Transform(Application::getInstance()->getUntranslatedViewMatrix())); if (_transforms.empty()) {
viewTransform->postTranslate(Application::getInstance()->getViewMatrixTranslation()); _transforms.push_back(gpu::TransformPointer(new gpu::Transform()));
}
_transforms[0]->evalFromRawMatrix(Application::getInstance()->getUntranslatedViewMatrix());
_transforms[0]->postTranslate(Application::getInstance()->getViewMatrixTranslation() + _translation);
batch.setViewTransform(viewTransform); batch.setViewTransform(_transforms[0]);
GLBATCH(glDisable)(GL_COLOR_MATERIAL); GLBATCH(glDisable)(GL_COLOR_MATERIAL);
@ -692,7 +696,6 @@ bool Model::render(float alpha, RenderMode mode, RenderArgs* args) {
{ {
PROFILE_RANGE("render Batch"); PROFILE_RANGE("render Batch");
::gpu::GLBackend::renderBatch(batch); ::gpu::GLBackend::renderBatch(batch);
batch.clear();
} }
// restore all the default material settings // restore all the default material settings
@ -1875,13 +1878,11 @@ int Model::renderMeshes(gpu::Batch& batch, RenderMode mode, bool translucent, fl
(const float*)state.clusterMatrices.constData()); (const float*)state.clusterMatrices.constData());
gpu::TransformPointer modelTransform(new gpu::Transform()); gpu::TransformPointer modelTransform(new gpu::Transform());
modelTransform->preTranslate(_translation);
batch.setModelTransform(modelTransform); batch.setModelTransform(modelTransform);
} else { } else {
// GLBATCH(glMultMatrixf)((const GLfloat*)&state.clusterMatrices[0]); // GLBATCH(glMultMatrixf)((const GLfloat*)&state.clusterMatrices[0]);
gpu::TransformPointer modelTransform(new gpu::Transform(state.clusterMatrices[0])); gpu::TransformPointer modelTransform(new gpu::Transform(state.clusterMatrices[0]));
modelTransform->preTranslate(_translation);
batch.setModelTransform(modelTransform); batch.setModelTransform(modelTransform);
} }

View file

@ -36,11 +36,10 @@ class ViewFrustum;
typedef QSharedPointer<AnimationHandle> AnimationHandlePointer; typedef QSharedPointer<AnimationHandle> AnimationHandlePointer;
typedef QWeakPointer<AnimationHandle> WeakAnimationHandlePointer; typedef QWeakPointer<AnimationHandle> WeakAnimationHandlePointer;
namespace gpu {
class Batch;
}
#include "gpu/Stream.h" #include "gpu/Stream.h"
#include "gpu/Transform.h"
#include "gpu/Batch.h"
/// A generic 3D model displaying geometry loaded from a URL. /// A generic 3D model displaying geometry loaded from a URL.
class Model : public QObject, public PhysicsEntity { class Model : public QObject, public PhysicsEntity {
@ -283,7 +282,9 @@ private:
QUrl _url; QUrl _url;
gpu::Buffers _blendedVertexBuffers; gpu::Buffers _blendedVertexBuffers;
gpu::Transforms _transforms;
gpu::Batch _renderBatch;
QVector<QVector<QSharedPointer<Texture> > > _dilatedTextures; QVector<QVector<QSharedPointer<Texture> > > _dilatedTextures;
QVector<Model*> _attachments; QVector<Model*> _attachments;