7

I find it surprisingly frustrating that it is so difficult to find examples of sampler2d arrays such as

uniform sampler2D myTextureSampler[5];

How would one store the uniform location of this?:

gl.GetUniformLocation(program, "myTextureSampler")

Would one simply use:

gl.GetUniformLocation(program, "myTextureSampler[0]")
gl.GetUniformLocation(program2, "myTextureSampler[2]")

How would one go about using multiple textures like this:

gl.BindTexture(gl.TEXTURE_2D, 1)
gl.BindTexture(gl.TEXTURE_2D, 2)

etc..

gl.ActiveTexture(gl.TEXTURE0)
gl.ActiveTexture(gl.TEXTURE1)

etc..

Note: this code is not pure c++ opengl. Just looking for the basic concept on how it would be done.

A simple example of passing, getting uniform location for a sampler2d array would be great. Does anyone have experience with this stuff?

2
  • 1
    What's wrong with the obvious answers? The way arrays of uniforms work from the GL side doesn't change just because you're using an opaque type like sampler2D. Commented Jul 5, 2016 at 13:59
  • The above are not answers. They are merely speculations of the way I think it would work. Currently, I have not had much success getting it to work. Seems like a pretty straightforward way of getting easy stack overflow points and help out a lot of people :) Commented Jul 5, 2016 at 14:02

2 Answers 2

5

I presume it's the same as all other shader array accesses and that:

glGetUniformLocation(program, "myTextureSampler[0]");

will work.

To use multiple textures you should set the slot which you want to put your texture in to active first:

glActiveTexture(GL_TEXTURE0);

and then you can bind your texture:

glBindTexture(GL_TEXTURE_2D, texture.handle);

The second parameter is the handle you got from glGenTextures().

Then you match the sampler2D with the appropriate texture by calling:

glUniform1i(location, 0);

The first parameter is the location you got back from calling glGetUniformLocation(). The second parameter is the active texture slot (GL_TEXTURE0 in this case).

Sign up to request clarification or add additional context in comments.

2 Comments

So the weird thing is when I try this and then i access myTextureSampler[1] in my fragment shader, everything works.. Should index 1 throw an error since I set the uniform location to index 0? I have yet to try it on two actual textures. Maybe when you assign myTextureSampler[0] and myTextureSampler[1] then it automatically fills them both in as separate units?
@efel That's because the default value of the uniform is already 0, so it refers to the texture bound to GL_TEXTURE0 by default.
3

The current answer has been here for a long time, but is not correct.

What you are using is uniform arrays.

For a shader declaration like:

...you get the uniform location of the array, without specifying a subscript:

glGetUniformLocation(my_uniform, "myTextureSampler");

...then set its values in a single operation from an array:

GLfloat values[5] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
glUniform1fv(my_uniform, 5, values);

Documentation: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glUniform.xhtml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.