2012-03-21 10:05:23 +08:00
|
|
|
/*
|
|
|
|
* cocos2d for iPhone: http://www.cocos2d-iphone.org
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Ricardo Quesada
|
|
|
|
* Copyright (c) 2011 Zynga Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* File autogenerated with Xcode. Adapted for cocos2d needs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Only compile this code on iOS. These files should NOT be included on your Mac project.
|
|
|
|
// But in case they are included, it won't be compiled.
|
|
|
|
|
|
|
|
#import "CCPlatformMacros.h"
|
|
|
|
#import "CCES2Renderer.h"
|
|
|
|
#import "OpenGL_Internal.h"
|
|
|
|
|
|
|
|
|
|
|
|
@implementation CCES2Renderer
|
|
|
|
|
|
|
|
@synthesize context=context_;
|
|
|
|
@synthesize defaultFramebuffer=defaultFramebuffer_;
|
|
|
|
@synthesize colorRenderbuffer=colorRenderbuffer_;
|
|
|
|
@synthesize msaaColorbuffer=msaaColorbuffer_;
|
|
|
|
@synthesize msaaFramebuffer=msaaFramebuffer_;
|
|
|
|
|
|
|
|
// Create an OpenGL ES 2.0 context
|
|
|
|
- (id) initWithDepthFormat:(unsigned int)depthFormat withPixelFormat:(unsigned int)pixelFormat withSharegroup:(EAGLSharegroup*)sharegroup withMultiSampling:(BOOL) multiSampling withNumberOfSamples:(unsigned int) requestedSamples
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! sharegroup )
|
|
|
|
context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
|
|
|
|
else
|
|
|
|
context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:sharegroup];
|
2012-03-21 10:05:23 +08:00
|
|
|
|
|
|
|
if (!context_ || ![EAGLContext setCurrentContext:context_] )
|
|
|
|
{
|
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
depthFormat_ = depthFormat;
|
|
|
|
pixelFormat_ = pixelFormat;
|
|
|
|
multiSampling_ = multiSampling;
|
2012-03-21 10:05:23 +08:00
|
|
|
|
|
|
|
// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
|
|
|
|
glGenFramebuffers(1, &defaultFramebuffer_);
|
2012-04-19 14:35:52 +08:00
|
|
|
NSAssert( defaultFramebuffer_, @"Can't create default frame buffer");
|
2012-03-21 10:05:23 +08:00
|
|
|
|
|
|
|
glGenRenderbuffers(1, &colorRenderbuffer_);
|
2012-04-19 14:35:52 +08:00
|
|
|
NSAssert( colorRenderbuffer_, @"Can't create default render buffer");
|
2012-03-21 10:05:23 +08:00
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer_);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer_);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer_);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (multiSampling_)
|
|
|
|
{
|
|
|
|
GLint maxSamplesAllowed;
|
|
|
|
glGetIntegerv(GL_MAX_SAMPLES_APPLE, &maxSamplesAllowed);
|
|
|
|
samplesToUse_ = MIN(maxSamplesAllowed,requestedSamples);
|
|
|
|
|
|
|
|
/* Create the MSAA framebuffer (offscreen) */
|
|
|
|
glGenFramebuffers(1, &msaaFramebuffer_);
|
|
|
|
NSAssert( msaaFramebuffer_, @"Can't create default MSAA frame buffer");
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, msaaFramebuffer_);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
CHECK_GL_ERROR();
|
2012-03-21 10:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// Allocate color buffer backing based on the current layer size
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer_);
|
2012-03-21 10:05:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! [context_ renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer] )
|
2012-03-21 10:05:23 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
NSLog(@"failed to call context");
|
2012-03-21 10:05:23 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth_);
|
|
|
|
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight_);
|
|
|
|
|
|
|
|
NSLog(@"cocos2d: surface size: %dx%d", (int)backingWidth_, (int)backingHeight_);
|
|
|
|
|
|
|
|
if (multiSampling_)
|
|
|
|
{
|
|
|
|
if ( msaaColorbuffer_) {
|
|
|
|
glDeleteRenderbuffers(1, &msaaColorbuffer_);
|
|
|
|
msaaColorbuffer_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the offscreen MSAA color buffer.
|
|
|
|
After rendering, the contents of this will be blitted into ColorRenderbuffer */
|
|
|
|
|
|
|
|
//msaaFrameBuffer needs to be binded
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, msaaFramebuffer_);
|
|
|
|
glGenRenderbuffers(1, &msaaColorbuffer_);
|
|
|
|
NSAssert(msaaFramebuffer_, @"Can't create MSAA color buffer");
|
|
|
|
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, msaaColorbuffer_);
|
|
|
|
|
|
|
|
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, samplesToUse_, pixelFormat_ , backingWidth_, backingHeight_);
|
|
|
|
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaColorbuffer_);
|
|
|
|
|
|
|
|
GLenum error;
|
|
|
|
if ( (error=glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
{
|
|
|
|
NSLog(@"Failed to make complete framebuffer object 0x%X", error);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_GL_ERROR();
|
|
|
|
|
|
|
|
if (depthFormat_)
|
|
|
|
{
|
|
|
|
if( ! depthBuffer_ ) {
|
|
|
|
glGenRenderbuffers(1, &depthBuffer_);
|
|
|
|
NSAssert(depthBuffer_, @"Can't create depth buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer_);
|
|
|
|
|
|
|
|
if( multiSampling_ )
|
|
|
|
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, samplesToUse_, depthFormat_,backingWidth_, backingHeight_);
|
|
|
|
else
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, depthFormat_, backingWidth_, backingHeight_);
|
|
|
|
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer_);
|
2012-11-13 11:06:32 +08:00
|
|
|
|
|
|
|
if (depthFormat_ == GL_DEPTH24_STENCIL8_OES) {
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthBuffer_);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// bind color buffer
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer_);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_GL_ERROR();
|
|
|
|
|
|
|
|
GLenum error;
|
|
|
|
if( (error=glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
{
|
|
|
|
NSLog(@"Failed to make complete framebuffer object 0x%X", error);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
2012-03-21 10:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(CGSize) backingSize
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return CGSizeMake( backingWidth_, backingHeight_);
|
2012-03-21 10:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) description
|
|
|
|
{
|
2012-08-21 05:01:48 +08:00
|
|
|
return [NSString stringWithFormat:@"<%@ = %08X | size = %ix%i>", [self class], (unsigned int)self, backingWidth_, backingHeight_];
|
2012-03-21 10:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned int) colorRenderBuffer
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return colorRenderbuffer_;
|
2012-03-21 10:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned int) defaultFrameBuffer
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return defaultFramebuffer_;
|
2012-03-21 10:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned int) msaaFrameBuffer
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return msaaFramebuffer_;
|
2012-03-21 10:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned int) msaaColorBuffer
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return msaaColorbuffer_;
|
2012-03-21 10:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
NSLog(@"cocos2d: deallocing %@", self);
|
2012-03-21 10:05:23 +08:00
|
|
|
|
|
|
|
// Tear down GL
|
|
|
|
if (defaultFramebuffer_) {
|
|
|
|
glDeleteFramebuffers(1, &defaultFramebuffer_);
|
|
|
|
defaultFramebuffer_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (colorRenderbuffer_) {
|
|
|
|
glDeleteRenderbuffers(1, &colorRenderbuffer_);
|
|
|
|
colorRenderbuffer_ = 0;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( depthBuffer_ ) {
|
|
|
|
glDeleteRenderbuffers(1, &depthBuffer_ );
|
|
|
|
depthBuffer_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( msaaColorbuffer_)
|
|
|
|
{
|
|
|
|
glDeleteRenderbuffers(1, &msaaColorbuffer_);
|
|
|
|
msaaColorbuffer_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( msaaFramebuffer_)
|
|
|
|
{
|
|
|
|
glDeleteRenderbuffers(1, &msaaFramebuffer_);
|
|
|
|
msaaFramebuffer_ = 0;
|
|
|
|
}
|
2012-03-21 10:05:23 +08:00
|
|
|
|
|
|
|
// Tear down context
|
|
|
|
if ([EAGLContext currentContext] == context_)
|
|
|
|
[EAGLContext setCurrentContext:nil];
|
|
|
|
|
|
|
|
[context_ release];
|
|
|
|
context_ = nil;
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|