Rather than having to explicitly provide a texture coordinate for each vertex, we can use texture coordinate generation (texgen) functions to have OpenGL automatically compute texture coordinates.
This uses the function glTexGen, and the glEnable modes GL_TEXTURE_GEN_S & GL_TEXTURE_GEN_T.
planeCoefficients = [ 1, 0, 0, 0 ] glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR) glTexGenfv(GL_S, GL_OBJECT_PLANE, planeCoefficients) glEnable(GL_TEXTURE_GEN_S) glBegin(GL_QUADS) glVertex3f(-3.25, -1, 0) glVertex3f(-1.25, -1, 0) glVertex3f(-1.25, 1, 0) glVertex3f(-3.25, 1, 0) glEnd()
Texgen modes GL_OBJECT_PLANE and GL_EYE_PLANE are generating texture coordinates based on the distances of vertices from a plane.
A plane in 3 dimensions can be defined by the equation:
Ax + By + Cz + D = 0
This is an implicit formula - it defines a plane as the set of points that satisfy the equation. In other words, it serves as a test to determine whether or not a particular point [x y z] is on the plane or not.
Another interpretation is that the value of
Ax + By + Cz + D
is proportional to the distance of the point [x y z] from the plane.
The vector [A B C] is normal (perpendicular) to the plane.
Thus, it defines the plane's orientation.
e.g.: A=1, B=0, C=0
defines a plane perpendicular to the X axis, or parallel to the
plane of the Y & Z axes.
The plane equation in this case would reduce to x = -D.
D controls the distance of the plane from the origin. If [A B C] is a unit vector, then D is equal to the distance from the plane to the origin.


Note that if, for a given [x y z], Ax + By + Cz + D = 0, then 2Ax + 2By + 2Cz + 2D = 0, and in general:
NAx + NBy + NCz + ND = 0
In other words, multiplying the four coefficients by the same constant N will still define the same plane.
However, the value of NAx + NBy + NCz + ND will be N times the value of Ax + By + Cz + D. This is useful in texture coordinate generation.
The texture generation mode can be one of
When the mode is GL_OBJECT_LINEAR, the texture coordinate is calculated using the the plane-equation coefficients that are passed via glTexGenfv(GL_S, GL_OBJECT_PLANE, planeCoefficients).
The coordinate (S in this case) is computed as:
S = Ax + By + Cz + D
using the [x y z] coordinates of the vertex (the values passed to glVertex).
If [A B C] is a unit vector, this means that the texture coordinate S is equal to the distance of the vertex from the texgen plane.
We can change how frequently the texture repeats (i.e. scale the texture) by multiplying the texgen coefficients by different amounts.
e.g., changing the coefficients from [1 0 0 0] to [3.5 0 0 0] changes
the results of contour.cpp
from:
to:
GL_EYE_LINEAR works similarly, with the coefficients passed via glTexGenfv(GL_S, GL_EYE_PLANE, planeCoefficients).
The difference is that GL_OBJECT_LINEAR operates in "object coordinates", while GL_EYE_LINEAR works in "eye coordinates".
This means that the texture coordinates computed with GL_OBJECT_LINEAR depend solely on the values passed to glVertex, and do not change as the object (or camera) moves via transformations.
The texture coordinates computed with GL_EYE_LINEAR use the positions of vertices after all modeling & viewing transformations - i.e. they use the positions of the vertices on the screen.
Both S & T coordinates can use texgen, to apply 2D textures with automatic coordinates.
This is done by making similar glTexGen calls for the T coordinate (but using a different plane).
SplaneCoefficients = [ 1, 0, 0, 0 ] glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR) glTexGenfv(GL_S, GL_EYE_PLANE, SplaneCoefficients) glTexGenfv(GL_S, GL_OBJECT_PLANE, SplaneCoefficients) TplaneCoefficients = [ 0, 1, 0, 0 ] glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR) glTexGenfv(GL_T, GL_EYE_PLANE, TplaneCoefficients) glTexGenfv(GL_T, GL_OBJECT_PLANE, TplaneCoefficients)
Sphere mapping, a.k.a. reflection mapping or environment mapping, is a texgen mode that simulates a reflective surface.
It is enabled by setting the texgen mode to GL_SPHERE_MAP.
There are no additional parameters.
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP) glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP)
| texture:
|
A sphere-mapped texture coordinate is computed by taking the vector from the viewpoint (the eye), and reflecting it about the surface's normal vector. The resulting direction is then used to look up a point in the texture. The texture image is assumed to be warped to provide a 360 degree view of the environment being reflected.


If you don't care about realistically exact reflections, though, any texture can be used.
| texture:
|