I’m trying to implement a 3D geometric shape by connecting the points of the shape with a line (starting with just a single line). I’ve already implemented the vertex and fragment shaders and they work whenever I’m using an array of float arrays to store the points, but it doesn’t work if I save the points as QVector3D in QVector – this is a bummer, since QVector allows me to have a variable array length and I don’t need to bother about any sort of resizing.
Why isn’t my code properly rendering the line?
void Renderer::initContours()
{
QVector<QVector3D> vectors;
QVector3D vector1 = QVector3D(0,0.5,0.5);
QVector3D vector2 = QVector3D(1,0.5,0.5);
vectors.append(vector1);
vectors.append(vector2);
QOpenGLBuffer vertexBuffer(QOpenGLBuffer::VertexBuffer);
vertexBuffer.create();
vertexBuffer.bind();
vertexBuffer.allocate(vectors.data(), vectors.size() * sizeof(QVector3D)); //should it be just sizeof(vectors) instead?
vertexBuffer.release();
QOpenGLVertexArrayObject vertexArrayObject;
QOpenGLVertexArrayObject::Binder vaoBinder(&vertexArrayObject);
if (vertexArrayObject.isCreated())
{
vertexBuffer.bind();
shaderProgram.setAttributeBuffer("vertexPosition", GL_FLOAT, 0, 3, sizeof(QVector3D));
shaderProgram.enableAttributeArray("vertexPosition"); //maybe the problem is with vertexPosition???
vertexBuffer.release();
}
}
void Renderer::draw(QMatrix4x4 mvpMatrix)
{
shaderProgram.bind();
vertexArrayObject.bind();
shaderProgram.setUniformValue("mvpMatrix", mvpMatrix);
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glLineWidth(2);
f->glDrawArrays(GL_LINE_STRIP, 0, 2); // I sure hope this works with QVector3Ds
vertexArrayObject.release();
shaderProgram.release();
}
I tried replacing the parameters in the glDrawArrays, setAttributeBuffer and enableAttributeArray functions, following the following two post Draw points in OpenGL with QVector3D, but nothing worked. I also checked various codes on GitHub that use QVector and try to render it
Thomas Dussault is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.