2
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ 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? $\endgroup$ – Tare Apr 27 '18 at 8:52
  • $\begingroup$ 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. $\endgroup$ – Jhovarie Guiang Apr 27 '18 at 9:07
  • $\begingroup$ 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. $\endgroup$ – Tare Apr 27 '18 at 10:09
1
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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).

$\endgroup$

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.