Answer the question
In order to leave comments, you need to log in
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));
}
Answer the question
In order to leave comments, you need to log in
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);
#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);
}
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question