user513064
user513064

Reputation:

OpenGL: Only some textures work

I am using the RayWenderlich OpenGL tutorial found here as a starting point for an app. Upon adding my own textures though, I ran into some difficulty. No matter what any image I add to the project will just come up black. I've tried different settings of png's (which are used in the sample) and JPEG's. I've tried just changing the file name that is loaded and drawn by default and making a different texture instance and both just come up black.

I tried opening the floor texture in Photoshop and just resaving it, and it just came up invisible in the game.

But I tried duplicating the projects original tile_floor.png file with a different name which worked perfectly, so I'm assuming opengl requires a very specific format.

What do I have to do to get this working?

EDIT:

This is the method used to load the images:

static GLuint setupTexture(NSString *fileName) {

// 1
CGImageRef spriteImage = [UIImage imageNamed:fileName].CGImage;
if (!spriteImage) {
    NSLog(@"Failed to load image %@", fileName);
    exit(1);
}

// 2
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);    

// 3
CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);

CGContextRelease(spriteContext);

// 4
GLuint texName;
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

free(spriteData);        
return texName;

}

Upvotes: 0

Views: 173

Answers (2)

zorgesho
zorgesho

Reputation: 46

For non-power-of-two textures you need to set parameter GL_CLAMP_TO_EDGE for texture before glTexImage2D.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

Upvotes: 3

user513064
user513064

Reputation:

I have discovered that my problem is that the dimensions of my images weren't a power of two (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024). Apparently OpenGL ES requires this.

Upvotes: 0

Related Questions