This is a library for OpenGL to load PNG graphics files as an
OpenGL texture as easily as possible. It also has a number of
options for generating the alpha channel and mipmaps. It is
implemented using modified versions of the LibPNG 1.0.2 and ZLib
1.1.3 libraries.
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable for
any damages arising from the use of this software.
Permission is hereby granted to use, copy, modify, and
distribute this source code, or portions hereof, for any purpose,
without fee, subject to the following restrictions:
The origin of this source code must not be misrepresented.
You must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
Altered versions must be plainly marked as such and must
not be misrepresented as being the original source.
This notice must not be removed or altered from any
source distribution.
Installation for MSDEV
Copy glpng.h to your include/GL folder and copy glpng.lib and
glpngd.lib to your lib folder. Then just do #include <GL/glpng.h>
and with a bit of luck, MSDEV will automatically link with glpng.lib
(release lib) or glpngd.lib (debug lib).
Installation for Any Other Compiler
Copy glpng.h to your include/GL folder. Then you'll have to
build the library yourself with the included source code.
Included are makefiles for Linux and SGI. If you need to modify
the source code to make it work on your system, please get in
contact so I can make future versions compatible.
Compiling with LibPNG or ZLib
If you are using LibPNG or ZLib in your project there may be
problems if you link with the glpng library. To solve this,
include glpng.c in your project and, if you're using MSDEV,
modify glpng.h to not automatically link with glpng.lib or glpngd.lib.
OpenGL DLL Dynamic Loading using glsetup
To use glpng with glsetup, include glpng.c, LibPNG and ZLib in
your project. In glpng.c, change #include <GL/gl.h> to
include the glsetup include and modify glpng.h to not
automatically link with glpng.lib or glpngd.lib.
Pointer to a pngInfo structure to store texture
info or NULL if you don't care. The pngInfo fields
are:
Width - width of the original image in pixels
Height - height of the original image in
pixels
Depth - depth of the original image, where
colours = 2Depth
Alpha - the number of bits used for the alpha
channel (0 if no alpha channel)
Loads a PNG file and calls glTexImage2D with appropriate
parameters. The texture will be resized if the dimensions are
not powers of 2 or over the maximum texture size. Should be
able to load all colour depths (except 64-bit) and alpha
channels if available. It converts them to an appropriate
format and gives them to glTexImage2D. The OpenGL paletted
texture extension is used if available.
Returns 1 on success or 0 if file could not be loaded.
Minification function for filtering (look up
glTexParameter)
magfilter
Magnification function for filtering (look up
glTexParamter)
Automates the process further - loads a PNG file, sets the
OpenGL parameters, binds it to an OpenGL texture and returns
it's ID, or 0 if the file couldn't be loaded.
Pointer to a function taking three unsigned char
parameters (red, green, blue) and returning an
unsigned char (alpha)
This sets the function to be called when using PNG_CALLBACK. During the alpha
channel generation process, this function will be called for
every pixel, with the appropriate RGB values, and will use
the result for the alpha value. The RGB and alpha values all
range from 0 to 255. The default callback function simply
returns 255.
By default, gamma correction is set to 1.0 for Windows, 1.7
for SGI and 1.45 for Macs. If the VIEWING_GAMMA environmental
variable is set, that is used instead. You can override both
of these values using pngSetViewingGamma().
By default, the image is loaded so that texture
coordinates 0,0 represent the top-left - a result of me not
knowing the OpenGL spec :-). If you wish to use the standard
OpenGL representation where 0,0 is the bottom-left, set this
to 1.
Examples
Here's an example of pngLoad(), to load "Texture.png"
with nearest filter, clamping on and no mipmaps or alpha channels...
if (id != 0) {
puts("Loaded Texture.png with resounding success");
printf("Size=%i,%i Depth=%i Alpha=%i\n", info.Width, info.Height, info.Depth, info.Alpha);
}
else {
puts("Can't load Texture.png");
exit(1);
}
If those two examples don't make sense, try the included full
source example (which needs GLUT).
Bugs
64-bit PNGs can't be loaded (missing LibPNG feature as
far as I can tell).
Possible Future Developments
Better attempts could be made to find the optimal texture
format for OpenGL. At the moment, it converts everything
to 24 or 32 bit, or uses the paletted texture extension
in certain (easy to handle) cases.
Other mipmap generating algorithms could be implemented (wavelet
stuff?). Source donations are welcome.
Saving the frame buffer to a PNG file.
Support for GL_INTENSITY, GL_LUMINANCE_ALPHA and others.
History
1.33 (20/5/99)
First public release.
1.34 (27/5/99)
Optimised alpha channel generating,
added Alpha property to the pngInfo structure, illegal
texture sizes are resized.
1.35 (4/6/99)
Added pngLoadRaw and pngLoadRawF
functions.
1.36 (9/6/99)
Fixed problem causing linking warnings/errors
(I think) and reduced the size of the library
considerably.
1.37 (13/6/99)
Added alpha channel generation callback
function.
1.38 (22/6/99)
Stopped it from disabling texturing on
calls to pngBind and pngBindF.
1.39 (8/7/99)
Fixed a bug in the extensions reading
code, which caused some machines to crash.
1.40 (27/9/99)
Added support for SGI, Linux, and gamma
correction (thanks to Mark B. Allan!). Fixed bug in raw
reading of gray textures (thanks to Johann Scholtz!).
Removed all use of GLU functions to make it easier to
dynamically load opengl32.dll or 3dfxvgl.dll or whatever.
Added simple mipmap generator.
1.41 (20/10/99)
Made a small optimisation and improved
documentation. Remembered to include the makefiles for
Linux and SGI in the zip (!).
1.42 (01/03/00)
Fixed problems with compiling on SGI (thanks
to Thomas Sondergaard!). Added pngSetStandardOrientation
(thanks to Scott Franke!).
1.43 (11/05/00)
Added debug library and fixed the crash
when there wasn't a terminating png info structure (thanks
to Dan Hawkins!).
1.44 (01/07/00)
Fixed release and debug libraries so
they stop producing warnings and errors in MSDEV.
1.45 (10/07/00)
Fixed bug where the standard orientation
flag was being ignored in pngLoadRawF (thanks to Mark B.
Allan!).