Instancing drawing and shaders + ofBufferObject + Attributes

Hi folks. this is another question about how pass information to a shader… i´ve a very disturbing situation in my rendering code:

I want to implement INSTANCING DRAWING in my software, using shaders. Following the examples i could pass a MATRIX data per instance to a OFBufferObject and send it to a VERTEX SHADER, just like the example does:

matrices.resize(100);

buffer.allocate();
    buffer.bind(GL_TEXTURE_BUFFER);
    buffer.setData(matrices,GL_STREAM_DRAW);
    tex.allocateAsBufferTexture(buffer,GL_RGBA32F);

// update

    buffer.updateData(0,matrices);

// 

    mainScene.shader.setUniformTexture("tex",tex,0);

in vertex:

 int x = gl_InstanceID*4;
    
    mat4 transformMatrix = mat4( 
        texelFetch(tex, x),
        texelFetch(tex, x+1),
        texelFetch(tex, x+2),
        texelFetch(tex, x+3)
    );
    
    
    vec3 pos = position.xyz;
    
    gl_Position = modelViewProjectionMatrix  * transformMatrix * vec4(pos, position.w);

this working fine, i´ve the positions and tranformations in vertex, keeping track values from OF.

But i dont sure if i understand how ofBufferObjects is working on the shader.
what if i want to pass a buffer with and array of FLOAT NUMBERS to change the sizes per instance?

Where i ve to do this multiplication in vertex shader? do i have to use texelFetch? how i acces to the information?

As i couldnt do it, i start trying pass this “Size” value with a custom attribute… but without succes.
Following the multiple post about this, i do:

vector< **float** >sizes;
sizes.resize(100);

 attLoc = mainScene.shader.getAttributeLocation("sizes");
    
    mesh.getVbo().setAttributeData(mainScene.shader.getAttributeLocation("sizes"), sizes.data(), 1,
                                   sizes.size(), GL_STATIC_DRAW,
                                   sizeof(float));
    
    mesh.getVbo().setAttributeDivisor(mainScene.shader.getAttributeLocation("sizes"),1);

also, trying to pass positions via attribute instead of a ofBufferObject.

    vector<ofVec3f>positions;

    positions.resize(100);


 mesh.getVbo().setAttributeData(mainScene.shader.getAttributeLocation("positions"), (float*)positions.data(), 3,
                                   positions.size(), GL_STATIC_DRAW,
                                   sizeof(ofVec3f));
    //
    mesh.getVbo().setAttributeDivisor(mainScene.shader.getAttributeLocation("positions"),1);

and in vertex:

**void** main()

{

**int** iCount = 100;

*// when drawing instanced geometry, we can use gl_InstanceID*

*// this tells you which primitive we are currently working on*

*// we would like to spread our primitives out evenly along the x and an y coordinates*

*// we calculate an x and an y coordinate value based on the current instance ID*

**float** instanceX = **float** (gl_InstanceID%(iCount) - iCount/2) / 50;

**float** instanceY = **float** (gl_InstanceID/(iCount) - iCount/2) / 50;

**vec4** vPos = position;

vPos.x = (vPos.x * sizes) * 10;

vPos.y = (vPos.y * sizes) * 10;

vPos.z = (vPos.z * sizes) * 10;

vPos = vPos + **vec4** (instanceX*10000,instanceY*1000, 0,0);

TexCoord = texcoord;

gl_Position = projectionMatrix * modelViewMatrix * vPos ;

}

or:

void main(){
    int x = gl_InstanceID*4;
    
    mat4 transformMatrix = mat4(
                                texelFetch(tex, x),
                                texelFetch(tex, x+1),
                                texelFetch(tex, x+2),
                                texelFetch(tex, x+3)
                                );
//    color = instanceColor;
    TexCoord = texcoord;
    Position = transformMatrix * position;
    
    vec3 pos = position.xyz*sizes;
    

    gl_Position = modelViewProjectionMatrix  * transformMatrix* position;
    
}

but isnt working.
what i doing wrong?
Need an approach or a clue
Thanks!!

You can pass any data to the shader through the ofBufferObject method but usually you will need these to be of a fixed size, passing to each instance the same number of data elements (I am not really sure but I am guessing as this would be needed to allow the asynchronous nature of shaders. You can, although, change the numbre of things you pass on each frame, thus, you can compute which is maximum number of data elements any instance will have, resizing the buffer accordingly and then just use the number of elements you need in the shader.

@pxt if you are using the matrix approach, and you need size can’t you just set the scale/size of the matrix on the cpu, update the buffer and in the vertex shader it should reflect it accordingly based on the transformMatrix?