您的位置:首页 > 运维架构

Shader特效——“Sephia等效果”的实现 【GLSL】

2016-08-26 01:01 621 查看
参考自:http://www.open-open.com/lib/view/open1328487204203.html

效果图:



GLSL代码:

//#version 330

uniform sampler2D quadTexture;

int filterNumber = 2;
vec2 vTex = gl_FragCoord.xy / vec2(512., 512.);

void main(void)
{
float step = 1.;
vec2 tcOffset[25]; // Texture coordinate offsets
float xInc = step / 512.;
float yInc = step / 512.;

for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
tcOffset[(((i*5)+j)*2)+0] = (-2.0 * xInc) + (float(i) * xInc);
tcOffset[(((i*5)+j)*2)+1] = (-2.0 * yInc) + (float(j) * yInc);
}
}
// ===========================================
// 标准
if (filterNumber == 0)
{
gl_FragColor = texture2D(quadTexture, vTex);
}

// 灰度图
if (filterNumber == 1)
{
// Convert to greyscale using NTSC weightings
float grey = dot(texture2D(quadTexture, vTex).rgb, vec3(0.299, 0.587, 0.114));

gl_FragColor = vec4(grey, grey, grey, 1.0);
}

// Sepia tone
if (filterNumber == 2)
{
// Convert to greyscale using NTSC weightings
float grey = dot(texture2D(quadTexture, vTex).rgb, vec3(0.299, 0.587, 0.114));

// Play with these rgb weightings to get different tones.
// (As long as all rgb weightings add up to 1.0 you won't lighten or darken the image)
gl_FragColor = vec4(grey * vec3(1.2, 1.0, 0.8), 1.0);
}

// 反色
if (filterNumber == 3)
{
vec4 texMapColour = texture2D(quadTexture, vTex);

gl_FragColor = vec4(1.0 - texMapColour.rgb, 1.0);
}

// 高斯滤波
if (filterNumber == 4)
{
vec4 sample[25];

for (int i = 0; i < 25; i++)
{
// Sample a grid around and including our texel
sample[i] = texture2D(quadTexture, vTex + tcOffset[i]);
}

// Gaussian weighting:
// 1 4 7 4 1
// 4 16 26 16 4
// 7 26 41 26 7 / 273 (i.e. divide by total of weightings)
// 4 16 26 16 4
// 1 4 7 4 1

gl_FragColor = (
(1.0 * (sample[0] + sample[4] + sample[20] + sample[24])) +
(4.0 * (sample[1] + sample[3] + sample[5] + sample[9] + sample[15] + sample[19] + sample[21] + sample[23])) +
(7.0 * (sample[2] + sample[10] + sample[14] + sample[22])) +
(16.0 * (sample[6] + sample[8] + sample[16] + sample[18])) +
(26.0 * (sample[7] + sample[11] + sample[13] + sample[17])) +
(41.0 * sample[12])
) / 273.0;

}

// 均值滤波
if (filterNumber == 5)
{
vec4 samples[25];
for (int i = 0; i < 25; i++)
{
// Sample a grid around and including our texel
samples[i] = texture2D(quadTexture, vTex + tcOffset[i]);
}

vec4 color;
for(int i=0; i<25; i++)
color += samples[i];

gl_FragColor = color/25.;
}

// 锐化
if (filterNumber == 6)
{
vec4 sample[25];

for (int i = 0; i < 25; i++)
{
// Sample a grid around and including our texel
sample[i] = texture2D(quadTexture, vTex + tcOffset[i]);
}

// Sharpen weighting:
// -1 -1 -1 -1 -1
// -1 -1 -1 -1 -1
// -1 -1 25 -1 -1
// -1 -1 -1 -1 -1
// -1 -1 -1 -1 -1

gl_FragColor = 25.0 * sample[12];

for (int i = 0; i < 25; i++)
{
if (i != 12)
gl_FragColor -= sample[i];
}
}

// 膨胀
if (filterNumber == 7)
{
vec4 sample[25];
vec4 maxValue = vec4(0.0);

for (int i = 0; i < 25; i++)
{
// Sample a grid around and including our texel
sample[i] = texture2D(quadTexture, vTex + tcOffset[i]);

// Keep the maximum value
maxValue = max(sample[i], maxValue);
}

gl_FragColor = maxValue;
}

// 腐蚀
if (filterNumber == 8)
{
vec4 sample[25];
vec4 minValue = vec4(1.0);

for (int i = 0; i < 25; i++)
{
// Sample a grid around and including our texel
sample[i] = texture2D(quadTexture, vTex + tcOffset[i]);

// Keep the minimum value
minValue = min(sample[i], minValue);
}

gl_FragColor = minValue;
}

// 拉普拉斯 边界检测 (和锐化效果十分相似!)
if (filterNumber == 9)
{
vec4 sample[25];

for (int i = 0; i < 25; i++)
{
// Sample a grid around and including our texel
sample[i] = texture2D(quadTexture, vTex + tcOffset[i]);
}

// Laplacian weighting:
// -1 -1 -1 -1 -1
// -1 -1 -1 -1 -1
// -1 -1 24 -1 -1
// -1 -1 -1 -1 -1
// -1 -1 -1 -1 -1

gl_FragColor = 24.0 * sample[12];

for (int i = 0; i < 25; i++)
{
if (i != 12)
gl_FragColor -= sample[i];
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  OpenGL