<p>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. </p>

<pre><code>float inv_coord = v_coord - 1.0;
</code></pre>

<p>There is a simpler method, you can do <code>1.0 - v_coord</code> and do away with the abs.</p>

CC BY-SA 3.0
2016-06-02T12:33:37.630Z
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.
Rotem
CC BY-SA 3.0
2016-06-02T08:43:46.127Z
2016-06-02T08:43:46.127Z
ratchet freak
<p>I am loading a texture in a WebGL fragment shader (GLSL version 1.00).</p>

<p>This is my working code:</p>

<pre><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));
}
</code></pre>

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

<pre><code>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));
}
</code></pre>

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

CC BY-SA 3.0
2016-06-02T08:42:53.983Z
"fails to compile" is kinda useless without the error messages, check the `shaderInfoLog` and/or the `programInfoLog` after attempting the compile and link.
ratchet freak
<texture><glsl><mathematics><webgl><fragment-shader>
Why does substracting -1 in shader fail for a float data type?
q9f
# Why does substracting -1 in shader fail for a float data type?

(Asked by: username_0 on 2016-06-02)

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?

**Tags:** texture, glsl, mathematics, webgl, fragment-shader

> **Comments:**
> **username_1**: "fails to compile" is kinda useless without the error messages, check the `shaderInfoLog` and/or the `programInfoLog` after attempting the compile and link.  

---

## 1 answers

### Answer by username_1 on 2016-06-02. Score: 7 | Accepted answer

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.

> **Comments:**
> **username_2**: 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.  

---

