我在这段代码中的glDrawElements()上获得了一个EXC_BAD_ACCESS,但只有当底层代码块存在时才会这样。我认为它必须是我缺少的概念,因为我没有找到任何nil指针或其他通常会导致此错误的事情。
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);
此代码设置我的纹理,并且不会在任何行上失败。
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);
答案 0 :(得分:2)
glTexCoordPointer(num_tex_coords*2, GL_FLOAT, 0, tex_coords);
什么是num_tex_coords? 你是如何设置tex_coords的?
glTexCoordPointer的第一个参数应该是PER VERTEX的数字(就像glVertexPointer一样)。通常对于texcoords,这将是2.
答案 1 :(得分:1)
我发现您发布的代码没有任何问题。对我来说,下一步是检查所有顶点数组是否大小相同,并且model_indices都是引用顶点数组中的合法值。