androidc++qtopengl-esqglwidget

QOpenGLWidget not drawing array


you´re my last resort for this question because Im out of ideas. So first of all I have already done some openGL stuff but not with the Qt Libraries.

And since I HAVE to do it with Qt Libraries because of the Qt Android compiler, theres no way around for me.

So I was trying to draw some vertex coordinates like in normal openGL. Save points in float array etc.. So I hoppyfully made the right vao, vbo.. shader.. but it won´t draw anything. paintGL() only draws something if its hardcoded with glBegin and end.

Please tell me where I´ve gone wrong and release me from my suffering^^ Ah and yes there is already a questions like mine Here but there was no answer so I want to try it again.

--MainWidget Header:

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QVector2D>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QVector3D>
#include <QOpenGLVertexArrayObject>


class MainWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
     MainWidget(QWidget *parent = 0);
    ~MainWidget();


protected:
    void initializeGL() Q_DECL_OVERRIDE;
    void resizeGL(int w, int h) Q_DECL_OVERRIDE;
    void paintGL() Q_DECL_OVERRIDE;

    GLfloat vertices[9];



private:
    QOpenGLShaderProgram program;
    QOpenGLBuffer arrayBuf;
    QOpenGLVertexArrayObject vertexArrayID;

};

#endif // MAINWIDGET_H

-- MainWidget Cpp:

#include "mainwidget.h"


MainWidget::MainWidget(QWidget *parent): QOpenGLWidget(parent){

}
MainWidget::~MainWidget(){

}

void MainWidget::initializeGL(){
    initializeOpenGLFunctions();
    glClearColor(0, 0, 0.4f, 0);
    glEnable(GL_DEPTH_TEST);
    //glEnable(GL_CULL_FACE);

   static const GLfloat g_vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f,  1.0f, 0.0f
    };
    for(int i = 0; i < 9; i++){
        vertices[i] = g_vertex_buffer_data[i];
    }

    vertexArrayID.create();
    vertexArrayID.bind();
    program.addShaderFromSourceFile(QOpenGLShader::Vertex, "vertex.vert");
    program.addShaderFromSourceFile(QOpenGLShader::Fragment, "fragment.frag");
    program.link();
    program.bind();
    arrayBuf.create();
    arrayBuf.bind();
    arrayBuf.allocate(&vertices, 9 * sizeof(GLfloat));
   /* glGenBuffers(1, &arrayBuf);
    glBindBuffer(GL_ARRAY_BUFFER, arrayBuf);*/
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);


}
void MainWidget::resizeGL(int w, int h){

}

void MainWidget::paintGL(){
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    arrayBuf.bind();
    arrayBuf.allocate(&vertices, 9*sizeof(GLfloat));



    int vertexLocation = program.attributeLocation("vertex");
    program.enableAttributeArray("vertexLocation");
    program.setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(GLfloat) );

    //glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);

}

--Main Cpp:

#include <QApplication>
#include <QLabel>
#include <QSurfaceFormat>

#ifndef QT_NO_OPENGL
#include "mainwidget.h"
#endif

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QSurfaceFormat format;
    format.setDepthBufferSize(24);
    QSurfaceFormat::setDefaultFormat(format);

    app.setApplicationName("GLtest");

#ifndef QT_NO_OPENGL
    MainWidget widget;
    widget.show();
#else
    QLabel note("OpenGL Support required");
    note.show();
#endif
    return app.exec();
}

--Vertex Shader as easy as possible:

    #version 330

layout(location = 0) in vec3 vertex;

void main(){
    gl_Position = vec4(vertex, 1.0);
}

--Fragment Shader:

#version 330
out vec3 color;
void main(){
    color = vec3(1,0,0);
}

Thanks in advance.. any kind of help is welcome! Cheers


Solution

  • You are enabling the wrong vertex attribute:

    program.enableAttributeArray("vertexLocation");
    

    It should either be (not the missing ")

    program.enableAttributeArray(vertexLocation);
    

    or (correct name)

    program.enableAttributeArray("vertex");