如何获得多纹理单位的最大数量
How to Get Max Number of Multitextured Units
假设我有一个函数,我希望用户能够以类型安全的方式选择适当的纹理。因此,我没有使用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_ENUM
。texture是形式为TEXTURE
i的符号常量,表示该纹理单位i需要修改。TEXTURE
i= TEXTURE0+
i (i在范围0到k & -;1,其中k为MAX_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的答案
相关文章:
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 二进制数组中最大连续 1 的起始和结束索引,以 C++ 为单位
- 找到不大于 A 的最大数的最有效方法,该数可被 B 整除
- C++:最大数组值函数工作不正常
- 在浮点数32中保存浮点数16最大数
- 使用 DP 的非相邻元素的最大数组总和
- 找到第 n 个最大数无限循环C++
- 最大数组大小
- C 最大数组大小
- 如何在GPU内存中获取最大数组大小
- 具有最大数组函数的程序
- 这是在包含非唯一元素的未排序数组中选择第k个最大数的最快算法
- 如何递归地找到最大数组元素的索引
- 递归最大数求和
- 如何找到数组中的最大数并用c++显示
- 给定一系列整数,提供第k个最大数
- 在任意加长的集合中查找最大数不起作用
- IEEE 754浮点数,1<的最大数是多少?
- 不与V互质的最大数
- C++是否有最大数组长度限制