Qt3d points and lines
-
@guy-incognito could you share the code for drawing lines ? thanks!
-
Let' s say i want to draw a triangle. I define the vertices p1 (0,0,0), p2 (2,0,0), and p3(1,2,0) and draw the lines between them. That's what i currently do. Now I want to draw a triangle with tubes instead of lines, so that it sort of looks like the triangle music instrument. I think thats called tubing and extrusion in OpenGL.
It was my question if there is a convenient way in qt3d to do this. Maybe my lines could be replaced with cylinders, so the bottom surface starts at (0,0,0), the top surface ends in (2,0,0) etc. (thats what i meant with "behave like a line").
-
Look at this thread's example code. It should be enough to get you started.
-
Thanks, the x,y and z axis look exactly like what I want. The only problem is, that QTransform for the QCylincerMesh is pretty unconvenient (as far as I see).
Take a look at this code. I used it to get an idea how to create custom meshes. You can adapt it to make it draw lines instead of TetraHedrons pretty easily.
-
This has been my PITA also. I have just found an example in qt5.6 that was dropped in all further releases but it provides a working set of code that creates a custom entity with your own set of vectors / normals / colors. If you look at this in code.qt.io
http://code.qt.io/cgit/qt/qt3d.git/tree/examples/qt3d/custom-mesh-cpp?h=5.6
I wish Qt (KDAB is actually doing the Qt3D stuff) would spend a bit of time and put together some documented examples at the level of what their qGraphicsItem module was. Or at a minimum put some details to the qt function documentation. It is very frustrating to try and figure things out when there is no documentation or good functional examples on the objects we are supposed to use.
-
@Steve-Brunton
I am trying to draw a line in qml. I used the custom_mesh_qml for checking how it works in qt 5.9. but I encountered an error in the main.cpp file in the line 'engine.aspectEngine()->setData(data);' like setData not found. Can you help me on this. Thank you. -
@MatPaul Why don't you post the actual error message?
I guess the compiler tells you that there is no such method: setData.
I guess aspectEngine() returns https://doc.qt.io/qt-5/qt3dcore-quick-qqmlaspectengine.html and QQmlAspectEngine does not have setData() method. -
@jsulm
I had used the code from this link
http://code.qt.io/cgit/qt/qt3d.git/tree/examples/qt3d/custom-mesh-qml?h=5.6And run that on qt5.9
I think the setData() functionality is removed in 5.9. is there a new function which does the similar behaviour -
Try this piece of code to draw lines using pipes. It assumes that your camera up vector is the Y axis:
double w, h, d, l, tx, ty, tz, lxz, anglex, angley;
w = to.GetX() - from.GetX();
h = to.GetY() - from.GetY();
d = to.GetZ() - from.GetZ();
l = sqrt(ww + hh + dd);
lxz = sqrt(ww + d*d);
tx = from.GetX() + w/2;
ty = from.GetY() + h/2;
tz = from.GetZ() + d/2;
anglex = acos(h/l)*180/3.14159265;
angley = acos(d/lxz)180/3.14159265;
QVector3D QVx(static_cast<float>(1), static_cast<float>(0), static_cast<float>(0));
QVector3D QVy(static_cast<float>(0), static_cast<float>(1((w<0)?-1:1)), static_cast<float>(0));// Cylinder shape data Qt3DExtras::QCylinderMesh *cylinder = new Qt3DExtras::QCylinderMesh(); cylinder->setRadius(.005f); cylinder->setLength(static_cast<float>(l)); cylinder->setRings(100); cylinder->setSlices(20); // CylinderMesh Transform Qt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform(); cylinderTransform->setScale(1.0f); cylinderTransform->setRotation(Qt3DCore::QTransform::fromAxesAndAngles(QVx,static_cast<float>(anglex), QVy, static_cast<float>(angley))); cylinderTransform->setTranslation(QVector3D(static_cast<float>(tx), static_cast<float>(ty), static_cast<float>(tz))); Qt3DExtras::QDiffuseSpecularMaterial *cylinderMaterial = new Qt3DExtras::QDiffuseSpecularMaterial(); cylinderMaterial->setDiffuse(QColor(QRgb(0xffffff))); // Cylinder m_cylinderEntity = new Qt3DCore::QEntity(m_rootEntity); m_cylinderEntity->addComponent(cylinder); m_cylinderEntity->addComponent(cylinderMaterial); m_cylinderEntity->addComponent(cylinderTransform);
}
-
@AlanWasHere
Hello,
what do you mean by : static_cast<float>(1((w<0)?-1:1)) please? -
@halima What exactly are you asking?
static_cast casts from one type to another:static_cast<T>(W) - this casts from W to T
So, in this case it casts from int to float.
Or do you want to know what(w<0)?-1:1)
means?
If so then see https://www.tutorialspoint.com/cplusplus/cpp_conditional_operator.htm -
Hi! I wrote resolution like @AlanWasHere, but a little clearer
void addLine(QEntity *parentEntity, const QVector3D &srcPos, const QVector3D &targPos) { auto edgeEntity = new QEntity{parentEntity}; auto cylinder = new QCylinderMesh{edgeEntity}; auto len = srcPos.distanceToPoint(targPos); cylinder->setLength(len); cylinder->setRadius(0.1f); auto transPoint = targPos - srcPos; auto xAngle = atan(sqrt(pow(transPoint.z(), 2) + pow(transPoint.x(), 2)) / transPoint.y()) / M_PI * 180; auto yAngle = (transPoint.x() == 0 && transPoint.z() == 0) ? 0 : atan(transPoint.x() / transPoint.z()) / M_PI * 180; auto transform = new Qt3DCore::QTransform{edgeEntity}; transform->setRotationX(xAngle); transform->setRotationY(yAngle); transform->setTranslation((srcPos + targPos) / 2); auto material = new QPhongMaterial{edgeEntity}; material->setDiffuse("#ffff00"); edgeEntity->addComponent(cylinder); edgeEntity->addComponent(transform); edgeEntity->addComponent(material); }