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

📄 paintingview.m

📁 一个可以演示OpenGL在xcode里的画点的程序
💻 M
字号:
/*File: PaintingView.mAbstract: The class responsible for the finger painting.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 "PaintingView.h"//CLASS IMPLEMENTATIONS:@implementation PaintingView@synthesize  location;@synthesize  previousLocation;- (id) initWithFrame:(CGRect)frame{	NSMutableArray*	recordedPaths;	CGImageRef		brushImage;	CGContextRef	brushContext;	GLubyte			*brushData;	size_t			width, height;		if((self = [super initWithFrame:frame pixelFormat:GL_RGB565_OES depthFormat:0 preserveBackbuffer:YES])) {		[self setCurrentContext];		// Create a texture from an image		// First create a UIImage object from the data in a image file, and then extract the Core Graphics image		brushImage = [UIImage imageNamed:@"Particle.png"].CGImage;				// Get the width and height of the image		width = CGImageGetWidth(brushImage);		height = CGImageGetHeight(brushImage);				// Texture dimensions must be a power of 2. If you write an application that allows users to supply an image,		// you'll want to add code that checks the dimensions and takes appropriate action if they are not a power of 2.				// Make sure the image exists		if(brushImage) {			// Allocate  memory needed for the bitmap context			brushData = (GLubyte *) malloc(width * height * 4);			// Use  the bitmatp creation function provided by the Core Graphics framework. 			brushContext = CGBitmapContextCreate(brushData, width, width, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);			// After you create the context, you can draw the  image to the context.			CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage);			// You don't need the context at this point, so you need to release it to avoid memory leaks.			CGContextRelease(brushContext);			// Use OpenGL ES to generate a name for the texture.			glGenTextures(1, &brushTexture);			// Bind the texture name. 			glBindTexture(GL_TEXTURE_2D, brushTexture);			// Specify a 2D texture image, providing the a pointer to the image data in memory			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);			// Release  the image data; it's no longer needed            free(brushData);					// Set the texture parameters to use a minifying filter and a linear filer (weighted average)			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);						// Enable use of the texture			glEnable(GL_TEXTURE_2D);			// Set a blending function to use			glBlendFunc(GL_SRC_ALPHA, GL_ONE);			// Enable blending			glEnable(GL_BLEND);		}				//Set up OpenGL states		glDisable(GL_DITHER);		glMatrixMode(GL_PROJECTION);		glOrthof(0, frame.size.width, 0, frame.size.height, -1, 1);		glMatrixMode(GL_MODELVIEW);		glEnable(GL_TEXTURE_2D);		glEnableClientState(GL_VERTEX_ARRAY);	    glEnable(GL_BLEND);		glBlendFunc(GL_SRC_ALPHA, GL_ONE);		glEnable(GL_POINT_SPRITE_OES);		glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);		glPointSize(width / kBrushScale);					//Make sure to start with a cleared buffer		[self erase];				//Playback recorded path, which is "Shake Me"		recordedPaths = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Recording" ofType:@"data"]];		if([recordedPaths count])			[self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.2];			}		return self;}// Releases resources when they are not longer needed.- (void) dealloc{			glDeleteFramebuffersOES(1, &drawingFramebuffer);	glDeleteTextures(1, &drawingTexture);	[super dealloc];}// Erases the screen- (void) erase{	//Clear the buffer	glClear(GL_COLOR_BUFFER_BIT);		//Display the buffer	[self swapBuffers];}// Drawings a line onscreen based on where the user touches- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end{	static GLfloat*		vertexBuffer = NULL;	static NSUInteger	vertexMax = 64;	NSUInteger			vertexCount = 0,						count,						i;		//Allocate vertex array buffer	if(vertexBuffer == NULL)		vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));		// Add points to the buffer so there are drawing points every X pixels	count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / kBrushPixelStep), 1);	for(i = 0; i < count; ++i) {		if(vertexCount == vertexMax) {			vertexMax = 2 * vertexMax;			vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));		}				vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);		vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);		vertexCount += 1;	}		//Render the vertex array	glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);	glDrawArrays(GL_POINTS, 0, vertexCount);		// Display the buffer	[self swapBuffers];}// Reads previously recorded points and draws them onscreen. This is the Shake Me message that appears when the application launches.- (void) playback:(NSMutableArray*)recordedPaths{	NSData*				data = [recordedPaths objectAtIndex:0];	CGPoint*			point = (CGPoint*)[data bytes];	NSUInteger			count = [data length] / sizeof(CGPoint),						i;		//Render the current path	for(i = 0; i < count - 1; ++i, ++point)		[self renderLineFromPoint:*point toPoint:*(point + 1)];		//Render the next path after a short delay 	[recordedPaths removeObjectAtIndex:0];	if([recordedPaths count])		[self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.01];}// Handles the start of a touch- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{	CGRect				bounds = [self bounds];    UITouch*	touch = [[event touchesForView:self] anyObject];	firstTouch = YES;	//Convert touch point from UIView referential to OpenGL one (upside-down flip)	location = [touch locationInView:self];	location.y = bounds.size.height - location.y;}// Handles the continuation of a touch.- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{     	  	CGRect				bounds = [self bounds];	UITouch*			touch = [[event touchesForView:self] anyObject];			//Convert touch point from UIView referential to OpenGL one (upside-down flip)	if (firstTouch) {		firstTouch = NO;		previousLocation = [touch previousLocationInView:self];		previousLocation.y = bounds.size.height - previousLocation.y;	} else {		location = [touch locationInView:self];	    location.y = bounds.size.height - location.y;		previousLocation = [touch previousLocationInView:self];		previousLocation.y = bounds.size.height - previousLocation.y;	}			// Render the stroke	[self renderLineFromPoint:previousLocation toPoint:location];		}// Handles the end of a touch event when the touch is a tap.- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{	CGRect				bounds = [self bounds];    UITouch*	touch = [[event touchesForView:self] anyObject];	if (firstTouch) {		firstTouch = NO;		previousLocation = [touch previousLocationInView:self];		previousLocation.y = bounds.size.height - previousLocation.y;		[self renderLineFromPoint:previousLocation toPoint:location];	}}// Handles the end of a touch event.- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{	// If appropriate, add code necessary to save the state of the application.	// This application is not saving state.}@end

⌨️ 快捷键说明

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