Re: OpenGL Texture Initialization In 10.5
Re: OpenGL Texture Initialization In 10.5
- Subject: Re: OpenGL Texture Initialization In 10.5
- From: David Spooner <email@hidden>
- Date: Sun, 9 Dec 2007 22:18:30 -0700
David,
You'll need to ensure your context is current before calling any gl
functions.
[[self openGLContext] makeCurrentContext];
This will be the case inside you view's -drawRect: (assuming it is
subclassed from NSOpenGLView), but not in -awakeFromNib.
dave
On 9-Dec-07, at 9:26 PM, David Harper wrote:
Hi,
I'm implementing a method to take an NSImage and pack it into an
OpenGL texture using the method shown below (which was provided in
some apple example). The image loads properly as does the
NSBitmapImageRep, but on the call:
glPixelStorei(GL_UNPACK_ROW_LENGTH, [bitmap pixelsWide]);
the application exits citing a bad access exception
(EXC_BAD_ACCESS). This method exists in a subclass of
NSOpenGLView. I have included the OpenGL framework in my Xcode
project and import the following in the subclass' header:
#import <Cocoa/Cocoa.h>
#import <OpenGL/gl.h>
I figure there may be two reasons this is happening. First, there
may be some imports or frameworks I have omitted (but you'd think
these problems would result in a compiler warning/error). Second,
the awakefromnib method in the class I'm working with is mostly
empty. All it currently calls is a function which loads all the
textures for the application:
-(void) awakeFromNib {
[self loadTextures];
}
Perhaps there are some openGL initializations that need to occur
before glPixelStorei can be called? The complete method is included
below. I appreciate any suggestions.
Thanks,
-Dave H.
P.S.
-(void) makeTextureFromImage:(NSImage*)theImg forTexture:
(GLuint*)texName {
NSBitmapImageRep* bitmap = [NSBitmapImageRep alloc];
int samplesPerPixel = 0;
NSSize imgSize = [theImg size];
[theImg lockFocus];
[bitmap initWithFocusedViewRect:NSMakeRect(0.0, 0.0,
imgSize.width, imgSize.height)];
[theImg unlockFocus];
// Set proper unpacking row length for bitmap.
glPixelStorei(GL_UNPACK_ROW_LENGTH, [bitmap pixelsWide]);
// Set byte aligned unpacking (needed for 3 byte per pixel
bitmaps).
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
// Generate a new texture name if one was not provided.
if (*texName == 0)
glGenTextures (1, texName);
glBindTexture (GL_TEXTURE_RECTANGLE_EXT, *texName);
// Non-mipmap filtering (redundant for texture_rectangle).
glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER,
GL_LINEAR);
samplesPerPixel = [bitmap samplesPerPixel];
// Nonplanar, RGB 24 bit bitmap, or RGBA 32 bit bitmap.
if(![bitmap isPlanar] && (samplesPerPixel == 3 || samplesPerPixel
== 4)) {
glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0,
samplesPerPixel == 4 ? GL_RGBA8 : GL_RGB8,
[bitmap pixelsWide],
[bitmap pixelsHigh],
0,
samplesPerPixel == 4 ? GL_RGBA : GL_RGB,
GL_UNSIGNED_BYTE,
[bitmap bitmapData]);
} else {
// Handle other bitmap formats.
}
// Clean up.
[bitmap release];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden