i was trying to develop a test case for instanced rendering. I am trying to render multiple cubes using glDrawArraysInstanced() as well as glDrawArrays().
but the offset values passed in glDrawArraysInstanced() do not give required output as obtained incase of rendering with glDrawArrays().
here is the code snippet as well as the shader code used.
if(!INSTANCE_RENDERING) { for(int j=0;j<INSTANCE;j++) { glUniform3f(offLocation, offset[n], offset[n+1], offset[n+2]); Matrix modelView = Matrix::createRotationXYZ(angle+i, angle+i, angle+i); glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, modelView.getAsArray()); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, (void*)0); if(offset[n] > th || offset[n+0] < -1*th) {dir[n+0] = -1* dir[n+0]; } if(offset[n+1] > th || offset[n+1] < -1*th) {dir[n+1] = -1 *dir[n+1];} offset[n] += dir[n]; offset[n+1] += dir[n+1]; offset[n+2] += dir[n+2]; n +=3; } //glClearColor(0.0f, 0.0f, 0.0f, 1.0f); } else //instance rendering enabled { glUniform3f(offLocation, offset[n], offset[n+1], offset[n+2]); Matrix modelView = Matrix::createRotationXYZ(angle+i, angle+i, angle+i); glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, modelView.getAsArray()); glDrawElementsInstanced ( GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, ( const void * )0, INSTANCE ); if(offset[n] > th || offset[n+0] < -1*th) {dir[n+0] = -1* dir[n+0]; } if(offset[n+1] > th || offset[n+1] < -1*th) {dir[n+1] = -1 *dir[n+1];} offset[n] += dir[n]; offset[n+1] += dir[n+1]; offset[n+2] += dir[n+2]; n +=3; }
shader source code
// Fragment and vertex shaders code std::string str = " precision mediump float;\ varying vec4 vColor;\ void main(void) {\ gl_FragColor = vColor;\ }"; const char *pszFragShader = str.c_str(); std::string str2 = "precision highp float;\ attribute vec3 aVertexPosition;\ attribute vec4 aVertexColor;\ uniform mat4 uMVMatrix;\ uniform mat4 uPMatrix;\ varying vec4 vColor;\ uniform vec3 aOffsetPosition;\ vec4 position;\ void main(void) {\ position = vec4(aVertexPosition, 1.0)+ vec4(aOffsetPosition, 45.0) ; \ gl_Position = uPMatrix * position;\ vColor = aVertexColor;\ }";
What exactly have you tried ? Twice now Pete has pointed out the exact cause of the issue and if you take a look at instancing ( cursory Google search ) it would be evident exactly what the issues is. In order to use instancing I would suggest reading the specification or some other resource that explains exactly how instancing works. Just trying to fish out the exact answer without doing any work does not help in the long run...understanding the basics will be of more help since you will be able to reason more about the potential cause of the issue you are currently having. I don't want to speak for Pete, but I think he was leaning more towards you taking his suggestion and actually investigating the cause of the issue. Chris has narrowed it down, so let see if that works for you.
The bottom line like Chris and Pete pointed out is that you are not using anything to differentiate between the different instances being rendered. The shader has to have a way of knowing that transform x or color c is for instance n. The shader you posted is using the same transform for all instances which will cause them to be rendered in the same location onscreen.