macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

A Simple Mac OS X libpng Example with OpenGL

by Michael J. Norton
10/14/2005

From my previous article, "Developing in OpenGL Using Makefiles," I left you with the means to develop OpenGL code in a cross-platform environment, such as with Mac OS X and Solaris. Well, as my endeavors progressed, I quickly outgrew simple line-drawn polygon examples and wanted to play with more challenging concepts. "What could possibly be more interesting than line-drawn polygons?" you ask. Why, putting texture maps onto them, of course!

My first experiments involved writing a DIB reader. I thought I was being clever by recycling old C code I wrote some time ago for the Microsoft DirectX environment, but there was that whole Endian thing to deal with. On various mailing lists, others recommended image file libraries to use--but these were Mac-OS-dependent. In order to even raise my brow in interest, I needed a library that, one, was absolutely free and, two, had to work cross-platform on the Macintosh and UNIX.

My curiosity was finally satisfied when a fellow programmer recommend I use the open source library libpng, from libpng.org. This library supports the Portable Network Graphics (PNG) format. Long story short, the PNG format is the open source workaround to the patented LZW-compressed GIF format. The GIF format incurs some royalty fees in commercial usage, where, with the PNG format, there are no patent restrictions. PNG is a supported format on the World Wide Web. Commercial applications, such as Adobe Photoshop, also support the PNG file format.

OK, venturing this far into the research, I was sold on the PNG file format. What I needed was to find a way to implement it on my Macintosh and use it with OpenGL.

What You'll Need

As mentioned previously, you can download libpng from its home site, libpng.org. The easiest way to install the libpng library is to use the fink package installer. The fink package installer will install libpng in the directory /sw/lib.

You'll also need the Apple Xcode development system installed, of course, if you are going to compile and build the demonstration program. The last item you will need is a sample 256 by 256, 72dpi texture map PNG file, which I have provided for you.

figure 1
Figure 1. Sample PNG file to use as a texture map

Now you'll need some source code to load the PNG file. Source code listing 1, pngLoad.h, is the header file for the C function pngLoad. Source code listing 2, pngLoad.c, is the C code for the function pngLoad, which is exported to your source code via the header file. As a little disclaimer, I pieced this code together by surfing the internet for PNG coding examples. A good portion of this code comes directly from the O'Reilly book PNG: The Definitive Guide, which is available online in HTML form . Chapter 13 of this book has the meat of what we're after in order to read a PNG file using C code.

Mac OS X Tiger in a Nutshell

Related Reading

Mac OS X Tiger in a Nutshell
A Desktop Quick Reference
By Andy Lester, Chris Stone, Chuck Toporek, Jason McIntosh

Pages: 1, 2, 3

Next Pagearrow