简体   繁体   English

通过glsl纹理处理单通道图像

[英]Process single channel image by glsl texture

Sharpening codes developed by glsl( source ) 锐化由glsl( )开发的代码

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? 适用于4通道图像,如果我希望它可用于单个通道怎么办? I can't find something like vec1, do I have other solution rather than transform a single channel images to a 3 channels image? 我找不到vec1之类的东西,是否有其他解决方案,而不是将单通道图像转换为3通道图像?

Anytime you access a non-shadow sampler, regardless of how many channels it's actual format has, you will get a g vec4 (where g can be nothing for float, i for integer, and u for unsigned integer). 每当您访问一个非阴影采样器时,无论它的实际格式有多少个通道,您都将得到g vec4(其中g不能代表浮点数, i代表整数, u代表无符号整数)。

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. 除非您要的组件是Alpha,否则您将获得1。

Don't forget: you can always create new values with the texture object's swizzle setting . 别忘了:您始终可以使用纹理对象的swizzle设置创建新值。 So with a simple swizzle mask, you can make a texture that only stores 1 value look like it stores 4: 因此,使用一个简单的蒙版蒙版,可以使仅存储1个值的纹理看起来像存储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. 就像旧的GL_INTENSITY纹理一样,这会将纹理的红色通道广播到所有4个组件。 You can swap them around so it only comes out of the alpha: 您可以交换它们,使其仅出现在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: 或者,也许您希望所有其他渠道为1:

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

The sampler object is always the same. 采样器对象始终是相同的。 Just use the first n channels of color (eg color.r for a single channel GL_RED texture). 只使用第n的通道color (例如color.r一个单信道GL_RED纹理)。

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) 您可以使用texture2D(sampler, gl_TexCoord[0].st + offset[i]).r来获取红色通道或其他通道的g / b(a表示alpha)

on the other hand you can use a float value for sum like 另一方面,您可以将浮点值用于求和

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM