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;\ }";
Hi Shanksanghoi,
In your latest code above, your gl_Position is still being worked out as
gl_Position = uPMatrix * position;\
This needs to be changed, otherwise each of the instanced meshes will be transformed exactly the same, and only one model will appear to be drawn. Pete's last response above has an example
Thanks,
Chris