本文介绍了如何获得最大数量的多纹理单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个功能,希望用户能够以类型安全的方式选择适当的纹理.因此,我定义了如下方法,而不是使用GL_TEXTUREX的GLenum.

Lets say I have a function where I want the user to be able to select the appropriate texture in a type safe manner. So instead of using a GLenum of GL_TEXTUREX I define a method as follows.

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规范的实现中使用,还是允许实现者拥有

Is this guaranteed to work under all implementations based on the opengl specification, or are implementers allowed to have

`GL_TEXTURE0 - GL_TEXTURE(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS -1)`

以非连续方式定义?

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

I was modifying my code aswell here in what I have:

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本身并不是一个有用的值,而是您传递给来检索实际值.为了解决这个问题,您可以像这样检索它:

I think GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS is not on its own a useful value, but is something that you pass to glGet to retrieve the actual value. To account for that, you'd retrieve it like this:

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 表示:

As for adding to GL_TEXTURE0, that is safe; §3.8 of the OpenGL 3.2 Core specification says this:

这篇关于如何获得最大数量的多纹理单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 14:12