简体   繁体   中英

GLSL Shader does not compile, Java and LWJGL shader error

I've encountered this error a few times now when compiling my shaders.

Here is the infoLog/Error

Vertex shader failed to compile with the following errors: ERROR: 0:1: error(#132) Syntax error: "<" parse error ERROR: error(#273) 1 compilation errors. No code generated

This occurs on the fragment shader aswell. Here is my very very basic test vertex and frag shader code:

@version 330

layout (location = 0) in vec3 position;

void main(){

    gl_Position = vec4(position, 1.0);
}

And Here's the frag:

@version 330

out vec4 fragColor;

void main(){

    fragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

Here is the Shader class that has all the load/compile/link methods:

public  abstract class Shader {

    private int program;

    public Shader(){


        program = glCreateProgram();

        if(program == 0){
            System.err.println("Shader creation failed: Could not find valid memory location");     
            System.exit(1);
        }
    }



    public void bind(){

         glUseProgram(program);

    }

    public void addProgram(String text, int type){

        int shader = glCreateShader(type);

        if(shader == 0)
        {
            System.err.println("Shader creation failed: Could not find valid memory location");      
            System.exit(1);
        }

        glShaderSource(shader, text);
        glCompileShader(shader);             

        if(glGetShaderi(shader, GL_COMPILE_STATUS) == 0){
            System.err.println("Shader compilation failed");
            System.err.println(glGetShaderInfoLog(shader, 1024));
            System.exit(1);
        }

        glAttachShader(program, shader);
    }


    public String loadShader(String fileName){


        StringBuilder shaderSource = new StringBuilder();
        BufferedReader shaderReader = null;

        try{

            shaderReader = new BufferedReader(new FileReader("./res/shaders/" + fileName));

            String line;
            while((line = shaderReader.readLine()) != null){

                shaderSource.append(line).append("\n");
            }

            shaderReader.close();


        }catch(Exception e){
            e.printStackTrace();
            System.out.println("HERE IT IS!!!!");
            System.exit(1);
        }

        return shaderSource.toString();

    }

    public void compileShader(){

        glLinkProgram(program);

        if(glGetProgrami(program, GL_LINK_STATUS) == 0){
            System.err.println(glGetShaderInfoLog(program, 1024));
            System.exit(1);
        }

        glValidateProgram(program);

        if(glGetProgrami(program, GL_VALIDATE_STATUS) == 0){
            System.err.println(glGetShaderInfoLog(program, 1024));
            System.exit(1);
        }

    }
}

Ive done a lot of experimenting to seee where the problem is and I've narrowed it down to one of two things.(A) somehow loading in the text from the file in the loadShader() method is returning a bunch of random symbols(notice in the infoLog up top the parse error indicates the character was "<" which is clearly not even in either of the shaders. Or (B) the error is with the glCompileShader(shader) call in the addProgram() method. I think option A is more likely, but as I said, the fileLoader has never done that before.

答案是.......在着色器代码中声明版本时,它不是#。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM