StereoMatching
StereoMatching

Reputation: 5019

Process single channel image by glsl texture

Sharpening codes developed by glsl(source)

uniform sampler2D sampler;

uniform vec2 offset[9];

uniform int kernel[9];

void main()
{
vec4 sum = vec4(0.0);
int i;

for (i = 0; i < 9; i++) {
vec4 color = texture2D(sampler, gl_TexCoord[0].st + offset[i]);
sum += color * kernel[i];
}

gl_FragColor = sum
}

works on 4 channels image, what if I want it to work on a single channel? I can't find something like vec1, do I have other solution rather than transform a single channel images to a 3 channels image?

Upvotes: 2

Views: 4753

Answers (3)

Nicol Bolas
Nicol Bolas

Reputation: 473352

Anytime you access a non-shadow sampler, regardless of how many channels it's actual format has, you will get a gvec4 (where g can be nothing for float, i for integer, and u for unsigned integer).

Any values that aren't in the texture's image format (ie: if you ask for the green component from a texture that has only red) will be filled in with zeros. Unless the component you ask for is alpha, in which case you get 1.

Don't forget: you can always create new values with the texture object's swizzle setting. So with a simple swizzle mask, you can make a texture that only stores 1 value look like it stores 4:

GLint swizzleMask[] = {GL_RED, GL_RED, GL_RED, GL_RED};
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);

This broadcasts the red channel of the texture to all 4 components, just like the old GL_INTENSITY texture. You can swap them around so it only comes out of the alpha:

GLint swizzleMask[] = {GL_ZERO, GL_ZERO, GL_ZERO, GL_RED};
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);

Or maybe you want all the other channels to be 1:

GLint swizzleMask[] = {GL_ONE, GL_ONE, GL_ONE, GL_RED};
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);

Upvotes: 10

glethien
glethien

Reputation: 2471

you can use texture2D(sampler, gl_TexCoord[0].st + offset[i]).r to get the red channel or g / b for the others (a for alpha)

on the other hand you can use a float value for sum like

sum += texture2D(sampler, gl_TexCoord[0].st + offset[i]).r ;

Upvotes: 2

Joe
Joe

Reputation: 6757

The sampler object is always the same. Just use the first n channels of color (e.g. color.r for a single channel GL_RED texture).

Upvotes: 3

Related Questions