GLGETTEXIMAGE(3G)

๐Ÿ“ฆ version 24.3.3, in opensuse-leap 16.0

๐Ÿ’… GLGETTEXIMAGE (3G) โ€” or, "bestie, give me that texture back"

Hey hi hello ๐Ÿซถ you're looking at the 3G manual page, which means we're deep in OpenGL land. This is glGetTexImage โ€” the function that reaches into the GPU and yoinks a texture image back out into your own memory. It's giving "I made this, actually give it back" energy. Let's go โœจ


๐ŸŒธ NAME ๐ŸŒธ

glGetTexImage โ€” return a texture image ๐ŸŽ€


โญ C SPECIFICATION โญ

void glGetTexImage( GLenum target,
                    GLint level,
                    GLenum format,
                    GLenum type,
                    GLvoid *pixels )

No cap, that's the whole signature. Copy it exactly, don't get cute with it ๐Ÿ’€


๐Ÿฆ‹ PARAMETERS ๐Ÿฆ‹

  • target โ€” tells the function which texture you're grabbing. Accepted values are GL_TEXTURE_1D, GL_TEXTURE_2D, and GL_TEXTURE_3D. Like, pick your dimension, bestie ๐ŸŒˆ
  • level โ€” the level-of-detail number of the image you want. Level 0 is the base image level, and level n is the *n*th mipmap reduction image. So like, level 0 is the main character and the rest are the ever-smaller understudies ๐Ÿ‘‘
  • format โ€” specifies a pixel format for the returned data ๐Ÿ’– The supported formats are GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_BGR, GL_RGBA, GL_BGRA, GL_LUMINANCE, and GL_LUMINANCE_ALPHA.
  • type โ€” specifies a pixel type for the returned data โœจ The supported types are GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, GL_UNSIGNED_INT, GL_INT, GL_FLOAT, GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_UNSIGNED_INT_8_8_8_8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, and GL_UNSIGNED_INT_2_10_10_10_REV. Yes that's a lot of them. Yes you should still read them all. Periodt ๐Ÿ’…
  • pixels โ€” returns the texture image ๐ŸŽ€ Should be a pointer to an array of the type specified by type. That's your destination, your little receiving basket ๐Ÿงบ

๐Ÿ”ฅ DESCRIPTION ๐Ÿ”ฅ

So glGetTexImage returns a texture image into pixels. target specifies whether the desired texture image is the one specified by glTexImage1D (GL_TEXTURE_1D), glTexImage2D (GL_TEXTURE_2D), or glTexImage3D (GL_TEXTURE_3D). level specifies the level-of-detail number of the desired image. format and type specify the and type of the desired image array โ€” and yes, that sentence reads a lil cursed in the original too, we're keeping it real ๐Ÿ™ˆ โ€” see the reference pages glTexImage1D and glDrawPixels for a description of the acceptable values for the format and type parameters, respectively.

Okay so to actually understand how glGetTexImage works, picture the selected internal four-component texture image as an RGBA color buffer that's the size of the image ๐Ÿ–ผ๏ธ Once you do that, the semantics of glGetTexImage are identical to those of glReadPixels, with one exception: no pixel transfer operations are performed, when called with the same format and type, with x and y set to 0, width set to the width of the texture image (including border if one was specified), and height set to 1 for 1D images, or to the height of the texture image (including border if one was specified) for 2D images. Literally identical energy, just no pixel transfers โœจ

And since the internal texture image is an RGBA image, the pixel formats GL_COLOR_INDEX, GL_STENCIL_INDEX, and GL_DEPTH_COMPONENT are not accepted, and pixel type GL_BITMAP is not accepted. Not invited to the party, sorry babes ๐Ÿ’€

Now โ€” if the selected texture image doesn't have four components, the following mappings get applied. Write these down, they're on the quiz ๐Ÿ“

  • Single-component textures are treated as RGBA buffers with red set to the single-component value, green set to 0, blue set to 0, and alpha set to 1.
  • Two-component textures are treated as RGBA buffers with red set to the value of component zero, alpha set to the value of component one, and green and blue set to 0.
  • Three-component textures are treated as RGBA buffers with red set to component zero, green set to component one, blue set to component two, and alpha set to 1.

Slay, the math is mathing ๐ŸŒˆ

To figure out the required size of pixels, use glGetTexLevelParameter to determine the dimensions of the internal texture image, then scale the required number of pixels by the storage required for each pixel, based on format and type ๐Ÿงฎ And be sure to take the pixel storage parameters into account, especially GL_PACK_ALIGNMENT โ€” this is the classic "why is my image skewed" bug, iykyk ๐Ÿ˜ญ


๐ŸŽ€ NOTES ๐ŸŽ€

  • If an error is generated, no change is made to the contents of pixels. Your buffer stays exactly as it was ๐Ÿ’–
  • When the GL_ARB_multitexture extension is supported, glGetTexImage returns the texture image for the active texture unit ๐Ÿ‘‘
  • The types GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_UNSIGNED_INT_8_8_8_8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, GL_UNSIGNED_INT_2_10_10_10_REV, and the formats GL_BGR and GL_BGRA are available only if the GL version is 1.2 or greater. Gatekeeping, honestly ๐Ÿšช

๐Ÿ’€ ERRORS ๐Ÿ’€

  • GL_INVALID_ENUM is generated if target, format, or type is not an accepted value. Wrong word, wrong vibe check ๐Ÿšซ
  • GL_INVALID_VALUE is generated if level is less than 0. Negative levels are not a thing, bestie ๐Ÿ™…
  • GL_INVALID_VALUE may be generated if level is greater than log2*max*, where max is the returned value of GL_MAX_TEXTURE_SIZE. "May" โ€” so like, don't push it ๐ŸŒธ
  • GL_INVALID_OPERATION is generated if glGetTexImage is executed between the execution of glBegin and the corresponding execution of glEnd. Do not do the thing inside the other thing ๐Ÿ˜ค
  • GL_INVALID_OPERATION is returned if type is one of GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, GL_UNSIGNED_SHORT_5_6_5, or GL_UNSIGNED_SHORT_5_6_5_REV and format is not GL_RGB.
  • GL_INVALID_OPERATION is returned if type is one of GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_UNSIGNED_INT_8_8_8_8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, or GL_UNSIGNED_INT_2_10_10_10_REV, and format is neither GL_RGBA or GL_BGRA.
Okay real talk: those last two are the pairing rules and they are strict. Mismatched type/format combos are an instant GL_INVALID_OPERATION, no negotiation, no vibes ๐Ÿซถ

๐Ÿงš ASSOCIATED GETS ๐Ÿงš

  • glGetTexLevelParameter with argument GL_TEXTURE_WIDTH
  • glGetTexLevelParameter with argument GL_TEXTURE_HEIGHT
  • glGetTexLevelParameter with argument GL_TEXTURE_BORDER
  • glGetTexLevelParameter with argument GL_TEXTURE_INTERNALFORMAT
  • glGet with arguments GL_PACK_ALIGNMENT and others

These are the little info-gathering besties that tell you what you're working with ๐Ÿ”


๐ŸŒˆ SEE ALSO ๐ŸŒˆ

glActiveTextureARB(3G), glDrawPixels(3G), glReadPixels(3G), glTexEnv(3G), glTexGen(3G), glTexImage1D(3G), glTexImage2D(3G), glTexImage3D(3G), glTexSubImage1D(3G), glTexSubImage2D(3G), glTexSubImage3D(3G), glTexParameter(3G)

The whole friend group, honestly ๐Ÿ‘ฏโ€โ™€๏ธ


And that's the whole page, bestie โ€” you ate that ๐Ÿ’…โœจ This one goes out from GLGETTEXIMAGE(3G), filed under the 3G manual, so go forth and grab those textures. ๐Ÿฆ‹๐ŸŽ€

๐Ÿ“ฆ where this page comes from