我有一個代碼,我接收YUV通道,並使用OpenGLES繪製它們。基本上,我有一個着色器將它們結合在一起。在OpenGLES中組合多個着色器
我想一個銳化濾鏡添加到結果(使用下面的例子:http://igortrindade.wordpress.com/2010/04/23/fun-with-opengl-and-shaders/)
我不知道如何運行的實際結果的另一個着色器(因爲我想以後運行它我以前的着色器將所有通道合併到一個幀中)。
我當前的代碼看起來像這樣:
glUniform1i(texLum, 0);
glUniform1i(texU, 1);
glUniform1i(texV, 2);
glEnableVertexAttribArray(positionLoc);
glVertexAttribPointer(positionLoc,
4,
GL_FLOAT,
GL_FALSE,
0,
&vertices[0]);
glEnableVertexAttribArray(texCoordLoc);
glVertexAttribPointer(texCoordLoc,
2,
GL_FLOAT,
GL_FALSE,
0,
&texCoords[0]);
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_BYTE, &indices[0]);
我想我需要在最後一行(glDrawElements)前右添加新的着色器,但我不知道如何調用它。
我着色器看起來是這樣的:
static char const *frag =
"uniform lowp sampler2D texLum; \n"
"uniform lowp sampler2D texU; \n"
"uniform lowp sampler2D texV; \n"
"varying mediump vec2 texCoordAtFrag; \n"
"void main() { \n"
" lowp float Y = texture2D(texLum, texCoordAtFrag).r; \n"
" lowp float U = texture2D(texU, texCoordAtFrag).r; \n"
" lowp float V = texture2D(texV, texCoordAtFrag).r; \n"
" lowp float R = 1.164 * (Y - 16.0/256.0) + 1.596 * (V - 0.5); \n"
" lowp float G = 1.164 * (Y - 16.0/256.0) - 0.813 * (V - 0.5) - 0.391 * (U - 0.5); \n"
" lowp float B = 1.164 * (Y - 16.0/256.0) + 2.018 * (U - 0.5); \n"
" gl_FragColor = vec4(R,G,B,1); \n"
"}\r\n";
static char const *vert =
"varying mediump vec2 texCoordAtFrag; \n"
"attribute vec4 Position; \n"
"attribute vec2 TexCoord; \n"
"void main() { \n"
" texCoordAtFrag = TexCoord; \n"
" gl_Position = Position; \n"
"}\r\n";
凡texLum,texU,texV是抱着通道的紋理。
我添加了代碼我的着色器。你能指點我如何結合過濾器?我有點困惑,因爲我的代碼一次只做1個像素,我不確定如何將其轉換爲矩陣操作。 – 2012-04-12 22:02:14
@Gilad - 我可以談論將顏色處理操作分解爲兩個獨立着色器並在兩次執行中進行卷積的好處。對於從3x3區域拉入數值的Sobel邊緣檢測濾波器,首先將圖像轉換爲其亮度值,然後拍攝該亮度圖像並對其執行卷積運算,導致iPhone 4的速度提高了10倍,而兩次操作用單一着色器進行一次傳遞。除非內存問題,否則我強烈建議將其分成兩個階段,一個用於YUV-> RGB,另一個用於銳化。 – 2012-04-15 16:09:25
@BradLarson我同意。我已經試過並最終決定分兩階段進行。它產生了一個更容易維護的代碼,結果變得更快。 – 2012-04-16 01:43:17