Re: OpenGL Texturing Example
Re: OpenGL Texturing Example
- Subject: Re: OpenGL Texturing Example
- From: Dylan Neild <email@hidden>
- Date: Wed, 25 Sep 2002 09:46:01 -0400
Hey John,
OpenGL is pretty flexible, at least under OS X, as far as what images
it can accept as textures.
This code is written from memory, so it might not compile straight
away. In particular, I might be missing an argument or something on the
glTexImage2D function.. so use with caution and be sure to double check
what I'm doing first. :)
Assuming you're working in cocoa:
// to load texture
GLuint myTexturePointer[1];
GLint imageType, imageWidth, imageHeight;
NSData *myImageData = [NSData
dataWithContentsOfFile:@"/images/myimage.jpg"];
NSBitmapImageRep *myImageRep = [NSBitmapImageRep
imageRepWith
Data:myImageData];
imageWidth = [myImageRep pixelsWide];
imageHeight = [myImageRep pixelsHigh];
if ([myImageRep.bytesPerPixel] == 3) imageType=GL_RGB;
else if ([myImageRep.bytesPerPixel == 4) imageType=GL_RGBA;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, myTexturePointer);
glBindTexture(GL_TEXTURE_2D, myTexturePointer[0]);
gTexImage2D(GL_TEXTURE_2D, 0, imageType, pixelsWide, pixelsHigh,
imageType, 0, GL_UNSIGNED_BYTE, [myImageRep bitmapData]);
// to use texture
glBindTexture(GL_TEXTURE_2D, myTexturePointer[0]);
glBegin(GL_QUADS);
glVertex2f(<positioning parameters>); glVertex3f(1.0f, 1.0f, -1.0f);
glVertex2f(<positioning parameters>); glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex2f(<positioning parameters>); glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex2f(<positioning parameters>); glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
glFlush(); (-or- preferably, [glContext flushBuffer] if you're using a
double buffered context, which I recommend you do). :)
Hope this helps a bit,
Dylan
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.