QOpenGLWidget: shader program is not linked
-
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,
-
Hi, welcome to devnet.
You should call link() before you try to get attributes location, not after.
-
Thank you very much , Chris!!!
That worked!