// // loadfunctions.m // CocoaGL // // Created by kat on Sat Aug 17 2002. // Copyright (c) 2001 __MyCompanyName__. All rights reserved. // #import "loadfunctions.h" @implementation loadfunctions @end void Error(const char* errorloc) { int a; if( (a=glGetError())!=0 ) { if( a == GL_INVALID_ENUM ) NSLog(@"In %s: GL_INVALID_ENUM", errorloc); else if( a == GL_INVALID_VALUE ) NSLog(@"In %s: GL_INVALID_VALUE", errorloc); else if( a == GL_INVALID_OPERATION ) NSLog(@"In %s: GL_INVALID_OPERATION", errorloc); else if( a = GL_STACK_OVERFLOW ) NSLog(@"In %s: GL_STACK_OVERFLOW", errorloc); else if( a == GL_STACK_UNDERFLOW ) NSLog(@"In %s: GL_STACK_UNDERFLOW", errorloc); else if( a == GL_OUT_OF_MEMORY ) NSLog(@"In %s: GL_OUT_OF_MEMORY", errorloc); else if( a == GL_TABLE_TOO_LARGE ) NSLog(@"In %s: GL_TABLE_TOO_LARGE", errorloc); } } // my jpgs stopped working.... GLuint getTextures( NSString *location ) { NSURL *theURL; NSImage *theImage; GLuint theTexture; NSLog(@"start getting the image"); // get the URL for the star image // the application is in the build directory, one level up from our star theURL = [[NSURL alloc] initFileURLWithPath: location]; if( theURL != nil ) NSLog(@"NSURL != nil"); else NSLog(@"the URL was invalid"); // load the star from the file theImage = [[NSImage alloc] initWithContentsOfURL: theURL ]; [theImage setName:@"NSGLParticleEnginePicture"]; // generate the texture objects references glGenTextures( 1, &theTexture ); Error("getTextures"); glBindTexture( GL_TEXTURE_2D, theTexture ); loadPicture( @"NSGLParticleEnginePicture" ); // release the memory that was allocated [theURL release]; [theImage release]; return theTexture; } void loadPicture( NSString *name ) { NSBitmapImageRep *bitmap; NSImage *image; // initialize the image to the correct file (name) image = [ NSImage imageNamed: name ]; if( image == nil ) { NSLog(@"image == nil"); return; } // create a bitmap with the correct image data bitmap = [[NSBitmapImageRep alloc]initWithData: [image TIFFRepresentation]]; // if this bitmap is null, we should really free the texture if (bitmap == nil) { NSLog(@"in LoadGLTextures : NSBitmapImageRep not loaded"); [bitmap release]; return; } // we are aligned by BYTES in an NSBitmapImageRep glPixelStorei(GL_UNPACK_ALIGNMENT, 1 ); Error("loadPicture glPixelStorei"); // put the image into texture memory // the image has Red-Green-Blue-Alpha chanels, is width*height in size // and is made up of unsigned bytes glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, [bitmap size].width, [bitmap size].height, 0, GL_RGB, GL_UNSIGNED_BYTE, [bitmap bitmapData]); Error("loadPicture glTexImage2D"); NSLog(@"width = %d, height = %d",[bitmap size].width, [bitmap size].height); // when we make the image smaller, use a linear filter on this texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); Error("loadPicture glTexParameteri GL_TEXTURE_MIN_FILTER"); // when we magnify the image, use a linear filter on this texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); Error("loadPicture glTexParameteri GL_TEXTURE_MAG_FILTER"); // let the bitmap go [bitmap release]; }