How to do it…

Let's get started by following this example:

  1. Open up renderwindow.h and add two more VBOs, one called vbo_vertices2 and another called vbo_colors, as highlighted in the following code:
private:
QOpenGLContext* openGLContext;
QOpenGLFunctions* openGLFunctions;
QOpenGLShaderProgram* shaderProgram;
QOpenGLVertexArrayObject* vao;
QOpenGLBuffer* vbo_vertices;
QOpenGLBuffer* vbo_vertices2;
QOpenGLBuffer* vbo_colors;
  1. Open up renderwindow.cpp and add the following code to the shader code, as highlighted in the following snippet:
static const char *vertexShaderSource =
"#version 330 core "
"layout(location = 0) in vec2 posAttr; "
"layout(location = 1) in vec3 colAttr; "
"out vec3 fragCol; "
"void main() { "
"fragCol = colAttr; "
"gl_Position = vec4(posAttr, 1.0, 1.0); }";
  1. Add the highlighted code to the fragment shader, which looks like this:
static const char *fragmentShaderSource =
"#version 330 core "
"in vec3 fragCol; "
"out vec4 col; "
"void main() { "
"col = vec4(fragCol, 1.0); }";
  1. Change the vertices array to something like this:
GLfloat vertices[] = {
-0.3f, -0.5f,
0.8f, -0.4f,
0.2f, 0.6f };

GLfloat vertices2[] = {
0.5f, 0.3f,
0.4f, -0.8f,
-0.6f, -0.2f };

GLfloat colors[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
  1. Since we already initialized vbo_vertices in the previous example, this time, we only need to initialize two other VBOs, namely vbo_vertices and vbo_colors:
vbo_vertices2 = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
vbo_vertices2->create();
vbo_vertices2->setUsagePattern(QOpenGLBuffer::StaticDraw);
vbo_vertices2->bind();
vbo_vertices2->allocate(vertices2, sizeof(vertices2) * sizeof(GLfloat));

vbo_colors = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
vbo_colors->create();
vbo_colors->setUsagePattern(QOpenGLBuffer::StaticDraw);
vbo_colors->bind();
vbo_colors->allocate(colors, sizeof(colors) * sizeof(GLfloat));
  1. Before we start drawing the triangle using glDrawArrays(), we must also add the data of vbo_colors into our shader's colAttr attribute. Make sure you call bind() to set the VBO as the current active VBO, before sending the data to the shader. The location ID (in this case, 0 and 1) must match the location ID that's used in your shader:
vbo_vertices->bind();
shaderProgram->bindAttributeLocation("posAttr", 0);
shaderProgram->enableAttributeArray(0);
shaderProgram->setAttributeBuffer(0, GL_FLOAT, 0, 2);

vbo_colors->bind();
shaderProgram->bindAttributeLocation("colAttr", 1);
shaderProgram->enableAttributeArray(1);
shaderProgram->setAttributeBuffer(1, GL_FLOAT, 0, 3);

glDrawArrays(GL_TRIANGLES, 0, 3);
  1. Right after the preceding code, we will send vbo_vertices2 and vbo_colors to the shader attribute and call glDrawArrays() again to draw the second triangle:
vbo_vertices2->bind();
shaderProgram->bindAttributeLocation("posAttr", 0);
shaderProgram->enableAttributeArray(0);
shaderProgram->setAttributeBuffer(0, GL_FLOAT, 0, 2);

vbo_colors->bind();
shaderProgram->bindAttributeLocation("colAttr", 1);
shaderProgram->enableAttributeArray(1);
shaderProgram->setAttributeBuffer(1, GL_FLOAT, 0, 3);

glDrawArrays(GL_TRIANGLES, 0, 3);
  1. If you build the program now, you should be able to see two triangles on screen, and one of the triangles sitting on top of the other:

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

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