<p>What <code>GL_LUMINANCE</code> as a texture format actually does is store one component of data per pixel that is used for the red, green and blue color components. So, a classic greyscale image (It doesn't use it for the alpha component, though, that's why there was also <code>GL_LUMINANCE_ALPHA</code> for two compnent storage and <code>GL_INTENSITY</code> that broadcasts it to all components).</p>
<p>When using shaders you don't actually need this distinction anymore, though, since you can do the swizzling yourself in the shader. So you just use a proper typed 1-component internal format, most probably <code>GL_R8</code> (i.e. 8-bit normalized fixed point red) in your case, but depending on your exact data type requirements really. And for setting it you would (in that case) use <code>GL_RED</code> as the format of the data in your RAM storage. So something like:</p>
<pre><code>glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, &lt;width&gt;, &lt;height&gt;, 0, GL_RED, &lt;type&gt;, &lt;data&gt;);
</code></pre>
<p>And in the shader you can do with that red color component whatever you want. If you just need a single value per pixel, you use that. If you need a grey color (i.e. <code>GL_LUMINANCE</code> behaviour), you swizzle it to the RGB components, and if you need it as color and opacity, you swizzle it into all four components. Or whatever else you want to do.</p>
<pre><code>float data = texture(...).r;
vec4 luminance = texture(...).rrra;
vec4 intensity = texture(...).rrrr;
</code></pre>
<hr />
<p>Though, this requires your shader to be somewhat aware of what texture you're working with. But if you want to use a more generic shader working with a 4-component color from an arbitrary texture, this might be too much of a hassle. In this case what you can also do is have the <a href="https://www.khronos.org/opengl/wiki/Texture#Swizzle_mask" rel="nofollow noreferrer">texture itself control its own swizzling behaviour</a>. This is an OpenGL 3.3+ feature and can be set with <code>glTexParameter</code> and for good old <code>GL_LUMIMANCE</code> behaviour this would be:</p>
<pre><code>glTexParameter(..., GL_TEXTURE_SWIZZLE_R, GL_RED);
glTexParameter(..., GL_TEXTURE_SWIZZLE_G, GL_RED);
glTexParameter(..., GL_TEXTURE_SWIZZLE_B, GL_RED);
glTexParameter(..., GL_TEXTURE_SWIZZLE_A, GL_ONE);
</code></pre>
<p>So then you can retrieve a normal 4-component color from your texture in the shader and get the 1-component it's storing conveniently broadcast into the RGBA components of the result.</p>

CC BY-SA 4.0
2019-02-15T11:12:46.580Z
2022-09-07T15:59:06.317Z
2022-09-07T15:59:06.317Z
Christian Rau
<p>I've been following these tutorials and i can't figure out how to send data to shaders using textures.</p>

<p><a href="https://en.wikibooks.org/wiki/OpenGL_Programming/Scientific_OpenGL_Tutorial_02" rel="nofollow noreferrer">https://en.wikibooks.org/wiki/OpenGL_Programming/Scientific_OpenGL_Tutorial_02</a></p>

<p>In this tutorial they are using GL_LUMINANCE which has been removed from OpenGL 3.1 and above. I was wondering what should i use instead of GL_LUMINANCE? I tried GL_RGB, GL_RED but i was unable to retrieve the values.</p>

<opengl><shader><texture><c++>
OpenGL Unable to retrieve the values of data sent to shader using textures
Moondefender
# OpenGL Unable to retrieve the values of data sent to shader using textures

(Asked by: username_0 on 2019-02-15)

I've been following these tutorials and i can't figure out how to send data to shaders using textures.

<https://en.wikibooks.org/wiki/OpenGL_Programming/Scientific_OpenGL_Tutorial_02>

In this tutorial they are using GL\_LUMINANCE which has been removed from OpenGL 3.1 and above. I was wondering what should i use instead of GL\_LUMINANCE? I tried GL\_RGB, GL\_RED but i was unable to retrieve the values.

**Tags:** opengl, shader, texture, c++


---

## 1 answers

### Answer by username_1 on 2019-02-15. Score: 3

What `GL_LUMINANCE` as a texture format actually does is store one component of data per pixel that is used for the red, green and blue color components. So, a classic greyscale image (It doesn't use it for the alpha component, though, that's why there was also `GL_LUMINANCE_ALPHA` for two compnent storage and `GL_INTENSITY` that broadcasts it to all components).

When using shaders you don't actually need this distinction anymore, though, since you can do the swizzling yourself in the shader. So you just use a proper typed 1-component internal format, most probably `GL_R8` (i.e. 8-bit normalized fixed point red) in your case, but depending on your exact data type requirements really. And for setting it you would (in that case) use `GL_RED` as the format of the data in your RAM storage. So something like:

```
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, <width>, <height>, 0, GL_RED, <type>, <data>);

```

And in the shader you can do with that red color component whatever you want. If you just need a single value per pixel, you use that. If you need a grey color (i.e. `GL_LUMINANCE` behaviour), you swizzle it to the RGB components, and if you need it as color and opacity, you swizzle it into all four components. Or whatever else you want to do.

```
float data = texture(...).r;
vec4 luminance = texture(...).rrra;
vec4 intensity = texture(...).rrrr;

```

---

Though, this requires your shader to be somewhat aware of what texture you're working with. But if you want to use a more generic shader working with a 4-component color from an arbitrary texture, this might be too much of a hassle. In this case what you can also do is have the [texture itself control its own swizzling behaviour](https://www.khronos.org/opengl/wiki/Texture#Swizzle_mask). This is an OpenGL 3.3+ feature and can be set with `glTexParameter` and for good old `GL_LUMIMANCE` behaviour this would be:

```
glTexParameter(..., GL_TEXTURE_SWIZZLE_R, GL_RED);
glTexParameter(..., GL_TEXTURE_SWIZZLE_G, GL_RED);
glTexParameter(..., GL_TEXTURE_SWIZZLE_B, GL_RED);
glTexParameter(..., GL_TEXTURE_SWIZZLE_A, GL_ONE);

```

So then you can retrieve a normal 4-component color from your texture in the shader and get the 1-component it's storing conveniently broadcast into the RGBA components of the result.


---

