<blockquote>
  <p>Now this texture coordinates supposed to be updated for each vertex processed by vertex shader because i set the glVertexAttribDivisor() to 0 which means it should be.</p>
</blockquote>

<p>No, it doesn't.</p>

<p>A divisor of 0 means that the texture coordinate work <em>exactly</em> as if you hadn't called <code>glVertexAttribDivisor</code> for that attribute at all. 0 is not a special case; it's the <em>default</em>.</p>

<p>That means that the texture coordinate will be updated at the same rate as the position. So it will get the same value <em>for each instance</em>, exactly like it does for the positions.</p>

<p>There is no way to use instancing to get an attribute to always increase. It either is a per-instance attribute (changing more-or-less based on <code>gl_InstanceID</code>) or it is a per-vertex attribute (changing based on <code>gl_VertexID</code>). To get the per-instance behavior, you use a divisor 1 or greater. To get the per-vertex behavior, you use a divisor of 0.</p>

CC BY-SA 3.0
2016-12-25T23:40:06.227Z
Worth noting also that he could get the behavior he wants using a texture buffer or SSBO, and manually indexing it with `gl_InstanceID * vertex_count + gl_VertexID` in the shader.
Nathan Reed
CC BY-SA 3.0
2016-12-26T01:29:12.170Z
@NathanReed: Heh heh heh, no you can't. Instance arrays and `gl_InstanceID` have different interactions with [base instance rendering](https://www.khronos.org/opengl/wiki/Vertex_Rendering#Instancing). And by "different interactions", I mean `gl_InstanceID` *completely ignores* the base instance. Now, if this were *Vulkan*, `gl_InstanceIndex` could do that. But if you want to do this in GL, you [have to use an extension](https://www.opengl.org/registry/specs/ARB/shader_draw_parameters.txt)... for some reason.
Nicol Bolas
CC BY-SA 3.0
2016-12-27T00:40:04.973Z
OK, but "no you can't" is a bit too pessimistic—that's only a problem if you're using nonzero base instances to begin with; and if you are, you could also store the base instance in a uniform and account for it in the shader. :)
Nathan Reed
CC BY-SA 3.0
2016-12-25T20:37:04.397Z
2016-12-25T20:37:04.397Z
Nicol Bolas
<p>I want to draw a string of text with a single draw call (Instancing). I am reading a bitmap for each character texture. I want to pass the texture coordinates for each vertex of a quad (a single character). Now these texture coordinates are supposed to be updated for each vertex processed by the vertex shader because I set the <code>glVertexAttribDivisor()</code> to 0 which means it should be. But it's not happening.</p>

<p>Here's the source code where I am sending the data to the GPU...</p>

<pre><code>//TEXTURE COORDS
glBindBuffer(GL_ARRAY_BUFFER, mVBOTexCoords);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2)*mTexCoords.size(), mTexCoords.data(), GL_STATIC_DRAW);

glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, 0);

glVertexAttribDivisor(3,0);
</code></pre>

<p>and the vertex shader...</p>

<pre><code>#version 400 

out vec2 tex_coords;

layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 offset;
layout(location = 2) in vec2 scale;
layout(location = 3) in vec2 tex;

uniform mat4 projection;

void main()
{
    tex_coords = tex;

 vec4 position = vec4(pos,0.0f,1.0f);
     position.xy*=scale;
     position.xy+=offset;

     gl_Position = projection*position;

}
</code></pre>

<p>Sorry for the mess. I hope you get it. I am not getting why its not updating the texture coordninates for each vertex.</p>

CC BY-SA 3.0
2016-12-25T18:58:35.793Z
First step in any GL debugging problem: have you tried setting up [KHR_debug](http://renderingpipeline.com/2013/09/opengl-debugging-with-khr_debug/), and/or sprinkling your code with [`glGetError`](http://docs.gl/gl4/glGetError) calls, to see if the driver reports any issues?
Nathan Reed
CC BY-SA 3.0
2016-12-25T19:02:17.280Z
Thanks for commenting. Yes i am using debug output extension and it didnt report any problem. I have also checked each iteration of vertex shader with RenderDoc. Everything is updating according to their vertex divisor settings besides texture coordinates. This is weird.
videogamechef
CC BY-SA 3.0
2016-12-25T19:14:58.093Z
Hmm, what _are_ you getting for the texture coordinates in RenderDoc? When you say they're not updating correctly, what are they doing instead?
Nathan Reed
CC BY-SA 3.0
2016-12-25T19:24:37.480Z
I mean its showing texture coordinates of the first instance for all instances. I checked all the data before sending it to the gpu memory.  I will put the whole code and check it one more time later.
videogamechef
<opengl><glsl>
Texture coordinates are not updating according to the vertex divisor specified
videogamechef
# Texture coordinates are not updating according to the vertex divisor specified

(Asked by: username_0 on 2016-12-25)

I want to draw a string of text with a single draw call (Instancing). I am reading a bitmap for each character texture. I want to pass the texture coordinates for each vertex of a quad (a single character). Now these texture coordinates are supposed to be updated for each vertex processed by the vertex shader because I set the `glVertexAttribDivisor()` to 0 which means it should be. But it's not happening.

Here's the source code where I am sending the data to the GPU...

```
//TEXTURE COORDS
glBindBuffer(GL_ARRAY_BUFFER, mVBOTexCoords);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2)*mTexCoords.size(), mTexCoords.data(), GL_STATIC_DRAW);

glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, 0);

glVertexAttribDivisor(3,0);

```

and the vertex shader...

```
#version 400 

out vec2 tex_coords;

layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 offset;
layout(location = 2) in vec2 scale;
layout(location = 3) in vec2 tex;

uniform mat4 projection;

void main()
{
    tex_coords = tex;

 vec4 position = vec4(pos,0.0f,1.0f);
     position.xy*=scale;
     position.xy+=offset;

     gl_Position = projection*position;

}

```

Sorry for the mess. I hope you get it. I am not getting why its not updating the texture coordninates for each vertex.

**Tags:** opengl, glsl

> **Comments:**
> **username_1**: First step in any GL debugging problem: have you tried setting up [KHR_debug](http://renderingpipeline.com/2013/09/opengl-debugging-with-khr_debug/), and/or sprinkling your code with [`glGetError`](http://docs.gl/gl4/glGetError) calls, to see if the driver reports any issues?  
> **username_0**: Thanks for commenting. Yes i am using debug output extension and it didnt report any problem. I have also checked each iteration of vertex shader with RenderDoc. Everything is updating according to their vertex divisor settings besides texture coordinates. This is weird.  
> **username_1**: Hmm, what _are_ you getting for the texture coordinates in RenderDoc? When you say they're not updating correctly, what are they doing instead?  
> **username_0**: I mean its showing texture coordinates of the first instance for all instances. I checked all the data before sending it to the gpu memory.  I will put the whole code and check it one more time later.  

---

## 1 answers

### Answer by username_2 on 2016-12-25. Score: 5 | Accepted answer

> Now this texture coordinates supposed to be updated for each vertex processed by vertex shader because i set the glVertexAttribDivisor() to 0 which means it should be.

No, it doesn't.

A divisor of 0 means that the texture coordinate work *exactly* as if you hadn't called `glVertexAttribDivisor` for that attribute at all. 0 is not a special case; it's the *default*.

That means that the texture coordinate will be updated at the same rate as the position. So it will get the same value *for each instance*, exactly like it does for the positions.

There is no way to use instancing to get an attribute to always increase. It either is a per-instance attribute (changing more-or-less based on `gl_InstanceID`) or it is a per-vertex attribute (changing based on `gl_VertexID`). To get the per-instance behavior, you use a divisor 1 or greater. To get the per-vertex behavior, you use a divisor of 0.

> **Comments:**
> **username_1**: Worth noting also that he could get the behavior he wants using a texture buffer or SSBO, and manually indexing it with `gl_InstanceID * vertex_count + gl_VertexID` in the shader.  
> **username_2**: @username_1: Heh heh heh, no you can't. Instance arrays and `gl_InstanceID` have different interactions with [base instance rendering](https://www.khronos.org/opengl/wiki/Vertex_Rendering#Instancing). And by "different interactions", I mean `gl_InstanceID` *completely ignores* the base instance. Now, if this were *Vulkan*, `gl_InstanceIndex` could do that. But if you want to do this in GL, you [have to use an extension](https://www.opengl.org/registry/specs/ARB/shader_draw_parameters.txt)... for some reason.  
> **username_1**: OK, but "no you can't" is a bit too pessimistic—that's only a problem if you're using nonzero base instances to begin with; and if you are, you could also store the base instance in a uniform and account for it in the shader. :)  

---

