1
$\begingroup$

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.

$\endgroup$

1 Answer 1

3
$\begingroup$

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

$\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.