【问题标题】:qt rotations around the x- and y-axesqt 绕 x 轴和 y 轴旋转
【发布时间】:2013-10-16 14:38:24
【问题描述】:

Qt 使用 3x3 变换矩阵进行透视和仿射变换。如果矩阵的最后一行等于 [0 0 1],则认为矩阵是仿射的。出于这个原因,x 轴和 y 轴旋转矩阵是“非仿射的”并且会导致透视失真。但还有进一步的影响。用户绘制的每个像素都有坐标 (x, y, 1)。无法将 z 坐标设置为 0,之后旋转并将 z 设置为其他值。是否仍有可能以某种方式伪造能够绕任意点旋转?也许通过将 z 设置为某个值“接近”为零然后旋转?

编辑:

我想做什么,正是。通过划掉“通常”透视投影矩阵的第 3 行和第 3 列,可以得到一个可与Qt 一起使用的矩阵。我还使用窗口矩阵转换为QGraphicsItem。现在有 2 个矩阵:

window * projection

一些代码:

float const w(rect.width());
float const h(rect.height());

// aspect ratio
auto const ar(h / w);

// symmetrical infinite frustum
f_[4] = 1.f;
f_[1] = ::std::tan(.5f * hFov_) * f_[4];
f_[0] = -f_[1];
f_[3] = ar * f_[1];
f_[2] = -f_[3];

// perspective projection matrix
auto const rml(f_[1] - f_[0]);
auto const tmb(f_[3] - f_[2]);

::etl::matrix<float, 3, 3> const pMatrix{
  2.f * f_[4] / rml, 0.f              , (f_[1] + f_[0]) / rml,
  0.f              , 2.f * f_[4] / tmb, (f_[3] + f_[2]) / tmb,
  0.f              , 0.f              , -1.f};

auto const halfW(.5f * w);
auto const halfH(.5f * h);

// window matrix
::etl::matrix<float, 3, 3> const wMatrix{
  halfW, 0.f   , halfW,
  0.f  , -halfH, halfH,
  0.f  , 0.f   , 1.f};

wpvMatrix_ = wMatrix * pMatrix;

现在我们想将QPixmap 转换为我们投影的世界。这是完成的,例如使用这个矩阵:

zMatrix =
  worldPixmapWidth / pixmap.width(), 0, p(0),
  0, -worldPixmapHeight / pixmap.height(), p(1),
  0, 0, p(2);

所以我们可以使用wMatrix * pMatrix * zMatrix 来转换像素图。

p 是我们要将QPixmap 转换为的点。现在像素图位于p = (p(0), p(1), p(2))。我们想绕平行于 y 轴的轴旋转,通过p。怎么做?通常,我们会做T(p) * Ry(phi) * T(-p),但我们不能,因为没有可以设置z 坐标的平移矩阵。直接应用Ry 会绕原点旋转。即使根本不进行任何转换,z 也将是 1,而不是 0,因为我们希望它是 Ry

【问题讨论】:

  • Qt 是一个 2D 工具包,它提供的转换应该适用于 2D 点。它根本不支持进行 3D 转换。当你拥有 OpenGL ES 2 的强大功能来绘制你想要的任何东西时,“伪装”是毫无意义的......
  • @KubaOber 如果您有任何想法,请与我们分享。我想一种方法是实现你自己的纹理:)
  • 你想改造什么?请确切地告诉我们从头到尾您打算如何使用此功能。

标签: c++ qt


【解决方案1】:

How to rotate a vertex around a certain point?

执行平移、旋转和向后平移的顺序将有效地围绕您平移的任何点进行旋转。

这同样适用于矩阵变换以及它们被调用和应用的顺序。

更新:

再次阅读您的问题后,听起来您真正要求的是QPixmap的3D投影,以模拟它在翻转过程中,就像纸牌游戏中的纸牌翻转一样。这种围绕平行于 y 轴的某个轴的旋转需要显示适当的失真,以使其看起来像图像的一侧更远而另一侧更近。

http://en.wikipedia.org/wiki/3D_projection

Qt 的旋转计划使用上面等式中的第三个 3x3 矩阵。绕 z 轴旋转。

或者更具体地说,你可以在这里看到它:

http://qt-project.org/doc/qt-5.1/qtgui/qmatrix.html#details,它显示了 2x2 矩阵,其中添加了行以解释平移。

现在说了这么多……这是我在不使用完整的 3D 图形库(如 OpenGL)的情况下可以找到的最接近的伪造它:

http://qt-project.org/forums/viewthread/18615/

http://gerrysweeney.com/horizontal-and-vertical-flip-transformations-of-a-qgraphicsitem-in-qt-qgraphicsview/

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include <QPropertyAnimation>

class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QGraphicsScene * m_pScene;
    QGraphicsView * m_pView;

    QSlider * sliderx;
    QSlider * slidery;
    QGraphicsPolygonItem* transformedItem;
    QPointF itemCenter;

public slots:
    void updateRotation();

};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setCentralWidget(new QWidget);
    QVBoxLayout * layout = new QVBoxLayout;


    m_pScene = new QGraphicsScene(0,0,800,480);

    m_pView = new QGraphicsView(m_pScene);

    m_pView->setFrameStyle(QFrame::NoFrame);
    m_pView->setGeometry(0,0,800,480);
    m_pView->setAutoFillBackground(false);

    layout->addWidget(m_pView);

    sliderx = new QSlider(Qt::Horizontal);
    slidery = new QSlider(Qt::Horizontal);

    sliderx->setRange(-100,100);
    sliderx->setSingleStep(1);
    sliderx->setValue(100);
    slidery->setRange(-100,100);
    slidery->setSingleStep(1);
    slidery->setValue(100);
    QObject::connect(sliderx, SIGNAL(valueChanged(int)),this, SLOT(updateRotation()));
    QObject::connect(slidery, SIGNAL(valueChanged(int)),this, SLOT(updateRotation()));


    layout->addWidget(sliderx);
    layout->addWidget(slidery);
    this->centralWidget()->setLayout(layout);
    QPolygonF polygon;
    polygon << QPointF(100.0,250.0);
    polygon << QPointF(170.0, 350.0);
    polygon << QPointF(30.0, 350.0);
    QGraphicsPolygonItem* testItem = new QGraphicsPolygonItem(polygon);
    m_pScene->addItem(testItem);

    transformedItem = new QGraphicsPolygonItem(polygon);
    transformedItem->setPen(QColor(Qt::red));
    m_pScene->addItem(transformedItem);


    // Here the fun starts:

    itemCenter = transformedItem->mapToParent(transformedItem->boundingRect().center());

//    // Method 1
//    QTransform transform = QTransform();
//    transform.translate(itemCenter.x(),
//                        itemCenter.y());
//    transform.scale(1.0, -1.0);
//    transform.translate(-itemCenter.x(),
//                        -itemCenter.y());
//    transformedItem->setTransform(transform);

//    // Method 2
//    transformedItem->setTransform(QTransform::fromTranslate(itemCenter.x(),
//                                                            itemCenter.y()),true);
//    transformedItem->setTransform(QTransform::fromScale(1.0, -1.0),true);
//    transformedItem->setTransform(QTransform::fromTranslate(-itemCenter.x(),
//                                                            -itemCenter.y()), true);

//    // Method 3
//    transformedItem->translate(itemCenter.x(),
//                               itemCenter.y());
//    transformedItem->scale(1.0, -1.0);
//    transformedItem->translate(-itemCenter.x(),
//                               -itemCenter.y());
}

void MainWindow::updateRotation()
{
    transformedItem->resetTransform();
    transformedItem->translate(itemCenter.x(),
                               itemCenter.y());
    transformedItem->scale((qreal)sliderx->value()/100., (qreal)slidery->value()/100.);
    transformedItem->translate(-itemCenter.x(),
                               -itemCenter.y());
}

MainWindow::~MainWindow() { }

希望对您有所帮助。

【讨论】:

  • 对不起。 :(你有一个显示错误的简单例子吗?
  • 没有错误,只是我无法将z坐标设置为我想要的。想想看,Qt是一个2D库,可以设置x和y,z隐含为1。
猜你喜欢
  • 2021-07-12
  • 1970-01-01
  • 2016-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 2012-05-09
相关资源
最近更新 更多