0

I am having a bad time trying to figure out why the call to glDrawArraysInstanced is drawing the correct number of instances but with wrong sizes of the objects. Here's my code (class declaration ommited)

#include <QtOpenGL/QGLWidget>
#include <QtOpenGL/QGLBuffer>
#include <QtOpenGL/QGLShaderProgram>
#include <iostream>

const float OpenGLViewer::_quad[] = {
    -1.0f, 0.0f, 0.0f,
    -0.9f, 0.0f, 0.0f,
    -1.0f,  1.0f, 0.0f,
    -0.9f,  1.0f, 0.0f
};

const float OpenGLViewer::_offset[] = {
    0.0, 0.2, 0.7, 0.4
};

...

void OpenGLViewer::initializeGL()
{
    QGLFormat glFormat = QGLWidget::format();
    if(!glFormat.sampleBuffers())
        std::cout << "Could not enable sample buffers.";

    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
    glViewport(0, 0, this->width(), this->height());

    if(!prepareShaderProgram(":/vertex.glsl", ":/fragment.glsl"))
        return;

    if(!_shaderProgram.bind())
    {
        std::cout << "Could not bind shader program to the context.";
        return;
    }

    glGenVertexArrays(1, &_vertexArraysObject);
    glBindVertexArray(_vertexArraysObject);         // BIND

    glGenBuffers(1, &_quadBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _quadBuffer);     // BIND
    glBufferData(GL_ARRAY_BUFFER, sizeof(_quad), _quad, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    //glVertexAttribDivisorARB(0, 0);

    glGenBuffers(1, &_offsetBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _offsetBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(_offset), _offset, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, 0);
    glVertexAttribDivisorARB(1, 1);

    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void OpenGLViewer::paintGL()
{
    // Clear the buffer with the current clearing color
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    // Draw stuff
    glBindVertexArray(_vertexArraysObject);

    glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, 4);
}

...

What I'm trying to do is to draw the same rectangle but at different positions using the value of offset in the vertex shader. It is important to say that I am using glVertexAttribDivisorARB(1, 1) (note the ARB suffix) since in Qt+OSX Mavericks I cannot find the function glVertexAttribDivisor. Any thoughts?

The resulting image looks like

enter image description here

EDIT

Forgot to include the shaders

// VERTEX shader
#version 330

layout(location = 0) in vec4 vertexPosition;
layout(location = 1) in float offset;

void main()
{
    gl_Position = vec4(vertexPosition.x + offset, vertexPosition.y + offset, vertexPosition.zw);
}

//FRAGMENT shader
#version 330

layout(location = 0, index = 0) out vec4 fragmentColor;

void main(void)
{
    fragmentColor = vec4(1.0, 0.0, 0.0, 1.0);
}
BRabbit27
  • 6,333
  • 17
  • 90
  • 161

1 Answers1

3
gl_Position = vec4(vertexPosition.x + offset, vertexPosition.y + offset, vertexPosition.zw);

adding the offset to y moves it upwards (off screen)

you should either not add the offset to y or subtract it instead:

gl_Position = vec4(vertexPosition.x + offset, vertexPosition.y - offset, vertexPosition.zw);
ratchet freak
  • 47,288
  • 5
  • 68
  • 106