⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eaglview.m

📁 一个可以演示OpenGL在xcode里的画点的程序
💻 M
字号:
/*File: EAGLView.mAbstract: Convenience class that wraps the CAEAGLLayer from CoreAnimation into aUIView subclass.Version: 1.6Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Inc.("Apple") in consideration of your agreement to the following terms, and youruse, installation, modification or redistribution of this Apple softwareconstitutes acceptance of these terms.  If you do not agree with these terms,please do not use, install, modify or redistribute this Apple software.In consideration of your agreement to abide by the following terms, and subjectto these terms, Apple grants you a personal, non-exclusive license, underApple's copyrights in this original Apple software (the "Apple Software"), touse, reproduce, modify and redistribute the Apple Software, with or withoutmodifications, in source and/or binary forms; provided that if you redistributethe Apple Software in its entirety and without modifications, you must retainthis notice and the following text and disclaimers in all such redistributionsof the Apple Software.Neither the name, trademarks, service marks or logos of Apple Inc. may be usedto endorse or promote products derived from the Apple Software without specificprior written permission from Apple.  Except as expressly stated in this notice,no other rights or licenses, express or implied, are granted by Apple herein,including but not limited to any patent rights that may be infringed by yourderivative works or by other works in which the Apple Software may beincorporated.The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NOWARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIEDWARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR INCOMBINATION WITH YOUR PRODUCTS.IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTEGOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/ORDISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OFCONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IFAPPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.Copyright (C) 2008 Apple Inc. All Rights Reserved.*/#import <QuartzCore/QuartzCore.h>#import <OpenGLES/EAGLDrawable.h>#import "EAGLView.h"//CLASS IMPLEMENTATIONS:@implementation EAGLView@synthesize delegate=_delegate, autoresizesSurface=_autoresize, surfaceSize=_size, framebuffer = _framebuffer, pixelFormat = _format, depthFormat = _depthFormat, context = _context;+ (Class) layerClass{	return [CAEAGLLayer class];}- (BOOL) _createSurface{	CAEAGLLayer*			eaglLayer = (CAEAGLLayer*)[self layer];	CGSize					newSize;	GLuint					oldRenderbuffer;	GLuint					oldFramebuffer;		if(![EAGLContext setCurrentContext:_context]) {		return NO;	}		newSize = [eaglLayer bounds].size;	newSize.width = roundf(newSize.width);	newSize.height = roundf(newSize.height);		glGetIntegerv(GL_RENDERBUFFER_BINDING_OES, (GLint *) &oldRenderbuffer);	glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *) &oldFramebuffer);		glGenRenderbuffersOES(1, &_renderbuffer);	glBindRenderbufferOES(GL_RENDERBUFFER_OES, _renderbuffer);		if(![_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)eaglLayer]) {		glDeleteRenderbuffersOES(1, &_renderbuffer);		glBindRenderbufferOES(GL_RENDERBUFFER_BINDING_OES, oldRenderbuffer);		return NO;	}		glGenFramebuffersOES(1, &_framebuffer);	glBindFramebufferOES(GL_FRAMEBUFFER_OES, _framebuffer);	glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, _renderbuffer);	if (_depthFormat) {		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);	}	_size = newSize;	if(!_hasBeenCurrent) {		glViewport(0, 0, newSize.width, newSize.height);		glScissor(0, 0, newSize.width, newSize.height);		_hasBeenCurrent = YES;	}	else {		glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFramebuffer);	}	glBindRenderbufferOES(GL_RENDERBUFFER_OES, oldRenderbuffer);		// Error handling here		[_delegate didResizeEAGLSurfaceForView:self];		return YES;}- (void) _destroySurface{	EAGLContext *oldContext = [EAGLContext currentContext];		if (oldContext != _context)		[EAGLContext setCurrentContext:_context];		if(_depthFormat) {		glDeleteRenderbuffersOES(1, &_depthBuffer);		_depthBuffer = 0;	}		glDeleteRenderbuffersOES(1, &_renderbuffer);	_renderbuffer = 0;	glDeleteFramebuffersOES(1, &_framebuffer);	_framebuffer = 0;		if (oldContext != _context)		[EAGLContext setCurrentContext:oldContext];}- (id) initWithFrame:(CGRect)frame{	return [self initWithFrame:frame pixelFormat:GL_RGB565_OES depthFormat:0 preserveBackbuffer:NO];}- (id) initWithFrame:(CGRect)frame pixelFormat:(GLuint)format {	return [self initWithFrame:frame pixelFormat:format depthFormat:0 preserveBackbuffer:NO];}- (id) initWithFrame:(CGRect)frame pixelFormat:(GLuint)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained{	if((self = [super initWithFrame:frame])) {		CAEAGLLayer*			eaglLayer = (CAEAGLLayer*)[self layer];				eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:										[NSNumber numberWithBool:YES], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];				_format = format;		_depthFormat = depth;				_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];		if(_context == nil) {			[self release];			return nil;		}				if(![self _createSurface]) {			[self release];			return nil;		}	}	return self;}- (void) dealloc{	[self _destroySurface];		[_context release];	_context = nil;		[super dealloc];}- (void) layoutSubviews{	CGRect				bounds = [self bounds];		if(_autoresize && ((roundf(bounds.size.width) != _size.width) || (roundf(bounds.size.height) != _size.height))) {		[self _destroySurface];		[self _createSurface];	}}- (void) setAutoresizesEAGLSurface:(BOOL)autoresizesEAGLSurface;{	_autoresize = autoresizesEAGLSurface;	if(_autoresize)	[self layoutSubviews];}- (void) setCurrentContext{	if(![EAGLContext setCurrentContext:_context]) {		printf("Failed to set current context %p in %s\n", _context, __FUNCTION__);	}}- (BOOL) isCurrentContext{	return ([EAGLContext currentContext] == _context ? YES : NO);}- (void) clearCurrentContext{	if(![EAGLContext setCurrentContext:nil])		printf("Failed to clear current context in %s\n", __FUNCTION__);}- (void) swapBuffers{	EAGLContext *oldContext = [EAGLContext currentContext];	GLuint oldRenderbuffer;		if(oldContext != _context)		[EAGLContext setCurrentContext:_context];		// CHECK_GL_ERROR();		glGetIntegerv(GL_RENDERBUFFER_BINDING_OES, (GLint *) &oldRenderbuffer);	glBindRenderbufferOES(GL_RENDERBUFFER_OES, _renderbuffer);		if(![_context presentRenderbuffer:GL_RENDERBUFFER_OES])		printf("Failed to swap renderbuffer in %s\n", __FUNCTION__);	if(oldContext != _context)		[EAGLContext setCurrentContext:oldContext];}- (CGPoint) convertPointFromViewToSurface:(CGPoint)point{	CGRect				bounds = [self bounds];		return CGPointMake((point.x - bounds.origin.x) / bounds.size.width * _size.width, (point.y - bounds.origin.y) / bounds.size.height * _size.height);}- (CGRect) convertRectFromViewToSurface:(CGRect)rect{	CGRect				bounds = [self bounds];		return CGRectMake((rect.origin.x - bounds.origin.x) / bounds.size.width * _size.width, (rect.origin.y - bounds.origin.y) / bounds.size.height * _size.height, rect.size.width / bounds.size.width * _size.width, rect.size.height / bounds.size.height * _size.height);}@end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -