Adding convenience return *this to many transform methods

This commit is contained in:
Brad Davis 2015-07-18 16:56:32 -07:00
parent e4f6586c3c
commit c446bb307d
2 changed files with 67 additions and 58 deletions

View file

@ -144,17 +144,13 @@ void Stars::render(RenderArgs* renderArgs, float alpha) {
gpu::Batch batch;
batch.setInputFormat(streamFormat);
batch.setInputBuffer(VERTICES_SLOT, gpu::BufferView(vertexBuffer, positionElement));
Transform tr;
// tr.setTranslation(renderArgs->_viewFrustum->getPosition());
tr.setRotation(glm::inverse(renderArgs->_viewFrustum->getOrientation()));
batch.setViewTransform(Transform());
batch.setModelTransform(tr);
batch.setProjectionTransform(renderArgs->_viewFrustum->getProjection());
batch.setModelTransform(Transform().setRotation(glm::inverse(renderArgs->_viewFrustum->getOrientation())));
auto geometryCache = DependencyManager::get<GeometryCache>();
auto textureCache = DependencyManager::get<TextureCache>();
geometryCache->useSimpleDrawPipeline(batch);
batch.setResourceTexture(0, textureCache->getWhiteTexture());
batch.draw(gpu::Primitive::POINTS, STARFIELD_NUM_STARS);
renderArgs->_context->syncCache();
renderArgs->_context->render(batch);
}

View file

@ -82,25 +82,25 @@ public:
return (*this);
}
void setIdentity();
Transform& setIdentity();
const Vec3& getTranslation() const;
void setTranslation(const Vec3& translation); // [new this] = [translation] * [this.rotation] * [this.scale]
void preTranslate(const Vec3& translation); // [new this] = [translation] * [this]
void postTranslate(const Vec3& translation); // [new this] = [this] * [translation] equivalent to:glTranslate
Transform& setTranslation(const Vec3& translation); // [new this] = [translation] * [this.rotation] * [this.scale]
Transform& preTranslate(const Vec3& translation); // [new this] = [translation] * [this]
Transform& postTranslate(const Vec3& translation); // [new this] = [this] * [translation] equivalent to:glTranslate
const Quat& getRotation() const;
void setRotation(const Quat& rotation); // [new this] = [this.translation] * [rotation] * [this.scale]
void preRotate(const Quat& rotation); // [new this] = [rotation] * [this]
void postRotate(const Quat& rotation); // [new this] = [this] * [rotation] equivalent to:glRotate
Transform& setRotation(const Quat& rotation); // [new this] = [this.translation] * [rotation] * [this.scale]
Transform& preRotate(const Quat& rotation); // [new this] = [rotation] * [this]
Transform& postRotate(const Quat& rotation); // [new this] = [this] * [rotation] equivalent to:glRotate
const Vec3& getScale() const;
void setScale(float scale);
void setScale(const Vec3& scale); // [new this] = [this.translation] * [this.rotation] * [scale]
void preScale(float scale);
void preScale(const Vec3& scale);
void postScale(float scale); // [new this] = [this] * [scale] equivalent to:glScale
void postScale(const Vec3& scale); // [new this] = [this] * [scale] equivalent to:glScale
Transform& setScale(float scale);
Transform& setScale(const Vec3& scale); // [new this] = [this.translation] * [this.rotation] * [scale]
Transform& preScale(float scale);
Transform& preScale(const Vec3& scale);
Transform& postScale(float scale); // [new this] = [this] * [scale] equivalent to:glScale
Transform& postScale(const Vec3& scale); // [new this] = [this] * [scale] equivalent to:glScale
bool isIdentity() const { return (_flags & ~Flags(FLAG_CACHE_INVALID_BITSET)).none(); }
bool isTranslating() const { return _flags[FLAG_TRANSLATION]; }
@ -109,8 +109,8 @@ public:
bool isUniform() const { return !isNonUniform(); }
bool isNonUniform() const { return _flags[FLAG_NON_UNIFORM]; }
void evalFromRawMatrix(const Mat4& matrix);
void evalFromRawMatrix(const Mat3& rotationScalematrix);
Transform& evalFromRawMatrix(const Mat4& matrix);
Transform& evalFromRawMatrix(const Mat3& rotationScalematrix);
Mat4& getMatrix(Mat4& result) const;
Mat4& getInverseMatrix(Mat4& result) const;
@ -182,18 +182,19 @@ protected:
Mat4& getCachedMatrix(Mat4& result) const;
};
inline void Transform::setIdentity() {
inline Transform& Transform::setIdentity() {
_translation = Vec3(0.0f);
_rotation = Quat(1.0f, 0.0f, 0.0f, 0.0f);
_scale = Vec3(1.0f);
_flags = Flags(FLAG_CACHE_INVALID_BITSET);
return *this;
}
inline const Transform::Vec3& Transform::getTranslation() const {
return _translation;
}
inline void Transform::setTranslation(const Vec3& translation) {
inline Transform& Transform::setTranslation(const Vec3& translation) {
invalidCache();
if (translation == Vec3()) {
unflagTranslation();
@ -201,20 +202,22 @@ inline void Transform::setTranslation(const Vec3& translation) {
flagTranslation();
}
_translation = translation;
return *this;
}
inline void Transform::preTranslate(const Vec3& translation) {
inline Transform& Transform::preTranslate(const Vec3& translation) {
if (translation == Vec3()) {
return;
return *this;
}
invalidCache();
flagTranslation();
_translation += translation;
return *this;
}
inline void Transform::postTranslate(const Vec3& translation) {
inline Transform& Transform::postTranslate(const Vec3& translation) {
if (translation == Vec3()) {
return;
return *this;
}
invalidCache();
flagTranslation();
@ -229,13 +232,14 @@ inline void Transform::postTranslate(const Vec3& translation) {
} else {
_translation += scaledT;
}
return *this;
}
inline const Transform::Quat& Transform::getRotation() const {
return _rotation;
}
inline void Transform::setRotation(const Quat& rotation) {
inline Transform& Transform::setRotation(const Quat& rotation) {
invalidCache();
if (rotation == Quat()) {
unflagRotation();
@ -243,11 +247,12 @@ inline void Transform::setRotation(const Quat& rotation) {
flagRotation();
}
_rotation = rotation;
return *this;
}
inline void Transform::preRotate(const Quat& rotation) {
inline Transform& Transform::preRotate(const Quat& rotation) {
if (rotation == Quat()) {
return;
return *this;
}
invalidCache();
if (isRotating()) {
@ -258,11 +263,12 @@ inline void Transform::preRotate(const Quat& rotation) {
flagRotation();
_translation = glm::rotate(rotation, _translation);
return *this;
}
inline void Transform::postRotate(const Quat& rotation) {
inline Transform& Transform::postRotate(const Quat& rotation) {
if (rotation == Quat()) {
return;
return *this;
}
invalidCache();
@ -289,15 +295,16 @@ inline void Transform::postRotate(const Quat& rotation) {
}
}
flagRotation();
return *this;
}
inline const Transform::Vec3& Transform::getScale() const {
return _scale;
}
inline void Transform::setScale(float scale) {
inline Transform& Transform::setScale(float scale) {
if (!isValidScale(scale)) {
return;
return *this;
}
invalidCache();
flagUniform();
@ -308,46 +315,49 @@ inline void Transform::setScale(float scale) {
flagScaling();
}
_scale = Vec3(scale);
return *this;
}
inline void Transform::setScale(const Vec3& scale) {
inline Transform& Transform::setScale(const Vec3& scale) {
if (!isValidScale(scale)) {
return;
return *this;
}
if ((scale.x == scale.y) && (scale.x == scale.z)) {
setScale(scale.x);
} else {
invalidCache();
flagScaling();
flagNonUniform();
_scale = scale;
}
return setScale(scale.x);
}
invalidCache();
flagScaling();
flagNonUniform();
_scale = scale;
return *this;
}
inline void Transform::preScale(float scale) {
setScale(getScale() * scale);
inline Transform& Transform::preScale(float scale) {
return setScale(getScale() * scale);
}
inline void Transform::preScale(const Vec3& scale) {
setScale(getScale() * scale);
inline Transform& Transform::preScale(const Vec3& scale) {
return setScale(getScale() * scale);
}
inline void Transform::postScale(float scale) {
inline Transform& Transform::postScale(float scale) {
if (!isValidScale(scale) || scale == 1.0f) {
return;
return *this;
}
if (isScaling()) {
// if already scaling, just invalid cache and apply uniform scale
invalidCache();
_scale *= scale;
} else {
setScale(scale);
if (!isScaling()) {
return setScale(scale);
}
// if already scaling, just invalid cache and apply uniform scale
invalidCache();
_scale *= scale;
return *this;
}
inline void Transform::postScale(const Vec3& scale) {
inline Transform& Transform::postScale(const Vec3& scale) {
if (!isValidScale(scale)) {
return;
return *this;
}
invalidCache();
if (isScaling()) {
@ -356,6 +366,7 @@ inline void Transform::postScale(const Vec3& scale) {
_scale = scale;
}
flagScaling();
return *this;
}
inline Transform::Mat4& Transform::getMatrix(Transform::Mat4& result) const {
@ -405,20 +416,22 @@ inline Transform::Mat4& Transform::getRotationScaleMatrixInverse(Mat4& result) c
return result;
}
inline void Transform::evalFromRawMatrix(const Mat4& matrix) {
inline Transform& Transform::evalFromRawMatrix(const Mat4& matrix) {
// for now works only in the case of TRS transformation
if ((matrix[0][3] == 0.0f) && (matrix[1][3] == 0.0f) && (matrix[2][3] == 0.0f) && (matrix[3][3] == 1.0f)) {
setTranslation(Vec3(matrix[3]));
evalFromRawMatrix(Mat3(matrix));
}
return *this;
}
inline void Transform::evalFromRawMatrix(const Mat3& rotationScaleMatrix) {
inline Transform& Transform::evalFromRawMatrix(const Mat3& rotationScaleMatrix) {
Quat rotation;
Vec3 scale;
evalRotationScale(rotation, scale, rotationScaleMatrix);
setRotation(rotation);
setScale(scale);
return *this;
}
inline Transform& Transform::evalInverse(Transform& inverse) const {