0

我对 OpenGL 和 LibGdx 很陌生。我从这些教程开始,但想应用 phong 纹理。我尝试合并一些示例,但遇到了问题。

我在屏幕中央有一个球体和旋转立方体。我还有数百件事要解决,但目前,我不明白为什么 LibGdx 报告说找不到我的制服材料......

线程“LWJGL 应用程序”com.badlogic.gdx.utils.GdxRuntimeException 中的异常:java.lang.IllegalArgumentException:着色器中没有名称为“uMvpMatrix”的统一

像素着色器

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

         #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;     
}                              

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

4

2 回答 2

2

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

uniform float yShift;

使用精度说明符解决了这个问题:

uniform lowp float yShift;

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

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

最后,还有忽略着色器错误的标志:

ShaderProgram.pedantic = false;
于 2013-04-04T16:19:38.530 回答
2

我遇到了类似的事情。我认为因为你的着色器不使用“uMvpMatrix”统一,它的声明被优化了,所以当你去设置它时它“不存在”。如果你改变你的着色器以某种方式引用矩阵,你应该走得更远。

请参阅(间接) (未使用的)GLSL 制服/输入/输出是否有助于注册压力?

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

于 2013-03-12T03:45:53.083 回答