Open GL Super Bible:Texture Mapping
To access the contents, click the chapter and section titles.
Open GL Super Bible
(Publisher: Macmillan Computer Publishing)
Author(s): Waite group Press
ISBN: 1571690735
Publication Date: 08/01/96
Previous
Table of Contents
Next
Defining 1D Textures
OpenGL provides a single function for defining 1D textures: glTexImage1D. The glTexImage1D function accepts eight arguments:
void glTexImage1D(GLenum target, GLint level, GLint components,
GLsizei width, GLint border, GLenum format,
GLenum type, const GLvoid *pixels)
The target argument specifies which texture should be defined; this argument must be GL_TEXTURE_1D. The level argument indicates the texture imagełs level of detail and is usually 0. Other values are used for mipmapped textures (described later in this chapter). The components argument specifies the number of color values used for each pixel. For color index textures, components must be 1. Values of 3 and 4 are used for RGB and RGBA texture images, respectively.
Width and border specify the size of the texture image. The border value controls the number of border pixels OpenGL should expect (and use) and may have a value of 0, 1, or 2. The width parameter specifies the width of the main texture image (without the border pixels) and must be a power of 2.
The format argument indicates the type of color values to expectGL_COLOR_INDEX, GL_LUMINANCE, GL_RGB, or GL_RGBA.
Youłll find an example of defining a 1D texture in Listing 12-1 and in the example code CH12\TEX1D.C on the source code CD-ROM.
Listing 12-1 Defining a 1D texture image
void
LoadAllTextures(void)
{
static unsigned char roygbiv_image[8][3] =
{
{ 0x3f, 0x00, 0x3f }, /* Dark Violet (for 8 colors) */
{ 0x7f, 0x00, 0x7f }, /* Violet */
{ 0xbf, 0x00, 0xbf }, /* Indigo */
{ 0x00, 0x00, 0xff }, /* Blue */
{ 0x00, 0xff, 0x00 }, /* Green */
{ 0xff, 0xff, 0x00 }, /* Yellow */
{ 0xff, 0x7f, 0x00 }, /* Orange */
{ 0xff, 0x00, 0x00 } /* Red */
};
glNewList(RainbowTexture = glGenLists(1), GL_COMPILE);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage1D(GL_TEXTURE_1D, 0, 3, 8, 0, GL_RGB, GL_UNSIGNED_BYTE,
roygbiv_image);
glEndList();
}
The example code creates a display list containing the texture image and the desired magnification and minification filter, GL_LINEAR. The minification filter is used when the polygon to be drawn is smaller than the texture image, in this case 8 pixels. The magnification filter is used when the polygon is larger than the texture image. By designating the GL_LINEAR filter, you tell OpenGL to linearly interpolate color values in the texture image before drawing anything on the screen. The other filters you can use for GL_TEXTURE_MIN_FILTER are listed in Table 12-1.
Table 12-1 Texture Image Filters
Filter
Description
GL_NEAREST
Nearest-neighbor filtering.
GL_LINEAR
Linear interpolation.
GL_NEAREST_MIPMAP_NEAREST
Nearest-neighbor mipmapped filtering.
GL_NEAREST_MIPMAP_LINEAR
Linear interpolated mipmaps.
GL_LINEAR_MIPMAP_NEAREST
Linear interpolation of mipmaps.
GL_LINEAR_MIPMAP_LINEAR
Linear interpolation of interpolated mipmaps.
GL_NEAREST filtering takes the closest pixel in the texture image rather than interpolating between pixels. Youłll learn more about mipmap filtering later in the chapter.
Defining 2D Textures
To define a 2D texture image in OpenGL, you call glTexImage2D. The glTexImage2D function takes a height argument in addition to the ones that glTexImage1D uses, as follows:
void glTexImage2D(GLenum target, GLint level, GLint components,
GLsizei width, GLsizei height, GLint border,
GLenum format, GLenum type, const GLvoid *pixels)
Like glTexImage1D, the width and height arguments must be a power of 2.
Listing 12-2 shows how to load a sky texture image complete with clouds.
Listing 12-2 Defining a 2D texure image
void
LoadAllTextures(void)
{
BITMAPINFO *info; /* Bitmap information */
void *bits; /* Bitmap pixel bits */
GLubyte *rgb; /* Bitmap RGB pixels */
/*
* Try loading the bitmap and converting it to RGB
*/
bits = LoadDIBitmap('textures/sky.bmpł, &info);
if (bits == NULL)
return;
rgb = ConvertRGB(info, bits);
if (rgb == NULL)
{
free(info);
free(bits);
return;
};
glNewList(SkyTexture = glGenLists(1), GL_COMPILE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
/*
* Define the 2D texture image.
*/
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); /* Force 4-byte alignment */
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glTexImage2D(GL_TEXTURE_2D, 0, 3, info->bmiHeader.biWidth,
info->bmiHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
rgb);
glEndList();
/*
* Free the bitmap and RGB images, then return 0 (no errors).
*/
free(rgb);
free(info);
free(bits);
}
A Note About Textures: Youłll notice that all the examples presented in this chapter use display lists to store texture images. Display lists generally speed up the drawing of static graphics commands, and texture images are no exception. In addition, the forthcoming OpenGL 1.1 API includes texture object support that optimizes texture images stored in display lists by keeping them loaded in the graphics hardware texture memory if available.
Previous
Table of Contents
Next
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
Wyszukiwarka
Podobne podstrony:
385 388385 388383 385III CSK 388 06 1381 385 bkxxfouzvuxv2ywm5ifjqi5m2dmkizz5w7ktqni10 (385)385 Poprawiamy błedy w zapisach księgowychwięcej podobnych podstron