首页 > 代码库 > 基于Qt的OpenGL可编程管线学习(18)- 平滑、锐化、边缘检测
基于Qt的OpenGL可编程管线学习(18)- 平滑、锐化、边缘检测
1、平滑
shader
// 平滑 uniform sampler2D U_MainTexture; uniform sampler2D U_SubTexture; varying vec2 M_coord; void main() { vec4 color = vec4(0.0); int coreSize = 3; float texelOffset = 1 / 300.0; float kernel[9]; kernel[6] = 1; kernel[7] = 1; kernel[8] = 1; kernel[3] = 1; kernel[4] = 1; kernel[5] = 1; kernel[0] = 1; kernel[1] = 1; kernel[2] = 1; int index = 0; for(int y = 0; y<coreSize; y++) { for(int x=0; x<coreSize; x++) { vec4 currentColor=texture2D(U_MainTexture, M_coord+vec2((-1+x)*texelOffset,(-1+y)*texelOffset)); color += currentColor * kernel[index++]; } } color /= 9.0; gl_FragColor = color; }
效果图
2、锐化
shader
// 锐化 uniform sampler2D U_MainTexture; uniform sampler2D U_SubTexture; varying vec2 M_coord; void main() { vec4 color = vec4(0.0); int coreSize = 3; float texelOffset = 1 / 300.0; float kernel[9]; kernel[6] = 0; kernel[7] = -1; kernel[8] = 0; kernel[3] = -1; kernel[4] = 4; kernel[5] = -1; kernel[0] = 0; kernel[1] = -1; kernel[2] = 0; int index = 0; for(int y = 0; y<coreSize; y++) { for(int x=0; x<coreSize; x++) { vec4 currentColor=texture2D(U_MainTexture, M_coord+vec2((-1+x)*texelOffset,(-1+y)*texelOffset)); color += currentColor * kernel[index++]; } } //color /= 9.0; gl_FragColor = 4.0 * color + texture2D(U_MainTexture, M_coord); }
效果图
3、边缘检测
shader
// 边缘检测 uniform sampler2D U_MainTexture; uniform sampler2D U_SubTexture; varying vec2 M_coord; void main() { vec4 color = vec4(0.0); int coreSize = 3; float texelOffset = 1 / 300.0; float kernel[9]; kernel[6] = 0; kernel[7] = 1; kernel[8] = 0; kernel[3] = 1; kernel[4] = -4; kernel[5] = 1; kernel[0] = 0; kernel[1] = 1; kernel[2] = 0; int index = 0; for(int y = 0; y<coreSize; y++) { for(int x=0; x<coreSize; x++) { vec4 currentColor=texture2D(U_MainTexture, M_coord+vec2((-1+x)*texelOffset,(-1+y)*texelOffset)); color += currentColor * kernel[index++]; } } //color /= 9.0; gl_FragColor = 4.0 * color + texture2D(U_MainTexture, M_coord); }
效果图
本文出自 “不会飞的纸飞机” 博客,请务必保留此出处http://douzhq.blog.51cto.com/12552184/1931094
基于Qt的OpenGL可编程管线学习(18)- 平滑、锐化、边缘检测
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。