PDA

View Full Version : Instancing Problem



bcthund
11-09-2011, 03:45 PM
I am trying to implement instancing for the first time and have it partially working. But I cannot seem to get my position array to work right and i'm not sure why. I have a class to manage my VAO/VBO data and it has worked perfectly until now. I copy all the data per instance starting with:

glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * uiMaxInstances, vData, GL_DYNAMIC_DRAW);

and each successive instance with:

glBufferSubData(GL_ARRAY_BUFFER, uiPositionOffset, sizeof(vData), vData);

The uiPositionOffset increases by the size of the added data each time. The data appears to be getting to this stage correctly with the use of a printf command. I am using glVertexAttribDivisor(GLA_POSITION, 1); so it will only grab the position data per instance.

The shader has an "in vec3 vPos;" that is attached with glBindAttribLocation(); so the data is theoretically getting to the shader.

Now my problem is that the data inside the shader appears to be correct for only a few objects, everything else acts as if the data is all 0's. Only about 12 objects out of 144 are in the correct spot. If I set the shader to position each object based on the gl_InstanceID everything is shifted as I would expect.

Can anyone give me some suggestions on what to look for that could be causing my data to be incorrect in the shader? Is there any way for me to access the data back from the buffer just to double check that it is correct? Any help or suggestions are appreciated.

tksuoran
11-10-2011, 04:02 AM
What is the vData array for your glBufferData() call, how large is it?

I would pass in NULL for glBufferData() call and only specify the size. Or even better, batch writing all instances with a single glBufferData() / glBufferSubData() call.

BionicBytes
11-10-2011, 04:11 AM
what does
sizeof(vData) evaluate to?

bcthund
11-10-2011, 01:28 PM
It is an array of type float[3] for each item in vData, so each call is very small. vData is actually a multi-dimensional array for managing larger sets of data. However there are around 90 instances for one of the groups, about 40 for another, and around 10 for the third. Each group is in it's own VBO. So 90x3=270 floats is the largest group I have right now. I could try batching everything into one call, I will look into reorganizing my code to attempt that and see what happens.


sizeof(vData) = 4

I've tried creating a buffer much larger than I need and it made no difference.

BionicBytes
11-10-2011, 02:23 PM
If sizeof(vData) is 4 bytes, then

Code:
glBufferSubData(GL_ARRAY_BUFFER, uiPositionOffset, sizeof(vData), vData);

Then the 3rd parameter is not large enough to upload your instance data which is 3 floats or 12 bytes?

bcthund
11-10-2011, 03:44 PM
Thanks for pointing out the sizeof problem, I got the syntax straight out of the superbible. I changed it to use sizeof(GLfloat) * 3 and that fixed it a little bit.

However, I have found the problem by overlooking one small little item. I was calling:

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uiPositionArray);

When I should have been calling:

glBindBuffer(GL_ARRAY_BUFFER, uiPositionArray);

Everything is now in the right spot, Thanks for your help.

BionicBytes
11-11-2011, 05:27 AM
However, I have found the problem by overlooking one small little item. I was calling:

Code:glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uiPositionArray);

When I should have been calling:

Code:glBindBuffer(GL_ARRAY_BUFFER, uiPositionArray);




....that's why you should have posted more complete code.