libgdx - 尝试使用ShapeRenderer时出错

时间:2015-03-28 19:30:54

标签: java libgdx

我尝试使用此代码使用ShapeRenderer进行淡入效果:

            Gdx.gl.glEnable(GL20.GL_BLEND);
            Gdx.gl20.glBlendFunc(GL20.GL_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
            //ShaderProgram.pedantic = false;

            shapeRenderer.setProjectionMatrix(stage.getCamera().combined);
            shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
            shapeRenderer.setColor(new Color(0,0,0,fadeTimeElapsed/1.5f));
            shapeRenderer.rect(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
            shapeRenderer.end();

            Gdx.gl20.glDisable(GL20.GL_BLEND);

我得到以下异常:

  

致命例外:GLThread 2715       java.lang.IllegalArgumentException:没有带名称的制服' u_projModelView'在着色器中               在com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:287)               在com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:277)               在com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:507)               在com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:498)               在com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20.flush(ImmediateModeRenderer20.java:147)               在com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20.end(ImmediateModeRenderer20.java:160)               在com.badlogic.gdx.graphics.glutils.ShapeRenderer.end(ShapeRenderer.java:1104)               at net.ukk.rpg.Level.render(Level.java:93)               在com.badlogic.gdx.Game.render(Game.java:46)               在com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:422)               在android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1542)               在android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1266)

ShaderProgram.pedantic=false BTW,我没有得到任何填充的矩形。 提前谢谢。

2 个答案:

答案 0 :(得分:3)

问题是我在Thread中实例化了ShapeRenderer。在show()方法中调用构造函数解决了这个问题。

答案 1 :(得分:0)

当我在最终调用shapeRenderer.end()的UI线程上调用我的方法(showAxesTicks)时,我遇到了同样的崩溃。我通过在@ user1702985中提到的Gdx.app.postRunnable(new Runnable()...)中包含showAxesTicks()来解决它。我认为它导致函数在同一个OpenGL线程上运行:

------------改变之前----------------

public void onGridMarkersMetricSelected() {
    mLibgdxFragment.showAxesTicks(true);

    hDebugConsole.consoleLog(TAG, "User enabled Metric Grid Markers");
    return;
}

------------改变后----------------

public void onGridMarkersMetricSelected() {
    Gdx.app.postRunnable(new Runnable() {
        @Override
        public void run() {
            mLibgdxFragment.showAxesTicks(true);                
        }
    });
    hDebugConsole.consoleLog(TAG, "User enabled Metric Grid Markers");
    return;
}