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. Can't Get Qt OpenGL to Draw
Forum Updated to NodeBB v4.3 + New Features

Can't Get Qt OpenGL to Draw

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.7k 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.
  • A Offline
    A Offline
    Andaharoo
    wrote on last edited by Andaharoo
    #1

    I've been trying to use OpenGL in Qt for the past few hours. I can't seem to figure out why the heck I can't get anything to draw after modifying one of the examples. Here's the shaders for starters. Very simple. Note, I've started trying stupid things...

    in vec3 pos;
    void main()
    {
        gl_Position = vec4(pos, 1.0);
    }
    
    out vec4 fColor;
    void main()
    {
        fColor = vec4(1.0, 0.0, 0.0, 1.0);
    }
    

    I can confirm the shaders are setup fine. No errors. Here's where I create a vertex buffer,

    arrayBuf = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
    arrayBuf->setUsagePattern(QOpenGLBuffer::StaticDraw);
    arrayBuf->create();
    
    GLfloat vertices[6][3] =
    {
        { -0.90f, -0.90f, -1.0f }, // Triangle 1
        { 0.85f, -0.90f, -1.0f },
        { -0.90f, 0.85f, -1.0f },
    
        { 0.90f, -0.85f, -1.0f }, // Triangle 2
        { 0.90f, 0.90f, -1.0f },
        { -0.85f, 0.90f, -1.0f }
    };
    
    arrayBuf->bind();
    arrayBuf->allocate(vertices, sizeof(vertices));
    

    Then to draw

    glClearColor(0.4f, 0.58f, 0.9f, 1.0f);
    
    program.bind();
    arrayBuf->bind();
    
    int vertexLocation = program->attributeLocation("pos");
    program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(GLfloat) * 3);
    program->enableAttributeArray(vertexLocation);
    
    glDrawArrays(GL_TRIANGLES, 0, 6);
    
    program->disableAttributeArray(vertexLocation);
    arrayBuf->release();
    program.release();
    

    I got a GL_INVALID_OPERATION setAttributeBuffer. I'd do a more in depth debugging to try and see what's going on but for some reason neither vs graphics debugging or Nividia nsight is working with Qt OpenGL.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Which example did you modify ?

      Did it run successfully before you started fiddling with it ?

      Which version of Qt ?
      On which platform ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Andaharoo
        wrote on last edited by
        #3

        Thanks, you motivated me to go back and draw more comparisons from the example. Just didn't want to start over and was so sure I was doing something wrong with the buffers but it turned out to be a problem with the OpenGL version I was asking it to use. On a side note do you have any idea why I can't use a std vector or qlist instead of the array shown above? If I replace GLfloat vertices[6][3]... and arrayBuf->allocate... with

        std::vector<VertexData> data;
        data.push_back(VertexData(-0.9f, -0.9f, -5.0f));
        data.push_back(VertexData(-0.85f, -0.9f, -5.0f));
        data.push_back(VertexData(-0.9f, -0.85f, -5.0f));
        
        data.push_back(VertexData(-0.9f, -0.85f, -5.0f));
        data.push_back(VertexData(-0.9f, -0.9f, -5.0f));
        data.push_back(VertexData(-0.85f, -0.9f, -5.0f));
        ...
        arrayBuf->allocate(&data[0], sizeof(VertexData) * data.size());
        

        Then it doesn't work anymore. I've been able to do this with OpenGL in the past.

        1 Reply Last reply
        0
        • jiancaiyangJ Offline
          jiancaiyangJ Offline
          jiancaiyang
          wrote on last edited by
          #4

          Hi have you tried Qt examples before? There are many Qt examples available that can help you manage rendering triangles correctly.

          我们自己的论坛:http://qtdream.com
          擅长三维角色仿真动画。

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Shouldn't you be using data.data for that ? See std::vector::data ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MartinChan 0
              wrote on last edited by MartinChan 0
              #6

              2017.01.18 Update:
              I draw a rectangle with texture correctly under QtOpenGL framework correctly,the source code u can get here,maybe u can enroll an account to get the code.


              2018.01.17 Update:
              I solve the question by using glDrawArrays rather than glDrawElements, I believe it must have some problems when I tried glDrawElements;


              Origin:
              @jiancaiyang I have encountered the same question when I try to draw a simple white angle,here is my GLWidget Code,my qt version is 5.6msvc.I intimate code from OpenGL OpenGLTutrioal

              glwidget.h:

              #ifndef GLWIDGET_H
              #define GLWIDGET_H
              #include <QOpenGLWidget>
              #include <QOpenGLShaderProgram>
              #include <QVector3D>
              #include <QOpenGLFunctions>
              
              class GLWidget : public QOpenGLWidget ,public QOpenGLFunctions
              {
                  Q_OBJECT
              
              public:
                  GLWidget(QWidget *parent = 0);
                  ~GLWidget();
              
                  QSize sizeHint() const;
              
              protected:
                  void initializeGL() Q_DECL_OVERRIDE;
                  void resizeGL(int width,int height) Q_DECL_OVERRIDE;
                  void paintGL();
              
              private:
                  QMatrix4x4 pMatrix;
                  QOpenGLShaderProgram shaderProgram;
                  QVector<QVector3D> vertices;
              
              };
              
              #endif // GLWIDGET_H
              
              

              glwidget.cpp:

              #include "glwidget.h"
              
              GLWidget::GLWidget(QWidget *parent)
              {
              
              }
              
              GLWidget::~GLWidget()
              {
              
              }
              
              QSize GLWidget::sizeHint() const
              {
                  return QSize(640,480);
              }
              
              void GLWidget::initializeGL()
              {
                  initializeOpenGLFunctions();
              
                  glEnable(GL_DEPTH_TEST);
                  glEnable(GL_CULL_FACE);
              
                  glClearColor(0.0f,0.0f,0.0f,1.0f);
              
                  bool ok;
                  ok = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/vertexShader.vsh");
                  ok = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/fragmentShader.fsh");
                  ok = shaderProgram.link();
              
              
                  vertices << QVector3D(1,0,-2) << QVector3D(0,1,-2) << QVector3D(-1,0,2);
              
              }
              
              void GLWidget::resizeGL(int width, int height)
              {
                  if ( height == 0 )
                  {
                      height = 1;
                  }
              
                  pMatrix.setToIdentity();
                  pMatrix.perspective(60.0,(float)width / (float)height,0.001,1000);
              
                  glViewport(0,0,width,height);
              
              }
              
              void GLWidget::paintGL()
              {
                  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
              
                  QMatrix4x4 mMatrix;
                  QMatrix4x4 vMatrix;
              
                  bool ok;
                  ok = shaderProgram.bind();
              
                  shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
              
                  shaderProgram.setUniformValue("color",QColor(Qt::white));
              
                  shaderProgram.setAttributeArray("vertex", vertices.constData());
                  shaderProgram.enableAttributeArray("vertex");
              
                  glDrawArrays(GL_TRIANGLES, 0, vertices.size());
              
                  shaderProgram.disableAttributeArray("vertex");
                  shaderProgram.release();
              }
              

              vertexShader.vsh:

              uniform mat4 mvpMatrix;
              
              in vec4 vertex;
              
              void main(void)
              {
                  gl_Position = mvpMatrix * vertex;
              }
              
              

              fragmentShader.fsh:

              uniform vec4 color;
              
              out vec4 fragColor;
              
              void main(void)
              {
                  fragColor = color;
              }
              
              1 Reply Last reply
              0

              • Login

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