如何获得多纹理单位的最大数量

How to Get Max Number of Multitextured Units

本文关键字:最大数 单位 纹理 何获得      更新时间:2023-10-16

假设我有一个函数,我希望用户能够以类型安全的方式选择适当的纹理。因此,我没有使用GL_TEXTUREX的GLenum,而是定义了一个方法,如下所示:

void activate_enable_bind(uint32_t texture_num)  {
  const uint32_t max_textures = GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS - GL_TEXTURE0;
  const uint32_t actual_texture = (GL_TEXTURE0 + texture_num);
  if (texture_num > max_textures) {
    throw std::runtime_error("ERROR: texture::activate_enable_bind()");
  }
  glActiveTexture(actual_texture);
  glEnable(target_type);
  glBindTexture(target_type, texture_id_);
}

是否保证在基于opengl规范的所有实现下都能工作,或者允许实现者使用

`GL_TEXTURE0 - GL_TEXTURE(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS -1)` 

以不连续的方式定义?

我也在这里修改我的代码:

void activate_enable_bind(uint32_t texture_num = 0)  {
  GLint max_textures = 0;
  glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_textures);
  if (static_cast<GLint>(texture_num) > max_textures - 1) {
    throw std::runtime_error("ERROR: texture::activate_enable_bind()");
  }
  const uint32_t actual_texture = (GL_TEXTURE0 + texture_num);
  glActiveTexture(actual_texture);
  glEnable(target_type);
  glBindTexture(target_type, texture_id_);
}

我认为GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS本身不是一个有用的值,而是您传递给glGet以检索实际值的东西。为了说明这一点,您可以这样检索它:

GLint max_combined_texture_image_units;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
// and then maybe check for errors

添加GL_TEXTURE0是安全的;OpenGL 3.2核心规范的3.8节这样说:

如果指定了无效的纹理,则

Active-Texture 生成错误INVALID_­ENUMtexture是形式为TEXTURE i的符号常量,表示该纹理单位i需要修改。TEXTURE i = TEXTURE0+ i (i在范围0到k & -;1,其中kMAX_­COMBINED_­TEXTURE_­IMAGE_­UNITS的值。

您的更正代码,(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS是一个enum):

void activate_enable_bind(uint32_t texture_num)  {
  int value;
  glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB,&value);
  if (texture_num+1 > value) {
    throw std::runtime_error("ERROR: texture::activate_enable_bind()");
  }
  const uint32_t actual_texture = (GL_TEXTURE0 + texture_num);
  glActiveTexture(actual_texture);
  glEnable(target_type);
  glBindTexture(target_type, texture_id_);
}

编辑:同时阅读@icktoofay的答案