• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: OpenGL Texturing Example
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 imageRepWithData: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.

  • Follow-Ups:
    • Re: OpenGL Texturing Example
      • From: Dylan Neild <email@hidden>
  • Prev by Date: Re: Log Out
  • Next by Date: Re: resource forks and cvs?
  • Previous by thread: Siemens Gigaset ISDN and Mac?
  • Next by thread: Re: OpenGL Texturing Example
  • Index(es):
    • Date
    • Thread