OpenGL纹理格式


  glTexImage* 系列函数绝对是 OpenGL 最常用的函数。它的声明为:

void glTexImage2D (GLenum target,
                   GLint level,
                   GLint internalformat,
                   GLsizei width,
                   GLsizei height,
                   GLint border,
                   GLenum format,
                   GLenum type,
                   const GLvoid *pixels);

  其中有三个描述纹理数据信息的变量 internalformat、format 和 type,它们有时会让人感到困扰。

  • internalformat : 表示在纹理中实际存储的数据信息,决定底层数据(比如上表的 image format),比如 gl_R8 表示底层只存储 8 位无符号整数表示 R 通道
  • format : 表示纹理的输入像素数据的通道格式它必须和传入数据相配合。也就是说,如果设定为 GL_RGB,type 为 GL_UNSIGNED_BYTE,则每个相邻像素的红色通道相隔三个字节。一个需要注意的是,其实 format 身居两职,如果声明为 GL_RED,表示在 glsl 读取浮点数;声明为 GL_RED_INTEGER,表示在 glsl 读取整数,需要和 usampler、isampler 等搭配使用。
  • type : 表示为数据赋值的单通道原始数据类型。比如常用 internalformat 的 GL_RGBA32F 和 GL_UNSIGHED_BYTE 配合时,虽然传入数据为无符号整数型,但是 OpenGL 会在 GPU 中自动转换存储为 GL_FLOAT 型

  从上面来看,三种类型分别各司其职。但它们仍有一定的关联性。比如 internalformat 其实可以粗略指定。  internalformat 也可以使用 GL_RED、GL_RED_INTEGER 等形式,这个时候为纹理赋值时,就会使用 type 指定的形式,比如 GL_RED、GL_RED 和 GL_UNSIGNED_BYTE 组合使用时,internalformat 实际为 GL_R8。这样有一定好处,避免了类型转换,但是强烈推荐不要使用粗略的 internalformat 形式


  OpenGL 的纹理数据格式繁多,以下是一个对应,已经省略了压缩纹理。

Image format (sized) Pixel format Pixel type
GL_R8 GL_RED GL_UNSIGNED_BYTE
GL_R8_SNORM GL_RED GL_BYTE
GL_R16 GL_RED GL_UNSIGNED_SHORT
GL_R16_SNORM GL_RED GL_SHORT
GL_R32F GL_RED GL_FLOAT
GL_R8I GL_RED_INTEGER GL_INT
GL_R8UI GL_RED_INTEGER GL_UNSIGNED_INT
GL_R16I GL_RED_INTEGER GL_INT
GL_R16UI GL_RED_INTEGER GL_UNSIGNED_INT
GL_R32I GL_RED_INTEGER GL_INT
GL_R32UI GL_RED_INTEGER GL_UNSIGNED_INT
GL_R16F GL_RED GL_HALF_FLOAT
GL_RG8 GL_RG GL_UNSIGNED_BYTE
GL_RG8_SNORM GL_RG GL_BYTE
GL_RG16 GL_RG GL_UNSIGNED_SHORT
GL_RG16_SNORM GL_RG GL_SHORT
GL_RG16F GL_RG GL_HALF_FLOAT
GL_RG32F GL_RG GL_FLOAT
GL_RG8I GL_RG_INTEGER GL_INT
GL_RG8UI GL_RG_INTEGER GL_UNSIGNED_INT
GL_RG16I GL_RG_INTEGER GL_INT
GL_RG16UI GL_RG_INTEGER GL_UNSIGNED_INT
GL_RG32I GL_RG_INTEGER GL_INT
GL_RG32UI GL_RG_INTEGER GL_UNSIGNED_INT
GL_R3_G3_B2 GL_RGB GL_UNSIGNED_BYTE_3_3_2
GL_RGB4 GL_RGB GL_UNSIGNED_BYTE
GL_RGB5 GL_RGB GL_UNSIGNED_BYTE
GL_RGB8 GL_RGB GL_UNSIGNED_BYTE
GL_RGB8_SNORM GL_RGB GL_BYTE
GL_RGB10 GL_RGB GL_UNSIGNED_SHORT
GL_RGB12 GL_RGB GL_UNSIGNED_SHORT
GL_RGB16_SNORM GL_RGB GL_SHORT
GL_RGBA2 GL_RGBA GL_UNSIGNED_SHORT_4_4_4_4
GL_RGBA4 GL_RGBA GL_UNSIGNED_SHORT_4_4_4_4
GL_SRGB8 GL_RGB GL_UNSIGNED_BYTE
GL_RGB16F GL_RGB GL_HALF_FLOAT
GL_RGB32F GL_RGB GL_FLOAT
GL_R11F_G11F_B10F GL_RGB GL_UNSIGNED_INT_10F_11F_11F_REV
GL_RGB9_E5 GL_RGB GL_UNSIGNED_INT_5_9_9_9_REV
GL_RGB8I GL_RGB_INTEGER GL_INT
GL_RGB8UI GL_RGB_INTEGER GL_UNSIGNED_INT
GL_RGB16I GL_RGB_INTEGER GL_INT
GL_RGB16UI GL_RGB_INTEGER GL_UNSIGNED_INT
GL_RGB32I GL_RGB_INTEGER GL_INT
GL_RGB32UI GL_RGB_INTEGER GL_UNSIGNED_INT
GL_RGB5_A1 GL_RGBA GL_UNSIGNED_SHORT_5_5_5_1
GL_RGBA8 GL_RGBA GL_UNSIGNED_BYTE
GL_RGBA8_SNORM GL_RGBA GL_BYTE
GL_RGB10_A2 GL_RGBA GL_UNSIGNED_INT_10_10_10_2
GL_RGB10_A2UI GL_RGBA_INTEGER GL_UNSIGNED_INT_10_10_10_2
GL_RGBA12 GL_RGBA GL_UNSIGNED_SHORT
GL_RGBA16 GL_RGBA GL_UNSIGNED_SHORT
GL_SRGB8_ALPHA8 GL_RGBA GL_UNSIGNED_BYTE
GL_RGBA16F GL_RGBA GL_HALF_FLOAT
GL_RGBA32F GL_RGBA GL_FLOAT
GL_RGBA8I GL_RGBA_INTEGER GL_INT
GL_RGBA8UI GL_RGBA_INTEGER GL_UNSIGNED_INT
GL_RGBA16I GL_RGBA_INTEGER GL_INT
GL_RGBA16UI GL_RGBA_INTEGER GL_UNSIGNED_INT
GL_RGBA32I GL_RGBA_INTEGER GL_INT
GL_RGBA32UI GL_RGBA_INTEGER GL_UNSIGNED_INT
GL_DEPTH_COMPONENT16 GL_DEPTH_COMPONENT GL_UNSIGNED_SHORT
GL_DEPTH_COMPONENT24 GL_DEPTH_COMPONENT GL_UNSIGNED_INT
GL_DEPTH_COMPONENT32 GL_DEPTH_COMPONENT GL_UNSIGNED_INT
GL_DEPTH_COMPONENT32F GL_DEPTH_COMPONENT GL_FLOAT
GL_DEPTH24_STENCIL8 GL_DEPTH_STENCIL GL_UNSIGNED_INT_24_8
GL_DEPTH32F_STENCIL8 GL_DEPTH_STENCIL GL_FLOAT_32_UNSIGNED_INT_24_8_REV

转载请带上本文永久固定链接:http://www.gleam.graphics/texture-color-type.html

Tags :

About the Author

6 thoughts on “OpenGL纹理格式

    • Author gravatar

      I really like your writing style, good information, regards for putting up eakccffdbefa

    • Author gravatar

      Thanks so much for giving everyone an extremely memorable opportunity to read articles and blog posts from this site. It’s always so awesome and also stuffed with a lot of fun for me personally and my office mates to visit your website more than thrice in a week to read the fresh guidance you will have. And of course, I’m so usually happy with your unique hints served by you. Some 4 facts in this post are surely the most impressive I have had.

    • Author gravatar
    • Author gravatar

      Levitra Wiki Viagra Generique En Belgique [url=http://adrugo.com]buy generic cialis online[/url] Amoxicilina In Canada Medicine Order Nexium Samples Online Propecia Rae

    • Author gravatar

      Thank you for each of your effort on this blog. My niece loves working on research and it’s easy to understand why. We know all of the powerful means you offer helpful tactics through your web site and in addition increase response from other ones on that topic while my princess is in fact understanding so much. Have fun with the rest of the new year. You are always conducting a useful job.

    • Author gravatar

      I have to show some appreciation to the writer just for bailing me out of such a trouble. After looking out throughout the internet and seeing principles that were not powerful, I figured my life was done. Existing minus the solutions to the problems you’ve solved by means of the article content is a serious case, as well as ones that would have badly affected my entire career if I had not encountered your website. Your good understanding and kindness in taking care of all things was important. I’m not sure what I would have done if I hadn’t encountered such a solution like this. It’s possible to at this time look ahead to my future. Thanks so much for the skilled and results-oriented help. I will not be reluctant to suggest your web blog to anyone who needs to have counselling about this matter.

发表评论

电子邮件地址不会被公开。