在phong着色器中,LibGdx着色器“没有名字的制服......”

时间:2013-03-11 23:57:54

标签: java opengl-es-2.0 libgdx vertex-shader

我是OpenGL和LibGdx的新手。我从these tutorials开始,但想要应用phong纹理。我试图合并一些例子,但我遇到了问题。

我在屏幕中央有一个球体和旋转立方体。我还有数以百计需要解决的问题,但目前我还不明白为什么LibGdx报告我的统一材料无法找到...

  

线程“LWJGL Application”中的异常com.badlogic.gdx.utils.GdxRuntimeException:java.lang.IllegalArgumentException:在着色器中没有名称为“uMvpMatrix”的制服

Pixel Shader

我不相信片段着色器是相关的,但它是在底部以防万一。

         #version 120
         uniform mat4 uMvpMatrix;
         varying vec3 diffuseColor; 
         // the diffuse Phong lighting computed in the vertex shader
         varying vec3 specularColor; 
         // the specular Phong lighting computed in the vertex shader
         varying vec4 texCoords; // the texture coordinates 

         void main()
         {                              
            vec3 normalDirection = 
               normalize(gl_NormalMatrix * gl_Normal);
            vec3 viewDirection = 
               -normalize(vec3(gl_ModelViewMatrix * gl_Vertex)); 
            vec3 lightDirection;
            float attenuation;

            if (0.0 == gl_LightSource[0].position.w) 
               // directional light?
            {
               attenuation = 1.0; // no attenuation
               lightDirection = 
                  normalize(vec3(gl_LightSource[0].position));
            } 
            else // point light or spotlight (or other kind of light) 
            {
               vec3 vertexToLightSource = 
                  vec3(gl_LightSource[0].position 
                  - gl_ModelViewMatrix * gl_Vertex);
               float distance = length(vertexToLightSource);
               attenuation = 1.0 / distance; // linear attenuation 
               lightDirection = normalize(vertexToLightSource);

               if (gl_LightSource[0].spotCutoff <= 90.0) // spotlight?
               {
                  float clampedCosine = max(0.0, dot(-lightDirection, 
                     gl_LightSource[0].spotDirection));
                  if (clampedCosine < gl_LightSource[0].spotCosCutoff) 
                     // outside of spotlight cone?
                  {
                     attenuation = 0.0;
                  }
                  else
                  {
                     attenuation = attenuation * pow(clampedCosine, 
                        gl_LightSource[0].spotExponent);
                  }
               }
            }

            vec3 ambientLighting = vec3(gl_LightModel.ambient); 
               // without material color!

            vec3 diffuseReflection = attenuation 
               * vec3(gl_LightSource[0].diffuse) 
               * max(0.0, dot(normalDirection, lightDirection)); 
               // without material color!

            vec3 specularReflection;
            if (dot(normalDirection, lightDirection) < 0.0) 
               // light source on the wrong side?
            {
               specularReflection = vec3(0.0, 0.0, 0.0); 
                  // no specular reflection
            }
            else // light source on the right side
            {
               specularReflection = attenuation 
                  * vec3(gl_LightSource[0].specular) 
                  * vec3(gl_FrontMaterial.specular) 
                  * pow(max(0.0, dot(reflect(-lightDirection, 
                  normalDirection), viewDirection)), 
                  gl_FrontMaterial.shininess);
            }

            diffuseColor = ambientLighting + diffuseReflection;
            specularColor = specularReflection;
            texCoords = gl_MultiTexCoord0;
            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
         }

设置

shader = new ShaderProgram(vertexShader, fragmentShader);
mesh = Shapes.genCube();
mesh.getVertexAttribute(Usage.Position).alias = "a_position";

渲染

...
float aspect = Gdx.graphics.getWidth() / (float) Gdx.graphics.getHeight();
projection.setToProjection(1.0f, 20.0f, 60.0f, aspect);
view.idt().trn(0, 0, -2.0f);
model.setToRotation(axis, angle);
combined.set(projection).mul(view).mul(model);

Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

shader.begin();
shader.setUniformMatrix("uMvpMatrix", combined);
mesh.render(shader, GL20.GL_TRIANGLES);
shader.end();

堆栈跟踪

at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: java.lang.IllegalArgumentException: no uniform with name 'uMvpMatrix' in shader
at com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:283)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:539)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:527)
at com.overshare.document.Views.Test.onRender(Test.java:150)
    ...

片段着色器

#ifdef GL_ES
precision mediump float;
#endif
precision mediump float;       
varying vec4 v_Color;          
void main()                    
{                              
    gl_FragColor = v_Color;     
}                              

有人可以告诉我我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

我遇到了类似的事情。我认为因为你的着色器没有使用“uMvpMatrix”制服,它的声明会被优化掉,所以当你设置它时它“不存在”。如果您更改着色器以某种方式引用矩阵,则应该更进一步。

见(间接) Do (Unused) GLSL uniforms/in/out Contribute to Register Pressure?

我相信有离线开发和编译着色器的方法,因此对于复杂的着色器,在Libgdx之外开发它可能是有意义的(希望你能得到更好的错误信息)。 Libgdx只是将巨型字符串传递给较低层,它对着色器本身没有太大作用,因此不存在兼容性问题。

答案 1 :(得分:2)

此外,问题可能出在精确说明符中。在设备(Nexus S)上,下一个统一定义将引发相同的错误:

uniform float yShift;

使用精确说明符可以解决问题:

uniform lowp float yShift;

LibGDX允许检查着色器编译并获取错误日志:

if(!shader.isCompiled()){
    String log = shader.getLog();
}

最后,有一个标志可以忽略着色器错误:

ShaderProgram.pedantic = false;