Loading and using textures with alpha in OpenGL with Cocoa

Posts: 2
Joined: 2007.12
Post: #1
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 );

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
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
There is no easy way to do this with Cocoa. There is a relatively easy way with CoreGraphics: http://gamewiki.evolpenguin.com/index.ph...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.
Quote this message in a reply
Posts: 1,199
Joined: 2004.10
Post: #3
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.
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #4
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.
Quote this message in a reply
Posts: 257
Joined: 2004.06
Post: #5
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.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Alpha Channel Problem Moganza 1 5,346 Jan 19, 2013 08:25 AM
Last Post: sealfin
  Opengl/Cocoa text rendering tesil 15 23,293 Mar 20, 2012 11:16 AM
Last Post: OneSadCookie
  [SOLVED]OpenGL edges of textures mk12 2 6,023 Sep 2, 2010 08:07 PM
Last Post: mk12
  Signed Distance Fields for Alpha Tested Magnification of Vector Textures maximile 2 8,015 Mar 29, 2010 10:59 AM
Last Post: Skorche
  OpenGL ES 2.0, 2D Alpha Transparency Artifacts Macmenace 3 10,456 Mar 28, 2010 11:18 PM
Last Post: AnotherJake