Draw several objects in OpenGL GLSL with use of indices
up vote
-2
down vote
favorite
I'm trying to draw several triangles with use of indices in GLSL Qt. There are plenty of questions on Stackoverflow and the entire Internet about this subject but I still can't make my code work. Can someone please explain me, what I did wrong? Should I call glGenBuffers as it is called in the tutorial or it is the same thing as QOpenGLBuffer::create()?
DivisionGL.h
#ifndef DIVISIONGL_H
#define DIVISIONGL_H
//-----------------------------//
#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
#include <QOpenGLVertexArrayObject>
#include <QVector>
//-----------------------------//
class DivisionGL : public QOpenGLWidget, protected QOpenGLFunctions {
Q_OBJECT
public:
explicit DivisionGL(QWidget* ParentP = nullptr);
~DivisionGL() override = default;
protected:
void initializeGL() override;
void paintGL() override;
void resizeGL(int WidthP, int HeightP) override;
private:
QOpenGLShaderProgram ShaderProgram;
QOpenGLBuffer CoordBuffer;
QOpenGLBuffer ColorBuffer;
QOpenGLVertexArrayObject VAO;
QOpenGLBuffer* EBO;
int u_modelToWorld;
QMatrix4x4 m_projection;
QMatrix4x4 matrix;
//----------//
void MakeShader();
void MakeTriangle();
};
//-----------------------------//
#endif
DivisionGL.cpp
#include "DivisionGL.h"
//-----------------------------//
DivisionGL::DivisionGL(QWidget* ParentP) : QOpenGLWidget(ParentP) {}
//-----------------------------//
void DivisionGL::initializeGL() {
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
glClearColor(0.1, 0.1, 0.2, 1.0);
MakeShader();
MakeTriangle();
matrix.ortho(-2.0f, 2.0, -2.0f, 2.0, 2.0, -2.0f);
matrix.translate(0.0, 0.0, 1.0);
}
void DivisionGL::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ShaderProgram.bind();
ShaderProgram.setUniformValue(u_modelToWorld, matrix);
EBO -> bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
EBO -> release();
ShaderProgram.release();
}
void DivisionGL::resizeGL(int WidthP, int HeightP) {
int side = qMin(WidthP, HeightP);
glViewport((WidthP - side) / 2, (HeightP - side) / 2, side, side);
m_projection.setToIdentity();
m_projection.perspective(45.0f, WidthP / float(HeightP), 0.0f, 1000.0f);
}
//-----------------------------//
void DivisionGL::MakeShader() {
if (!ShaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, "shaders/vShader.glsl")) {
qCritical() << QObject::tr("Could not compile vertex shader: ") << ShaderProgram.log();
}
if (!ShaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, "shaders/fShader.glsl")) {
qCritical() << QObject::tr("Could not compile fragment shader: ") << ShaderProgram.log();
}
if (!ShaderProgram.link()) {
qCritical() << QObject::tr("Could not link shader program: ") << ShaderProgram.log();
}
ShaderProgram.bind();
}
void DivisionGL::MakeTriangle() {
QVector <float> VerticesL = {
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
0.0, 1.0, 0.0,
0.0, -3.0, 0.0
};
QVector <int> IndicesL = {
0, 1, 2,
0, 4, 1
};
QVector <float> ColorsL = {
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
};
u_modelToWorld = ShaderProgram.uniformLocation("modelToWorld");
VAO.create();
VAO.bind();
CoordBuffer.create();
CoordBuffer.bind();
CoordBuffer.allocate(VerticesL.constData(), VerticesL.count() * sizeof(float));
ShaderProgram.enableAttributeArray(0);
ShaderProgram.setAttributeBuffer(0, GL_FLOAT, 0, 3);
ColorBuffer.create();
ColorBuffer.bind();
ColorBuffer.allocate(ColorsL.constData(), ColorsL.count() * sizeof(float));
ShaderProgram.enableAttributeArray(1);
ShaderProgram.setAttributeBuffer(1, GL_FLOAT, 0, 3);
EBO = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
EBO -> create();
EBO -> bind();
EBO -> allocate(IndicesL.constData(), IndicesL.count() * sizeof(int));
VAO.release();
CoordBuffer.release();
ColorBuffer.release();
EBO -> release();
ShaderProgram.release();
}
Vertex shader
#version 450
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
out vec4 vColor;
uniform mat4 modelToWorld;
uniform mat4 worldToView;
void main() {
gl_Position = modelToWorld * vec4(position, 1.0);
vColor = vec4(color, 1.0);
}
Fragment shader
#version 450
in vec4 vColor;
out vec4 fColor;
void main() {
fColor = vColor;
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
//-----------------------------//
#include <QMainWindow>
#include <QOpenGLWidget>
#include <QLineEdit>
#include <QIntValidator>
#include <QPushButton>
#include <QWidget>
#include <QGridLayout>
#include <QSpacerItem>
//-----------------------------//
#include "Include/DivisionGL.h"
//-----------------------------//
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* ParentP = nullptr);
~MainWindow() override = default;
private:
QWidget* MainWidget;
QGridLayout* MainLayout;
//----------//
QLineEdit* DivivsionLine;
QIntValidator* DivisionValidator;
QPushButton* DivisionButton;
QSpacerItem* DivideSpacer;
DivisionGL* DivisionDisplay;
//----------//
void InitMain();
};
//-----------------------------//
#endif
MainWindow.cpp
#include "MainWindow.h"
//-----------------------------//
MainWindow::MainWindow(QWidget* ParentP) : QMainWindow(ParentP) {
InitMain();
DivivsionLine = new QLineEdit;
DivivsionLine -> setFixedSize(100, 25);
DivisionValidator = new QIntValidator(1, 5);
DivivsionLine -> setValidator(DivisionValidator);
DivisionButton = new QPushButton("Divide");
DivisionButton -> setFixedSize(100, 25);
DivideSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
DivisionDisplay = new DivisionGL;
DivisionDisplay -> setMinimumSize(800, 600);
MainLayout -> addWidget(DivivsionLine, 0, 0, 1, 1);
MainLayout -> addWidget(DivisionButton, 1, 0, 1, 1);
MainLayout -> addItem(DivideSpacer, 2, 0, 1, 1);
MainLayout -> addWidget(DivisionDisplay, 0, 1, 3, 1);
}
//-----------------------------//
void MainWindow::InitMain() {
MainWidget = new QWidget;
setCentralWidget(MainWidget);
MainLayout = new QGridLayout;
MainWidget -> setLayout(MainLayout);
}
main.cpp
#include <QApplication>
//-----------------------------//
#include "MainWindow.h"
//-----------------------------//
int main(int argc, char *argv) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return QApplication::exec();
}
c++ opengl qt5 glsl
add a comment |
up vote
-2
down vote
favorite
I'm trying to draw several triangles with use of indices in GLSL Qt. There are plenty of questions on Stackoverflow and the entire Internet about this subject but I still can't make my code work. Can someone please explain me, what I did wrong? Should I call glGenBuffers as it is called in the tutorial or it is the same thing as QOpenGLBuffer::create()?
DivisionGL.h
#ifndef DIVISIONGL_H
#define DIVISIONGL_H
//-----------------------------//
#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
#include <QOpenGLVertexArrayObject>
#include <QVector>
//-----------------------------//
class DivisionGL : public QOpenGLWidget, protected QOpenGLFunctions {
Q_OBJECT
public:
explicit DivisionGL(QWidget* ParentP = nullptr);
~DivisionGL() override = default;
protected:
void initializeGL() override;
void paintGL() override;
void resizeGL(int WidthP, int HeightP) override;
private:
QOpenGLShaderProgram ShaderProgram;
QOpenGLBuffer CoordBuffer;
QOpenGLBuffer ColorBuffer;
QOpenGLVertexArrayObject VAO;
QOpenGLBuffer* EBO;
int u_modelToWorld;
QMatrix4x4 m_projection;
QMatrix4x4 matrix;
//----------//
void MakeShader();
void MakeTriangle();
};
//-----------------------------//
#endif
DivisionGL.cpp
#include "DivisionGL.h"
//-----------------------------//
DivisionGL::DivisionGL(QWidget* ParentP) : QOpenGLWidget(ParentP) {}
//-----------------------------//
void DivisionGL::initializeGL() {
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
glClearColor(0.1, 0.1, 0.2, 1.0);
MakeShader();
MakeTriangle();
matrix.ortho(-2.0f, 2.0, -2.0f, 2.0, 2.0, -2.0f);
matrix.translate(0.0, 0.0, 1.0);
}
void DivisionGL::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ShaderProgram.bind();
ShaderProgram.setUniformValue(u_modelToWorld, matrix);
EBO -> bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
EBO -> release();
ShaderProgram.release();
}
void DivisionGL::resizeGL(int WidthP, int HeightP) {
int side = qMin(WidthP, HeightP);
glViewport((WidthP - side) / 2, (HeightP - side) / 2, side, side);
m_projection.setToIdentity();
m_projection.perspective(45.0f, WidthP / float(HeightP), 0.0f, 1000.0f);
}
//-----------------------------//
void DivisionGL::MakeShader() {
if (!ShaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, "shaders/vShader.glsl")) {
qCritical() << QObject::tr("Could not compile vertex shader: ") << ShaderProgram.log();
}
if (!ShaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, "shaders/fShader.glsl")) {
qCritical() << QObject::tr("Could not compile fragment shader: ") << ShaderProgram.log();
}
if (!ShaderProgram.link()) {
qCritical() << QObject::tr("Could not link shader program: ") << ShaderProgram.log();
}
ShaderProgram.bind();
}
void DivisionGL::MakeTriangle() {
QVector <float> VerticesL = {
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
0.0, 1.0, 0.0,
0.0, -3.0, 0.0
};
QVector <int> IndicesL = {
0, 1, 2,
0, 4, 1
};
QVector <float> ColorsL = {
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
};
u_modelToWorld = ShaderProgram.uniformLocation("modelToWorld");
VAO.create();
VAO.bind();
CoordBuffer.create();
CoordBuffer.bind();
CoordBuffer.allocate(VerticesL.constData(), VerticesL.count() * sizeof(float));
ShaderProgram.enableAttributeArray(0);
ShaderProgram.setAttributeBuffer(0, GL_FLOAT, 0, 3);
ColorBuffer.create();
ColorBuffer.bind();
ColorBuffer.allocate(ColorsL.constData(), ColorsL.count() * sizeof(float));
ShaderProgram.enableAttributeArray(1);
ShaderProgram.setAttributeBuffer(1, GL_FLOAT, 0, 3);
EBO = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
EBO -> create();
EBO -> bind();
EBO -> allocate(IndicesL.constData(), IndicesL.count() * sizeof(int));
VAO.release();
CoordBuffer.release();
ColorBuffer.release();
EBO -> release();
ShaderProgram.release();
}
Vertex shader
#version 450
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
out vec4 vColor;
uniform mat4 modelToWorld;
uniform mat4 worldToView;
void main() {
gl_Position = modelToWorld * vec4(position, 1.0);
vColor = vec4(color, 1.0);
}
Fragment shader
#version 450
in vec4 vColor;
out vec4 fColor;
void main() {
fColor = vColor;
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
//-----------------------------//
#include <QMainWindow>
#include <QOpenGLWidget>
#include <QLineEdit>
#include <QIntValidator>
#include <QPushButton>
#include <QWidget>
#include <QGridLayout>
#include <QSpacerItem>
//-----------------------------//
#include "Include/DivisionGL.h"
//-----------------------------//
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* ParentP = nullptr);
~MainWindow() override = default;
private:
QWidget* MainWidget;
QGridLayout* MainLayout;
//----------//
QLineEdit* DivivsionLine;
QIntValidator* DivisionValidator;
QPushButton* DivisionButton;
QSpacerItem* DivideSpacer;
DivisionGL* DivisionDisplay;
//----------//
void InitMain();
};
//-----------------------------//
#endif
MainWindow.cpp
#include "MainWindow.h"
//-----------------------------//
MainWindow::MainWindow(QWidget* ParentP) : QMainWindow(ParentP) {
InitMain();
DivivsionLine = new QLineEdit;
DivivsionLine -> setFixedSize(100, 25);
DivisionValidator = new QIntValidator(1, 5);
DivivsionLine -> setValidator(DivisionValidator);
DivisionButton = new QPushButton("Divide");
DivisionButton -> setFixedSize(100, 25);
DivideSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
DivisionDisplay = new DivisionGL;
DivisionDisplay -> setMinimumSize(800, 600);
MainLayout -> addWidget(DivivsionLine, 0, 0, 1, 1);
MainLayout -> addWidget(DivisionButton, 1, 0, 1, 1);
MainLayout -> addItem(DivideSpacer, 2, 0, 1, 1);
MainLayout -> addWidget(DivisionDisplay, 0, 1, 3, 1);
}
//-----------------------------//
void MainWindow::InitMain() {
MainWidget = new QWidget;
setCentralWidget(MainWidget);
MainLayout = new QGridLayout;
MainWidget -> setLayout(MainLayout);
}
main.cpp
#include <QApplication>
//-----------------------------//
#include "MainWindow.h"
//-----------------------------//
int main(int argc, char *argv) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return QApplication::exec();
}
c++ opengl qt5 glsl
Please provide an MCVE. Currently, there are only five recognizable OpenGL function calls in your code: glEnable, glClearColor, glClear, glDrawElements, glViewport. No one can know what you are doing in all those undisclosed other methods and classes.
– httpdigest
2 days ago
@httpdigest, sorry, now I have added the whole project.
– Андрей Садков
2 days ago
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I'm trying to draw several triangles with use of indices in GLSL Qt. There are plenty of questions on Stackoverflow and the entire Internet about this subject but I still can't make my code work. Can someone please explain me, what I did wrong? Should I call glGenBuffers as it is called in the tutorial or it is the same thing as QOpenGLBuffer::create()?
DivisionGL.h
#ifndef DIVISIONGL_H
#define DIVISIONGL_H
//-----------------------------//
#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
#include <QOpenGLVertexArrayObject>
#include <QVector>
//-----------------------------//
class DivisionGL : public QOpenGLWidget, protected QOpenGLFunctions {
Q_OBJECT
public:
explicit DivisionGL(QWidget* ParentP = nullptr);
~DivisionGL() override = default;
protected:
void initializeGL() override;
void paintGL() override;
void resizeGL(int WidthP, int HeightP) override;
private:
QOpenGLShaderProgram ShaderProgram;
QOpenGLBuffer CoordBuffer;
QOpenGLBuffer ColorBuffer;
QOpenGLVertexArrayObject VAO;
QOpenGLBuffer* EBO;
int u_modelToWorld;
QMatrix4x4 m_projection;
QMatrix4x4 matrix;
//----------//
void MakeShader();
void MakeTriangle();
};
//-----------------------------//
#endif
DivisionGL.cpp
#include "DivisionGL.h"
//-----------------------------//
DivisionGL::DivisionGL(QWidget* ParentP) : QOpenGLWidget(ParentP) {}
//-----------------------------//
void DivisionGL::initializeGL() {
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
glClearColor(0.1, 0.1, 0.2, 1.0);
MakeShader();
MakeTriangle();
matrix.ortho(-2.0f, 2.0, -2.0f, 2.0, 2.0, -2.0f);
matrix.translate(0.0, 0.0, 1.0);
}
void DivisionGL::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ShaderProgram.bind();
ShaderProgram.setUniformValue(u_modelToWorld, matrix);
EBO -> bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
EBO -> release();
ShaderProgram.release();
}
void DivisionGL::resizeGL(int WidthP, int HeightP) {
int side = qMin(WidthP, HeightP);
glViewport((WidthP - side) / 2, (HeightP - side) / 2, side, side);
m_projection.setToIdentity();
m_projection.perspective(45.0f, WidthP / float(HeightP), 0.0f, 1000.0f);
}
//-----------------------------//
void DivisionGL::MakeShader() {
if (!ShaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, "shaders/vShader.glsl")) {
qCritical() << QObject::tr("Could not compile vertex shader: ") << ShaderProgram.log();
}
if (!ShaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, "shaders/fShader.glsl")) {
qCritical() << QObject::tr("Could not compile fragment shader: ") << ShaderProgram.log();
}
if (!ShaderProgram.link()) {
qCritical() << QObject::tr("Could not link shader program: ") << ShaderProgram.log();
}
ShaderProgram.bind();
}
void DivisionGL::MakeTriangle() {
QVector <float> VerticesL = {
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
0.0, 1.0, 0.0,
0.0, -3.0, 0.0
};
QVector <int> IndicesL = {
0, 1, 2,
0, 4, 1
};
QVector <float> ColorsL = {
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
};
u_modelToWorld = ShaderProgram.uniformLocation("modelToWorld");
VAO.create();
VAO.bind();
CoordBuffer.create();
CoordBuffer.bind();
CoordBuffer.allocate(VerticesL.constData(), VerticesL.count() * sizeof(float));
ShaderProgram.enableAttributeArray(0);
ShaderProgram.setAttributeBuffer(0, GL_FLOAT, 0, 3);
ColorBuffer.create();
ColorBuffer.bind();
ColorBuffer.allocate(ColorsL.constData(), ColorsL.count() * sizeof(float));
ShaderProgram.enableAttributeArray(1);
ShaderProgram.setAttributeBuffer(1, GL_FLOAT, 0, 3);
EBO = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
EBO -> create();
EBO -> bind();
EBO -> allocate(IndicesL.constData(), IndicesL.count() * sizeof(int));
VAO.release();
CoordBuffer.release();
ColorBuffer.release();
EBO -> release();
ShaderProgram.release();
}
Vertex shader
#version 450
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
out vec4 vColor;
uniform mat4 modelToWorld;
uniform mat4 worldToView;
void main() {
gl_Position = modelToWorld * vec4(position, 1.0);
vColor = vec4(color, 1.0);
}
Fragment shader
#version 450
in vec4 vColor;
out vec4 fColor;
void main() {
fColor = vColor;
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
//-----------------------------//
#include <QMainWindow>
#include <QOpenGLWidget>
#include <QLineEdit>
#include <QIntValidator>
#include <QPushButton>
#include <QWidget>
#include <QGridLayout>
#include <QSpacerItem>
//-----------------------------//
#include "Include/DivisionGL.h"
//-----------------------------//
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* ParentP = nullptr);
~MainWindow() override = default;
private:
QWidget* MainWidget;
QGridLayout* MainLayout;
//----------//
QLineEdit* DivivsionLine;
QIntValidator* DivisionValidator;
QPushButton* DivisionButton;
QSpacerItem* DivideSpacer;
DivisionGL* DivisionDisplay;
//----------//
void InitMain();
};
//-----------------------------//
#endif
MainWindow.cpp
#include "MainWindow.h"
//-----------------------------//
MainWindow::MainWindow(QWidget* ParentP) : QMainWindow(ParentP) {
InitMain();
DivivsionLine = new QLineEdit;
DivivsionLine -> setFixedSize(100, 25);
DivisionValidator = new QIntValidator(1, 5);
DivivsionLine -> setValidator(DivisionValidator);
DivisionButton = new QPushButton("Divide");
DivisionButton -> setFixedSize(100, 25);
DivideSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
DivisionDisplay = new DivisionGL;
DivisionDisplay -> setMinimumSize(800, 600);
MainLayout -> addWidget(DivivsionLine, 0, 0, 1, 1);
MainLayout -> addWidget(DivisionButton, 1, 0, 1, 1);
MainLayout -> addItem(DivideSpacer, 2, 0, 1, 1);
MainLayout -> addWidget(DivisionDisplay, 0, 1, 3, 1);
}
//-----------------------------//
void MainWindow::InitMain() {
MainWidget = new QWidget;
setCentralWidget(MainWidget);
MainLayout = new QGridLayout;
MainWidget -> setLayout(MainLayout);
}
main.cpp
#include <QApplication>
//-----------------------------//
#include "MainWindow.h"
//-----------------------------//
int main(int argc, char *argv) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return QApplication::exec();
}
c++ opengl qt5 glsl
I'm trying to draw several triangles with use of indices in GLSL Qt. There are plenty of questions on Stackoverflow and the entire Internet about this subject but I still can't make my code work. Can someone please explain me, what I did wrong? Should I call glGenBuffers as it is called in the tutorial or it is the same thing as QOpenGLBuffer::create()?
DivisionGL.h
#ifndef DIVISIONGL_H
#define DIVISIONGL_H
//-----------------------------//
#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
#include <QOpenGLVertexArrayObject>
#include <QVector>
//-----------------------------//
class DivisionGL : public QOpenGLWidget, protected QOpenGLFunctions {
Q_OBJECT
public:
explicit DivisionGL(QWidget* ParentP = nullptr);
~DivisionGL() override = default;
protected:
void initializeGL() override;
void paintGL() override;
void resizeGL(int WidthP, int HeightP) override;
private:
QOpenGLShaderProgram ShaderProgram;
QOpenGLBuffer CoordBuffer;
QOpenGLBuffer ColorBuffer;
QOpenGLVertexArrayObject VAO;
QOpenGLBuffer* EBO;
int u_modelToWorld;
QMatrix4x4 m_projection;
QMatrix4x4 matrix;
//----------//
void MakeShader();
void MakeTriangle();
};
//-----------------------------//
#endif
DivisionGL.cpp
#include "DivisionGL.h"
//-----------------------------//
DivisionGL::DivisionGL(QWidget* ParentP) : QOpenGLWidget(ParentP) {}
//-----------------------------//
void DivisionGL::initializeGL() {
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
glClearColor(0.1, 0.1, 0.2, 1.0);
MakeShader();
MakeTriangle();
matrix.ortho(-2.0f, 2.0, -2.0f, 2.0, 2.0, -2.0f);
matrix.translate(0.0, 0.0, 1.0);
}
void DivisionGL::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ShaderProgram.bind();
ShaderProgram.setUniformValue(u_modelToWorld, matrix);
EBO -> bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
EBO -> release();
ShaderProgram.release();
}
void DivisionGL::resizeGL(int WidthP, int HeightP) {
int side = qMin(WidthP, HeightP);
glViewport((WidthP - side) / 2, (HeightP - side) / 2, side, side);
m_projection.setToIdentity();
m_projection.perspective(45.0f, WidthP / float(HeightP), 0.0f, 1000.0f);
}
//-----------------------------//
void DivisionGL::MakeShader() {
if (!ShaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, "shaders/vShader.glsl")) {
qCritical() << QObject::tr("Could not compile vertex shader: ") << ShaderProgram.log();
}
if (!ShaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, "shaders/fShader.glsl")) {
qCritical() << QObject::tr("Could not compile fragment shader: ") << ShaderProgram.log();
}
if (!ShaderProgram.link()) {
qCritical() << QObject::tr("Could not link shader program: ") << ShaderProgram.log();
}
ShaderProgram.bind();
}
void DivisionGL::MakeTriangle() {
QVector <float> VerticesL = {
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
0.0, 1.0, 0.0,
0.0, -3.0, 0.0
};
QVector <int> IndicesL = {
0, 1, 2,
0, 4, 1
};
QVector <float> ColorsL = {
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
};
u_modelToWorld = ShaderProgram.uniformLocation("modelToWorld");
VAO.create();
VAO.bind();
CoordBuffer.create();
CoordBuffer.bind();
CoordBuffer.allocate(VerticesL.constData(), VerticesL.count() * sizeof(float));
ShaderProgram.enableAttributeArray(0);
ShaderProgram.setAttributeBuffer(0, GL_FLOAT, 0, 3);
ColorBuffer.create();
ColorBuffer.bind();
ColorBuffer.allocate(ColorsL.constData(), ColorsL.count() * sizeof(float));
ShaderProgram.enableAttributeArray(1);
ShaderProgram.setAttributeBuffer(1, GL_FLOAT, 0, 3);
EBO = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
EBO -> create();
EBO -> bind();
EBO -> allocate(IndicesL.constData(), IndicesL.count() * sizeof(int));
VAO.release();
CoordBuffer.release();
ColorBuffer.release();
EBO -> release();
ShaderProgram.release();
}
Vertex shader
#version 450
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
out vec4 vColor;
uniform mat4 modelToWorld;
uniform mat4 worldToView;
void main() {
gl_Position = modelToWorld * vec4(position, 1.0);
vColor = vec4(color, 1.0);
}
Fragment shader
#version 450
in vec4 vColor;
out vec4 fColor;
void main() {
fColor = vColor;
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
//-----------------------------//
#include <QMainWindow>
#include <QOpenGLWidget>
#include <QLineEdit>
#include <QIntValidator>
#include <QPushButton>
#include <QWidget>
#include <QGridLayout>
#include <QSpacerItem>
//-----------------------------//
#include "Include/DivisionGL.h"
//-----------------------------//
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* ParentP = nullptr);
~MainWindow() override = default;
private:
QWidget* MainWidget;
QGridLayout* MainLayout;
//----------//
QLineEdit* DivivsionLine;
QIntValidator* DivisionValidator;
QPushButton* DivisionButton;
QSpacerItem* DivideSpacer;
DivisionGL* DivisionDisplay;
//----------//
void InitMain();
};
//-----------------------------//
#endif
MainWindow.cpp
#include "MainWindow.h"
//-----------------------------//
MainWindow::MainWindow(QWidget* ParentP) : QMainWindow(ParentP) {
InitMain();
DivivsionLine = new QLineEdit;
DivivsionLine -> setFixedSize(100, 25);
DivisionValidator = new QIntValidator(1, 5);
DivivsionLine -> setValidator(DivisionValidator);
DivisionButton = new QPushButton("Divide");
DivisionButton -> setFixedSize(100, 25);
DivideSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
DivisionDisplay = new DivisionGL;
DivisionDisplay -> setMinimumSize(800, 600);
MainLayout -> addWidget(DivivsionLine, 0, 0, 1, 1);
MainLayout -> addWidget(DivisionButton, 1, 0, 1, 1);
MainLayout -> addItem(DivideSpacer, 2, 0, 1, 1);
MainLayout -> addWidget(DivisionDisplay, 0, 1, 3, 1);
}
//-----------------------------//
void MainWindow::InitMain() {
MainWidget = new QWidget;
setCentralWidget(MainWidget);
MainLayout = new QGridLayout;
MainWidget -> setLayout(MainLayout);
}
main.cpp
#include <QApplication>
//-----------------------------//
#include "MainWindow.h"
//-----------------------------//
int main(int argc, char *argv) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return QApplication::exec();
}
c++ opengl qt5 glsl
c++ opengl qt5 glsl
edited 2 days ago
asked 2 days ago
Андрей Садков
13
13
Please provide an MCVE. Currently, there are only five recognizable OpenGL function calls in your code: glEnable, glClearColor, glClear, glDrawElements, glViewport. No one can know what you are doing in all those undisclosed other methods and classes.
– httpdigest
2 days ago
@httpdigest, sorry, now I have added the whole project.
– Андрей Садков
2 days ago
add a comment |
Please provide an MCVE. Currently, there are only five recognizable OpenGL function calls in your code: glEnable, glClearColor, glClear, glDrawElements, glViewport. No one can know what you are doing in all those undisclosed other methods and classes.
– httpdigest
2 days ago
@httpdigest, sorry, now I have added the whole project.
– Андрей Садков
2 days ago
Please provide an MCVE. Currently, there are only five recognizable OpenGL function calls in your code: glEnable, glClearColor, glClear, glDrawElements, glViewport. No one can know what you are doing in all those undisclosed other methods and classes.
– httpdigest
2 days ago
Please provide an MCVE. Currently, there are only five recognizable OpenGL function calls in your code: glEnable, glClearColor, glClear, glDrawElements, glViewport. No one can know what you are doing in all those undisclosed other methods and classes.
– httpdigest
2 days ago
@httpdigest, sorry, now I have added the whole project.
– Андрей Садков
2 days ago
@httpdigest, sorry, now I have added the whole project.
– Андрей Садков
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238482%2fdraw-several-objects-in-opengl-glsl-with-use-of-indices%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Please provide an MCVE. Currently, there are only five recognizable OpenGL function calls in your code: glEnable, glClearColor, glClear, glDrawElements, glViewport. No one can know what you are doing in all those undisclosed other methods and classes.
– httpdigest
2 days ago
@httpdigest, sorry, now I have added the whole project.
– Андрей Садков
2 days ago