0
0
0ct0g3n2015-02-10 16:56:49
Qt
0ct0g3n, 2015-02-10 16:56:49

QT OpenGL how to draw a line with an array?

Good afternoon/evening everyone.
I've been struggling for the third day, and I can't figure out how to draw a line in OpenGL passing the coordinates as an array (glDrawArrays). I'm not strong in OpenGL, I can't find any suitable OpenGL tutorials in Qt.
I try this code

void CGLMap::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT);

    GLuint VBO;
    float arr[4];
    arr[0] = -0.5;
    arr[1] = -0.5;
    arr[2] = 5;
    arr[3] = 5;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(arr), arr, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glColor4f(255.0, 0.0, 0.0, 1.0);
    glDrawArrays(GL_LINES, 0, sizeof(arr));
}

Doesn't draw. Please advise, I will be grateful.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stanislav Silin, 2015-02-10
@byme

Copy-paste is bad (if you don't know what copy-paste is)!

-glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
+glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

You draw lines, therefore, you only need 2 values ​​from the buffer, not 4. I suspect that in the example they drew squares :)
ZY . Tested in OpenTK, everything worked.
UPD. Sorry for the delay, I didn't use linux for a long time. Maybe I'm late, but still the code will be useful to those who come here from Google. I tested your code like this:
#include "widget.h"

Widget::Widget(QWidget *parent)
    : QOpenGLWidget(parent)
{
}
GLuint VBO;
GLfloat arr[4];

void Widget::initializeGL()
{
    glewExperimental = true;
    glewInit();
    glClearColor(0,0,0,1);
    glColor3f(1,1,1);

    arr[0] = -1;
    arr[1] = -1;
    arr[2] = 1;
    arr[3] = 1;

    glGenBuffers( 1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(arr), arr, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
}

void Widget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    glColor4f(1.0f, 0.0, 0.0, 1.0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_LINES, 0, sizeof(arr));
}

void Widget::resizeGL(int w, int h)
{
    glViewport(0,0,w,h);
}

T
TriKrista, 2015-02-10
@TriKrista

It doesn't matter which libraries OpenGL is bundled with, the code, in principle, will be the same everywhere.
Look in the book "OpenGL super book" there is a description of this function (glDrawArrays).
You can also look at the project where this function is used.

D
Don Kaban, 2015-02-10
@donkaban

Where is the shader?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question