0
votes

I want to draw instanced figures, using glDrawElementsInstanced. So I set Uniform buffer block that way:

struct data
{
    float px, py, pz;
};

static data *buffer;

buffer = new data[100];

for(int i = 0; i < 10; i++)
{
    for(int j = 0; j < 10; j++)
    {
        static int a = 0;
        buffer[a].px = float(i);
        buffer[a].py = float(j);
        buffer[a].pz = 0.0f;
        a++;
    }
}

glGenBuffers(1, &bufd);
glBindBuffer(GL_UNIFORM_BUFFER, bufd);
glBufferData(GL_UNIFORM_BUFFER,
             sizeof(buffer),
             buffer,
             GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, bufd);

And after this in vertex shader Uniform block is empty. All figures draw in the same place, like all of the have world coord 0.0, 0.0, 0.0. My Vertex shader:

#version 430

in vec4 position;

struct lol//lack of idea
{
    vec3 pozycja;//pozycja means position
};

layout (binding = 0) uniform dane
{
    lol bufin[100];
};

layout ( location = 0 ) uniform mat4 proj_matrix;
layout ( location = 1 ) uniform mat4 camera_matrix;

void main(void)
{
    mat4 mv_matrix = {vec4(1.0, 0.0, 0.0, 0.0),
                      vec4(0.0, 1.0, 0.0, 0.0),
                      vec4(0.0, 0.0, 1.0, 0.0),
                      vec4(bufin[gl_InstanceID].pozycja, 1.0)};


    gl_Position = proj_matrix * camera_matrix * mv_matrix * position;
}
1

1 Answers

3
votes

You are using sizeof(buffer) in your glBufferData() call:

glBufferData(GL_UNIFORM_BUFFER,
             sizeof(buffer),
             buffer,
             GL_DYNAMIC_DRAW);

buffer is declared as a pointer:

static data *buffer;

So sizeof(buffer) is the size of a pointer, which is 4 bytes in a 32-bit environment, 8 bytes in a 64-bit environment.

You need to use the actual size of the data you want to store in the buffer. In your example, that will be something like:

glBufferData(GL_UNIFORM_BUFFER,
             100 * sizeof(buffer[0]),
             buffer,
             GL_DYNAMIC_DRAW);