Libgdx着色器设置NullPointerException [英] Libgdx shader setup NullPointerException

查看:83
本文介绍了Libgdx着色器设置NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试这样设置着色器:

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)

我的着色器是这些:

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

#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!

推荐答案

以下是ShaderProgramloadShader的来源: 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.

我怀疑它是这一行:

GL20 gl = Gdx.graphics.getGL20();

(您可以直接在代码中自行尝试一下,看看它是否崩溃了.)

(You can try this yourself directly in your code to see if it blows up).

文档说getGL20() 将返回如果您的应用未针对OpenGL ES 2.0配置,则为null .默认情况下,您获得1.x支持. OpenGL ES 1.x版本不支持着色器,因此必须通过在应用程序配置中设置useGL20标志来配置应用程序.请参见 https://code.google.com/p/libgdx/wiki/ApplicationConfiguration

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

这篇关于Libgdx着色器设置NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆