I originally asked this on the thread about vertex culling, but I thought I’d move the question to its own thread as it was slightly off-topic.
Is instanced drawing (aka geometry instancing) possible in Codea? Many 3D projects (such as my blast-em-up) rely on repeatedly drawing the same model in different positions and orientations; if instancing were possible, then the data for the mesh would only have to be sent to the GPU once, with potentially large performance savings.
According to the iOS Dev docs, instanced drawing (or geometry instancing) is supported on iOS, although the implementation is different on OpenGL 3.0 than it is on 2.0 (on 2.0 you need to enable instancing in the shader with #extension GL_EXT_draw_instanced : enable
. More info in these threads on Stack Exchange: 1 and 2 )
Below you can see a short example of a shader (7.6) and how the binding would work (7.5) from the iOS dev docs. I don’t know Objective C, so I can’t tell whether a binding like the one in 7.5 would be possible in Codea. Is it just a case of setting up some custom mesh buffers, or is it more involved than that?
A second part to the question is, are Open GL 3.0 shaders supported by Codea, if the device hardware supports 3.0? (ie iPad Air onwards)
Thanks in advance.
From iOS dev docs :
With the instanced arrays strategy, you store per-instance information in a vertex array attribute. Your vertex shader can then access that attribute to make use of per-instance information. Call the glVertexAttribDivisor function to specify how that attribute advances as OpenGL ES draws each instance. Listing 7-5 demonstrates setting up a vertex array for instanced drawing, and Listing 7-6 shows the corresponding shader.
Listing 7-5 Using a vertex attribute for per-instance information
#define kMyInstanceDataAttrib 5
glGenBuffers(1, &_instBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _instBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(instData), instData, GL_STATIC_DRAW);
glEnableVertexAttribArray(kMyInstanceDataAttrib);
glVertexAttribPointer(kMyInstanceDataAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribDivisor(kMyInstanceDataAttrib, 1);
Listing 7-6 OpenGL ES 3.0 vertex shader using instanced arrays
#version 300 es
layout(location = 0) in vec4 position;
layout(location = 5) in vec2 inOffset;
uniform mat4 modelViewProjectionMatrix;
void main()
{
vec4 offset = vec4(inOffset, 0.0, 0.0)
gl_Position = modelViewProjectionMatrix * (position + offset);
}
Instanced drawing is available in the core OpenGL ES 3.0 API and in OpenGL ES 2.0 through the EXT_draw_instanced and EXT_instanced_arrays extensions.