0

Can anyone tell me why the following code in my paintGL does not draw when using the DrawArrays function? I'm using Qt 5.14.2 in Qt Creator on the last Windows 10.

A few comments;

I'm experimenting to try to understand the differences between implementations of OpenGL in Qt using the following declarations. In time, I will write my app using the implementation that I like best.

  1. class OpenGLWindow : public QWindow, public QOpenGLFunctions

This works fine when I place the following code in the render() function, no issues at all, very nice!!

  1. class myOpenGLWidget : public QOpenGLWidget, public QOpenGLFunctions

I place the code in the paintGL function. I can color the background, but glDrawArrays() does nothing. However, I can draw a triangle, the code between the glBegin and glEnd statements is successful. I'm not getting any errors. I test for the result of the m_program->bind() call and it comes back true.

  1. class myQOpenGLWindow : public QOpenGLWindow, protected QOpenGLFunctions

Same as #2 except that I place the code in the render() function. I can color the background, but glDrawArrays() does nothing. However, I can draw a triangle, the code between the glBegin and glEnd statements is successful. I test for the result of the m_program->bind() call and it comes back true.

If anybody needs to ask, I'm doing it this way after perusing dozens of different tutorials, it's the best information that I've been able to find.

thank you!!

{ // Draw the scene:

QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();

const qreal retinaScale = devicePixelRatio();

f->glViewport(0, 0, width() * retinaScale, height() * retinaScale);

f->glClearColor(red, green, blue, 1.0f);
f->glClear(GL_COLOR_BUFFER_BIT);

QMatrix4x4 matrix;
matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f);
matrix.translate(0, 0, 0);
matrix.rotate(0, 1, 0, 0);

m_program->setUniformValue(m_matrixUniform, matrix);

glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(-0.5, -0.5, 0);
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f( 0.5, -0.5, 0);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f( 0.0,  0.5, 0);
glEnd();

//f->glBindTexture(GL_TEXTURE_2D, 0);

bound = m_program->bind();

GLfloat line_vertices[2160];

for (int v=0;v<360;v++)
    {
    line_vertices[(6*v)]=-3.5+float(v)/25;
    line_vertices[(6*v)+1]=1.1+qSin(5*2*v*(M_PI)/180);
    line_vertices[(6*v)+2]=-5;
    line_vertices[(6*v)+3]=-3.5+float(v+1)/25;
    line_vertices[(6*v)+4]=1.1+qSin(5*2*(v+1)*(M_PI)/180);;
    line_vertices[(6*v)+5]=-5;
    }

GLfloat line_colors[2160];

for (int v=0;v<360;v++)
    {
    line_colors[(6*v)]=1.0;
    line_colors[(6*v)+1]=0;
    line_colors[(6*v)+2]=0;
    line_colors[(6*v)+3]=1.0;
    line_colors[(6*v)+4]=0;
    line_colors[(6*v)+5]=0;
    }

f->glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, line_vertices);
f->glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, line_colors);

f->glEnableVertexAttribArray(m_posAttr);
f->glEnableVertexAttribArray(m_colAttr);

f->glDrawArrays(GL_LINES, 0, 360);

f->glDisableVertexAttribArray(m_colAttr);
f->glDisableVertexAttribArray(m_posAttr);

m_program->release();

}

  • Please show how you set up the VBO and (if using version 3.3 or later) the VAO. Apart from anything else your use of [`glVertexAttribPointer`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml) doesn't look right -- the last two parameters should be the stride and offset into the buffer currently bound to `GL_ARRAY_BUFFER` (if memory serves). – G.M. Jul 12 '20 at 18:08
  • Sure, will do. While I’m doing that, can you think of any reason why it works in the first case, but not in the two other cases? – stevereine Jul 12 '20 at 18:35
  • GL_VERSION = 4.6.0 – stevereine Jul 21 '20 at 03:24

2 Answers2

0

GL_VERSION = 4.6.0

Here is the section of code where I define the position and color vertices, the VBO, various setting up of position and color buffers, and then the glDrawArrays function. I have tried this code in both the render() and in the paintgl() functions. I get no errors, but I get no pretty lines either. The triangle vertices defined between begin() and end() do show up though.

GLfloat line_vertices[2160];

for (int v=0;v<360;v++)
    {
    line_vertices[(6*v)]=-3.5+float(v)/25;
    line_vertices[(6*v)+1]=1.1+qSin(5*2*v*(M_PI)/180);
    line_vertices[(6*v)+2]=-5;
    line_vertices[(6*v)+3]=-3.5+float(v+1)/25;
    line_vertices[(6*v)+4]=1.1+qSin(5*2*(v+1)*(M_PI)/180);;
    line_vertices[(6*v)+5]=-5;
    }

GLfloat line_colors[2160];

for (int v=0;v<360;v++)
    {
    line_colors[(6*v)]=1.0;
    line_colors[(6*v)+1]=0;
    line_colors[(6*v)+2]=0;
    line_colors[(6*v)+3]=1.0;
    line_colors[(6*v)+4]=0;
    line_colors[(6*v)+5]=0;
    }

QOpenGLBuffer m_vertexBufferCube;
QOpenGLBuffer m_colorBufferCube;

m_program->setUniformValue(m_matrixUniform, matrix);

m_vertexBufferCube.create();
m_vertexBufferCube.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_vertexBufferCube.allocate(line_vertices, 3 * 360 * sizeof(float));

m_colorBufferCube.create();
m_colorBufferCube.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_colorBufferCube.allocate(line_colors, 3 * 360 * sizeof(float));

bound = m_vertexBufferCube.bind();
//m_program->setAttributeBuffer("vertexPosition", GL_FLOAT,0,3);
m_program->setAttributeBuffer(m_posAttr, GL_FLOAT,0,3);

m_colorBufferCube.bind();
//m_program->setAttributeBuffer("colorPosition", GL_FLOAT,0,3);
m_program->setAttributeBuffer(m_colAttr, GL_FLOAT,0,3);

//glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, line_vertices);
//glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, line_colors);

//glEnableVertexAttribArray(m_posAttr);
//glEnableVertexAttribArray(m_colAttr);

glDrawArrays(GL_LINES, 0, 360);
0

I believe this is what you are looking for when you ask to see how I've set up my VAO, please correct me if I'm wrong.

// Create Shader (Do not release until VAO is created)
m_program = new QOpenGLShaderProgram(this);
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, ogl_vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, ogl_fragmentShaderSource);
m_program->link();
m_posAttr = m_program->attributeLocation("posAttr");
m_colAttr = m_program->attributeLocation("colAttr");
m_matrixUniform = m_program->attributeLocation("matrix");

m_program->release();
  • I'm quickly learning that my level of understanding how to implement a VAO or VBO in QT is at the level where I don't think I'm even asking a good question. The earlier response pointed me in the right direction though, I need to study VAO, VBO and how they interact with the pipeline in QT. I will study this some more and when I figure it out, I will post my answer. Thanks to all for helping. – stevereine Jul 21 '20 at 16:14