Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QOpenGLWidget: shader program is not linked
Forum Updated to NodeBB v4.3 + New Features

QOpenGLWidget: shader program is not linked

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dunaj
    wrote on 4 Mar 2015, 00:04 last edited by
    #1

    Hi All,
    I tried to write a simple Qt OpenGL program with custom shaders but I am stuck and don't know where to look for an error.
    When I am running the code below there is only background cleared on my openGL widget and nothing more is presented. On the console however, the following message is displayed: "QOpenGLShaderProgram::attributeLocation( coord2d) : shader program is not linked"

    Could anyone tell me what should I do, to properly link the shader program?
    I am new to openGL and Qt , so sorry if my error is common or simple.

    The code:
    GLWidget.h
    @
    #ifndef GLWIDGET__H_
    #define GLWIDGET__H_

    #include <qopenglfunctions.h>
    #include <qopenglwidget.h>
    #include <qopenglshaderprogram.h>

    class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
    {
    Q_OBJECT
    public:
    GLWidget(QWidget *parent);
    ~GLWidget() {};
    protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);
    //void mousePressEvent(QMouseEvent * event);
    //void mouseMoveEvent(QMouseEvent * event);

    private:
    QOpenGLShaderProgram program;
    QOpenGLShader *vertexShad;
    QOpenGLShader *fragmentShad;
    GLint coord2d;
    GLuint positionBufferObject;
    GLfloat rotatex;
    GLfloat rotatey;
    QPoint mousePos;
    };

    #endif // GLWIDGET_H__
    @

    glwidget.cpp:
    @
    #include "GLWidget.h"

    GLWidget::GLWidget(QWidget * parent = 0) : QOpenGLWidget(parent)
    {
    rotatex = 0.0;
    rotatey = 0.0;
    }

    void GLWidget::initializeGL() {
    initializeOpenGLFunctions();
    GLfloat triangleVertices[] = {
    0.75f, 0.75f, 0.0f, 1.0f,
    0.75f, -0.75f, 0.0f, 1.0f,
    -0.75f, -0.75f, 0.0f, 1.0f,
    };
    glGenBuffers(1, &positionBufferObject);

    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    vertexShad = new QOpenGLShader(QOpenGLShader::Vertex);
    fragmentShad = new QOpenGLShader(QOpenGLShader::Fragment);
    const char *vertexShadSrc = // Vertex shader src
    "attribute vec4 coord2d; \n"
    "void main() \n"
    "{\n"
    " gl_Position = coord2d; \n"
    "}\n";
    const char *fragmentShadSrc = // Fragment Shader src
    "void main(void) { "
    " gl_FragColor[0] = 0.0; "
    " gl_FragColor[1] = 0.0; "
    " gl_FragColor[2] = 1.0; "
    "}";
    bool flag = vertexShad->compileSourceCode(vertexShadSrc);
    if (flag) printf("compiled vertex\n");
    flag = fragmentShad->compileSourceCode(fragmentShadSrc);
    if (flag) printf("compiled fragment\n");

    flag = program.addShader(vertexShad);
    if (flag) printf("linked vertexShad\n");
    flag = false;
    flag =program.addShader(fragmentShad);
    if (flag) printf("linked fragmentShad\n");

    this->coord2d = program.attributeLocation("coord2d");
    program.enableAttributeArray(coord2d);
    program.setAttributeArray(coord2d, triangleVertices, 4);
    program.link();

    program.bind();
    //coord2d = program.attributeLocation("coord2d");
    //program.enableAttributeArray(coord2d);

    //program.setAttributeArray(coord2d, triangleVertices, 2);
    }

    void GLWidget::paintGL() {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);

    glVertexAttribPointer(coord2d, 4, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);
    }

    void GLWidget::resizeGL(int width, int height) {
    glViewport(0,0, width, height);
    }
    @

    main.cpp:
    @
    #include "symmgaitmodels.h"
    #include <QtWidgets/QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    SymmGaitModels w;
    QSurfaceFormat format;
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    format.setProfile(QSurfaceFormat::CoreProfile);

    w.show();
    return a.exec();
    }
    @

    Thanks,

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 4 Mar 2015, 01:38 last edited by
      #2

      Hi, welcome to devnet.

      You should call link() before you try to get attributes location, not after.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dunaj
        wrote on 4 Mar 2015, 07:45 last edited by
        #3

        Thank you very much , Chris!!!
        That worked!

        1 Reply Last reply
        0

        1/3

        4 Mar 2015, 00:04

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved