views:

887

answers:

2

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);
+1  A: 

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.

Kai
They all seem good and it draws fine with no texture. Do I need to define texture values for each face somehow? I am parsing from .obj and I just realized I am not using all of the face info. (i.e f x1/x2/x3 y1/y2/y3 z1/z2/z3) I am not using the 2's or the 3's at all.
Joe Cannatti
Yes, you need to setup your normal array and texture array using the normal and texture coordinates supplied for each face. A line looks like "f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3". You need to extract all the "vt" into a texture array and pass that to glTexCoordPointer. Do the same for "vn" and pass to glNormalPointer. Also note that you might have to duplicate vertices due to the .obj format defining the same vertices several times for different faces in the texture and normal arrays. If you want, ask another question, post a link here, and I'll elaborate more.
Kai
+2  A: 
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.

Dolphin
tex_coords come the vt values that I am parsing from the .obj file. num_tex_coords is a count of those lines so I multiply by 2 to get the number of values in the array. A snippet is const float tex_coords[][2]={ {0.25, 0.375} , {0.375, 0.25} , {0.25, 0.25} , {0.375, 0.25} , {0.25, 0.375} , {0.375, 0.375} ,
Joe Cannatti