170 likes | 182 Views
Learn how to define, build, and apply mipmaps efficiently in OpenGL for optimized texture rendering. Explore mipmap creation, filters, texture objects, and enabling/disabling textures with practical examples.
E N D
Mipmaps in OpenGL Lecture 33 Wed, Dec 3, 2003
Defining Mipmaps • If the mipmaps have already been created, then we define them as textures using the glTexImage2D() function, specifying the mipmap level. glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, ptr);
Example of Defining Mipmaps • Suppose we have a 64 64 image, a 32 32 image, and so on, down to a 1 1 image and we want to create mipmaps from them. • We make repeated calls to glTexImage2D(), passing the level, size of the image, and a pointer to the image.
Example of Defining Mipmaps glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, image64); glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, image32); // And so on… glTexImage2D(GL_TEXTURE_2D, 6, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, image1);
Building Mipmaps Automatically • An option is to let OpenGL build the mipmaps for us. • To create mipmaps from level 0 to a 1 1, write glBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, ptr);
Minification Filters with Mipmaps • If we are using mipmaps, then we need to specify whether we want to use the nearest mipmap or use a weighted average of the nearest two mipmaps. • This choice is independent of how we select a texel or texels from a single mipmap.
Minification Filters with Mipmaps • To choose the nearest texel and the nearest mipmap, write glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
Minification Filters with Mipmaps • To choose the nearest texel and interpolate the mipmaps, write glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
Minification Filters with Mipmaps • To interpolate the texels and choose the nearest mipmap, write glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
Minification Filters with Mipmaps • To interpolate the texels and interpolate the mipmaps, write glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
Texture Objects • To improve efficiency when working with more than one texture, we may create the textures and store them as texture objects. • A texture object has a texture name which is an unsigned integer.
Texture Objects • The values of these integers are used internally. • We should let OpenGL choose the values for us. const int NUM_TEXTURES = 10; unsigned int textureName[NUM_TEXTURES]; glGenTextures(NUM_TEXTURES, textureName);
Texture Objects • When first creating a texture, we may bind it to a texture name. • Whatever texture commands follow will be associated with this texture name. glBindTexture(GL_TEXTURE_2D, textureName[i]); // Define a texture…
Texture Objects • Later, when the texture is to be used, make another call to glBindTextures(). • OpenGL will see that this texture object has already been bound. • Thus, it will retrieve the texture information and use it. glBindTexture(GL_TEXTURE_2D, textureName[i]); // Assign texture coordinates to vertices…
Texture Objects • When we are finished with the texture objects, we may delete them, in order to recycle the memory. glDeleteTextures(GL_TEXTURE_2D, textureName); // Don’t use textureName anymore…
Enabling and Disabling Textures • Finally, we need to enable textures. • This would typically be done in the display() function. glEnable(GL_TEXTURE_2D); // Draw stuff… glDisable(GL_TEXTURE_2D);
Examples • TextureDemo.cpp • MipmapDemo.cpp • RgbImage.cpp