Applying Transformations and MaterialsIn this tutorial we will apply transformations and materials to a simple cube object to demonstrate how to modify the QGLPainter state to achieve different effects. Cube in QMLIt is quite simple to achieve these effects in Qt3D: import QtQuick 2.0 import Qt3D 1.0 import Qt3D.Shapes 1.0 Viewport { width: parent.width; height: parent.height fillColor: "darkblue" renderMode: "DirectRender" objectName: "cube viewport" Cube { scale: 1.5 objectName: "cube" transform: Rotation3D { angle: 45 axis: Qt.vector3d(1, 1, 1) } effect: Effect { color: "#aaca00" texture: "qtlogo.png" decal: true } } } Cube in C++In the C++ side of Qt3D we start by declaring a view class to show our cube on the screen: class CubeView : public QGLView { Q_OBJECT public: CubeView(QWindow *parent = 0); ~CubeView(); protected: void paintGL(QGLPainter *painter); private: QGLSceneNode *cube; }; QGLView provides most of the logic for initializing the view, setting the camera position, and handling navigation via keyboard and mouse events. In the constructor we use QGLBuilder to create the geometry for the cube object that we will be using in later steps: CubeView::CubeView(QWindow *parent) : QGLView(parent) { QGLBuilder builder; builder << QGL::Faceted; builder << QGLCube(); cube = builder.finalizedSceneNode(); } Then it is a simple matter to draw the object in our paintGL() method: void CubeView::paintGL(QGLPainter *painter) { cube->draw(painter); } If we run the program now, we get the following output, which isn't very cube-like: The problem is that we are looking at the cube straight onto its front face. So the rest of the cube is hidden from our view and it looks like a square. Let's modify the modelview transformation matrix a little bit to apply a 45 degree rotation around the axis (1, 1, 1): void CubeView::paintGL(QGLPainter *painter) { painter->modelViewMatrix().rotate(45.0f, 1.0f, 1.0f, 1.0f); cube->draw(painter); } Now the results are a little better: The cube is still a little odd-looking however. This is because up until now we have been using the default flat color effect in QGLPainter that colors the faces with a uniform color (white in this case). So let's change to a lit material effect with a nice green color: void CubeView::paintGL(QGLPainter *painter) { painter->setStandardEffect(QGL::LitMaterial); painter->setFaceColor(QGL::AllFaces, QColor(170, 202, 0)); painter->modelViewMatrix().rotate(45.0f, 1.0f, 1.0f, 1.0f); cube->draw(painter); } That's much better; now it looks like a cube: To complete this tutorial, let's make the cube a little more interesting by adding a texture to the side: class CubeView : public QGLView { ... private: QGLSceneNode *cube; QGLTexture2D logo; }; CubeView::CubeView(QWindow *parent) : QGLView(parent) { ... logo.setImage(QImage(QLatin1String(":/qtlogo.png"))); } void CubeView::paintGL(QGLPainter *painter) { painter->setStandardEffect(QGL::LitDecalTexture2D); painter->setFaceColor(QGL::AllFaces, QColor(170, 202, 0)); logo.bind(); painter->modelViewMatrix().rotate(45.0f, 1.0f, 1.0f, 1.0f); cube->draw(painter); } Return to the main Tutorials page. Files: |