2015-10-26 58 views
1

我想使用片段着色器使用着色器將RGB轉換爲YUV420P。使用着色器RGB到YUV

1,我想出了當前紋理顏色的座標。

2,我計算了4 Y並把它放在當前顏色上。

我的代碼:

precision mediump float; 
    varying vec2 vTextureCoord; 
    uniform sampler2D tex; 
    uniform float width; 
    uniform float height; 
    uniform float offset; 
    uniform vec3 scal; 

    void main(void) { 
     vec2 nowTxtPos = vTextureCoord; 
     vec2 size = vec2(width, height); 
    // y 
     if(nowTxtPos.y<0.25){ 
    // y1 postion 
      vec2 now_pos = nowTxtPos * size; 
      vec2 basePos = now_pos * vec2(4.0,4.0); 
      float addY = float(int(basePos.x/width)); 
      basePos.x = basePos.x - addY * width; 
      basePos.y += addY; 

      float y1,y2,y3,y4; 
      vec2 samplingPos = basePos/size; 
      vec4 texel = texture2D(tex, samplingPos); 
      y1 = dot(texel.rgb, scal); 
      y1 += offset; 

      basePos.x+=1.0; 
      samplingPos = basePos/size; 
      texel = texture2D(tex, samplingPos); 
      y2 = dot(texel.rgb, scal); 
      y2 += offset; 

      basePos.x+=1.0; 
      samplingPos = basePos/size; 
      texel = texture2D(tex, samplingPos); 
      y3 = dot(texel.rgb, scal); 
      y3 += offset; 

      basePos.x+=1.0; 
      samplingPos = basePos/size; 
      texel = texture2D(tex, samplingPos); 
      y4 = dot(texel.rgb, scal); 
      y4 += offset; 

      gl_FragColor = vec4(y1, y2, y3, y4); 
     } 
    } 

畫面(YUV420P):

picture

Enlarged post picture

它具有混疊。 我不知道爲什麼。

回答

1

我已經解決了。 因爲計算精度不夠。

編輯在這裏:

「precision mediump float;」 - >「高精度浮點數;」