Shader Instanced Drawing?

Hi all,

Im using the vboInstanced example as a base as I couldn’t get it working on my own code.
What I want to do is a very basic instanced draw of cubes at different locations and colors.

For a start Im trying to push the coordinated via a ofTexture to the shader but Im not getting love back… (I must be missing something very stupid??)

(btw, I can’t use hardcoded vec3/4 arrays in the shader as the number of instances I need changes)

I build a dummy XYZ array to feed to the texture:

float* locations = new float[1024*4];
int c = 0;
for (int i=0; i<1024*4; i+=4) {
    locations[i]=ofRandom(100, 200);
    locations[i+1]=ofRandom(100, 200);
    locations[i+2]=ofRandom(-50, 50);
    ofLog() << locations[i] << " " << locations[i+1] << " " << locations[i+2];
locTex.loadData(locations, 1024, 1, GL_RGBA);

I draw with:

mShdInstanced->setUniformTexture("tex0", mTexDepth, 0);
mShdInstanced->setUniformTexture("tex1", locTex, 0);

mVboBox.drawInstanced(OF_MESH_FILL, 1024);


And the shader looks like this, but I tried a number of things…

#version 150

uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform vec4 globalColor = vec4(1.0);

uniform	sampler2D tex1;

in vec4  position;
in vec2  texcoord;
in vec4  color_coord;
in vec3  normal;

out vec4 colorVarying;
out vec2 texCoordVarying;

void main()

    vec2 sPos = vec2(gl_InstanceID, 0);
    //float xx = texture(tex1,samplePos).r;
    //float yy = texture(tex1,samplePos).g;
    //float zz = texture(tex1,samplePos).b;
    vec4 mm = texture(tex1,sPos);

	// dummy
	colorVarying = vec4(0.1,0.6,0.6,1.0);

	vec4 vPos = position + vec4(mm.x ,mm.y,mm.z,0);

	gl_Position = projectionMatrix * modelViewMatrix * vPos  ;

Any ideas more than welcome!

textures in OF are by default mapped as arb rect instead of 2d so you need to use sampler2DRect when declaring it in the shader

Thanks a lot! This did it in the modified example.

Instanced vbo drawing is a bit of hit and miss for me.

  1. In the in vbo example you can draw more that 16000 boxes.
  2. If I move the working code over to my main project (simple things again) it downs work and I cant figure out that I stupidly done there.