Important: Please read the Qt Code of Conduct -

Qt3D and shaders (GLSL) ?

  • Hi,
    does Qt3d (Qt3dRender) generate shaders (GLSL) and compile them at run time ?
    (because we don't write shaders when we write a Qt3D app but Qt3D use OpenGL.)

  • @stephane78 Hi! No, Qt3D doesn not generate shaders at runtime. For its default pipeline it comes with predefined default shaders. But you are free to change the pipeline as you wish (in C++ and also in QML) and you can use your own shaders.

  • @Wieland Hi, thanks for your reply. and how can I use my own shaders (vertex and fragments shaders) in a Qt3D app ?

  • @Wieland, Because there is only Qml examples about shaders with Qt3D. and I use C++....

  • Take a look at this example. In WireframeEffect.qml there is:

    shaderProgram: ShaderProgram {
      vertexShaderCode: loadSource("qrc:/shaders/robustwireframe.vert")
     geometryShaderCode: loadSource("qrc:/shaders/robustwireframe.geom")
     fragmentShaderCode: loadSource("qrc:/shaders/robustwireframe.frag")

    The documentation on Qt3D is very thin yet but a handful of people here have some knowlegde about this module, so don't hesitate to ask :-)

  • @stephane78 said:

    Because there is only Qml examples about shaders with Qt3D. and I use C++

    Oh! Ok! What do you want to do?

  • @Wieland ,is it not possible with C++ Qt3D app ? because I don't use Qml...

  • @Wieland, For example I have made a little Qt3D program with a cube in rotation and with a texture. I wanted to modify this program with my own vertex and fragments shaders.I have written this program from the example dynamicscene

  • Will be my program faster, if I write my own vertex and fragments shaders or isn't it usefull ?

  • Yes I have thought a little bit and I think it is not very useful, because if I want to use shaders I can write a Qt+OpenGL app...

  • Writing your own shaders is useful when you want to do stuff that the default shaders don't do. The default shaders implement phong lighting etc. With your own shaders you can e.g. do stuff like this:

  • @Wieland, Yes but it is perhaps not available with C++ in the Qt5.6 version of Qt3D ? I didn't found anything about that in the documentation of C++ classes of Qt3D

  • Well, of course it is possible to do it in C++ simply because it's possible in QML and the Qt3D QML implementation uses the C++ implementation. BUT: Looking at the sources, I think figuring out how to do it will take you some time. It's not soo much code to study but still.

  • @Wieland, OK thanks Wieland. I will look at the sources. you answered my first question.

  • @Wieland, I have found the class Qt3dRender::QShaderProgram that has for example the two slots:
    void setVertexShaderCode(const QByteArray &vertexShaderCode);
    void setFragmentShaderCode(const QByteArray &fragmentShaderCode);

    I think I will try to use them in the main.cpp of my program that is derivated from the example dynamicscene.....

  • @stephane78 Great! Please let us know when you get this to work. I bet many people (including me *g*) would appreciate it.

  • Yes I come back here (this evening or later) later to tell if it works.

  • @Wieland,Hi, I have looked at the code of my Qt3D app and of my Qt+OpenGL app,and I think I must change the architecture of my Qt3d app if I want to use a vertex shader,because my rotation(transformation) takes places in the scene in my Qt3D app (see example dynamicscene) and the transformation matrix is in the vertex shader in the Qt+OpenGL app. and I don't know how to use the QShaderdata class to use the uniform (the transformation matrix) and the attribute (the positions vertices),if I put a vertex shader in the QT3D app.

    So I close the subject here,because I have already two ways of making 3D with Qt+OpenGL and Qt3D and I don't really need to use shaders in Qt3D (I have two apps that do the same,one with Qt+OpenGL and one with Qt3D).


Log in to reply