Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

beginning opengl game programming 2004 phần 6 pps
Nội dung xem thử
Mô tả chi tiết
By default, you have to specify all levels starting from level 0 to the level at which the texture becomes 1 × 1 (which is equivalent to log2 of the largest dimension of the base texture). You can, however, change these limits by using the glTexParameter() function with its
pname parameter set to GL_TEXTURE_BASE_LEVEL or GL_TEXTURE_MAX_LEVEL, respectively. The
value passed to either of these parameters must be a positive integer.
Mipmapping is first enabled by specifying one of the mipmapping values for texture
minification. Once the texture minification filter has been set, you then only need to specify the texture mipmap levels with one of the glTexImage3D(), glTexImage2D(), or glTexImage1D() functions, depending on your texture dimensionality. The following example code
sets up a seven-level mipmap with a minification filter of GL_NEAREST_MIPMAP_LINEAR and
starting at a 64 × 64 base texture:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64,64,0, GL_RGB, GL_UNSIGNED_BYTE, texImage0);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, 32,32,0, GL_RGB, GL_UNSIGNED_BYTE, texImage1);
glTexImage2D(GL_TEXTURE_2D, 2, GL_RGB, 16,16,0, GL_RGB, GL_UNSIGNED_BYTE, texImage2);
glTexImage2D(GL_TEXTURE_2D, 3, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, texImage3);
glTexImage2D(GL_TEXTURE_2D, 4, GL_RGB, 4, 4, 0, GL_RGB, GL_UNSIGNED_BYTE, texImage4);
glTexImage2D(GL_TEXTURE_2D, 5, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, texImage5);
glTexImage2D(GL_TEXTURE_2D, 6, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, texImage6);
Mipmaps and the OpenGL Utility Library
The GLU library provides the gluBuild2DMipmaps() and gluBuild1DMipmaps() functions to
build mipmaps automatically for two- and one-dimensional textures, respectively. These
Mipmaps 167
Figure 7.6 Mipmaps help control the level of detail for textured objects.
07 BOGL_GP CH07 3/1/04 10:03 AM Page 167
TLFeBOOK
functions replace the set of function calls you would normally make to the glTexImage2D()
and glTexImage1D() functions to specify mipmaps.
int gluBuild2DMipmaps(GLenum target, GLint components, GLint width, GLint height,
GLenum format, GLenum type, const void *data);
int gluBuild1DMipmaps(GLenum target, GLint components GLint width, GLenum format,
GLenum type, const void *data);
One of the nice features about these functions is that you do not have to pass a power-of2 image because gluBuild2DMipmaps() and gluBuild1DMipmaps() automatically rescale your
images’ width and height to the closest power of 2 for you.
The following code uses the gluBuild2DMipmaps() function to specify mipmaps in the same
way as the previous mipmap example using glTexImage2D():
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 64, 64, GL_RGB, GL_UNSIGNED_BYTE, texImage0);
Automatic Mipmap Generation
Extension
Extension name: SGIS_generate_mipmap
Name string: GL_SGIS_generate_mipmap
Promoted to core: OpenGL 1.4
Function names: None
Tokens: GL_GENERATE_MIPMAP_SGIS
As of Version 1.4, OpenGL has introduced a new method for automatically generating
mipmaps with the texture parameter GL_GENERATE_MIPMAP. Setting this parameter to GL_TRUE
will induce a mechanism that automatically generates all mipmap levels higher than the
base level. The internal formats and border widths of the derived mipmap images all
match those of the base level image, and each increasing mipmap level reduces the size of
the image by half. The actual contents of the mipmap images are computed by a repeated,
filtered reduction of the base mipmap level.
One of the nice features of this parameter is that if you change the texture image data, the
mipmap data is calculated automatically, which makes it extremely useful for textures
you’re changing on the fly.
We are actually discussing texture parameters in the next section. This means you should
read on to find out how to use the automatic mipmap generation functionality of
OpenGL!
168 Chapter 7 ■ Texture Mapping
07 BOGL_GP CH07 3/1/04 10:03 AM Page 168
TLFeBOOK
Texture Parameters
OpenGL provides several parameters to control how textures are treated when specified,
changed, or applied as texture maps. Each parameter is set by calling the glTexParameter()
function (as mentioned in the section “Texture Filtering”):
void glTexParameter{if}(GLenum target, GLenum pname, TYPE param);
void glTexParameter{if}v(GLenum target, GLenum pname, TYPE params);
As mentioned before, the value of the target parameter refers to the texture target and can
be equal to GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, or GL_TEXTURE_CUBE_MAP. The pname
parameter is a constant indicating the parameter to be set, a list of which is shown in Table
7.5. In the first form of the glTexParameter() function, param is a single-valued parameter;
in the second form, params is an array of parameters whose type depends on the parameter being set.
Texture Parameters 169
Table 7.5 Texture Parameters
Name Type Values
GL_TEXTURE_WRAP_S integer GL_CLAMP, GL_CLAMP_TO_EDGE*, GL_REPEAT,
GL_CLAMP_TO_BORDER*, GL_MIRRORED_REPEAT*
GL_TEXTURE_WRAP_T integer GL_CLAMP, GL_CLAMP_TO_EDGE*, GL_REPEAT,
GL_CLAMP_TO_BORDER*, GL_MIRRORED_REPEAT*
GL_TEXTURE_WRAP_R integer GL_CLAMP, GL_CLAMP_TO_EDGE*, GL_REPEAT,
GL_CLAMP_TO_BORDER*, GL_MIRRORED_REPEAT*
GL_TEXTURE_MIN_FILTER integer GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST,
GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST,
GL_LINEAR_MIPMAP_LINEAR
GL_TEXTURE_MAG_FILTER integer GL_NEAREST, GL_LINEAR
GL_TEXTURE_BORDER_COLOR 4 floats any 4 values from 0 to 1
GL_TEXTURE_PRIORITY float any value from 0 to 1
GL_TEXTURE_MIN_LOD* float any value
GL_TEXTURE_MAX_LOD* float any value
GL_TEXTURE_BASE_LEVEL* integer any non-negative integer
GL_TEXTURE_MAX_LEVEL* integer any non-negative integer
GL_TEXTURE_LOD_BIAS* float any value
GL_DEPTH_TEXTURE_MODE** enum GL_LUMINANCE, GL_INTENSITY, GL_ALPHA
GL_TEXTURE_COMPARE_MODE** enum GL_NONE, GL_COMPARE_R_TO_TEXTURE
GL_TEXTURE_COMPARE_FUNC** enum GL_LEQUAL, GL_GEQUAL, GL_LESS, GL_GREATER, GL_EQUAL,
GL_NOTEQUAL, GL_ALWAYS, GL_NEVER
GL_GENERATE_MIPMAP* boolean GL_TRUE or GL_FALSE
* Available only via extensions under Windows. See the explanation of the parameter in this chapter for details.
** Available only via the ARB_depth_texture and ARB_shadow extensions under Windows.
07 BOGL_GP CH07 3/1/04 10:03 AM Page 169
TLFeBOOK