What do I have now? I have a cube consisting of 6 planes. All these planes I generate in XY coordinates and then place them by matrix transformations.
I need to rotate my cube along the global axis and move my planes along cube rotation;
Then I need to move one of the planes correctly. But the planes move along the global axis. The red line shows how the plane moves now. The green line shows how it should move.
How I create a cube. All vertices are in the range (0,0) - (2, 2);
planeXY.setupMesh();
planeXY.setOrigin({1, 1, 1});
planeXY1.setupMesh();
planeXY1.setOrigin({1, 1, 1});
planeXY1.moveAlongGlobalAxis(QVector3D(0.0, 0.0, 2.0));
planeZY.setupMesh();
planeZY.setOrigin({1, 1, 1});
planeZY.rotate(QVector3D(0.0f, -90.0f, 0.0f));
planeZY.moveAlongGlobalAxis(QVector3D(-2.0, 0.0, 0.0));
planeZY1.setupMesh();
planeZY1.setOrigin({1, 1, 1});
planeZY1.rotate(QVector3D(0.0f, -90.0f, 0.0f));
planeXZ.setupMesh();
planeXZ.setOrigin({1, 1, 1});
planeXZ.rotate(QVector3D(90.0f, 0.0f, 0.0f));
planeXZ.moveAlongGlobalAxis(QVector3D(0.0, -2.0, 0.0));
planeXZ1.setupMesh();
planeXZ1.setOrigin({1, 1, 1});
planeXZ1.rotate(QVector3D(90.0f, 0.0f, 0.0f));
Mesh.cpp
void Mesh::moveAlongGlobalAxis(QVector3D coordinates)
{
QMatrix4x4 identityMatrix;
identityMatrix.translate(coordinates);
position += coordinates;
this->translationMatrix = identityMatrix * translationMatrix;
}
void Mesh::moveAlongLocalAxis(QVector3D coordinates)
{
this->moveAlongGlobalAxis(coordinates);
}
void Mesh::rotate(QVector3D rotation)
{
QMatrix4x4 identityMatrix;
identityMatrix.translate((-1) * this->position + this->origin);
identityMatrix.rotate(rotation.x(), QVector3D(1.0, 0.0, 0.0));
identityMatrix.rotate(rotation.y(), QVector3D(0.0, 1.0, 0.0));
identityMatrix.rotate(rotation.z(), QVector3D(0.0, 0.0, 1.0));
identityMatrix.translate(this->position - this->origin);
this->rotationMatrix = identityMatrix * this->rotationMatrix;
}
void Mesh::setOrigin(QVector3D origin)
{
this->origin = origin;
}
const QMatrix4x4 Mesh::getModelMatrix() const
{
return translationMatrix * rotationMatrix;
}
This function I want to implement:
void Mesh::moveAlongLocalAxis(QVector3D coordinates)
{
this->moveAlongGlobalAxis(coordinates);
}
How I rotate my cube
else if(ev->key() == Qt::Key_R)
{
planeXY.rotate(QVector3D(20.0f, 0.0f, 0.0f));
planeXY1.rotate(QVector3D(20.0f, 0.0f, 0.0f));
planeZY.rotate(QVector3D(20.0f, 0.0f, 0.0f));
planeZY1.rotate(QVector3D(20.0f, 0.0f, 0.0f));
planeXZ.rotate(QVector3D(20.0f, 0.0f, 0.0f));
planeXZ1.rotate(QVector3D(20.0f, 0.0f, 0.0f));
}