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:



// update




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 =;
    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;

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

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



 mesh.getVbo().setAttributeData(mainScene.shader.getAttributeLocation("positions"), (float*), 3,
                                   positions.size(), GL_STATIC_DRAW,

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 ;



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 =*sizes;

    gl_Position = modelViewProjectionMatrix  * transformMatrix* position;

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

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?