1
\$\begingroup\$

As I understand texture usage consist of two parts

  1. How to store this discrete data about texture internally. How much dimensions, channels, etc.

  2. How to fetch/sample/filter

The question relative to OpenGL are:

q1: What parameters are stored in texture object (glGenTexture) and what parameters are store in texture unit (glActiveTexture) ?

q2: Does glTexParameter perform setup per texture object or per texture unit?

\$\endgroup\$
4
\$\begingroup\$

Q1. You would call glGenTexture() like soglGenTextures(1, &handle). handle would now be an object containing an empty GL_TEXTURE_1D, GL_TEXTURE_2D, etc.. that you can fill with a texture by using e.g. glBindTexture(GL_TEXTURE_2D, handle) followed by glTexImage2D(GL_TEXTURE_2D, ...). I don't think there is a real limit to have many texture objects you could have.

There is a limit to how many you can use at the time however. You might have multiple textures per model in your scene, diffuse-map, normal-map, etc... so you would link each of your texture handles to their own texture slot so you can sample all of them at the same time.

Imagine each time your render something you can only use 8 colors at the time. You may create as many colors as you like and store that in GPU memory. But when it comes to rendering you can only select 8 color slots to render with and no more.

Example:

// Generate the textures
glGenTexture(1, &handle1)
glGenTexture(1, &handle2)

// Set the active texture slot to 0, bind handle1 to it and fill it with a texture.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, handle1);
glTexImage2D(GL_TEXTURE_2D, ...)

// Set the active texture slot to 0, bind handle1 to it and fill it with a texture.
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, handle2);
glTexImage2D(GL_TEXTURE_2D, ...)

// Get the uniform samplers from the shader program.
GLint sampler1 = glGetUniformLocation(program_handle, "sampler1");
GLint sampler2 = glGetUniformLocation(program_handle, "sampler2");

// Set sampler1 to sampler from texture slot 0 and sampler2 will sample from texture
// slot 1.
glUniform1i(sampler1, 0);
glUniform1i(sampler2, 1);

Q2. Let me quote this page: https://www.opengl.org/sdk/docs/man/html/glTexParameter.xhtml

glTexParameter specifies the texture parameters for the active texture unit, specified by calling glActiveTexture. glTextureParameter specifies the texture parameters for the texture object with ID texture.

To clarify, glTexParameteri(GL_TEXTURE_2D, ...) should affect the texture associated with the handle abound to GL_TEXTURE_2D. So if you called glBindTexture(GL_TEXTURE_2D, handle1) then all glTexParameteri(GL_TEXTURE_2D, ...) should be associated with handle1. This assumes you didn't change active texture unit as that would have a different GL_TEXTURE_2D bound to it. glTextureParameter() is new in OpenGL 4.5 and can be used to avoid all that binding by passing the handle directly.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks for answer. So as I understood I can specify something like periodization via glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) and this will applied for all textures when they will be bind to this texture slot and It doesn't need to reconfigure slot. "glTextureParameter" seems something new from GL 4.5. \$\endgroup\$ – bruziuz Nov 26 '15 at 12:42
  • \$\begingroup\$ Yes that should be correct, and you do need to call that again if you want the same for another texture slot/unit. Consider accepting this answer if it helped you. \$\endgroup\$ – Christer Nov 26 '15 at 12:51
  • \$\begingroup\$ @bruziuz Improved my answer a bit. Might have misunderstood you a bit with my previous comment, so ignore it. Though do consider accepting this answer if it helped you. \$\endgroup\$ – Christer Nov 26 '15 at 13:21
  • \$\begingroup\$ As a possible addition, since version 3.3 OpenGL has Sampler Objects that provide a separate kind of object to store filter parameters which when bound override the ones stored in the texture object. \$\endgroup\$ – Lars Viklund Nov 26 '15 at 15:35
  • \$\begingroup\$ Your example code is excellent. Exactly what I wanted to know about samplers. Concise and to the point. Noticed typo in comment #3: "handle1" should be "handle2", "slot 0" should be "slot 1" \$\endgroup\$ – J MADISON Jun 19 '19 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.