Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: from NSImage to a Texture
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: from NSImage to a Texture



First, this list has been very helpful to me and I'm glad to hear that more samples are in the works. A Cocoa/Objective-C version of the OpenGL Image sample would be very helpful to me so that I can better understand generating multiple textures from an image. The current OpenGL Image uses Carbon/C.
Thanks, Chuck
P.S. I understand that isn't the right channel for requests. Should I file a feature request somewhere?

At 10:22 AM -0600 9/29/03, Geoff Stahl wrote:
Use Kent's stuff or you can look at the "Cocoa OpenGL" sample as is
uses a NSImage of a string and goes to a texture in its string texture
class. See <http://developer.apple.com/opengl> for a link to the
sample.

On the list to do is a better sample that shows exactly what you are
looking for.

On Sep 28, 2003, at 7:20 PM, Kent Miller wrote:

On Sunday, September 28, 2003, at 12:00 AM,
email@hidden wrote:
Alright, ive gone throught the sample code (OpenGL Image) and im upto
the 'load an image into an NSImage ' stage. now what ? how do i get
the info i need from the NSImage into a texture to use in OpenGL ?
I had asked a similar question sometime back and i got responses
asking me to check out NSImage and NSImageRep and the 'OpenGL Image'
sample code. i've looked around at tutorials and im still unclear on
how to get from an NSImage to a texture that i can use in OpenGL.
please help :).


Someone may have a better version but this ought to get you started:


static unsigned int truncPowerOf2(unsigned int x)
{
int i = 0;
while (x = x>>1) i++;
return (1<<i);
}


void TextureFromNSImage(NSImage *image, GLuint *texID, GLuint *
width,GLuint *height)
{
NSBitmapImageRep *sourceImageRep;
GLenum imageFormat = GL_RGBA;
GLubyte *sourcePic;
long sourceRowBytes, pixelSize;
NSSize imageSize;

sourceImageRep = [[NSBitmapImageRep alloc]initWithData: [image
TIFFRepresentation]];

if ([sourceImageRep hasAlpha] == YES)
{
if ([sourceImageRep bitsPerPixel] != 32) return;
imageFormat = GL_RGBA;
pixelSize = 4;
}
else
{
if ([sourceImageRep bitsPerPixel] != 24) return;
imageFormat = GL_RGB;
pixelSize = 3;
}

sourceRowBytes = [sourceImageRep bytesPerRow];
sourcePic = (GLubyte*) [sourceImageRep bitmapData];
imageSize = [sourceImageRep size];

//Do the OpenGL flip
> {
> GLubyte *pic = malloc( imageSize.height * sourceRowBytes );
> GLuint i;
>
> for (i = 0; i < imageSize.height; i++)
> {
> memcpy ( pic + (i * sourceRowBytes),
sourcePic + ((((int)imageSize.height - i) - 1) *
sourceRowBytes),
sourceRowBytes);
}
sourcePic = pic;
}

glPixelStorei(GL_UNPACK_ROW_LENGTH, sourceRowBytes/pixelSize);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

if (*texID == 0)
glGenTextures(1, texID);
glBindTexture(GL_TEXTURE_2D,*texID);

*width = truncPowerOf2(imageSize.width);
*height = truncPowerOf2(imageSize.height);

glTexImage2D(GL_TEXTURE_2D, 0, imageFormat, *width, *height, 0,
imageFormat, GL_UNSIGNED_BYTE, sourcePic);

[sourceImageRep release];
}
_______________________________________________
mac-opengl mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/mac-opengl
Do not post admin requests to the list. They will be ignored.


---
Geoff Stahl
3D Software Engineer
Apple
_______________________________________________
mac-opengl mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/mac-opengl
Do not post admin requests to the list. They will be ignored.
_______________________________________________
mac-opengl mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/mac-opengl
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: from NSImage to a Texture (From: Kent Miller <email@hidden>)
 >Re: from NSImage to a Texture (From: Geoff Stahl <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.