0

私はOpenGLとLibGdxに非常に慣れていません。私はこれらのチュートリアルから始めましたが、フォンテクスチャを適用したかったのです。いくつかの例をマージしようとしましたが、問題が発生しています。

画面の中央に球と回転する立方体があります。私はまだ何百もの解決すべきことがありますが、今のところ、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

私は似たようなものに出くわしました。シェーダーは「uMvpMatrix」ユニフォームを使用していないため、宣言が最適化され、設定するときに「そこにない」と思います。何らかの方法でマトリックスを参照するようにシェーダーを変更した場合は、さらに遠くまで行く必要があります。

(間接的 に)GLSLユニフォーム/イン/アウトがレジスター圧力に寄与するかどうかを参照してください。

シェーダーをオフラインで開発およびコンパイルする方法はあると思います。したがって、複雑なシェーダーの場合は、Libgdxの外部で開発するのが理にかなっているかもしれません(より良いエラーメッセージが表示されることを願っています)。Libgdxは巨大な文字列を下位レイヤーに渡すだけで、シェーダー自体にはあまり影響しないため、互換性の問題は発生しないはずです。

于 2013-03-12T03:45:53.083 に答える
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 に答える