I’ve already used stb_image in some desktop (PC) projects and it worked fine, but now I’m trying to use it to load some tags to use them with OpenGL ES and I can’t.
This is the code I use:
void TextureManager::LoadFontTexture(const char *filename,int rows,int columns)
{
glGenTextures(1,&textures[texNum]);
texNum++;
glBindTexture(GL_TEXTURE_2D,textures[texNum-1]);
int x,y,n;
GLenum format;
unsigned char *data=stbi_load(filename,&x,&y,&n,0);
if (data==NULL)
{
NSLog(@"Data loaded incorrectly");
NSLog(@"Failure reason:%s",stbi_failure_reason());
}
else {
NSLog(@"Width:%d height:%d",x,y);
}
if (n==3)
{
format=GL_RGB;
}
else if (n==4)
{
format=GL_RGBA;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
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,n,x,y,0,format,GL_UNSIGNED_BYTE,data);
}
This code worked fine on desktop, but when I try to texturize something with this image I get a black square. The width and height values are correct and always the (n==4) if is called (the image has an alpha channel, so this is correct). I don’t know what is wrong. I’ve also tried changing the format=GL_RGBA with format=GL_BGRA but it doesn’t work
glTexImage2D call should look like this:
3 or 4 are not valid values for internalformat in OpenGL ES. See the documentation here: http://www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml
EDIT: here’s more info how to dectect OpenGL errors:
To detect this type of errors use the glGetError function. It will tell which function failed.
For example – create following macro:
Then use it around every OpenGL function like this:
This immediately will let you know where is the error, including file name and line number.
Of course, disable checking in release mode, when you will ship application. Just use different macro: