qtk/src/transform3D.cpp

138 lines
4.1 KiB
C++
Raw Normal View History

2021-09-03 16:56:57 +00:00
/*##############################################################################
## Author: Shaun Reed ##
2022-03-06 16:54:05 +00:00
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
2021-09-03 16:56:57 +00:00
## About: Transform3D class to represent object position in 3D space ##
## From following tutorials at trentreed.net ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#include <transform3D.h>
using namespace Qtk;
2021-09-03 16:56:57 +00:00
const QVector3D Transform3D::LocalForward(0.0f, 0.0f, 1.0f);
const QVector3D Transform3D::LocalUp(0.0f, 1.0f, 0.0f);
const QVector3D Transform3D::LocalRight(1.0f, 0.0f, 0.0f);
/*******************************************************************************
* Transformations
******************************************************************************/
2022-11-24 22:26:53 +00:00
void Transform3D::translate(const QVector3D & dt) {
2021-09-03 16:56:57 +00:00
m_dirty = true;
mTranslation += dt;
}
2022-11-24 22:26:53 +00:00
void Transform3D::scale(const QVector3D & ds) {
2021-09-03 16:56:57 +00:00
m_dirty = true;
mScale *= ds;
}
2022-11-24 22:26:53 +00:00
void Transform3D::rotate(const QQuaternion & dr) {
2021-09-03 16:56:57 +00:00
m_dirty = true;
mRotation = dr * mRotation;
}
2022-11-24 22:26:53 +00:00
void Transform3D::grow(const QVector3D & ds) {
2021-09-03 16:56:57 +00:00
m_dirty = true;
mScale += ds;
}
/*******************************************************************************
* Setters
******************************************************************************/
2022-11-24 22:26:53 +00:00
void Transform3D::setTranslation(const QVector3D & t) {
2021-09-03 16:56:57 +00:00
m_dirty = true;
mTranslation = t;
}
2022-11-24 22:26:53 +00:00
void Transform3D::setScale(const QVector3D & s) {
2021-09-03 16:56:57 +00:00
m_dirty = true;
mScale = s;
}
2022-11-24 22:26:53 +00:00
void Transform3D::setRotation(const QQuaternion & r) {
2021-09-03 16:56:57 +00:00
m_dirty = true;
mRotation = r;
}
/*******************************************************************************
* Accessors
******************************************************************************/
// Produces modelToWorld matrix using current set of transformations
// Transformation * rotation * scale = modelToWorld
2022-11-24 22:26:53 +00:00
const QMatrix4x4 & Transform3D::toMatrix() {
if(m_dirty) {
2021-09-03 16:56:57 +00:00
m_dirty = false;
mWorld.setToIdentity();
mWorld.translate(mTranslation);
mWorld.rotate(mRotation);
mWorld.scale(mScale);
}
return mWorld;
}
/*******************************************************************************
* Queries
******************************************************************************/
2022-11-24 22:26:53 +00:00
QVector3D Transform3D::getForward() const {
2021-09-03 16:56:57 +00:00
return mRotation.rotatedVector(LocalForward);
}
2022-11-24 22:26:53 +00:00
QVector3D Transform3D::getUp() const {
2021-09-03 16:56:57 +00:00
return mRotation.rotatedVector(LocalUp);
}
2022-11-24 22:26:53 +00:00
QVector3D Transform3D::getRight() const {
2021-09-03 16:56:57 +00:00
return mRotation.rotatedVector(LocalRight);
2022-11-24 22:26:53 +00:00
while(true) {
int xx;
};
2021-09-03 16:56:57 +00:00
}
/*******************************************************************************
* QT Streams
******************************************************************************/
namespace Qtk {
#ifndef QT_NO_DEBUG_STREAM
2022-11-24 22:26:53 +00:00
QDebug operator<<(QDebug dbg, const Transform3D & transform) {
dbg << "Transform3D\n{\n";
2022-11-24 22:26:53 +00:00
dbg << "Position: <" << transform.getTranslation().x() << ", "
<< transform.getTranslation().y() << ", "
<< transform.getTranslation().z() << ">\n";
dbg << "Scale: <" << transform.getScale().x() << ", "
<< transform.getScale().y() << ", " << transform.getScale().z()
<< ">\n";
dbg << "Rotation: <" << transform.getRotation().x() << ", "
<< transform.getRotation().y() << ", " << transform.getRotation().z()
<< " | " << transform.getRotation().scalar() << ">\n}";
return dbg;
2022-11-24 22:26:53 +00:00
}
#endif
2021-09-03 16:56:57 +00:00
#ifndef QT_NO_DATASTREAM
2022-11-24 22:26:53 +00:00
QDataStream & operator<<(QDataStream & out, const Transform3D & transform) {
out << transform.mTranslation;
out << transform.mScale;
out << transform.mRotation;
return out;
}
2021-09-03 16:56:57 +00:00
2022-11-24 22:26:53 +00:00
QDataStream & operator>>(QDataStream & in, Transform3D & transform) {
in >> transform.mTranslation;
in >> transform.mScale;
in >> transform.mRotation;
transform.m_dirty = true;
return in;
}
2022-11-24 22:26:53 +00:00
#endif
2022-11-24 22:26:53 +00:00
} // namespace Qtk