How to do it…

Let's get started by following these steps:

  1. We will create a new class called RenderWindow, which inherits from the QOpenGLWindow class. Go to File | New File or Project, then select C++ Class under Files and Classes category. Name the class RenderWindow and set its base class as QOpenGLWindow. Then, proceed to create the C++ class:

  1. Go to the renderwindow.h file we just created and add the following headers at the top of the source code:
#include <GL/glu.h>
#include <QtOpenGL>
#include <QSurfaceFormat>
#include <QOpenGLFunctions>
#include <QOpenGLWindow>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
  1. We need to create several functions and variables that look like this:
class RenderWindow : public QOpenGLWindow {
public:
RenderWindow();

protected:
void initializeGL();
void paintGL();
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);
  1. We will continue and add some private variables:
private:
QOpenGLContext* openGLContext;
QOpenGLFunctions* openGLFunctions;
QOpenGLShaderProgram* shaderProgram;
QOpenGLVertexArrayObject* vao;
QOpenGLBuffer* vbo_vertices;
};
  1. Open up renderwindow.cpp and define the class constructor, as follows. We must tell the render window to use the OpenGL surface type; enable core profile (rather than compatibility profile) that runs version 3.2; create an OpenGL context; and, finally, apply the profile we just created into the context:
RenderWindow::RenderWindow() {
setSurfaceType(QWindow::OpenGLSurface);

QSurfaceFormat format;
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(3, 2);
setFormat(format);

openGLContext = new QOpenGLContext();
openGLContext->setFormat(format);
openGLContext->create();
openGLContext->makeCurrent(this);
}

  1. We need to define the initializeGL() function, as follows. This function will be called before the rendering starts. First, we define the vertex and fragment shaders:
void RenderWindow::initializeGL() {
openGLFunctions = openGLContext->functions();

static const char *vertexShaderSource =
"#version 330 core "
"layout(location = 0) in vec2 posAttr; "
"void main() { "
"gl_Position = vec4(posAttr, 0.0, 1.0); }";

static const char *fragmentShaderSource =
"#version 330 core "
"out vec4 col; "
"void main() { "
"col = vec4(1.0, 0.0, 0.0, 1.0); }";
  1. We initiate shaderProgram and declare a vertices array. Then, we also create a QOpenGLVertexArrayObject object:
    shaderProgram = new QOpenGLShaderProgram(this);
shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
shaderProgram->link();

GLfloat vertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
0.0f, 1.0f };

vao = new QOpenGLVertexArrayObject();
vao->create();
vao->bind();

  1. Let's continue to write our code by defining vbo_vertices:
    vbo_vertices = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
vbo_vertices->create();
vbo_vertices->setUsagePattern(QOpenGLBuffer::StaticDraw);
vbo_vertices->bind();
vbo_vertices->allocate(vertices, sizeof(vertices) * sizeof(GLfloat));

vao->release();
}
  1. We will start by adding some code to the paintEvent() function:
void RenderWindow::paintEvent(QPaintEvent *event) {
Q_UNUSED(event);
glViewport(0, 0, width(), height());
glClearColor(0.39f, 0.58f, 0.93f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
  1. We will then bind the VAO and shader program before calling glDrawArrays():
    vao->bind();
shaderProgram->bind();
shaderProgram->bindAttributeLocation("posAttr", 0);
shaderProgram->enableAttributeArray(0);
shaderProgram->setAttributeBuffer(0, GL_FLOAT, 0, 2);
glDrawArrays(GL_TRIANGLES, 0, 3);
shaderProgram->release();
vao->release();
}
  1. You can refresh the viewport whenever the render window is being resized by adding the following code:
void RenderWindow::resizeEvent(QResizeEvent *event) {
Q_UNUSED(event);
glViewport(0, 0, this->width(), this->height());
this->update();
}

  1. If you compile and run the project now, you should be able to see a red rectangle being drawn in front of a blue background:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset