[Solved, sort of] How to draw a wireframe in Qt3D
-
Hello!
This is (obviously) a Qt3D oriented post...I wasn't sure where to put this; if it's in the wrong place, let me know.
I'm trying to teach myself Qt3D for an upcoming project. Part of what I need to do is to draw a wireframe of my geometry, however, I can't seem to get Qt3D to behave correctly for me. I've simplified this down to simply trying to draw one triangle. I subclass QQuickItem3D to create a new QML element, and use QGLBuilder and QGeometryData to construct a new QGLSceneNode. However, no matter what I set "setDrawingMode" to, I still end up with a white triangle in my scene, instead of the wanted wireframe.
Anyone know anything about Qt3D that can help?
EDIT: I can fix the problem by ignoring the QGLBuilder and the QGLSceneNode, and simply calling the "draw" method on the QGeometryData.
Here's my code:
Wireframe.h:
@
#ifndef WIREFRAME_H
#define WIREFRAME_H#include "qquickitem3d.h"
#include "qglscenenode.h"class Wireframe : public QQuickItem3D
{
Q_OBJECT
public:
Wireframe(QObject *parent = 0);
~Wireframe();protected:
void drawItem(QGLPainter *painter);private:
QGLSceneNode* myScene;
};QML_DECLARE_TYPE(Wireframe)
#endif
@Wireframe.cpp:
@
#include "Wireframe.h"
#include <QGeometryData>
#include <QGLBuilder>Wireframe::Wireframe(QObject *parent)
: QQuickItem3D(parent)
{
QGLBuilder builder;
QGeometryData geometry;
geometry.appendVertex(QVector3D(0,0,0));
geometry.appendVertex(QVector3D(4,0,0));
geometry.appendVertex(QVector3D(0,4,0));
builder << geometry;
myScene = builder.finalizedSceneNode();
myScene->setDrawingMode(QGL::LineLoop);
myScene->setDrawingWidth(10);
myScene->setParent(this);
}Wireframe::~Wireframe(){
delete myScene;
}void Wireframe::drawItem(QGLPainter *painter)
{
// Draw the geometry.
myScene->draw(painter);
}
@main.cpp:
@
#include <QApplication>
#include <QQuickView>
#include <QDeclarativeEngine>
#include "Wireframe.h"int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<Wireframe>("CustomComponents", 1, 0, "Wireframe");
QSurfaceFormat f;
f.setSamples(16);
QQuickView view;
view.setFormat(f);
view.setResizeMode(QQuickView::SizeRootObjectToView);view.setSource(QUrl::fromLocalFile("../wireframe.qml"));
#ifdef Q_OS_SYMBIAN
view.setAttribute(Qt::WA_LockLandscapeOrientation, true);
view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
view.showFullScreen();
#else
if (QApplication::arguments().contains(QLatin1String("-maximize")))
view.showMaximized();
else if (QApplication::arguments().contains(QLatin1String("-fullscreen")))
view.showFullScreen();
else
view.show();
#endifreturn app.exec();
}
@wireframe.qml:
@
import QtQuick 2.0
import CustomComponents 1.0
import Qt3D 2.0Rectangle {
width: 640
height: 480
color: "pink"
Viewport {
width: 640; height: 480
Wireframe { id: wf }
}
}
@