QtOpenGLWidget子类不绘制任何内容

时间:2018-10-09 19:33:05

标签: qt opengl

我有一个简单的顶点和片段着色器。我在从QtOpenGLWidget派生的类中使用它们。该程序应该绘制一个三角形,但是它什么也没有显示。 glClearColor工作正常,我可以更改背景颜色。当我从命令行运行该程序时,该程序输出一行,但什么也没画:

QOpenGLShaderProgram :: attributeLocation(position):未链接着色器程序

当我在gl命令后插入getGLError时,没有一个报告错误。  这是类的定义:

class GLCanvas:public QOpenGLWidget , protected QOpenGLFunctions
{
  QMatrix4x4 matrix;
  QOpenGLShaderProgram* shader;
  GLuint posLocation;
  GLuint colLocation;
  GLuint matLocation;
  void cleanup();
public:
  GLCanvas(QWidget* parent);
  void initializeGL() override;
  void paintGL() override;
  void resizeGL(int w, int h) override;

  QStringList getGLinfo();
};



static const char *vertexShaderSource =
    R"(
    #version 330
    in vec3 position;
    void main()
    {
      gl_Position = vec4(position, 1);
    })";


static const char *fragmentShaderSource =
    R"(
    #version 330
    void main()
    {
       gl_FragColor = vec4(0.4,0.4,0.8,1.0);
    })";

GLCanvas::GLCanvas(QWidget* parent): QOpenGLWidget(parent)
{
  setFormat(QSurfaceFormat::defaultFormat());
  matrix.setToIdentity();

  posLocation = -1;
  colLocation = -1;
  matLocation = -1;
}


void GLCanvas::initializeGL()
{
    makeCurrent();
    connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &GLCanvas::cleanup);
    constexpr int glNoError = 0;
    GLenum error = -1;
    initializeOpenGLFunctions();
    glClearColor(.1f, .1f, .1f, 1.0f);
    error = glGetError();
    if (error != glNoError)
      {
        qDebug() << "glclearcolor failed";
      }

    shader = new QOpenGLShaderProgram(this);
    shader->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
    error = glGetError();
    if (error != glNoError)
      {
        qDebug() << "failed after adding vertex shader.";
      }

    shader->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
    error = glGetError();
    if (error != glNoError)
      {
        qDebug() << "failed after adding fragment shader.";
      }

    Q_ASSERT(shader->link());


    error = glGetError();
    if (error != glNoError)
      {
        qDebug() << "gl error at link.";
      }
    //colLocation = shader->attributeLocation("color");
    //matLocation = shader->uniformLocation("matrix");

    //shader->release();
    posLocation = shader->attributeLocation("position");
    error = glGetError();
    if (error != glNoError)
      {
        qDebug() << "gl error at link.";
      }

    glEnable(GL_DEPTH_TEST);

    //colLocation = shader->attributeLocation("color");
    //matLocation = shader->uniformLocation("matrix");

    //shader->release();


    //glEnable(GL_CULL_FACE);

}
//###################################################################

void GLCanvas::paintGL()
{
     glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
     GLfloat vertices[] = { 0.0f, 0.707f, .0f,  0.5f, -0.5f, .0f ,-0.5f, -0.5f, .0f};
     Q_ASSERT(shader->bind());
     shader->setAttributeArray(posLocation, vertices, 3, 0);
     glEnableVertexAttribArray(posLocation);
     glDrawArrays(GL_LINES, 0, 3);
/*
     GLfloat lvertices[] = { 0.907f, 0.907f, 0.5f, -0.957f, -0.957f, 0.5f };
     shader->setAttributeArray(posLocation, lvertices, 0, 3);
     glDrawArrays(GL_LINE_STRIP, 0, 2);
     glDisableVertexAttribArray(posLocation);

*/
     //glDisableVertexAttribArray(colLocation);
     shader->release();
  //   qDebug() << getGLinfo();
}

void GLCanvas::resizeGL(int w, int h)
{
  Q_UNUSED(w);
  Q_UNUSED(h);
  //matrix.setToIdentity();
  //matrix.perspective(45.0f, w / float(h), 0.01f, 1000.0f);
  glViewport(0, 0, w, h);
}
/*
void GLCanvas::mousePressEvent(QMouseEvent *event)
{
Q_UNUSED(event);
}

void GLCanvas::mouseMoveEvent(QMouseEvent *event)
{
Q_UNUSED(event);
}
*/
void GLCanvas::cleanup()
{
    if (shader == nullptr)
          return;
    makeCurrent();
    delete shader;
    //shader = nullptr;
    doneCurrent();
}

1 个答案:

答案 0 :(得分:0)

从一开始我就认真地做了所有事情,并且一切正常。可能是一个愚蠢的错误。谢谢大家。