ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

OpenGL马赛克(八-2)

2022-06-16 01:01:27  阅读:175  来源: 互联网

标签:offset1 OpenGL 0.5 texture tex vec2 马赛克 TexCoord


上一节写了如何实现全部马赛克

本篇做的是部分马赛克:

需要理解的点:片元着色器,每一个片元(像素)执行一次片元着色器的程序;所以你在片元着色器中写的程序都是针对逐个像素的;

片元着色器代码:(本博客直接修改原图显示的像素,未进行图片叠加,融合,等马赛克方式)

 1 #version 330 core
 2     layout(location = 0) out vec4 FragColor;
 3     in vec2 TexCoord;
 4     uniform sampler2D dataY;
 5     uniform sampler2D dataU;
 6     uniform sampler2D dataV;
 7     vec3 yuv;
 8     vec3 rgb;
 9     //马赛克块大小
10     const vec2 mosaicSize = vec2(30.0, 30.0);
11     //图片大小
12     const vec2 TexSize = vec2(1920.0, 1080.0);
13 
14     void main()
15     {
16      //计算实际图像位置 换算成1920*1080的坐标
17     vec2 intXY = vec2(TexCoord.x * TexSize.x, TexCoord.y * TexSize.y);
18     //部分马赛克
19     if( TexCoord.x > 0.3 && TexCoord.x <0.6 &&  TexCoord.y > 0.3 && TexCoord.y< 0.6)
20     {
21         //floor(x)返回⼩于等于X的最⼤整数值.计算出整数个马赛克的坐标
22        vec2 XYMosaic = vec2(floor(intXY.x / mosaicSize.x) * mosaicSize.x, floor(intXY.y / mosaicSize.y) * mosaicSize.y);
23        //换算回纹理坐标
24        vec2 UVMosaic = vec2(XYMosaic.x / TexSize.x, XYMosaic.y / TexSize.y);
25 
26         yuv.x = texture2D(dataY,UVMosaic).r-0.0625;
27         yuv.y = texture2D(dataU,UVMosaic).r-0.5;
28         yuv.z = texture2D(dataV,UVMosaic).r-0.5;
29     }else
30     {
31         yuv.x = texture2D(dataY,TexCoord).r-0.0625;
32         yuv.y = texture2D(dataU,TexCoord).r-0.5;
33         yuv.z = texture2D(dataV,TexCoord).r-0.5;
34     } 
35    
36 
37        rgb = mat3(1,              1,      1,     
38                 0,       -0.18732, 1.8556,    
39                 1.57481, -0.46813,      0) * yuv;  
40                  
41         FragColor = vec4(rgb.x, rgb.y,rgb.z,1); 
42     };

 

有了局部马赛克,局部模糊如何实现呢?我们先做一个周围平均:

 

 中间那个像覆盖上一层纱一样的东西的片元着色器:

 1 #version 330 core
 2     layout(location = 0) out vec4 FragColor;
 3     in vec2 TexCoord;
 4     uniform sampler2D dataY;
 5     uniform sampler2D dataU;
 6     uniform sampler2D dataV;
 7     vec3 yuv;
 8     vec3 rgb;
 9     
10     
11 //片元着色器主要功能为通过重复执行(每片元一次),将3D物体中的图元光栅化后产生的每个片元的颜色等属性计算出来送入后继阶段
12    void main()
13    {
14     
15    if( TexCoord.x > 0.3 && TexCoord.x <0.6 &&  TexCoord.y > 0.3 && TexCoord.y< 0.6)
16    {
17       
18      vec2 tex_offset = 3.0 / textureSize(dataY, 0); //步长3 ,周围9个点
19      float result = texture(dataY, TexCoord).r ;
20      {
21        
22           result += texture(dataY, TexCoord + vec2(tex_offset.x * 1, 0.0)).r ;
23           result += texture(dataY, TexCoord - vec2(tex_offset.x * 1, 0.0)).r;
24           result += texture(dataY, TexCoord + vec2(0.0, tex_offset.y * 1)).r ;
25           result += texture(dataY, TexCoord - vec2(0.0, tex_offset.y * 1)).r;
26           result += texture(dataY, TexCoord + vec2(tex_offset.x * 1, tex_offset.y * 1)).r ;
27           result += texture(dataY, TexCoord - vec2(tex_offset.x * 1, tex_offset.y * 1)).r;
28           result += texture(dataY, TexCoord + vec2(tex_offset.x * 1,-tex_offset.y * 1)).r ;
29           result += texture(dataY, TexCoord + vec2(-tex_offset.x * 1, tex_offset.y * 1)).r;
30         
31          result =  result/9.0;
32 
33      }
34 
35   
36     vec2 tex_offset1 = 3.0 / textureSize(dataU, 0); 
37     float result1 = (texture(dataU, TexCoord).r-0.5) ; 
38    
39     {
40           result1 += texture(dataU, TexCoord + vec2(tex_offset1.x * 1, 0.0)).r-0.5 ;
41           result1 += texture(dataU, TexCoord - vec2(tex_offset1.x * 1, 0.0)).r-0.5;
42           result1 += texture(dataU, TexCoord + vec2(0.0, tex_offset1.y * 1)).r-0.5 ;
43           result1 += texture(dataU, TexCoord - vec2(0.0, tex_offset1.y * 1)).r-0.5;
44           result1 += texture(dataU, TexCoord + vec2(tex_offset1.x * 1, tex_offset1.y * 1)).r-0.5 ;
45           result1 += texture(dataU, TexCoord - vec2(tex_offset1.x * 1, tex_offset1.y * 1)).r-0.5;
46           result1 += texture(dataU, TexCoord + vec2(tex_offset1.x * 1,-tex_offset1.y * 1)).r-0.5 ;
47           result1 += texture(dataU, TexCoord + vec2(-tex_offset1.x * 1, tex_offset1.y * 1)).r-0.5;
48         
49          result1 =  result1/9.0;
50 
51     }
52 
53     vec2 tex_offset2 = 1.0 / textureSize(dataV, 0);
54     float result2 = (texture(dataV, TexCoord).r-0.5) ; 
55    
56     {
57           result2 += texture(dataV, TexCoord + vec2(tex_offset1.x * 1, 0.0)).r -0.5;
58           result2 += texture(dataV, TexCoord - vec2(tex_offset1.x * 1, 0.0)).r-0.5;
59           result2 += texture(dataV, TexCoord + vec2(0.0, tex_offset1.y * 1)).r -0.5;
60           result2 += texture(dataV, TexCoord - vec2(0.0, tex_offset1.y * 1)).r-0.5;
61           result2 += texture(dataV, TexCoord + vec2(tex_offset1.x * 1, tex_offset1.y * 1)).r-0.5 ;
62           result2 += texture(dataV, TexCoord - vec2(tex_offset1.x * 1, tex_offset1.y * 1)).r-0.5;
63           result2 += texture(dataV, TexCoord + vec2(tex_offset1.x * 1,-tex_offset1.y * 1)).r-0.5 ;
64           result2 += texture(dataV, TexCoord + vec2(-tex_offset1.x * 1, tex_offset1.y * 1)).r-0.5;
65         
66          result2 =  result2/9.0;
67     
68     }
69 
70        yuv.x =result; 
71        yuv.y = result1;
72        yuv.z = result2;
73 
74 
75    }else
76     {
77         yuv.x = texture2D(dataY,TexCoord).r-0.0625;
78         yuv.y = texture2D(dataU,TexCoord).r-0.5;
79         yuv.z = texture2D(dataV,TexCoord).r-0.5;
80 
81     } 
82     
83 
84          rgb = mat3(1,              1,      1,     
85                 0,       -0.18732, 1.8556,    
86                 1.57481, -0.46813,      0) * yuv; 
87          
88         FragColor = vec4(rgb.x, rgb.y,rgb.z,1); 
89     };

 完整代码见马赛克1:OpenGL马赛克(八) - 邗影 - 博客园 (cnblogs.com)

标签:offset1,OpenGL,0.5,texture,tex,vec2,马赛克,TexCoord
来源: https://www.cnblogs.com/8335IT/p/16380494.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有