Question

In the android,use this code is worked:

glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, content.width, content.height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, content.pixels);

but in the iOS not work, this code only draw 64 *64(power of 2)

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, content.width, content.height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, content.pixels);

how can i ?

very thankful!!!

Was it helpful?

Solution 2

The size of texture depends on the GPU and OpenGL ES implementation. On iOS only Power of 2 textures are accepted. And in PVR formate only Square Texture with Power of 2 size is accepted. You need to read their documents before implementing. But for cross-platform it is advisable to use Power of 2 with square texture.

OTHER TIPS

Use this before your glTexImage2D call:

// Use tightly packed data
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

More information here: http://www.khronos.org/opengles/sdk/1.1/docs/man/glPixelStorei.xml

[...]GL_PACK_ALIGNMENT Specifies the alignment requirements for the start of each pixel row in memory. The allowable values are 1 (byte-alignment), 2 (rows aligned to even-numbered bytes), 4 (word-alignment), and 8 (rows start on double-word boundaries). The initial value is 4. [...]

This may not be available on all devices. But any serious device targeted nowadays should support it. However, one drawback that remains with this method is, that the OpenGL read buffer only reads one byte at a time. So when using 1 instead of the default value 4, the call to glTexImage2D will probably be around 4 times slower. And of course the data isn't aligned properly in memory, but this should not be a problem. I have to recommend to use textures with sizes at powers of two though, since the drawback of using a 63x63 texture is much worse than just loading a 64x64 texture and adjusting texture coordinates.

If I recall iOS OpenGLES2 can draw non power of 2 textures but on some cases it won't draw them. First we are talking about uncompressed textures because that is what you are trying to do in the code. Secondly, you didn't give a lot of information. What exactly "doesn't work"? I will assume that by doesn't work you mean you are getting a black texture.

It is possible you have a mipmap mismatch, for instance if you specify:

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

Specifying a mip map min scale filter when your texture doesn't have mip map levels will result in a black texture. Actually, you can't use mipmaps at all with non power two textures.

Another thing is you can't use GL_REPEAT with non power 2 textures. You can't do this on iOS for non power 2 textures:

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                        GL_REPEAT);

Instead you can do this:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
        GL_CLAMP_TO_EDGE);

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top