开发者

iPhone OpenGLES textures - colour banding

I've got a problem with openGL on iPhone which I'm sure must have a simple solution!

When I load a texture and display it, I get a lot of what I believe is called 'Colour Banding', whereby the colours, particularly on gradients, seem to get automatically 'optimized'.

Just to demonstrate that this wasn't anything wrong with my own c开发者_运维知识库ode, I downloaded the iPhone 'Crashlanding' app and replaced the background image, and as you can see in the image below (Taken from the simulator), the exact same thing happens. The image on the left is the original PNG, and on the right is it in the game. It's almost as if it's palette is being downsized to a 256 colour one.

Screenshot

I'm sure this is related to the format I'm saving the image as, although it doesn't just happen with PNG's, it seems to happen no matter what image format I chose.

Doing my head in! If you want to recreate this, simply download the crash landing app, and replace the background. Thanks so much in advance for any help.


iPhone does not support 24bit color format (8 bits per color). It supports (uncompressed):

  • 565 format, red = 5 bits, green = 6 bits, blue = 5 bits
  • 5551 format, red = 5 bits, green = 5 bits, blue = 5 bits, alpha (transparency) - 1 bit
  • 4444 format, 4 bits for red/green/blue/transparency.

So, if you'll draw gradient from 0 to 255 in red color, you'll see 256 gradations in 24bit format, but only 32 gradations in 565/5551 format or even 16 gradations in 4444. 24bit color is being transformed "on the fly", so it looks like it is being supported but it's not.

That's why there is color banding.

/That's true for OpenGL ES 1.0, don't know details about 2.0 yet/

Update: My mistake. iPhone does support other formats (8888), but, maybe, there is a conversion in the app itself?

Update 2: Ah, you are using cocos2d. And do not use (I assume) transparency in your image. Cocos2d transforms this kind of images to a 565 format. I think you'll have to modify cocos2d to "unimpliment" this optimisation, if you need.


You should change the pixel format of Cocos2d's GLView. Here's how to do it.

If you use Cocos2d's template to create a new project, there's a file called AppDelegate.m. Find this method inside that file:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Then inside the method you will find:

CCGLView *glView = [CCGLView viewWithFrame:[_window bounds]
                               pixelFormat:kEAGLColorFormatRGB565
                               depthFormat:0
                        preserveBackbuffer:NO
                                sharegroup:nil
                             multiSampling:NO
                           numberOfSamples:0];

(might be a bit different depending on your version of Cocos2d. Mine is 2.1)

Anyway, you just need to change the pixelFormat from kEAGLColorFormatRGB565 (red, green, blue components each 5, 6, and 5 bits) to kEAGLColorFormatRGBA8 (red, green, blue, and alpha components 8 bits each). Note that it means more memory usage (16 bit vs 24 bit) and possibly lower performance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜