iDevGames Forums
Loading and using textures with alpha in OpenGL with Cocoa - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Loading and using textures with alpha in OpenGL with Cocoa (/thread-2905.html)



Loading and using textures with alpha in OpenGL with Cocoa - corporatenewt - Dec 5, 2007 09:51 PM

I've been searching for quite a while now for an easy (or at least straight forward) way to load textures into OpenGL through Cocoa, but I'm no OpenGL guru... I've messed around with the Nehe tutorials and I'm still slightly confused.

Ultimately, what I would like to accomplish is to load a texture file (using an image format with transparency such as tiff or png) and draw it to an OpenGLView with the transparency intact.

I know that to enable the blending, and set up the transparency I'll need to use:

glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

I've also done plenty of searching on this site (and others), but I can't seem to find a comprehensive guide, or source code that shows what I need. I'm hoping to use this info to build a 2D side-scroller.

Hopefully I've explained my problem well enough. If anyone needs any clarification, let me know. And thanks in advance.

- Corporate Newt


Loading and using textures with alpha in OpenGL with Cocoa - OneSadCookie - Dec 6, 2007 12:18 AM

There is no easy way to do this with Cocoa. There is a relatively easy way with CoreGraphics: http://gamewiki.evolpenguin.com/index.php?title=ImageIO_Texture_Loading&oldid=3461 . Note that this does not work on Intel machines, you need to use GL_UNSIGNED_INT_8_8_8_8 instead of the _REV variant if __LITTLE_ENDIAN__ is defined. There's also a bug where it uses "malloc" instead of "calloc" to allocate the image buffer. Both CoreGraphics and Cocoa premultiply images, so your blend function will be ONE, ONE_MINUS_SRC_ALPHA.


Loading and using textures with alpha in OpenGL with Cocoa - TomorrowPlusX - Dec 7, 2007 07:37 AM

I think a lot of us here have been bitten by this. Technically, it's no big deal to get correct blending with premultiplied alpha, but there are situations where you don't want that at all ( such as a shader which treats an alpha channel as a specular map, or some other bit of swizzling ).

I use libPNG to open png files with an alpha channel. I'm considering migrating to stb_image ( http://nothings.org/stb_image.c ) which has a dirt-simple API and has received good marks from a lot of developers and is in use in a lot of projects.


Loading and using textures with alpha in OpenGL with Cocoa - OneSadCookie - Dec 7, 2007 02:07 PM

On the subject of alternatives, there's FreeImage which handles a bunch of image types, you can use libjpeg and libpng directly, or you can use http://onesadcookie.com/svn/SimpleImage which wraps both with a very simple API.


Loading and using textures with alpha in OpenGL with Cocoa - Malarkey - Dec 8, 2007 02:06 PM

I'm using FreeImage for the Mac build of the application engine we're putting together at work and it was pretty painless to set up to use with OpenGL.