Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8695951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:09:49+00:00 2026-06-13T01:09:49+00:00

I have seen many samples to hard code the number of texture unit in

  • 0

I have seen many samples to hard code the number of texture unit in the program. For example:

glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, object1BaseImage);
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, object1NormalMap);
glActiveTexture(GL_TEXTURE0 + 4);
glBindTexture(GL_TEXTURE_2D, shadowMap);

The problem is that two simultaneously used textures may conflict with each other by using the same texture unit when rendering a scene. my question is how to automatically allocate the texture unit in OpenGL by implementing a class or function? since there are many different constants that define how many sampler object I can use:

GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS,
GL_MAX_GEOMETRY_TEXTURE_IMAGE_UNITS,
GL_MAX_TESS_CONTROL_TEXTURE_IMAGE_UNITS,
GL_MAX_TESS_EVALUATION_TEXTURE_IMAGE_UNITS,
GL_MAX_TEXTURE_IMAGE_UNITS and
GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS.

I am confused by the constants, isn’t there should be a constant like GL_MAX_FRAGMENT_TEXTURE_IMAGE_UNITS? I think I need to get the max number of texture unit when allocating it.
So there are two questions:

  1. How to understand the six constant mentioned above, why there isn’t a constant named GL_MAX_FRAGMENT_TEXTURE_IMAGE_UNITS.
  2. How to write a class or function automatically allocate the texture unit using the constants.

Any ideas? code snippet would be very appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:09:51+00:00Added an answer on June 13, 2026 at 1:09 am

    How to write a class or function automatically allocate the texture unit using the constants.

    This is easy: don’t.

    The “allocation” of texture image units is typically done by a convention set down by the program(s) you intend to use. You decide that unit 0 is where the diffuse texture goes; if a particular mesh&shader don’t have a diffuse texture, you don’t put something in unit 0. Unit 12 could be where shadow maps go.

    This is why shading_language_420pack allows you to set the texture unit directly within the shader. Even without that, you should generally set the sampler’s texture unit after compiling the shader and don’t move it.

    No point in changing program state when you don’t have to.

    On 3.3+ hardware, you have no less than 48+ texture units available. You’re not likely to run out.

    since there are many different constants that define how many sampler object I can use:

    No, there is one constant that defines how many “sampler object” you can use: GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS. That is the hard limit on the total number of texture image units that OpenGL provides. glActiveTexture takes a number between GL_TEXTURE0 and GL_TEXTURE0+GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS.

    The individual ones are the maximum number of GLSL sampler types that can be active in a single shader stage. As for which one you use for fragment shaders, it’s GL_MAX_TEXTURE_IMAGE_UNITS. This is for legacy reasons, since initially only fragment shaders could access textures.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen many samples of Ruby code with this line (for example, http://www.sinatrarb.com/
I have seen many number of software used for HTML code encryption, the pages
I have seen many code samples for loading textures for OpenGL , many of
I have seen this statement in many of the documention samples, like here This
many Places in the sample code i have seen 2 different way of @synthesize
I have seen quite a few code samples/plugins that promote uploading assets directly to
I've seen it in many code samples, tutorials and other people's code that I've
I have seen simple example Ajax source codes in many online tutorials. What I
I have seen many samples how to upload and download files using the brecham.obex
I have seen many posting this issue in SO. I have gone through those

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.