iPhone texture loading issue

Nibbie
Posts: 1
Joined: 2009.03
Post: #1
Hey everybody,
[As a note, where I mention iPhone I am testing on the simulator if that makes any difference]
This is my first time posting here and as you might guess from that, I am fairly new to iPhone programming (though not programming in general).
I have an issue with loading of images for textures in OpenGL ES.
In a nutshell, I can load an image and use it as a texture for a sprite (in Ortho mode), however when I try to do the same thing for a 3D object, all I'm seeing are untextured vertices. As a little bit more info, aside from the loading code being different, the rendering of my 3D object is exactly the same on OSX and iPhone and it works on OSX.
So there's this weird triangle of oddities about this and I've been trying to work out why for the past 2 days with no luck (aside from what I've been able to tell you here).
Basically:
On Mac OSX: Sprite textures -> OK, Mesh textures -> OK
On iPhone: Sprite textures -> OK, Mesh textures -> not rendering, but apparently loading

Here's the relevant code for loading and rendering these 2 object types:
First is Sprite loading then rendering:
Code:
// The following is a member of my Sprite class
GLuint spritesheet; //Used to set the texture for drawing instances of this sprite

// Loading Texture is done through this (OPENGL = Mac, IPHONE = iPhone)
#ifdef OPENGL
spritesheet = SOIL_load_OGL_texture(filename.c_str(),SOIL_LOAD_RGBA,0,0,&wholewidth,&wholeheight);
#endif
#ifdef IPHONE
NSString *file_name = [[NSString alloc] initWithCString:filename.c_str()];
CGImageRef image = [UIImage imageNamed:file_name].CGImage;
[file_name release];

if (image)
{
    const UInt8* pixeldata;
    size_t bytesPerRow = CGImageGetBytesPerRow(image);
    size_t bitsPerComponent = CGImageGetBitsPerComponent(image);
    size_t Width = CGImageGetWidth(image);
    size_t Height = CGImageGetHeight(image);
    CFDataRef dataref = CGDataProviderCopyData(CGImageGetDataProvider(image));
    if (dataref)
        pixeldata = CFDataGetBytePtr(dataref);
    CGContextRef context = CGBitmapContextCreate((void*)pixeldata, Width, Height, bitsPerComponent, bytesPerRow,    CGImageGetColorSpace(image), CGImageGetBitmapInfo(image));
    CGContextDrawImage(context, CGRectMake(0.0, 0.0, (CGFloat)Width, (CGFloat)Height), image);
    CGContextRelease(context);
    glGenTextures(1,&spritesheet);
    glBindTexture(GL_TEXTURE_2D, spritesheet);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixeldata);
}
#endif

// Now the rendering of sprites with their texture loaded from this:
// Note that "half" is defined earlier as a reference for drawing sprites from their middle
glBindTexture(GL_TEXTURE_2D,spritesheet);
VERTEX_2D point[4];
    
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glEnable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glScalef(calcscale.x, calcscale.y, 1.0);
glRotatef(rotation.z, 0.0, 0.0, 1.0);
    
if (lefttop)
    half = V3(0,0,0);
    
point[0] = VERTEX_2D(-(int)half.x,-(int)half.y,0.0,frame.left,frame.top);
point[1] = VERTEX_2D(-(int)half.x,-(int)half.y+tilesize.y,0.0,frame.left,frame.bottom);
point[2] = VERTEX_2D(-(int)half.x+tilesize.x,-(int)half.y+tilesize.y,0.0,frame.right,frame.bottom);
point[3] = VERTEX_2D(-(int)half.x+tilesize.x,-(int)half.y,0.0,frame.right,frame.top);

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,sizeof(VERTEX_2D),&point[0].x);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2,GL_FLOAT,sizeof(VERTEX_2D),&point[0].u);
glColor4f(colour[0],colour[1],colour[2],colour[3]);
    
glDrawArrays(GL_TRIANGLE_FAN,0,4);
glPopMatrix();
And now we have the Mesh loading then rendering:
Code:
// The following is a member of my Mesh class
/* Part of the loading of my mesh builds this list of textures for use
when rendering the mesh. Basically the MESHTEXTURE struct has a GLuint
for the OpenGL texture reference as well as the number of vertices that use this
texture in the mesh, followed by the definition of the vertices themselves.*/
// GLunit texturedata is part of MESHTEXTURE
std::list<MESHTEXTURE> textures;

#ifdef OPENGL
textures.back().texturedata = SOIL_load_OGL_texture(texfile.c_str(),SOIL_LOAD_RGBA,0,0,NULL,NULL);
#endif
#ifdef IPHONE
NSString *file_name = [[NSString alloc] initWithCString:texfile.c_str()];
CGImageRef image = [UIImage imageNamed:file_name].CGImage;
[file_name release];
if (image)
{
    const UInt8* pixeldata;
    size_t bytesPerRow = CGImageGetBytesPerRow(image);
    size_t bitsPerComponent = CGImageGetBitsPerComponent(image);
    size_t Width = CGImageGetWidth(image);
    size_t Height = CGImageGetHeight(image);
    CFDataRef dataref = CGDataProviderCopyData(CGImageGetDataProvider(image));
    if (dataref)
        pixeldata = CFDataGetBytePtr(dataref);
    CGContextRef context = CGBitmapContextCreate((void*)pixeldata, Width, Height, bitsPerComponent, bytesPerRow,    CGImageGetColorSpace(image), CGImageGetBitmapInfo(image));
    CGContextDrawImage(context, CGRectMake(0.0, 0.0, (CGFloat)Width, (CGFloat)Height), image);
    CGContextRelease(context);
    glGenTextures(1,&textures.back().texturedata);
    glBindTexture(GL_TEXTURE_2D, textures.back().texturedata);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixeldata);
}
#endif

// Now how those meshes are rendered:
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// We draw the mesh by its textures to save binding textures more than what is necessary
for (std::list<MESHTEXTURE>::iterator i = base_mesh->GetTextures()->begin(); i != base_mesh->GetTextures()->end(); ++i)
{
    if (!(*i).tex_vertices)
        continue;

    glBindTexture(GL_TEXTURE_2D,(*i).texturedata);
    glPushMatrix();
    glTranslatef(position.x, position.y, position.z);
    glScalef(scale.x, scale.y, scale.z);
    glRotatef(rotation.x, 1.0, 0.0, 0.0);
    glRotatef(rotation.y, 0.0, 1.0, 0.0);
    glRotatef(rotation.z, 0.0, 0.0, 1.0);                    
                    
    glVertexPointer(3,GL_FLOAT,sizeof(VERTEX),&(*i).tex_vertices[0].x);
    glTexCoordPointer(2,GL_FLOAT,sizeof(VERTEX),&(*i).tex_vertices[0].u);
    glColorPointer(4,GL_FLOAT,sizeof(VERTEX),&(*i).tex_vertices[0].colour[0]);
                
    glDrawArrays(GL_TRIANGLES,0,(*i).tex_vert_count);
    glPopMatrix();
}
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);

OK so hopefully that is clear enough in what is happening - if not, please let me know and I will provide more comments to make it easier to understand what is happening.

If anybody can take a look at this and put forward suggestions as to why my meshes aren't rendering textures while my sprites are, I would greatly appreciate it.

Any input from others is what I'm after because at the moment I've exhausted every option I am familiar with so I need opinions from others Wink
I do get the feeling it's going to be something different between OpenGL and ES, but I'll be honest in saying that I don't know enough about the differences between them to be able to say any more.

Thanks in advance,
yotryu
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Texture Mapping: Loading a texture from a .bmp file? ishrock 5 6,074 Dec 13, 2008 09:27 AM
Last Post: ThemsAllTook
  OpenGL Texture Loading &amp; Sprites corporatenewt 2 11,046 Jan 30, 2008 12:39 PM
Last Post: ynda20
  Texture Loading in Cocoa... dave05 3 8,064 Dec 11, 2007 03:12 AM
Last Post: DoG
  Cocoa Texture Loading Code Problem Nick 1 3,044 Oct 28, 2005 11:44 PM
Last Post: OneSadCookie
  loading a texture in opengl with carbon hyperutila 3 3,168 Nov 25, 2004 03:07 PM
Last Post: FCCovett