views:

58

answers:

1

I have been at this for quite some time now, reading tutorials and so on, and I can't work out what the heck is wrong with this, is anyone else able to help me understand what is going wrong?

// Sets up an array of values to use as the sprite vertices.
const GLfloat spriteVertices[] = {
    20.0f, 50.5f,
    305.0f, 50.0f,
    10.5f,  200.5f,
    300.5f,  300.5f,
};

// Sets up an array of values for the texture coordinates.
const GLshort spriteTexcoords[] = {
    0, 0,
    100, 0,
    0, 100,
    100, 100,
};

-(void)setupView:(GLView*)view {
    glMatrixMode(GL_PROJECTION); 
    CGRect rect = view.bounds; 

    glOrthof(0, rect.size.width, rect.size.height, 0, -1, 1);
    glViewport(0, 0, rect.size.width, rect.size.height);  
    glMatrixMode(GL_MODELVIEW);
    glDepthMask(GL_FALSE);
    glLoadIdentity();

    CGImageRef spriteImage = [UIImage imageNamed:@"rss_icon.png"].CGImage;
    size_t width = CGImageGetWidth(spriteImage);
    size_t height = CGImageGetHeight(spriteImage);  

    GLubyte *spriteData = 
      (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));

    CGContextRef spriteContext = CGBitmapContextCreate(
      spriteData, width, height, 8, width * 4,
      CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast
    );

    CGContextDrawImage(
      spriteContext, 
      CGRectMake( 0.0, 0.0, (CGFloat)width, (CGFloat)height), 
      spriteImage
    );

    CGContextRelease(spriteContext);

    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); 
    glTexImage2D(
      GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, 
      GL_RGBA, GL_UNSIGNED_BYTE, spriteData
    );
    free(spriteData);

    glEnable(GL_TEXTURE_2D);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
}

Draw code:

- (void)drawView:(UIView *)theView {
    glLoadIdentity();
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);

    glVertexPointer(2, GL_FLOAT, 0, spriteVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glTexCoordPointer(2, GL_SHORT, 0, spriteTexcoords);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
+1  A: 

The obvious problem might be that your texture has dimensions that are not a power of two. There's a hardware restriction on iphone that requires them to be that way

From Apple's iPhone OpenGL ESProgramming Guide:

The following are known limitations as of iPhone OS 3.0: The PowerVR SGX does not support non–power of two cube mapped or mipmapped textures

Kornel Kisielewicz
They are never disabled, To be sure, I have added them in just before drawing.
Jacob
are the image dimensions a power of 2?
Kornel Kisielewicz
ie 8,16,32,64,128... No, it wasnt mentioned that was important in anything I have read! trying it now, thanks.
Jacob
That was it! Thanks! Now to work out why!
Jacob
It might not be a restriction in the main OpenGL spec, but it's certainly good practice to make texture sizes power of 2. Otherwise the GPU has to resize the texture image every frame. I'm guessing that they removed this code completely for the iPhone to avoid performance problems.
ChrisF
Found a official source on that fact.
Kornel Kisielewicz
It’s not just good practice—the OpenGL ES specifications explicitly require this. In the absence of any extensions stating otherwise, texture dimensions must be power-of-two in OpenGL ES 1.1 (section 3.7.1), and may only be non-power-of-two in OpenGL ES 2.0 if and only if mipmapping is disabled (i.e. the minification filter is GL_NEAREST or GL_LINEAR) and the wrap mode is GL_CLAMP_TO_EDGE (sections 3.7.1 and 3.8.2).There’s an extension (APPLE_texture_2D_limited_npot) that loosens the restrictions in ES 1.1 to match ES 2.0, but it’s only available on the SGX, not the MBX.
Pivot
Its a shame not many (none i have seen) iphone tutorials dont mention this issue.
corydoras