以下是我尝试使用的片段着色器代码(在OpenGL ES 2.0中,OpenGLES GLSL ES 1.00):
GLchar strFragmentShader[] =
"precision mediump float; \n"
"varying vec2 vTexCoord; \n"
"uniform sampler2D sTexture; \n"
"uniform float offset[] = float[]( 0.0000, 1.3846, 3.2307); \n"
"uniform float weight[] = float[]( 0.2270, 0.3162, 0.0702); \n"
"void main() \n"
"{ \n"
" vec4 sum = texture2D( sTexture, vec2(vTexCoord)/1024.0)*weight[0]; \n"
" for (int i=0;i<3;i++) { \n"
" sum += texture2D( sTexture, ( vec2(vTexCoord+vec2(0.0,offset[i])/1024.0 )*weight[i]; \n"
" sum += texture2D( sTexture, ( vec2(vTexCoord-vec2(0.0,offset[i])/1024.0 )*weight[i]; \n"
" } \n"
" gl_FragColor = sum; \n"
"} \n";
我基本上是关注this page上的示例,但我得到ERROR:LEX/PARSE-1 (fragment shader, line 4) Syntax error
。
如果我尝试将它们声明为const
而不是uniform
,建议here(在数组构造函数下),我会得到ERROR:CUSTOM-5 (fragment shader, line 4) Array cannot be const
。
显然第4和第5行会遇到同样的问题。如何编译?什么是正确的语法?
答案 0 :(得分:0)
我会这样做:
GLchar strFragmentShader[] =
"precision mediump float; \n"
"varying vec2 vTexCoord; \n"
"uniform sampler2D sTexture; \n"
"void main() \n"
"{
" vec3 offset = vec3(0.0000, 1.3846, 3.2307); \n"
" vec3 weight = vec3( 0.2270, 0.3162, 0.0702); \n" \n"
" vec4 sum = texture2D( sTexture, vec2(vTexCoord)/1024.0)*weight[0]; \n"
" for (int i=0;i<3;i++) { \n"
" sum += texture2D( sTexture, ( vec2(vTexCoord+vec2(0.0,offset[i])/1024.0 )*weight[i]; \n"
" sum += texture2D( sTexture, ( vec2(vTexCoord-vec2(0.0,offset[i])/1024.0 )*weight[i]; \n"
" } \n"
" gl_FragColor = sum; \n"
"}"
或者如果你需要它作为制服传递到着色器:
GLchar strFragmentShader[] =
"precision mediump float; \n"
"varying vec2 vTexCoord; \n"
"uniform sampler2D sTexture; \n"
"uniform vec3 offset; \n"
"uniform vec3 weight; \n"
"void main() \n"
"{ \n"
" vec4 sum = texture2D( sTexture, vec2(vTexCoord)/1024.0)*weight[0]; \n"
" for (int i=0;i<3;i++) { \n"
" sum += texture2D( sTexture, ( vec2(vTexCoord+vec2(0.0,offset[i])/1024.0 )*weight[i]; \n"
" sum += texture2D( sTexture, ( vec2(vTexCoord-vec2(0.0,offset[i])/1024.0 )*weight[i]; \n"
" } \n"
" gl_FragColor = sum; \n"
"}"
或使用uniform float[n]
如果你需要一个数组,那么n
是第二个例子中所需的(常量)大小,如果你需要一个数组,你可以使用第一个例子中的const float[n]
不想传递C ++代码中的值。
答案 1 :(得分:0)
原来这只是GLES的限制。它无法完成。需要找到另一种方法。