0

I'm trying to follow the suggestion in Apple's OpenGL ES Programming Guide section on instanced drawing: Use Instanced Drawing to Minimize Draw Calls. I have started with the example project that XCode generates for a Game app with OpenGL and Swift and converted it to OpenGL ES 3.0, adding some instanced drawing to duplicate the cube.

This works fine when I use the gl_InstanceID technique and simply generate an offset from that. But when I try to use the 'instanced arrays' technique to pass data in via a buffer I am not seeing any results.

My updated vertex shader looks like this:

#version 300 es

in vec4 position;
in vec3 normal;
layout(location = 5) in vec2 instOffset;

out lowp vec4 colorVarying;

uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

void main()
{
    vec3 eyeNormal = normalize(normalMatrix * normal);
    vec3 lightPosition = vec3(0.0, 0.0, 1.0);
    vec4 diffuseColor = vec4(0.4, 0.4, 1.0, 1.0);

    float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));

    colorVarying = diffuseColor * nDotVP;

//    gl_Position = modelViewProjectionMatrix * position + vec4( float(gl_InstanceID)*1.5, float(gl_InstanceID)*1.5, 1.0,1.0);
    gl_Position = modelViewProjectionMatrix * position + vec4(instOffset, 1.0, 1.0);

}

and in my setupGL() method I have added the following:

//glGenVertexArraysOES(1, &instArray)  // EDIT: WRONG
//glBindVertexArrayOES(instArray)      // EDIT: WRONG
let kMyInstanceDataAttrib = 5
glGenBuffers(1, &instBuffer)
glBindBuffer(GLenum(GL_ARRAY_BUFFER), instBuffer)
glBufferData(GLenum(GL_ARRAY_BUFFER), GLsizeiptr(sizeof(GLfloat) * instData.count), &instData, GLenum(GL_STATIC_DRAW))
glEnableVertexAttribArray(GLuint(kMyInstanceDataAttrib))
glVertexAttribPointer(GLuint(kMyInstanceDataAttrib), 2, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0/*or 8?*/, BUFFER_OFFSET(0))
glVertexAttribDivisor(GLuint(kMyInstanceDataAttrib), 1);

along with some simple instance offset data:

var instData: [GLfloat] = [
    1.5, 1.5,
    2.5, 2.5,
    3.5, 3.5,
]

I am drawing the same way with the above as with the instance id technique:

glDrawArraysInstanced(GLenum(GL_TRIANGLES), 0, 36, 3)

But it seems to have no effect. I just get the single cube and it doesn't even seem to fail if I remove the buffer setup, so I suspect my setup is missing something.

EDIT: Fixed the code by removing two bogus lines from init.

Pat Niemeyer
  • 5,930
  • 1
  • 31
  • 35

1 Answers1

0

I had an unecessary gen and bind for the attribute vertex array. The code as edited above now works.

Pat Niemeyer
  • 5,930
  • 1
  • 31
  • 35