# Qt3D - Drawing simple geometric shapes [lines, circles and so on]

• Hi guys, Qt3D is very nice (but sometimes crashing), but i can’t understand some simple things:

1. How to draw simple 3D line from (0,0,0) to (10,10,10) through Qt3D with given color and thickness?
Like on native OpenGL:
``````glLineWidth(2.5);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(15, 0, 0);
glEnd();
``````
1. How to draw simple 3D circle with center in (0,0,0) and radius 10 through Qt3D?
2. How to use QPainter to draw on textures in Qt3D on show it on the screen?

P.S.: I have seen examples on Qt3D with drawing lines, but sources is unavailable :(
here is images from this site: https://blog.qt.io/blog/2016/06/16/introducing-qt-3d/

P.P.S.: First I just like to draw a line in 3D :-)

• Hi! I have bad news for you: There's no simple way to do that yet. Only the hard way. Better use one of the good old OpenGL libraries until Qt3D has become more mature. E.g. GLUT.

• @Wieland said in Qt3D - Drawing simple geometric shapes [lines, circles and so on]:

Hi! I have bad news for you: There's no simple way to do that yet. Only the hard way. Better use one of the good old OpenGL libraries until Qt3D has become more mature. E.g. GLUT.

Hi! I'm not looking for easy ways, I will approach and complex ways :-)
I'm not new in 3D graphics, I am new to Qr3D...

Can you tall me any method to draw a simple 3D line via Qt3D... ?
I want to use Qt3D for 3D CAD Software... Its' a bad idea?

I was able to draw a line through the vertex buffer filling... But it's too hard... Look:
[i dont know, how to use spoiler on this forum :-) ]

``````bool LayerMesh::initialize(Qt3DCore::QEntity *parent)
{
meshRenderer = new Qt3DRender::QGeometryRenderer;
geometry = new Qt3DRender::QGeometry(meshRenderer);

vertexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, geometry);
indexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::IndexBuffer, geometry);

int lineSize = 4;
int hLineSize = ((qAbs(netX1 - netX0) / netMajorStep) + 1) * lineSize * 3;
int vLineSize = ((qAbs(netZ1 - netZ0) / netMajorStep) + 1) * lineSize * 3;
int vertexNum = hLineSize + vLineSize;

float* vertexRawData = new float[vertexNum];
int idx = 0;
QColor majorColor = QColor(220,220,220);
QColor minorColor = QColor(243,243,243);
for(float x = netX0; x <= netX1; x += netMajorStep)
{
vertexRawData[idx++] = x; vertexRawData[idx++] = netY; vertexRawData[idx++] = netZ0;
vertexRawData[idx++] = majorColor.redF(); vertexRawData[idx++] = majorColor.greenF(); vertexRawData[idx++] = majorColor.blueF();
vertexRawData[idx++] = x; vertexRawData[idx++] = netY; vertexRawData[idx++] = netZ1;
vertexRawData[idx++] = majorColor.redF(); vertexRawData[idx++] = majorColor.greenF(); vertexRawData[idx++] = majorColor.blueF();
}

for(float z = netZ0; z <= netZ1; z += netMajorStep)
{
vertexRawData[idx++] = netX0; vertexRawData[idx++] = netY; vertexRawData[idx++] = z;
vertexRawData[idx++] = majorColor.redF(); vertexRawData[idx++] = majorColor.greenF(); vertexRawData[idx++] = majorColor.blueF();
vertexRawData[idx++] = netX1; vertexRawData[idx++] = netY; vertexRawData[idx++] = z;
vertexRawData[idx++] = majorColor.redF(); vertexRawData[idx++] = majorColor.greenF(); vertexRawData[idx++] = majorColor.blueF();
}

QByteArray ba;
int bufferSize = vertexNum * sizeof(float);
ba.resize(bufferSize);
memcpy(ba.data(), reinterpret_cast<const char*>(vertexRawData), bufferSize);
vertexDataBuffer->setData(ba);

int stride = 6 * sizeof(float);

// Attributes
Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute();
positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
positionAttribute->setBuffer(vertexDataBuffer);
positionAttribute->setDataType(Qt3DRender::QAttribute::Float);
positionAttribute->setDataSize(3);
positionAttribute->setByteOffset(0);
positionAttribute->setByteStride(stride);
positionAttribute->setCount(vertexNum / 2);
positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());

Qt3DRender::QAttribute *colorAttribute = new Qt3DRender::QAttribute();
colorAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
colorAttribute->setBuffer(vertexDataBuffer);
colorAttribute->setDataType(Qt3DRender::QAttribute::Float);
colorAttribute->setDataSize(3);
colorAttribute->setByteOffset(3 * sizeof(float));
colorAttribute->setByteStride(stride);
colorAttribute->setCount(vertexNum / 2);
colorAttribute->setName(Qt3DRender::QAttribute::defaultColorAttributeName());

meshRenderer->setInstanceCount(1);
meshRenderer->setIndexOffset(0);
meshRenderer->setFirstInstance(0);
meshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
meshRenderer->setGeometry(geometry);
meshRenderer->setVertexCount(vertexNum / 2);

material = new Qt3DExtras::QPerVertexColorMaterial(parentEntity);
transform = new Qt3DCore::QTransform;
transform->setScale(1.0f);

Qt3DCore::QEntity *entity = new Qt3DCore::QEntity(parentEntity);

entity->setParent(parentEntity);

return true;
}
``````

• @THE_MASTER Yepp, that's exactly the code you have to come up with right now: Building your own QGeometry and QGeometryRenderer. That's what I meant by the hard way :-)

• @Wieland said in Qt3D - Drawing simple geometric shapes [lines, circles and so on]:

@THE_MASTER Yepp, that's exactly the code you have to come up with right now: Building your own QGeometry and QGeometryRenderer. That's what I meant by the hard way :-)

I'm starting to develop a 3D CAD system, what do you think about Qt3D, can he be used as main 3D engine? It's a good idea or not? :-)

Maybe you know good free (opensource) 3D engine with aslo good integration with Qt ?

P.S.: about old good OpenGL Functions, like glBegin.... I need crossplatform support and i think, sheuld use OpenGL ES, but it does not have these functions :(

• @THE_MASTER Do you want to do it completely from scratch? If so, I'd say using Qt3D would be an interesting choice as it provides you with a scene graph, rendering and also stuff like object picking. Also it's extendable, so you could integrate a physics engine (like Bullet). On the other hand, drawing 3D stuff on the screen isn't the biggest problem. The hardest task is of course all the CAD functionality. If this is only a fun project, then okay, start writing your own CAD kernel and see how far you can get. Otherwise, use an existing CAD kernel. I don't know about any good free ones, although OPEN CASCADE looks somewhat okay-ish. E.g. the folks at FreeCAD use it. The real deal would of course be to use one of the real kernels, like Siemens' Parasolid or that thing from Dassault.

• Hi,
there is still the class QOpenGLWidget, where you can use the gl* functions freely. But I expect it to become more and more neglected, just like QWidgets as a whole.
-Michael.

• @Wieland said in Qt3D - Drawing simple geometric shapes [lines, circles and so on]:

Do you want to do it completely from scratch?

yes

If this is only a fun project

No, this is a real big commercial project...

I think I'll use Kt3D and will hope that this project (Qt3D) will not die... :-)

• Hi THE_MASTER