Is it possible to get a 16-bit depth buffer on the iPhone?
We're making a game using cocos2d
but are having problems with the depth buffer.
I'm trying to setup a 16-bit depth buffer on the iPhone but so far I only get 24-bit depth.
The reason I want 16-bit depth buffer is:
- A: I don't need the precision of 24-bit.
- B: I'm hoping it will be faster.
This is how I set the depth format in cocos2d:
[[CCDirector sharedDirector] setDepthBufferFormat:kDepthBuffer16];
Which at some point ends up in EAGLView
(v1.3)
glGenRenderbuffersOES(1, &_depthBuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _depthBuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, _depthFormat, newSize.width, newSize.height);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, _depthBuffer);
Where _depthFormat
is GL_DEPTH_COMPONENT16_OES
(verified while debugging)
And here's how I check the number of bits:
GLint depthBufferBits;
glGetIntegerv( GL_DEPTH_BITS, &depthBufferBits );
NSLog( @"Depth buffer bits: %d", depthBufferBits );
And the output is:
Depth buffer bits: 24
What am I missing? I've tried the same code on iPod Touch (2nd gen) and iPhone 3GS, always comes back as 24 bit.
Update:
I've now updated cocos2d to the latest version from the GIT repo. Here's how I initialize the director and window:
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
// cocos2d will inherit these values
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
[CCDirector setDirectorType:CCDirectorTypeDisplayLink];
CCDirector* director = [CCDirector sharedDirector];
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8 // RGBA8 color buffer
depthFormat:GL_DEPTH_COMPONENT16_OES // 16-bit depth buffer
preserveBackbuffer:NO];
[director setOpenGLView:glView];
[director setProjection:kCCDirectorProjection2D];
[window addSubview:glView];
[window makeKeyAndVisible];
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
[director runWithScene:[LevelScene scene]];
}
Still getting a 24-bit depth buffer according to glGetIntegerv( GL_DEPTH_BITS, &depthBufferBits );
I'm not sure about the internals but setDepthBufferFormat is deprecated. I can also imagine that it's simply (no longer) possible to change the depth buffer after the GL view has been created. With Cocos2D v0.99.5 beta 3 the initialization has changed quite a bit. I suggest you update to the latest version, even though it's beta.
Then take a look here: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:setup_buffers
The important part is the initialization of the EAGLView, where you can set the depth buffer:
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:GL_DEPTH_COMPONENT16_OES
preserveBackbuffer:NO];
精彩评论