1
$\begingroup$

I am loading a texture in a WebGL fragment shader (GLSL version 1.00).

This is my working code:

precision highp float;
uniform sampler2D u_texture;
varying float v_coord;

void main(void) {
  float inv_coord;
  gl_FragColor = texture2D(u_texture, vec2(v_coord, 0.5));
}

v_coord holds the texture coordinates for each vertex. What I'm trying now, is to invert the texture coordinates. I thought about something like abs(v_coord - 1). However, whenever I try to substract - 1 from v_coord, my shader fails to compile.

precision highp float;
uniform sampler2D u_texture;
varying float v_coord;

void main(void) {
  float inv_coord = v_coord - 1; // fails here
  inv_coord = abs(inv_coord);
  gl_FragColor = texture2D(u_texture, vec2(inv_coord, 0.5));
}

Why does substracting minus one in fragment shader fail for a float data type? How to invert the texture coordinate value correctly?

$\endgroup$
  • 3
    $\begingroup$ "fails to compile" is kinda useless without the error messages, check the shaderInfoLog and/or the programInfoLog after attempting the compile and link. $\endgroup$ – ratchet freak Jun 2 '16 at 8:42
7
$\begingroup$

Without seeing the error message I can't be sure but I think it's failing on the 1 being int instead of a float.

float inv_coord = v_coord - 1.0;

There is a simpler method, you can do 1.0 - v_coord and do away with the abs.

$\endgroup$
  • 3
    $\begingroup$ More annoyingly, this fails on only some drivers/cards, which makes it hard to debug in the wild. As best practice, never rely on implicit casts to float in GLSL, always specify the decimal dot. $\endgroup$ – Rotem Jun 2 '16 at 12:33

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.