在构建jar

时间:2016-05-20 18:27:45

标签: java opengl jar shader lwjgl

我正在尝试为学校项目制作一个游戏,一切正常,除非我建立一个罐子,它没有渲染任何东西,我已经确定这是因为它无法找到着色器文件。这是读取文件的代码。

public static int loadShader(String filepath, int type){
    StringBuilder result = new StringBuilder();

    try{
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filepath));
        String buffer = "";
        while((buffer = bufferedReader.readLine()) != null)
            result.append(buffer + "\n");
        bufferedReader.close();
    }catch (IOException e1){
        e1.printStackTrace();
    }

    int shaderID = glCreateShader(type);
    glShaderSource(shaderID, result.toString());
    glCompileShader(shaderID);
    if(glGetShaderi(shaderID, GL_COMPILE_STATUS) == GL_FALSE){
        System.err.println(glGetShaderInfoLog(shaderID, 500));
        System.err.println("Could not compile shader.");
        System.err.println(-1);
    }
    return shaderID;

}

这是我需要放置文件路径的地方

public static void loadAll(){
    shader1 = new Shader("src/com/myGame/shaders/vertex.shader", "src/com/myGame/shaders/fragment.shader");
    shader3 = new Shader("src/com/myGame/shaders/vertex3.shader", "src/com/myGame/shaders/fragment3.shader");
}

这是Shader类

public class Shader {
    private int programID;
    private int vertexShaderID;
    private int fragmentShaderID;

public Shader(String vertexFile, String fragmentFile){
    vertexShaderID = loadShader(vertexFile, GL_VERTEX_SHADER);
    fragmentShaderID = loadShader(fragmentFile, GL_FRAGMENT_SHADER);
    programID = glCreateProgram();
    glAttachShader(programID, vertexShaderID);
    glAttachShader(programID, fragmentShaderID);
    glLinkProgram(programID);
    glValidateProgram(programID);
}

public void start(){
    glUseProgram(programID);
}

public void stop(){
    glUseProgram(0);
}

public int getID(){
    return this.programID;
}

public int getUniform(String name){
    int result = glGetUniformLocation(programID, name);
    if(result == -1)
        System.err.println("Could not find uniform variable'" + name + "'!");
    return glGetUniformLocation(programID, name);
}

public void setUniform3f(String name, Vector3f vector) {
    glUniform3f(getUniform(name), vector.x, vector.y, vector.z);
}

}

当我在IntelliJ中编译时,这一切都有效,但是当我编译,构建并以jar形式运行时,这一切都无效。当我尝试从命令提示符运行它作为jar时,它表示它无法找出放入着色器类中pos方法的变量setUniform3f

编辑:我正在尝试使用InputStream,这就是我所拥有的,但它获得了空指针异常。

    Exception in thread "Bot Fighting" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at com.myGame.utils.Utilities.loadShader(Utilities.java:22)
at com.myGame.graphicsEngine.Shader.<init>(Shader.java:20)
at com.myGame.graphicsEngine.ShaderManager.loadAll(ShaderManager.java:20)
at com.myGame.gameEngine.Level.<init>(Level.java:21)
at com.myGame.main.BotMain.init(BotMain.java:50)
at com.myGame.main.BotMain.run(BotMain.java:58)
at java.lang.Thread.run(Thread.java:745)

这是我的代码InputStream

     public static int loadShader(String filepath, int type){
    StringBuilder result = new StringBuilder();
    InputStream in = ClassLoader.getSystemResourceAsStream(filepath);
    try{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
        String buffer = "";
        while((buffer = bufferedReader.readLine()) != null)
            result.append(buffer + "\n");
        bufferedReader.close();
    }catch (IOException e1){
        e1.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    int shaderID = glCreateShader(type);
    glShaderSource(shaderID, result.toString());
    glCompileShader(shaderID);
    if(glGetShaderi(shaderID, GL_COMPILE_STATUS) == GL_FALSE){
        System.err.println(glGetShaderInfoLog(shaderID, 500));
        System.err.println("Could not compile shader.");
        System.err.println(-1);
    }
    return shaderID;

我显然没有使用这个权利,所以我该如何使用它?

1 个答案:

答案 0 :(得分:0)

试试这个:

public static void loadAll(){
    shader1 = new Shader("com/myGame/shaders/vertex.shader", "com/myGame/shaders/fragment.shader");
    shader3 = new Shader("com/myGame/shaders/vertex3.shader", "com/myGame/shaders/fragment3.shader");
}

没有“src /”

如果它不起作用,请使用:

InputStreamReader(cls.getResourceAsStream(name))

其中Class<?> cls是与着色器位于同一个包中的某个类,而String name是文件名,如:

public static int loadShader(Class<?> cls, String name, int type){
    StringBuilder result = new StringBuilder();
    InputStream in = ClassLoader.getSystemResourceAsStream(cls, name);
    try{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
        String buffer = "";
        while((buffer = bufferedReader.readLine()) != null)
            result.append(buffer + "\n");
        bufferedReader.close();
    }catch (IOException e1){
        e1.printStackTrace();
    }

然后致电loadShader(com.myGame.shaders.Shader.class, "vertex.shader", GL_VERTEX_SHADER)。 问题是,您需要在与文件相同的包中使用类。

希望有所帮助