Light | Dark

# docs.GL

OpenGL 4
OpenGL 3
OpenGL ES 3
OpenGL ES 2

# glGenerateMipmap

## Name

glGenerateMipmap — generate mipmaps for a specified texture target

## C Specification

`void glGenerateMipmap(` | GLenum target`)`;

## Parameters

`target`

  Specifies the target to which the texture whose mimaps to generate is bound. `target` must be `GL_TEXTURE_2D`, `GL_TEXTURE_3D`, `GL_TEXTURE_2D_ARRAY` or `GL_TEXTURE_CUBE_MAP`.

## Description

`glGenerateMipmap` generates mipmaps for the texture attached to `target` of the active texture unit. For cube map textures, a `GL_INVALID_OPERATION` error is generated if the texture attached to `target` is not cube complete.

Mipmap generation replaces texel array levels level base + 1 through q with arrays derived from the level base array, regardless of their previous contents. All other mimap arrays, including the level base array, are left unchanged by this computation.

The internal formats of the derived mipmap arrays all match those of the level base array. The contents of the derived arrays are computed by repeated, filtered reduction of the level base array. For two-dimensional texture arrays, each layer is filtered independently.

## Errors

`GL_INVALID_ENUM` is generated if `target` is not one of the accepted texture targets.

`GL_INVALID_OPERATION` is generated if `target` is `GL_TEXTURE_CUBE_MAP` and the texture bound to the `GL_TEXTURE_CUBE_MAP` target of the active texture unit is not cube complete.

`GL_INVALID_OPERATION` is generated if the level base array is stored in a compressed internal format.

`GL_INVALID_OPERATION` is generated if the level base array was not specified with an unsized internal format or a sized internal format that is both color-renderable and texture-filterable.

## Examples

Create a texture object with linear mipmaps and edge clamping.

    GLuint texture_id;
    glGenTextures(1, &texture_id);
    glBindTexture(GL_TEXTURE_2D, texture_id);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    // texture_data is the source data of your texture, in this case
    // its size is sizeof(unsigned char) * texture_width * texture_height * 4
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width, texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_data);
    glGenerateMipmap(GL_TEXTURE_2D); // Unavailable in OpenGL 2.1, use gluBuild2DMipmaps() instead

    glBindTexture(GL_TEXTURE_2D, 0);

## Tutorials

Songho - OpenGL Frame Buffer Object (FBO)
open.gl - Textures Objects and Parameters
opengl-tutorial.org - Tutorial 5 : A Textured Cube

## API Version Support

| OpenGL ES API Version
--- | ---
Function Name | 2.0 | 3.0 | 3.1
glGenerateMipmap | ✔ | ✔ | ✔

## See Also

glTexImage2D, glBindTexture, glGenTextures

## Copyright

Copyright © 2010-2014 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. https://opencontent.org/openpub/.

Think you can improve this page? Edit this page on GitHub.