I've recently started learning Qt/QML/C++ and trying to build a very basic 3D scene to rotate the camera around a mesh object.
I'm finding it very difficult to follow the examples and I'm finding the documentation doesn't provide any useful instructions. There doesn't seem to be many tutorials out there either, perhaps I'm looking in the wrong places.
main.cpp
#include <Qt3DQuickExtras/qt3dquickwindow.h>
#include <Qt3DQuick/QQmlAspectEngine>
#include <QGuiApplication>
#include <QtQml>
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
Qt3DExtras::Quick::Qt3DQuickWindow view;
// Expose the window as a context property so we can set the aspect ratio
view.engine()->qmlEngine()->rootContext()->setContextProperty("_window", &view);
view.setSource(QUrl("qrc:/main.qml"));
view.setWidth(800);
view.setHeight(600);
view.show();
return app.exec();
}
main.qml
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.0
Entity {
id: sceneRoot
Camera {
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 25
aspectRatio: _window.width / _window.height
nearPlane : 0.1
farPlane : 1000.0
position: Qt.vector3d( 0, 0.0, 20.0 )
upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
}
OrbitCameraController {
camera: camera
}
components: [
RenderSettings {
activeFrameGraph: ForwardRenderer {
clearColor: Qt.rgba(0, 0.5, 1, 1)
camera: camera
}
},
InputSettings { }
]
PhongMaterial {
id: carMaterial
}
Mesh {
id: carMesh
source: "resources/aventador.obj"
}
Entity {
id: carEntity
components: [ carMesh, carMaterial ]
}
}
How do I get the camera to rotate around the mesh object?
The OrbitCameraController allows to move the camera along an orbital path. To make it rotate around the mesh, you could set the viewCenter of the camera to the position of the mesh (translation of the transform of the entity containing the mesh) and use your keyboard/mouse to rotate it around.
So add:
and add the transform to the components of the entity. Change the viewCenter of the camera to