OpenGL 的着色器显示YUV2的图像

拜读雷神的 OpenGL播放YUV420P 感觉很牛强大,可惜就是不能实际使用,所以这里做了一个直接YUV2图像显示的例子!

基本知识参考: YUV2像素, OpenGL纹理有效格式, 着色器函数

基本原理:  

1. yuv2 是 422 所以需要取除当前像素外的前后三个像素

2. 根据三个像素与当前的奇偶值,分别取出YUV,

3. 与RGB进行转化

4. 完成! 简单吧!

自已截屏一张: 

 

基本代码大家自己学习这里只是给出实际使用的着色器:

	varying vec2 textureOut; \
	varying vec4 texturePos;	\
	uniform sampler2D tex_video;	\
	uniform float tex_offset_x;	\
	uniform float tex_offset_y;	\
	void main(void)											\
	{		
		vec3 yuv;													\
		vec3 rgb;													\
		float pos_offset ; \
		float img_pos_x = (texturePos.x + 1.0)*0.5;	\
		float img_pos_y = (texturePos.y + 1.0)*0.5;	\
		vec4 tmp_current = texture2D(tex_video, vec2(img_pos_x, img_pos_y ));	\
		vec4 tmp_before = texture2D(tex_video, vec2(img_pos_x - tex_offset_x, img_pos_y ));	\
		vec4 tmp_after = texture2D(tex_video, vec2(img_pos_x + tex_offset_x, img_pos_y ));	\
						
		float pixel_row = floor(img_pos_x / tex_offset_x + 0.5);	
		float pixel_row_helf = floor(pixel_row*0.5) * 2;	\
		float pixel_row_offset = pixel_row - pixel_row_helf;			\
		
		if (pixel_row_offset < 1.0) { 
			yuv.x = tmp_current.x; \
			yuv.y = tmp_current.y - 0.5; \
			yuv.z = tmp_after.y - 0.5; \		\
		} else { \
			yuv.x = tmp_current.x; \
			yuv.y = tmp_before.y - 0.5; 
			yuv.z = tmp_current.y - 0.5; 
		}
		rgb = mat3(1.0, 1.0, 1.0, 0, -0.39465, 2.03211, 1.13983, -0.58060, 0) * yuv;	\
		gl_FragColor = vec4(rgb, 1.0);					\
	}";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【可乐爱加冰】

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值