{"id":1404,"date":"2012-01-19T03:32:18","date_gmt":"2012-01-19T09:32:18","guid":{"rendered":"http:\/\/blog.eastfist.com\/?p=1404"},"modified":"2012-01-19T03:32:18","modified_gmt":"2012-01-19T09:32:18","slug":"qts-qtransform-and-mode-7-fake-3d-attempted-implementation","status":"publish","type":"post","link":"https:\/\/eastfist.com\/blog\/2012\/01\/19\/qts-qtransform-and-mode-7-fake-3d-attempted-implementation\/","title":{"rendered":"Qt&#8217;s QTransform and Mode 7 (fake 3D) attempted implementation"},"content":{"rendered":"<p>If you can&#8217;t implement real 3D, then fake 2D 3D graphics might work. <!--more--><\/p>\n<p><b>UPDATE Apr. 9, 2013<\/b> &#8211; Looky, looky, success!<\/p>\n<p><iframe loading=\"lazy\" title=\"Qt 2D Game F-Zero Mode 7 Real 3D Demo + No OpenGL + No DirectX + QTransform\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/KuTBzhBqU4Y?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>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).<\/p>\n<p><b>UPDATE Aug. 10, 2012<\/b> &#8211; Here&#8217;s how you use Qt&#8217;s QTransform on a paint device for &#8220;true&#8221; 3D transformations.<\/p>\n<p>Sub-classing your widget&#8217;s PaintEvent:<\/p>\n<blockquote>\n<pre>\n<code>void MainWidget::paintEvent(PaintEvent e)\n{\n    int rotationAngle = 45;\n    QPixmap pixmap(\"somefile.jpg\");\n    QTransform transform;\n\n    \/\/you need to translate it to the center of the\n    \/\/viewport because the default origin of\n    \/\/transformation (0,0) the top-left of the screen\n    transform.translate(this.width()\/2,this\/height()\/2);\n\n    transform.rotate(rotationAngle,Qt::XAxis);\n\n    \/\/then you have to change it back\n    transform.translate(-this.width()\/2,-this\/height()\/2);\n\n    QPainter painter(this);\n    pntr.setTransform(transform);\n    pntr.drawPixmap(QRect(0,0,320,240),pixmap);\n    pntr.resetTransform();\n    pntr.end();\n}\n<\/code><\/pre>\n<\/blockquote>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Back in the Super Nintendo (SNES) days, we enjoyed some pretty rad &#8220;fake&#8221; 3D games like F-Zero and Mario Kart.  SNES had &#8220;mode 7&#8221;.  Now that I&#8217;m developing my own 2D game engine, I really want to implement at the least some parallax for my backgrounds.  While I&#8217;m sure I could accomplish this with OpenGL or DirectX, I don&#8217;t want to rely on anymore external DLL dependencies unless I need to.  Qt provides a QTransform function, but it&#8217;s not quite there.  I&#8217;ve been playing around with it.<\/p>\n<figure id=\"attachment_1405\" aria-describedby=\"caption-attachment-1405\" style=\"width: 360px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.eastfist.com\/wp-content\/uploads\/2012\/01\/mode7implement.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.eastfist.com\/wp-content\/uploads\/2012\/01\/mode7implement-360x360.jpg\" alt=\"My Mode 7 wannabe implementation concept\" title=\"My Mode 7 wannabe implementation concept\" width=\"360\" height=\"360\" class=\"size-medium wp-image-1405\" \/><\/a><figcaption id=\"caption-attachment-1405\" class=\"wp-caption-text\">My Mode 7 wannabe implementation concept<\/figcaption><\/figure>\n<p>In order to complete a full illusion of 3D, I&#8217;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.<\/p>\n<p>I was able to produce the mock-up in Photoshop, which seems to have the perspective algorithm down.  Now, it&#8217;s just a matter of figuring it out on my end for my Qt\/c++ implementation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you can&#8217;t implement real 3D, then fake 2D 3D graphics might work.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,9],"tags":[43,46,557,726,905,1053,1237,1670,1767,1773,2028,2060,2379,2575],"class_list":["post-1404","post","type-post","status-publish","format-standard","hentry","category-artistic","category-tech","tag-2d","tag-3d","tag-concept","tag-development","tag-fake-3d","tag-game","tag-howto","tag-mode-7","tag-nintendo","tag-nokia","tag-programming","tag-qt","tag-snes","tag-test"],"_links":{"self":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/posts\/1404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/comments?post=1404"}],"version-history":[{"count":0,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/posts\/1404\/revisions"}],"wp:attachment":[{"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/media?parent=1404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/categories?post=1404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eastfist.com\/blog\/wp-json\/wp\/v2\/tags?post=1404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}