Hi,
I have a bit of a jumble of code that I'm trying to use to cobble together some dynamic 3D rendering. I've been using Jeff LaMarche's tutorials (here).
The code is here:
Code:
NSArray *row = [plane objectAtIndex:index];
GLushort connectionStrips[[row count] * 2];
if (index % 2 == 0) //Even = on the mark, so the one above is off the mark
{
int enterIndex = 0;
for (int i = 0; i < [row count]; i++)
{
connectionStrips[enterIndex] = index * [row count] + i + [row count];
connectionStrips[enterIndex + 1] = index * [row count] + i;
enterIndex += 2;
}
}
else //Odd
{
int enterIndex = 0;
for (int i = 0; i < [row count]; i++)
{
connectionStrips[enterIndex] = (index * [row count]) + i;
connectionStrips[enterIndex + 1] = (index * [row count]) + i + [row count];
enterIndex += 2;
}
}
glDrawElements(GL_TRIANGLE_STRIP, sizeof(connectionStrips) / sizeof(connectionStrips[0]), GL_UNSIGNED_SHORT, connectionStrips);
The EXC_BAD_ACCESS is on the very last line. The connectionStrips array is properly populated. Any ideas what might be causing this code to go astray?
Thanks!
Are you also using a call to glVertexPointer? glDrawElements reads from the currently enabled vertex/color/texture/etc arrays.
glDrawElements will march through the arrays used in glVertexPointer (et all), so if those are set up incorrectly, you'll get an error when you call glDrawElements.
But yes, I think more of the crash log is in order.
Thanks for the replies
Actually, the crash log doesn't really say anything except
Code:
[Switching to process 30113 thread 0x207]
which I don't think is part of the crash log. Also the green bar in Xcode 4 with the "Program received signal: EXC_BAD_ACCESS" message points to the glDrawElements line. I am calling glVertexPointer as well.
I don't think I should post the entire vertices set as it has 65,536 objects
Thanks for the replies
Actually, the crash log doesn't really say anything except
Code:
[Switching to process 30113 thread 0x207]
which I don't think is part of the crash log. Also the green bar in Xcode 4 with the "Program received signal: EXC_BAD_ACCESS" message points to the glDrawElements line. I am calling glVertexPointer as well.
I don't think I should post the entire vertices set as it has 65,536 objects
Are you using just glVertexPointer or other pointers as well (color/texcoord)? If just glVertexPointer are you sure you aren't enabling any arrays that you are NOT using?
If you're actually trying to draw a triangle strip with 65k+ verts, I'd recommend just trying a strip of 3 verts (yes, one triangle), just so you can verify all the data properly without your head exploding.
Usually a bad access from glDrawElements means its reading into one of the bound arrays (verts/color/texcoords) past the valid memory. For example, if you bound 3 verts, but then told it to draw a triangle strip using glDrawElements and one of the indices was something greater than 2, it would blow up.
I'm not using color arrays, but I am also using normal arrays.
I did as 7twenty7 advised (sort of) and I found that the EXC_BAD_ACCESS only shows up on and after the fifth loop (the code I posted is inside a for loop). I have 65,536 vertices, but in each loop I'm doing one row of triangle strips to make a sort of mesh. The connectionStrips[] array in the fifth loop is properly populated, and I'm using sizeof instead of a static number.