Joe Cannatti
Joe Cannatti

Reputation: 5089

How can glDrawElements() cause a EXC_BAD_ACCESS while drawing with texture?

I am getting a EXC_BAD_ACCESS on the the glDrawElements() in this code, but only when the bottom block of code is present. I am thinking it must be something conceptual that I am missing because I am not finding any nil pointers or other things that can typically cause this error.

glScalef(6, 6, 6);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3 ,GL_FLOAT, 0, model_verts);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, model_normals); 
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(num_tex_coords*2, GL_FLOAT, 0, tex_coords);
glDrawElements(GL_TRIANGLES, num_model_indices0, GL_UNSIGNED_SHORT, &model_indices0);
glDrawElements(GL_TRIANGLES, num_model_indices1, GL_UNSIGNED_SHORT, &model_indices1);
glDrawElements(GL_TRIANGLES, num_model_indices2, GL_UNSIGNED_SHORT, &model_indices2);
glDrawElements(GL_TRIANGLES, num_model_indices3, GL_UNSIGNED_SHORT, &model_indices3);

this code sets up my texture and does not fail on any lines.

    glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBlendFunc(GL_ONE, GL_SRC_COLOR);
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
NSString *path = [[NSBundle mainBundle] pathForResource:@"booktex" ofType:@"jpg"];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:texData];
if (image == nil)
    NSLog(@"Do real error checking here");
GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
imageData = malloc( height * width * 4 );
context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( context, 0, height - height );
CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

Upvotes: 1

Views: 3542

Answers (2)

Dolphin
Dolphin

Reputation: 4772

glTexCoordPointer(num_tex_coords*2, GL_FLOAT, 0, tex_coords);

what is num_tex_coords? how are you setting up tex_coords?

The first argument to glTexCoordPointer should be the number PER VERTEX (just like glVertexPointer). Normally for texcoords this will be 2.

Upvotes: 2

Kai
Kai

Reputation: 9552

I don't see anything wrong with the code you've posted. For me the next step would be to check that all of your vertex arrays are the same size and that the model_indices are all referencing legal values within your vertex arrays.

Upvotes: 1

Related Questions