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];
}
}
}
效果图:
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];
}
}
}
相关文章推荐
- 解决Vista系统OpenGL驱动问题的方法整理
- Delphi下OpenGL2d绘图之画四边形的方法
- Delphi下OpenGL2d绘图之画点的方法
- Delphi下OpenGL2d绘图之初始化流程详解
- Delphi使用OpenGL2d绘图之画图片Bmp的方法
- 通过OpenGL ES混合模式缩放视频缓冲区来适应显示尺寸
- VC运用OPENGL加载BMP纹理图的实现方法汇总
- java实现OpenGL ES纹理映射的方法
- java基于OpenGL ES实现渲染实例
- android调用GLES20.glGenerateMipmap报错glError 1280
- OpenGL坐标系介绍
- 【翻译】安卓opengl ES教程之四——添加颜色
- 翻译:GLSL的顶点位移贴图
- linux下opengl的安装(with qt)
- OpenGL超级宝典笔记——显示列表
- OpenGL超级宝典笔记——顶点数组
- OpenGL生成轮廓
- OpenGL超级宝典笔记——性能比较
- OpenGL超级宝典笔记——顶点缓冲区对象
- OpenGL超级宝典笔记——选择