<p>What you do is shifting the texture coordinates. This will move the texture on your polygon, and depending on your settings it will handle the borders which are now probably wrong. however, this will not move your polygon - if you want to move an entire object, you should not do that within the shader. you do that via modifying your model matrix.</p>

<p>I assume you have a rectangle, and you want to move it by <code>0.2</code> along the x axis. Then you would edit the correct element of your model matrix for the rectangle. </p>

<pre><code>//depending on your setup, you may need [0][3] rather than [3][0]
modelMatrix[3][0] += 0.2;
//now upload the matrix to your shader
//the entire object will be moved by 0.2 units 
</code></pre>

<p>If that is what you wanted, then you need to remove the fragment shader position calculation.</p>

<p>On a sidenote: I had problems with using <code>+=</code> in a shader the other day, so it might just be that this doesn't compile. Try using <code>position = position + 0.2;</code> instead.</p>

CC BY-SA 3.0
2018-04-27T09:25:48.270Z
2018-04-27T09:25:48.270Z
Tare
<p>When you use <code>GL_TEXTURE_RECTANGLE</code> in your source code and <code>texture2DRect()</code> in your shader, you're telling OpenGL that you want to represent the texture coordinates as 0 to width of texture and 0 to height of texture rather than using normalized (0-1) coordinates in both directions, which you would get with <code>GL_TEXTURE_2D</code> and <code>texture2D()</code>. A shift of 0.2 might be hard to notice on a large texture, as it's less than 1 texel. If you want to move it by 20% of the width, you need to pass in the texture width and height to the shader and calculate it (or just pass in what 20% of the width is).</p>

CC BY-SA 3.0
2018-04-28T00:51:45.160Z
2018-04-28T04:01:58.617Z
2018-04-28T04:01:58.617Z
user1118321
<p>Crossposted <a href="https://forum.openframeworks.cc/t/shader-cannot-move-texture-x-y-location/29395" rel="nofollow noreferrer">https://forum.openframeworks.cc/t/shader-cannot-move-texture-x-y-location/29395</a>
Hello I am trying to move the x y location of the texture using this code but it doest not work.</p>

<pre><code>//Fragment shader
#version 120
#extension GL_ARB_texture_rectangle : enable
#extension GL_EXT_gpu_shader4 : enable

uniform sampler2DRect texture0;

void main(){
    //Getting coordinates of the current pixel in texture
    vec2 pos = gl_TexCoord[0].xy;

    // pos.x = (pos.x/0.5); //working
    pos.x += 0.2 //&lt;-- not working how to fixed this?

    //Getting the pixel color from the texture texture0 in pos
    vec4 color = texture2DRect(texture0, pos);

    //Changing the color - invert red, green, blue components
    //color.r = 1.0 - color.r;
    color.g = 1.0 - color.g; //working..
    //color.b = 1.0 - color.b;

    //Output the color of shader
    gl_FragColor = color;
}
</code></pre>

<hr>

<pre><code>//Vertex shader
#version 120
#extension GL_ARB_texture_rectangle : enable
#extension GL_EXT_gpu_shader4 : enable

void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_FrontColor = gl_Color;
}
</code></pre>

<p>whats wrong with this code?
pos.x += 0.2 //not working how to fixed this?</p>

<p>////////////////////////////////////////////////////////////////////////</p>

<p>I found the solution  pos.x += 0.2 already working but it is not visible.
if I tried bigger number like  pos.x += 100 it is clear that the textures moves.</p>

CC BY-SA 3.0
2018-04-27T08:52:57.290Z
it'd be really helpful, if you added a screenshot and/or explanation of what is happening and of what you want to achieve. `move x, y location of the texture` as in move the object that the texture is applied on? offsetting the texture on the object?
Tare
CC BY-SA 3.0
2018-04-27T09:07:58.020Z
its like moving the object... I am trying a code to set the x,y,w,h of the texture (rectangle) I am expecting that I can used this . pos.x += 0.2 because I already used this. pos.x += 0.2  it works if the vertex shader dont used this gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; and texture2DRect .. if I am not mistaken it works on texture2D but I cannot modify my fragment and vertex shader because it is necessary for openframeworks.
Jhovarie Guiang
CC BY-SA 3.0
2018-04-27T10:09:55.633Z
`I found the solution pos.x += 0.2 already working but it is not visible. if I tried bigger number like pos.x += 100 it is clear that the textures moves.` You should read up on how texture coordinates work. They are within the range of [0, 1]. They can repeat outside that - but in that case, the coordinates (0.2, 0.2) and (100.2, 50.2) are still the same.
Tare
<shader><fragment-shader>
Shader cannot move texture x, y location
Jhovarie Guiang
# Shader cannot move texture x, y location

(Asked by: username_0 on 2018-04-27)

Crossposted <https://forum.openframeworks.cc/t/shader-cannot-move-texture-x-y-location/29395>
Hello I am trying to move the x y location of the texture using this code but it doest not work.

```
//Fragment shader
#version 120
#extension GL_ARB_texture_rectangle : enable
#extension GL_EXT_gpu_shader4 : enable

uniform sampler2DRect texture0;

void main(){
    //Getting coordinates of the current pixel in texture
    vec2 pos = gl_TexCoord[0].xy;

    // pos.x = (pos.x/0.5); //working
    pos.x += 0.2 //<-- not working how to fixed this?

    //Getting the pixel color from the texture texture0 in pos
    vec4 color = texture2DRect(texture0, pos);

    //Changing the color - invert red, green, blue components
    //color.r = 1.0 - color.r;
    color.g = 1.0 - color.g; //working..
    //color.b = 1.0 - color.b;

    //Output the color of shader
    gl_FragColor = color;
}

```

---

```
//Vertex shader
#version 120
#extension GL_ARB_texture_rectangle : enable
#extension GL_EXT_gpu_shader4 : enable

void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_FrontColor = gl_Color;
}

```

whats wrong with this code?
pos.x += 0.2 //not working how to fixed this?

////////////////////////////////////////////////////////////////////////

I found the solution pos.x += 0.2 already working but it is not visible.
if I tried bigger number like pos.x += 100 it is clear that the textures moves.

**Tags:** shader, fragment-shader

> **Comments:**
> **username_1**: it'd be really helpful, if you added a screenshot and/or explanation of what is happening and of what you want to achieve. `move x, y location of the texture` as in move the object that the texture is applied on? offsetting the texture on the object?  
> **username_0**: its like moving the object... I am trying a code to set the x,y,w,h of the texture (rectangle) I am expecting that I can used this . pos.x += 0.2 because I already used this. pos.x += 0.2  it works if the vertex shader dont used this gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; and texture2DRect .. if I am not mistaken it works on texture2D but I cannot modify my fragment and vertex shader because it is necessary for openframeworks.  
> **username_1**: `I found the solution pos.x += 0.2 already working but it is not visible. if I tried bigger number like pos.x += 100 it is clear that the textures moves.` You should read up on how texture coordinates work. They are within the range of [0, 1]. They can repeat outside that - but in that case, the coordinates (0.2, 0.2) and (100.2, 50.2) are still the same.  

---

## 2 answers

### Answer by username_1 on 2018-04-27. Score: 1

What you do is shifting the texture coordinates. This will move the texture on your polygon, and depending on your settings it will handle the borders which are now probably wrong. however, this will not move your polygon - if you want to move an entire object, you should not do that within the shader. you do that via modifying your model matrix.

I assume you have a rectangle, and you want to move it by `0.2` along the x axis. Then you would edit the correct element of your model matrix for the rectangle.

```
//depending on your setup, you may need [0][3] rather than [3][0]
modelMatrix[3][0] += 0.2;
//now upload the matrix to your shader
//the entire object will be moved by 0.2 units 

```

If that is what you wanted, then you need to remove the fragment shader position calculation.

On a sidenote: I had problems with using `+=` in a shader the other day, so it might just be that this doesn't compile. Try using `position = position + 0.2;` instead.


---

### Answer by username_2 on 2018-04-28. Score: 0

When you use `GL_TEXTURE_RECTANGLE` in your source code and `texture2DRect()` in your shader, you're telling OpenGL that you want to represent the texture coordinates as 0 to width of texture and 0 to height of texture rather than using normalized (0-1) coordinates in both directions, which you would get with `GL_TEXTURE_2D` and `texture2D()`. A shift of 0.2 might be hard to notice on a large texture, as it's less than 1 texel. If you want to move it by 20% of the width, you need to pass in the texture width and height to the shader and calculate it (or just pass in what 20% of the width is).


---

