问题
I try to setup my shader like this:
fog = new ShaderProgram(Gdx.files.local("shader/shad.vert"), Gdx.files.local("shader/shad.frag"));
But it gives me this exception:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NullPointerException
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: java.lang.NullPointerException
at com.badlogic.gdx.graphics.glutils.ShaderProgram.loadShader(ShaderProgram.java:194)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.compileShaders(ShaderProgram.java:173)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.<init>(ShaderProgram.java:156)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.<init>(ShaderProgram.java:165)
at Comet.Avoider.CometAvoider.create(CometAvoider.java:73)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:127)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)
My shaders are these:
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position;
}
and
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
void main() {
gl_FragColor = v_color * texture2D(u_texture, v_texCoords);
}
I don't know where the error is :( i cannot fix it. i hope u can help me. Thanks!
回答1:
Here's the source to ShaderProgram
's loadShader
:
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/glutils/ShaderProgram.java#L196
The constructor reads the contents of the files, so since you got past that part the files are probably okay.
I suspect its this line:
GL20 gl = Gdx.graphics.getGL20();
(You can try this yourself directly in your code to see if it blows up).
The doc says getGL20()
will return null if your app is not configured for OpenGL ES 2.0. By default you get 1.x support. Shaders are not supported by the 1.x versions of OpenGL ES, so you must configure your application by setting the useGL20
flag in your application configuration. See https://code.google.com/p/libgdx/wiki/ApplicationConfiguration
回答2:
Debug:
Gdx.files or Gdx are null? this should be your problem.
来源:https://stackoverflow.com/questions/17364186/libgdx-shader-setup-nullpointerexception