This is how I load texture:
texture_name[2] = [[NSBundle mainBundle] pathForResource: @"Plane2" ofType: @"jpg"];
object.objects_type[2].id_texture = LoadBitmap(texture_name[2]);
This is how I Read image/texture:
int LoadBitmap(NSString *filename)
{
NSBitmapImageRep *image = [NSBitmapImageRep imageRepWithContentsOfFile: filename];
NSInteger samples = [image samplesPerPixel],
bitsPerPixel = [image bitsPerPixel],
bitsPerSample = [image bitsPerSample],
bytes = [image bytesPerRow];
GLenum type, format = [image hasAlpha] ? GL_RGBA : GL_RGB;
GLint internalformat;
switch (samples) {
case 4:
if (bitsPerSample == 8) {
internalformat = GL_RGBA8;
}
else if (bitsPerSample == 16) {
internalformat = GL_RGBA16;
}
else {
internalformat = GL_RGBA;
}
break;
case 3:
if (bitsPerSample == 8) {
internalformat = GL_RGB8;
}
else if (bitsPerSample == 16) {
internalformat = GL_RGB16;
}
else {
internalformat = GL_RGB;
}
break;
default:
NSLog(@"GetOpenGLTexture: Can't handle samples per pixel of %d.", samples);
return -1;
}
switch (bitsPerPixel) {
case 24:
case 32:
type = GL_UNSIGNED_BYTE;
break;
case 48:
case 64:
type = GL_UNSIGNED_SHORT;
break;
case 96:
case 128:
type = GL_UNSIGNED_INT;
break;
default:
NSLog(@"GetOpenGLTexture: Can't handle bits per pixel of %d.", bitsPerPixel);
return -1;
}
NSSize size = NSMakeSize([image pixelsWide], [image pixelsHigh]);
void *imageData = [image bitmapData];
void *data = calloc(bytes, size.height);
if( data != NULL ) {
int rowNum, Num = 0;
for(rowNum = size.height - 1; rowNum >= 0; rowNum--, Num++) {
// Copy the entire row in one shot
memcpy( data + ( Num * bytes ),
imageData + ( rowNum * bytes ),
bytes );
}
}
GLuint num_texture;
glGenTextures(1, &num_texture);
glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter
// Set proper unpacking row length for bitmap
glPixelStorei(GL_UNPACK_ROW_LENGTH, [image pixelsWide]);
// Set memory alignment parameters for unpacking the bitmap
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
// The next commands sets the texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); //The minifying function
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // Create mipmaps automatically
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // We don't combine the color with the original surface color, use only the texture map.
// Finally we define the 2d texture
// And create 2d mipmaps for the minifying function
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, size.width, size.height, 0, format, type, data);
free(data); // Free the memory we used to load the texture
return num_texture; // Returns the current texture OpenGL ID
}
This is how I bind & use it:
glBindTexture(GL_TEXTURE_2D, object.objects_type[o_index].id_texture);
glDrawRangeElements(...);
Anyone can spot the problem???