Qt’s QTransform and Mode 7 (fake 3D) attempted implementation

If you can’t implement real 3D, then fake 2D 3D graphics might work.

UPDATE Apr. 9, 2013 – Looky, looky, success!

This is real 3D rotation transformations using QTransform and QPainter. No third-party 3D library reference to OpenGL or DirectX. That means it has a higher chance of working across different operating systems (assuming Qt is cooperative).

UPDATE Aug. 10, 2012 – Here’s how you use Qt’s QTransform on a paint device for “true” 3D transformations.

Sub-classing your widget’s PaintEvent:

void MainWidget::paintEvent(PaintEvent e)
{
    int rotationAngle = 45;
    QPixmap pixmap("somefile.jpg");
    QTransform transform;

    //you need to translate it to the center of the
    //viewport because the default origin of
    //transformation (0,0) the top-left of the screen
    transform.translate(this.width()/2,this/height()/2);

    transform.rotate(rotationAngle,Qt::XAxis);

    //then you have to change it back
    transform.translate(-this.width()/2,-this/height()/2);

    QPainter painter(this);
    pntr.setTransform(transform);
    pntr.drawPixmap(QRect(0,0,320,240),pixmap);
    pntr.resetTransform();
    pntr.end();
}

———————————————

Back in the Super Nintendo (SNES) days, we enjoyed some pretty rad “fake” 3D games like F-Zero and Mario Kart. SNES had “mode 7”. Now that I’m developing my own 2D game engine, I really want to implement at the least some parallax for my backgrounds. While I’m sure I could accomplish this with OpenGL or DirectX, I don’t want to rely on anymore external DLL dependencies unless I need to. Qt provides a QTransform function, but it’s not quite there. I’ve been playing around with it.

My Mode 7 wannabe implementation concept

My Mode 7 wannabe implementation concept

In order to complete a full illusion of 3D, I’ll have to implement many separate buffers and composite those onto a final output buffer. Animation is just a bunch of output buffers played really fast to give you the illusion of movement.

I was able to produce the mock-up in Photoshop, which seems to have the perspective algorithm down. Now, it’s just a matter of figuring it out on my end for my Qt/c++ implementation.

Tags: , , , , , , , , , , , , ,

Comments are closed.