具有相同着色器类中的2个着色器程序的LWJGL仅链接1个着色器

时间:2018-10-25 20:17:13

标签: java opengl shader lwjgl

我正在开发一个着色器类,其中必须加载2个着色器程序。 (一个用于纹理,一个用于没有纹理)。我已经加载了没有链接纹理的着色器,但没有链接纹理的着色器。这是我的2个着色器文件的着色器加载方法:

public void setupShader(String subPathBasic, String vertexFileBasic, String fragmentFileBasic, String subPathTextured) {
        this.vertexFileBasic = vertexFileBasic;
        this.fragmentFileBasic = fragmentFileBasic;
        this.vertexFileTextured = vertexFileBasic;
        this.fragmentFileTextured = fragmentFileBasic;
        //Get the ID's
        if(!getFileExtention(vertexFileBasic).equals("vs")) {
            ExceptionThrower.throwException(new ShaderIncompatableException(vertexFileBasic));
        }
        if(!getFileExtention(fragmentFileBasic).equals("fs")) {
            ExceptionThrower.throwException(new ShaderIncompatableException(fragmentFileBasic));
        }
        if(!getFileExtention(vertexFileTextured).equals("vs")) {
            ExceptionThrower.throwException(new ShaderIncompatableException(vertexFileTextured));
        }
        if(!getFileExtention(fragmentFileTextured).equals("fs")) {
            ExceptionThrower.throwException(new ShaderIncompatableException(fragmentFileTextured));
        }
        vertexShaderIDBasic = loadShader(this.vertexFileBasic, GL20.GL_VERTEX_SHADER, subPathBasic);
        fragmentShaderIDBasic = loadShader(this.fragmentFileBasic, GL20.GL_FRAGMENT_SHADER, subPathBasic);
        programIDBasic = GL20.glCreateProgram();

        vertexShaderIDTextured = loadShader(this.vertexFileTextured, GL20.GL_VERTEX_SHADER, subPathTextured);
        fragmentShaderIDTextured = loadShader(this.fragmentFileTextured, GL20.GL_FRAGMENT_SHADER, subPathTextured);
        programIDTextured = GL20.glCreateProgram();
        // attach shaders to program
        GL20.glAttachShader(programIDBasic, vertexShaderIDBasic);
        GL20.glAttachShader(programIDBasic, fragmentShaderIDBasic);

        GL20.glAttachShader(programIDTextured, vertexShaderIDTextured);
        GL20.glAttachShader(programIDTextured, fragmentShaderIDTextured);
        // Link the program.
        bindAttributes();
        GL20.glLinkProgram(programIDBasic);
        GL20.glLinkProgram(programIDTextured);
        // Validate the program. 
        GL20.glValidateProgram(programIDBasic);
        GL20.glValidateProgram(programIDTextured);

        getAllUniformLocations();

        int linkStatusBasic = GL20.glGetProgrami(programIDBasic, GL20.GL_LINK_STATUS);
        System.out.println("         Link status 1:   " + toString() + " : " +  linkStatusBasic);
        if(OptionHandler.getProperty(EngineOptions.DEBUGENABLED_KEY, OptionHandler.ENGINE_OPTION_ID).equals("true")) {
            System.out.println("         Loaded shader 1: " + toString() + " from subfolder: " + subPathBasic);
        }
        int linkStatusTextured = GL20.glGetProgrami(programIDTextured, GL20.GL_LINK_STATUS);
        System.out.println("         Link status 2:   " + toString() + " : " +  linkStatusTextured);
        if(OptionHandler.getProperty(EngineOptions.DEBUGENABLED_KEY, OptionHandler.ENGINE_OPTION_ID).equals("true")) {
            System.out.println("         Loaded shader 2: " + toString() + " from subfolder: " + subPathTextured);
        }

    }

protected static int loadShader(String file, int type, String subPath){
        //create a string builder.
        StringBuilder shaderSource = new StringBuilder();
        //read the file and store in shaderSource.
        try{
            BufferedReader reader = new BufferedReader(new FileReader(OptionHandler.getProperty(EngineOptions.PATHSHADERFILES_KEY, OptionHandler.ENGINE_OPTION_ID) + subPath + file));
            String line;
            while((line = reader.readLine())!=null){
                shaderSource.append(line).append("//\n");
            }
            reader.close();
        }catch(IOException e){
            System.out.println(OptionHandler.getProperty(EngineOptions.PATHSHADERFILES_KEY, OptionHandler.ENGINE_OPTION_ID) + subPath + file);
            ExceptionThrower.throwException(new InternalErrorException());
        }
        //get shader ID.
        int shaderID = GL20.glCreateShader(type);
        //create shader from source.
        GL20.glShaderSource(shaderID, shaderSource);
        //compile shader.
        GL20.glCompileShader(shaderID);
        //check for errors.
        if(GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS )== GL11.GL_FALSE){
            System.err.println("[ERROR]: Could not compile shader: " + file);
            System.out.println(GL20.glGetShaderInfoLog(shaderID, 500));
            ExceptionThrower.throwException(new InternalErrorException());
        }
        //Return shader ID.
        return shaderID;
}

我正在打印如下输出:

 Link status 1:   [vertexFile=VI.vs, fragmentFile=VI.fs] : 1
 Loaded shader 1: [vertexFile=VI.vs, fragmentFile=VI.fs] from subfolder: EntityShader/
 Link status 2:   [vertexFile=VI.vs, fragmentFile=VI.fs] : 0
 Loaded shader 2: [vertexFile=VI.vs, fragmentFile=VI.fs] from subfolder: TexturedEntityShader/

1个着色器链接和另一个以相同方式加载的着色器不链接可能是什么原因?

0 个答案:

没有答案
相关问题