2011-10-25 05:03:10 +00:00
|
|
|
//
|
|
|
|
|
// KRObjViewGLView.m
|
|
|
|
|
// KRObjView
|
|
|
|
|
//
|
2012-03-15 20:09:01 +00:00
|
|
|
// Copyright 2012 Kearwood Gilbert. All rights reserved.
|
2011-10-25 06:16:47 +00:00
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
|
//
|
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
|
//
|
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other materials
|
|
|
|
|
// provided with the distribution.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
|
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
|
|
|
|
|
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
|
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
|
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
//
|
|
|
|
|
// The views and conclusions contained in the software and documentation are those of the
|
|
|
|
|
// authors and should not be interpreted as representing official policies, either expressed
|
|
|
|
|
// or implied, of Kearwood Gilbert.
|
2011-10-25 05:03:10 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "KRObjViewGLView.h"
|
|
|
|
|
|
2012-03-15 23:58:37 +00:00
|
|
|
|
2011-10-25 05:03:10 +00:00
|
|
|
#import <QuartzCore/QuartzCore.h>
|
|
|
|
|
|
|
|
|
|
@implementation KRObjViewGLView
|
|
|
|
|
|
|
|
|
|
// Override the class method to return the OpenGL layer, as opposed to the normal CALayer
|
|
|
|
|
+ (Class) layerClass
|
|
|
|
|
{
|
|
|
|
|
return [CAEAGLLayer class];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id)initWithFrame:(CGRect)frame {
|
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
|
if (self) {
|
|
|
|
|
|
|
|
|
|
// Do OpenGL Core Animation layer setup
|
|
|
|
|
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
|
|
|
|
|
|
|
|
|
|
// Set scaling to account for Retina display
|
|
|
|
|
if ([self respondsToSelector:@selector(setContentScaleFactor:)])
|
|
|
|
|
{
|
|
|
|
|
self.contentScaleFactor = [[UIScreen mainScreen] scale];
|
2012-02-10 05:48:59 +00:00
|
|
|
}
|
2011-10-25 05:03:10 +00:00
|
|
|
|
|
|
|
|
eaglLayer.opaque = YES;
|
|
|
|
|
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
|
2012-09-20 09:32:20 +00:00
|
|
|
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
|
2011-10-25 05:03:10 +00:00
|
|
|
|
2012-09-20 09:32:20 +00:00
|
|
|
if (!_context)
|
2011-10-25 05:03:10 +00:00
|
|
|
{
|
|
|
|
|
[self release];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-20 09:32:20 +00:00
|
|
|
if (![EAGLContext setCurrentContext:_context])
|
2011-10-25 05:03:10 +00:00
|
|
|
{
|
|
|
|
|
[self release];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (![self createFramebuffers])
|
|
|
|
|
{
|
|
|
|
|
[self release];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize KREngine
|
2013-01-13 08:09:45 +00:00
|
|
|
_engine = [[KREngine alloc] init];
|
2011-10-25 05:03:10 +00:00
|
|
|
[self loadObjects];
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)dealloc {
|
2012-08-17 01:04:49 +00:00
|
|
|
[_engine release]; _engine = nil;
|
2012-09-20 09:32:20 +00:00
|
|
|
[_context release]; _context = nil;
|
2011-10-25 05:03:10 +00:00
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
#pragma mark OpenGL drawing
|
|
|
|
|
|
|
|
|
|
- (BOOL)loadObjects
|
2012-04-12 06:04:15 +00:00
|
|
|
{
|
2011-10-25 05:03:10 +00:00
|
|
|
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
|
|
|
|
|
NSFileManager* fileManager = [NSFileManager defaultManager];
|
|
|
|
|
|
|
|
|
|
|
2012-04-12 01:27:30 +00:00
|
|
|
for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) {
|
2012-04-12 19:43:08 +00:00
|
|
|
NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName];
|
2012-08-17 01:04:49 +00:00
|
|
|
[self.engine loadResource: path];
|
2012-04-12 01:27:30 +00:00
|
|
|
}
|
2012-04-13 23:24:07 +00:00
|
|
|
|
2012-08-17 01:04:49 +00:00
|
|
|
[self.engine setNearZ: 5.0];
|
2012-09-26 20:07:48 +00:00
|
|
|
[self.engine setFarZ: 1000.0];
|
2012-05-08 23:39:52 +00:00
|
|
|
//[renderEngine setNearZ: 1.0];
|
|
|
|
|
//[renderEngine setFarZ: 3000.0];
|
2012-04-13 23:24:07 +00:00
|
|
|
|
2012-05-08 23:39:52 +00:00
|
|
|
|
2011-10-25 05:03:10 +00:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)createFramebuffers
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// ===== Create onscreen framebuffer object =====
|
2012-09-13 20:09:19 +00:00
|
|
|
GLDEBUG(glGenFramebuffers(1, &viewFramebuffer));
|
|
|
|
|
GLDEBUG(glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer));
|
2011-10-25 05:03:10 +00:00
|
|
|
|
|
|
|
|
// ----- Create color buffer for viewFramebuffer -----
|
2012-09-13 20:09:19 +00:00
|
|
|
GLDEBUG(glGenRenderbuffers(1, &viewRenderbuffer));
|
|
|
|
|
GLDEBUG(glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer));
|
2012-09-20 09:32:20 +00:00
|
|
|
[_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
|
2012-09-13 20:09:19 +00:00
|
|
|
GLDEBUG(glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth));
|
|
|
|
|
GLDEBUG(glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight));
|
2011-10-25 05:03:10 +00:00
|
|
|
NSLog(@"Backing width: %d, height: %d", backingWidth, backingHeight);
|
2012-09-13 20:09:19 +00:00
|
|
|
GLDEBUG(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, viewRenderbuffer));
|
2011-10-25 05:03:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-13 20:09:19 +00:00
|
|
|
GLDEBUG(
|
|
|
|
|
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
|
NSLog(@"Failure with depth buffer generation");
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
);
|
2011-10-25 05:03:10 +00:00
|
|
|
|
2012-09-13 20:09:19 +00:00
|
|
|
GLDEBUG(
|
|
|
|
|
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
|
NSLog(@"Incomplete FBO: %d", status);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
);
|
2011-10-25 05:03:10 +00:00
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)destroyFramebuffer;
|
|
|
|
|
{
|
|
|
|
|
if (viewFramebuffer)
|
|
|
|
|
{
|
2012-09-13 20:09:19 +00:00
|
|
|
GLDEBUG(glDeleteFramebuffers(1, &viewFramebuffer));
|
2011-10-25 05:03:10 +00:00
|
|
|
viewFramebuffer = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (viewRenderbuffer)
|
|
|
|
|
{
|
2012-09-13 20:09:19 +00:00
|
|
|
GLDEBUG(glDeleteRenderbuffers(1, &viewRenderbuffer));
|
2011-10-25 05:03:10 +00:00
|
|
|
viewRenderbuffer = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setDisplayFramebuffer;
|
|
|
|
|
{
|
2012-09-20 09:32:20 +00:00
|
|
|
if (_context)
|
2011-10-25 05:03:10 +00:00
|
|
|
{
|
|
|
|
|
if (!viewFramebuffer)
|
|
|
|
|
{
|
|
|
|
|
[self createFramebuffers];
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-13 20:09:19 +00:00
|
|
|
//GLDEBUG(glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer));
|
2011-10-25 05:03:10 +00:00
|
|
|
|
2012-09-13 20:09:19 +00:00
|
|
|
GLDEBUG(glViewport(0, 0, backingWidth, backingHeight));
|
2011-10-25 05:03:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)presentFramebuffer;
|
|
|
|
|
{
|
|
|
|
|
BOOL success = FALSE;
|
|
|
|
|
|
2012-09-20 09:32:20 +00:00
|
|
|
if (_context)
|
2011-10-25 05:03:10 +00:00
|
|
|
{
|
2012-09-13 20:09:19 +00:00
|
|
|
//GLDEBUG(glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer));
|
2011-10-25 05:03:10 +00:00
|
|
|
|
2012-09-20 09:32:20 +00:00
|
|
|
success = [_context presentRenderbuffer:GL_RENDERBUFFER];
|
2011-10-25 05:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-17 01:04:49 +00:00
|
|
|
- (KRScene *)getScene
|
2011-10-25 05:03:10 +00:00
|
|
|
{
|
2012-08-17 01:04:49 +00:00
|
|
|
return self.engine.context->getSceneManager()->getFirstScene();
|
2011-10-25 05:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
#pragma mark Accessors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@end
|