我有点沮丧。我即将使用mac(OS X mavericks)来编写代码。Opengl es 2.0在使用const时,OSX上的GLSL编译失败
我的着色器在Windows 7和Android下工作正常。 当我在OS X下运行我的应用程序时,运行期间出现以下编译器错误。
Exception in thread "LWJGL Application" java.lang.IllegalStateException: ERROR: 0:6: Initializer not allowed
ERROR: 0:6: Use of undeclared identifier 'light'
ERROR: 0:6: Use of undeclared identifier 'spec'
ERROR: 0:6: Use of undeclared identifier 'spec'
ERROR: 0:6: Use of undeclared identifier 'spec'
片断着色段:
String fragmentShader =
"#ifdef GL_ES\n" +
"precision mediump float;\n" +
"#endif\n" +
"uniform sampler2D cubeMap;\n"+
"uniform sampler2D normalMap;"+
"varying vec2 v_TexCoords;"+
"void main() {\n"+
" const vec4 ambientColor = vec4(1.0, 1.0, 1.0, 1.0);" +
" const vec4 diffuseColor = vec4(1.0, 1.0, 1.0, 1.0);"+
" const vec3 light = normalize(vec3(-0.5,-0.8,-1.0));"+
" vec3 camDir = normalize(vec3(v_TexCoords,0) - vec3(0.5,0.5,10.0));"+
" vec4 normalPxl = texture2D(normalMap,v_TexCoords);" +
" vec3 normal = normalize(normalPxl.xyz - vec3(0.5,0.5,0.5));"+
" vec3 reflectDir = reflect(camDir,normal);"+
" float spec = pow(max(0.0,dot(camDir,reflect(light,normal))),4.0);"+
" gl_FragColor = vec4(texture2D(cubeMap, reflectDir.xy * 0.5+0.5).xyz,normalPxl.w-0.1)"+
....
我已经红透的OpenGL规格并没有发现使用const或声明的主变量的一些问题。
感谢您的帮助
确定,即“常量”就是这个道理或这只是一个假设吗? – Springrbua
它在我看来,问题是线'const vec3 light = normalize(vec3(-0.5,-0.8,-1.0));',在那里你设置一个常数值一个函数的结果,着色器编译器可能不知道将其减少到一个常量值,所以你可以用实际的向量归一化它的结果吗? –
是的,你在哪里,这是正常化调用。 Thx很多,你做了我的da年。有趣的是,android GL驱动程序正在处理的不仅仅是桌面系统的驱动程序。请将您的解决方案作为答案发布。 – fky