In addition to the default glTexGen modes that are supplied by OpenGL, the tubing library also contains some of its own automatic texture coordinate generation routines. In addition, user-defined texture coord generation routines can be supplied. To use texture mapping with the extrusion library, one must remember to "do the obvious":
gleTextureMode can be used to set the type of automatic texture coordinate generation to be used. The argument should be a bitwise-OR of any of the following flags:
The way in which the automatic texture coordinate generation occurs is determined by one of the following flags. One and only one of these should be selected at a time. These tokens are enumerants, not bit-flags.
Uses
φ |
2π |
1 |
2π |
vy |
vx |
as the texture "u" coordinate, and the accumulated segment length as the "v" coordinate. In the above equation, "vx" and "vy" stand for the vertex's x and y coordinates.
Uses
φ |
2π |
1 |
2π |
ny |
nx |
as the texture "u" coordinate, and the accumulated segment length as the "v" coordinate. In the above equation, "nx" and "ny" stand for the normal's x and y coordinates.
Uses
φ |
2π |
1 |
2π |
vy |
vx |
as the texture "u" coordinate, and
θ |
2π |
1 |
2π |
as the texture "v" coordinate. In the above equation, "vx", "vy" and "vz" stand for the vertex's x, y and z coordinates.
Uses
φ |
2π |
1 |
2π |
ny |
nx |
as the texture "u" coordinate, and
θ |
2π |
1 |
2π |
as the texture "v" coordinate. In the above equation, "nx", "ny" and "nz" stand for the normal's x, y and z coordinates.
To best understand how to use the above functions, it is best to understand how the tubing is actually drawn. Let us start by defining some terms. The tubing library "extrudes" a "contour" along a "path". The contour is a 2D polyline. The path is a 3D polyline. We use the word "segment" to refer to a straight-line segment of the path polyline. We also interchangeably use the word "segment" to stand for the section of the extrusion that lies along a path segment.
The tubing library draws segments one at a time. It uses glPushMatrix and glPopMatrix to orient each segment along the negative z-axis. The segment starts at z=0 and ends at some negative z-value (equal to the length of the segment). The segment is then drawn by calling glVertex3f (and glNormal3f) by drawing the 2D contour at z=0 and again at z=-len. (Of course, if the join style is one of the fancy ones, then the end-points are trimmed in a variety of ways, and do not land exactly on z=0, or z=-len, but they do come close). Note that glBegin and glEnd are called around each segment. (Note also that additional glBegin/glEnds may be called to draw end-caps or filleting triangles for the more complex join styles.)
The obvious way to automatically generate textures is to warp the glVertex and glNormal functions, and compute texture coordinates based on the 3-space vertex and normal coordinates. This is essentially what the tubing code does, except that it passes some extra parameters. The glBegin calls are wrapped, and the integer segment number and the floating-point length of the segment are passed in. By knowing the segment number, and the segment length, the texture coordinates can be adjusted. Knowing the length allows the length to be accumulated, so that a texture is applied lengthwise along the extrusion. It is this accumulated length that is used in the FLAT and CYL mapping modes.
For each vertex, not only are the vertex x,y,z coordinates available, but so is a contour vertex counter indicating which contour vertex this corresponds to. There is also a flag indicating whether the vertex corresponds to a front or back vertex (i.e. a z=0 or z=-len vertex). Again, this info can be used to avoid confusion when drawing the more complex join styles.
Here are a few hints, tips, and techniques: