<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QOpenGL fragment shader]]></title><description><![CDATA[<p dir="auto">Being new to it, I'm trying to run a very simple example of QOpenGL functionalities. I can build a program that runs, but appears to completely ignore the fragment shader even though its compilation completes successfully.</p>
<p dir="auto">MainWindow.h, defining the custom widget.</p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QtOpenGL&gt;

class MainWindow : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);

private:
    QOpenGLVertexArrayObject m_vao;
    QOpenGLBuffer m_vbo;
    QOpenGLShaderProgram *m_program;

protected:
    void initializeGL();
    void paintGL();
};

#endif // MAINWINDOW_H
</code></pre>
<p dir="auto">MainWindow.ccp</p>
<pre><code>// Shader sources
const GLchar* vertexSource =
    "in vec2 position;"
    "void main()"
    "{"
    "    gl_Position = vec4(position, 0.0, 1.0);"
    "}";

const GLchar* fragmentSource =
    "void main()"
    "{"
    "   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
    "}";


MainWindow::MainWindow(QWidget *parent) :
    QOpenGLWidget(parent),
    m_program(0)
{
    ;
}


void MainWindow::initializeGL()
{
    // Expose GL functions
    initializeOpenGLFunctions();

    // Set clear color
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    // Instantiate program, link shaders, bind program
    m_program = new QOpenGLShaderProgram();
    m_program-&gt;addShaderFromSourceCode(QOpenGLShader::Vertex, vertexSource);
    m_program-&gt;addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentSource);
    m_program-&gt;link();
    m_program-&gt;bind();

    // Vertex array object
    m_vao.create();
    m_vao.bind();

    // Vertex buffer object
    m_vbo.create();
    m_vbo.bind();

    GLfloat vertices[] = {
         0.0f,  0.5f,
         0.5f, -0.5f,
        -0.5f, -0.5f
    };

    m_vbo.allocate(vertices, sizeof(vertices));

    // Specify the layout of the vertex data
    int posAttrib = m_program-&gt;attributeLocation("position");
    glEnableVertexAttribArray(posAttrib);
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);

    m_program-&gt;release();
}


void MainWindow::paintGL()
{
    // Clear the screen to black
    glClear(GL_COLOR_BUFFER_BIT);

    // Draw a triangle from the 3 vertices
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

</code></pre>
<p dir="auto">It draws the triangle as it should, but it's white instead of red. If I comment out the line where I compile the fragment shader, I get the same result.<br />
Main loop just instantiates a QApplication and a MainWindow.<br />
I've run the hellogl2 example project, which works regularly. Thanks in advance.</p>
]]></description><link>https://forum.qt.io/topic/63324/qopengl-fragment-shader</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Apr 2026 21:31:32 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/63324.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 25 Jan 2016 17:39:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QOpenGL fragment shader on Mon, 25 Jan 2016 19:25:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/aanok">@<bdi>Aanok</bdi></a> I always find OpenGL code very hard to debug. Glad this solved it! :-)</p>
]]></description><link>https://forum.qt.io/post/309489</link><guid isPermaLink="true">https://forum.qt.io/post/309489</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 25 Jan 2016 19:25:16 GMT</pubDate></item><item><title><![CDATA[Reply to QOpenGL fragment shader on Mon, 25 Jan 2016 19:23:11 GMT]]></title><description><![CDATA[<p dir="auto">@Wieland That did it! Thanks everyone :)</p>
]]></description><link>https://forum.qt.io/post/309488</link><guid isPermaLink="true">https://forum.qt.io/post/309488</guid><dc:creator><![CDATA[Aanok]]></dc:creator><pubDate>Mon, 25 Jan 2016 19:23:11 GMT</pubDate></item><item><title><![CDATA[Reply to QOpenGL fragment shader on Mon, 25 Jan 2016 19:18:13 GMT]]></title><description><![CDATA[<p dir="auto">Sorry I might be sending you on a wild goose chase, I haven't done OpenGL in QT C++ since 4.x, I use QML these days.</p>
<p dir="auto">Check out: <a href="http://doc.qt.io/qt-5/qtopengl-hellogl2-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qtopengl-hellogl2-example.html</a></p>
<p dir="auto">Get that example or one of the many others running, and then work backwards to simply drawing vertex's in a triangle with your shader.  Thats going to be your fast way to solve the problem at this point.</p>
]]></description><link>https://forum.qt.io/post/309485</link><guid isPermaLink="true">https://forum.qt.io/post/309485</guid><dc:creator><![CDATA[Orby]]></dc:creator><pubDate>Mon, 25 Jan 2016 19:18:13 GMT</pubDate></item><item><title><![CDATA[Reply to QOpenGL fragment shader on Mon, 25 Jan 2016 19:15:46 GMT]]></title><description><![CDATA[<p dir="auto">Hi! Releasing the program in the init function is correct but you have to bind it again in the rendering function. Same for the buffers.</p>
]]></description><link>https://forum.qt.io/post/309484</link><guid isPermaLink="true">https://forum.qt.io/post/309484</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 25 Jan 2016 19:15:46 GMT</pubDate></item><item><title><![CDATA[Reply to QOpenGL fragment shader on Mon, 25 Jan 2016 19:12:25 GMT]]></title><description><![CDATA[<p dir="auto">Does removing the line: m_program-&gt;release(); fix the program?  It looks like you are setting up the shader, and then destroying it before its actually used.</p>
]]></description><link>https://forum.qt.io/post/309482</link><guid isPermaLink="true">https://forum.qt.io/post/309482</guid><dc:creator><![CDATA[Orby]]></dc:creator><pubDate>Mon, 25 Jan 2016 19:12:25 GMT</pubDate></item><item><title><![CDATA[Reply to QOpenGL fragment shader on Mon, 25 Jan 2016 19:06:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/orby">@<bdi>Orby</bdi></a> Forgot to mention it, addShader, link and bind all return true and the log is accordingly empty. Can't really tell if anything's wrong from the docs, it looks in order to me.</p>
]]></description><link>https://forum.qt.io/post/309480</link><guid isPermaLink="true">https://forum.qt.io/post/309480</guid><dc:creator><![CDATA[Aanok]]></dc:creator><pubDate>Mon, 25 Jan 2016 19:06:33 GMT</pubDate></item><item><title><![CDATA[Reply to QOpenGL fragment shader on Mon, 25 Jan 2016 18:43:09 GMT]]></title><description><![CDATA[<p dir="auto">Start your debugging by checking the return values of link() and bind().  The log() call will return information on errors and warnings.  Reference QT Assistant for further detail on the QOpenGLShaderProgram.  If all is well after checking those, then the problem is in the assignment of the shader and we'll need to start debugging there.</p>
<p dir="auto">-Orby</p>
]]></description><link>https://forum.qt.io/post/309478</link><guid isPermaLink="true">https://forum.qt.io/post/309478</guid><dc:creator><![CDATA[Orby]]></dc:creator><pubDate>Mon, 25 Jan 2016 18:43:09 GMT</pubDate></item></channel></rss>