|
In this tutorial, we'll start out by learning another way to load a picture. The picture will be star.bmp, located one level below our running directory. First we declare an NSURL to hold the location of the star. Our initial location is going to be "../star.bmp". We try to open the default location, then ask the user for a location if we don't find star.bmp there (more on that method later). We can the name the star so that it is accessible in the same way that our NIB dice were accessible.
NSURL *starURL;
NSImage *starimage;
NSLog(@"get the image");
starURL = [[NSURL alloc] initFileURLWithPath: @"../star.bmp"];
if( starURL != nil )
NSLog(@"NSURL != nil");
starimage = [[NSImage alloc] initWithContentsOfURL: starURL ];
if( starimage == nil )
starimage = [self findImage: @"star.bmp"];
[starimage setName:@"star"];
glGenTextures( NUMTEXTURES, texture );
glBindTexture( GL_TEXTURE_2D, texture[0] );
[self loadPicture: @"star"];
[starURL release];
[starimage release];
|
Here's the code for the findImage method. It takes a string and looks for any bmp files that are named that string. It asks the user to input the string.
- (NSImage *) findImage: (NSString *)resource
{
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
NSArray *fileTypes = [NSArray arrayWithObject:@"bmp"];
NSImage *image;
int result;
[oPanel setAllowsMultipleSelection:NO];
result = [oPanel runModalForDirectory:NSHomeDirectory() file:resource types:fileTypes];
if( result == NSOKButton )
{
NSString *aFile = [[oPanel filenames] objectAtIndex:0];
image = [[NSImage alloc] initWithContentsOfFile:aFile];
}
if( image != nil )
return image;
else
{
NSRunCriticalAlertPanel(@"Can't find a resource",
[resource stringByAppendingString:@" was not found"],
@"OK",nil,nil);
exit(-1);
}
}
|
Here's the definition for the star structure. We'll want many different stars, each with a different red, green and blue value, a different distance and angle, and a possibly different spin and tilt. The tilt will in fact be simply 90°.
typedef struct _star
{
int r;
int g;
int b;
float i;
GLfloat distance;
GLfloat angle;
GLfloat tilt;
GLfloat spin;
} Star;
|
In this method, we define each of the stars. We are using an unchanging C array, so there is no need to allocate memory in this method. Not much to this, but note that we get a random number (0 <= RV <= MAX_FLOAT ) and then convert it to a number 0 <= RV <= 256. We lose some of the randomness by doing this because we are discarding some of the bits, but we aren't concerned with true pseudo-randomness so much as we are concerned with the fact that the stars are different colors.
int i;
for( i = 0; i < NUMSTARS; i++ )
{
StarArray[i].distance = ((float)i/(float)NUMSTARS)*20.0f;
StarArray[i].angle = (float)i/(float)NUMSTARS;
StarArray[i].r = rand()%256;
StarArray[i].g = rand()%256;
StarArray[i].b = rand()%256;
StarArray[i].tilt = 90;
StarArray[i].i = i;
NSLog(@"star %i: d=%f, a=%f", i, StarArray[i].distance, StarArray[i].angle);
}
|
In this method, we define each of the stars. We are using an unchanging C array, so there is no need to allocate memory in this method. Not much to this, but note that we get a random number (0 <= RV <= MAX_FLOAT ) and then convert it to a number 0 <= RV <= 256. We lose some of the randomness by doing this because we are discarding some of the bits, but we aren't concerned with true pseudo-randomness so much as we are concerned with the fact that the stars are different colors.
for( i = 0; i < NUMSTARS; i++ )
{
Star s = StarArray[i];
glLoadIdentity();
glTranslatef(0,0,-40);
glRotatef(s.tilt,1.0f,1.0f,0.0f);
glRotatef(s.angle,0.0,1.0,0.0);
glTranslatef(s.distance,0,0);
glRotatef(-s.angle,0.0,1.0,0.0);
glRotatef(-s.tilt,1.0f,1.0f,0.0f);
glColor4ub(s.r,s.g,s.b,255);
glBindTexture( GL_TEXTURE_2D, texture[0] );
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
StarArray[i].distance-=0.05f;
StarArray[i].angle+=(StarArray[i].i/(float)NUMSTARS);
if (StarArray[i].distance<0.0f)
{
StarArray[i].distance+=20.0f;
StarArray[i].r=rand()%256;
StarArray[i].g=rand()%256;
StarArray[i].b=rand()%256;
}
}
|
|
|