I have two arrays that I declared on the stack in a function, and verified that they both contain the exact same data:
/// Rasters the textured quad using the specified parameters.
- (void)privateConfigureRasterWithTexture:(GLuint)theTexture
bottomLeftX:(GLfloat)bottomLeftX
bottomLeftY:(GLfloat)bottomLeftY
topRightX:(GLfloat)topRightX
topRightY:(GLfloat)topRightY
{
const GLfloat texices[] =
{ bottomLeftX, bottomLeftY, // bottom left corner
bottomLeftX, topRightY, // top left corner
topRightX, topRightY, // top right corner
topRightX, bottomLeftY }; // bottom right corner
const GLfloat texices2[] =
{ 0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f };
for(int x=0;x<8;x++)
if(texices[x] != texices2[x])
{
NSLog(@"Mismatch!");
abort();
}
When I execute the following line of code in (the bottom of) that function
glVertexAttribPointer(_attributeTexturedTexCoords, 2, GL_FLOAT, GL_FALSE,
2*sizeof(GLfloat), texices2);
I get the expected result, however if I instead execute
glVertexAttribPointer(_attributeTexturedTexCoords, 2, GL_FLOAT, GL_FALSE,
2*sizeof(GLfloat), texices);
I get a bad result. I don’t get what the difference is.
Edit: Here is how I’m invoking this function.
[self privateConfigureRasterWithTexture:_atlasTexture1
bottomLeftX:0.0f
bottomLeftY:0.0f
topRightX:1.0f
topRightY:1.0f];
Nevermind, I think I’ve found it (probably at around the exact same time Martins did). It looks like
glVertexAttribPointerdoesn’t make a copy of the data until the call toglDrawElements. Since myglDrawElementsis outside of that function, the stack memory was being released (yet for some reasontexices2remained valid).I’ve rewritten the code a bit so that
texicesis heap allocated once when that class is allocated, and just reused over and over again and free’d when the application deallocs.