How to get the maximum number of multitextured units

Let's say that I have a function where I want the user to be able to select the appropriate texture with a safe type. Therefore, instead of using GLenum GL_TEXTUREX, I define the 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_); } 

Is it guaranteed that it works in all implementations based on the opengl specification, or are developers allowed to have

 `GL_TEXTURE0 - GL_TEXTURE(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS -1)` 

certain non-contiguous way?

I modified my code here too, 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_); } 
+4
source share
2 answers

I think that GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS in itself is not a useful value, but you go to glGet to get the actual value. To account for this, you will get it as follows:

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

As for adding to GL_TEXTURE0 , it is safe; & sect; 3.8 The basic specification of OpenGL 3.2 says the following:

ActiveTexture generates an INVALID_ENUM error if an invalid texture is specified. texture is a symbolic constant of the TEXTURE i shape, indicating that the texture unit i should be changed. Constants obey TEXTURE i = TEXTURE0+ i (i is in the range from 0 to k & minus; 1, where k is the value of MAX_COMBINED_TEXTURE_IMAGE_UNITS ).

+6
source

The corrected code, ( GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS - enumeration):

 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_); } 

EDIT: also read @icktoofay answer.

+1
source

Source: https://habr.com/ru/post/1481569/


All Articles