0

I have an c++ application being built with Visual Studio, where I am trying to incorporate using OpenGL through Qt for 3D renderings. When I build my application, the shader files (shader.vert, shader.frag) are included, except it seems that an older version is cached or something similar, because none of the changes I make are applied. As well, when I output the text from the file which is read in, it is for sure the old file. If I rebuild the whole application (which I would like to avoid, it takes several minutes), they are re-included correctly.

Why might this be occurring? I have seen references to "Copy to Output Directory" option in the preferences, but that does not seem to exist in VS22.

QFile file(":/path_to/shader.vert");
    if (!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::information(0, "error", file.errorString());
    }
    QTextStream in(&file);
    QOpenGLShader vertexShader(QOpenGLShader::Vertex);
    QString str = in.readAll();
    vertexShader.compileSourceCode(str);

    QByteArray b = vertexShader.sourceCode();

Through reading the QByteArray b in debug mode (or the QString str), I see that the file contents are not correct, as they are continuously the unchanged file.

I'm using VS22, OpenGL 4.5, Qt 6.5, MSVC with c++17

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Nick T
  • 63
  • 8
  • is `:/path_to/shader.vert` your actual file path? If not what is the path and where are the shader files located? – Alan Birtles Jun 01 '23 at 18:22
  • 3
    The colon at the start of the path indicates that shader.vert is a Qt resource file. These are most commonly "embedded into your application executable" ([ref](https://doc.qt.io/qt-6/resources.html)). In other words, these are read at compile time and included in the executable, and not read from file at runtime. – Yun Jun 01 '23 at 18:34
  • 3
    If you want it to read at runtime you could change this: `QFile file(":/path_to/shader.vert");` to use the path to the shader file directly. – drescherjm Jun 01 '23 at 19:10
  • @Yun is there a way that I can force it to recompile this, if there are changes? – Nick T Jun 02 '23 at 14:43
  • What build system are you using? For CMake, you can include resources.qrc under `qt_add_executable`, which should do the trick. I don't know what the equivalent would be for other build systems. Another option is, like drescherjm suggested, to read the shaders from file at runtime. – Yun Jun 02 '23 at 16:23
  • @Yun Thank you! for what drescherjm said, presumably that means using the full path (C:/Users/....), which I couldn't use for deploying the application? – Nick T Jun 02 '23 at 16:55
  • You're welcome! For deployment, couldn't you use the resources method, as the shaders won't change? If you want to avoid using the resources method, you could also use a relative path, but I'm not sure how portable that would be. Another option is to not use files at all, but to put the shader sources in a source/header file as strings. – Yun Jun 02 '23 at 17:17

0 Answers0