Plan

Texture Mapping Concepts

Texture Mapping in OpenGL

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL/GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT/GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT/GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST/GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST/GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
             GL_LUMINANCE/GL_RGB, GL_UNSIGNED_BYTE, texture_array);
glEnable(GL_TEXTURE_2D);
glTexCoord2f(s,t);

Also

twTex2D(filename,verbose);  // deprecated, but may work when NETPBM library isn't available
twPPM_Tex2D(filename,verbose);  // preferred; uses NETPBM library
twPPM_Tex2D(filename,verbose,&width,&height);  // sets the width and height (in texture coords)

The latter two will load a file from either the CWD (current working directory), or $TWHOMEDIR/textures. This environment variable is set when you do "make alias" and copy/paste those lines into your shell. It's defined for you on the Linux machines.

Of course, you may always use an absolute pathname, but that is more difficult if you're working on both Linux and Mac OS X.

Viewing a Texture File

You can look at a PPM file using the "display" program in Linux. Alternatively, you can use Graphic Converter on the Macs. There is a whole directory of textures with TW in tw/textures. There are a few PPM files in tw/tutors/data/, for use by Nate Robins's tutors, but they are upside-down.

On both machines, there is a program called "tw/bin/show-texture" that takes the name of a texture file on the command line, reads it into an OpenGL program, and texture-maps the texture onto a quad.

You can get texture files by downloading images off the web and converting them to PPM using the programs in the NETPBM package, such as jpegtopnm and giftopnm.

We'll mostly concentrate on program-generated textures today, saving texture files until next time.

Texture Mapping Examples

All demos in tw/demos/texture-mapping

Next time:

Exercise

Try some manipulations of the "SimplestTextures" demo, such as switching the MAG FILTER to LINEAR or trying grays other than white/black or other colors.

Texture-map the US Flag onto a rectangle of the appropriate aspect ratio, using the computed flag: twUSflag(). If you set up the texture-mapping stuff before your display() function, the result should be pretty speedy.

Download a graphic from the web, convert it to a PPM file, and texture-map it onto some quad, modeling your code on QuadPPM.cc. Try rotating it!

Note that the NETPBM package defines a ton of useful programs for manipulating PPM files, including converting to/from GIF and JPG. On the Linux machines, those programs are already in your path and you can find out about them by doing "man netpbm" or visiting http://netpbm.sourceforge.net/doc/directory.html; the same programs are installed on the SCI 257 Macs in /opt/local/bin.

Written by Scott D. Anderson
scott.anderson@acm.org
Creative Commons License
This work is licensed under a Creative Commons License.

Valid HTML 4.01!