implement snap to center in model

This commit is contained in:
ZappoMan 2014-05-01 09:02:41 -07:00
parent bb6444f5a9
commit 293963c546
3 changed files with 46 additions and 7 deletions

View file

@ -35,10 +35,9 @@ var originalProperties = {
//modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/meshes/birarda/birarda_head.fbx",
//modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/meshes/pug.fbx",
//modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/meshes/newInvader16x16-large-purple.svo",
//modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/meshes/mino_full.fst",
modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/meshes/minotaur/mino_full.fbx",
//modelRotation: rotation
modelRotation: rotation
};
var positionDelta = { x: 0, y: 0, z: 0 };

View file

@ -37,6 +37,8 @@ Model::Model(QObject* parent) :
_scaleToFit(false),
_scaleToFitLargestDimension(0.0f),
_scaledToFit(false),
_snapModelToCenter(false),
_snappedToCenter(false),
_shapesAreDirty(true),
_boundingRadius(0.f),
_boundingShape(),
@ -63,6 +65,13 @@ Model::SkinLocations Model::_skinNormalMapLocations;
Model::SkinLocations Model::_skinShadowLocations;
void Model::setScale(const glm::vec3& scale) {
setScaleInternal(scale);
// if anyone sets scale manually, then we are no longer scaled to fit
_scaleToFit = false;
_scaledToFit = false;
}
void Model::setScaleInternal(const glm::vec3& scale) {
float scaleLength = glm::length(_scale);
float relativeDeltaScale = glm::length(_scale - scale) / scaleLength;
@ -798,7 +807,7 @@ void Model::setScaleToFit(bool scaleToFit, float largestDimension) {
}
}
void Model::checkScaleToFit() {
void Model::scaleToFit() {
Extents modelMeshExtents = getMeshExtents();
// size is our "target size in world space"
@ -807,16 +816,34 @@ void Model::checkScaleToFit() {
float maxDimension = glm::max(glm::max(dimensions.x, dimensions.y), dimensions.z);
float maxScale = _scaleToFitLargestDimension / maxDimension;
glm::vec3 scale(maxScale, maxScale, maxScale);
setScale(scale);
setScaleInternal(scale);
_scaledToFit = true;
}
void Model::setSnapModelToCenter(bool snapModelToCenter) {
if (_snapModelToCenter != snapModelToCenter) {
_snapModelToCenter = snapModelToCenter;
_snappedToCenter = false; // force re-centering
}
}
void Model::snapToCenter() {
Extents modelMeshExtents = getUnscaledMeshExtents();
glm::vec3 halfDimensions = (modelMeshExtents.maximum - modelMeshExtents.minimum) * 0.5f;
glm::vec3 offset = -modelMeshExtents.minimum - halfDimensions;
_offset = offset;
_snappedToCenter = true;
}
void Model::simulate(float deltaTime, bool fullUpdate) {
fullUpdate = updateGeometry() || fullUpdate || (_scaleToFit && !_scaledToFit);
if (isActive() && fullUpdate) {
// check for scale to fit
if (_scaleToFit && !_scaledToFit) {
checkScaleToFit();
scaleToFit();
}
if (_snapModelToCenter && !_snappedToCenter) {
snapToCenter();
}
simulateInternal(deltaTime);
}

View file

@ -39,11 +39,19 @@ public:
void setRotation(const glm::quat& rotation) { _rotation = rotation; }
const glm::quat& getRotation() const { return _rotation; }
/// enables/disables scale to fit behavior, the model will be automatically scaled to the specified largest dimension
void setScaleToFit(bool scaleToFit, float largestDimension = 0.0f);
bool getScaleToFit() const { return _scaleToFit; } /// is scale to fit enabled
bool getIsScaledToFit() const { return _scaledToFit; } /// is model scaled to fit
bool getScaleToFitDimension() const { return _scaleToFitLargestDimension; } /// the dimension model is scaled to
void setSnapModelToCenter(bool snapModelToCenter);
bool getSnapModelToCenter() { return _snapModelToCenter; }
void setScale(const glm::vec3& scale);
const glm::vec3& getScale() const { return _scale; }
void setOffset(const glm::vec3& offset) { _offset = offset; }
void setOffset(const glm::vec3& offset) { _offset = offset; _snapModelToCenter = false; _snappedToCenter = false; }
const glm::vec3& getOffset() const { return _offset; }
void setPupilDilation(float dilation) { _pupilDilation = dilation; }
@ -215,6 +223,9 @@ protected:
float _scaleToFitLargestDimension; /// this is the dimension that scale to fit will use
bool _scaledToFit; /// have we scaled to fit
bool _snapModelToCenter; /// is the model's offset automatically adjusted to center around 0,0,0 in model space
bool _snappedToCenter;
class JointState {
public:
glm::vec3 translation; // translation relative to parent
@ -241,7 +252,9 @@ protected:
// returns 'true' if needs fullUpdate after geometry change
bool updateGeometry();
void checkScaleToFit();
void setScaleInternal(const glm::vec3& scale);
void scaleToFit();
void snapToCenter();
void simulateInternal(float deltaTime);