简体   繁体   English

GLSL Shader无法编译,Java和LWJGL着色器错误

[英]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 这是infoLog /错误

Vertex shader failed to compile with the following errors: ERROR: 0:1: error(#132) Syntax error: "<" parse error ERROR: error(#273) 1 compilation errors. 顶点着色器无法编译,并出现以下错误:错误:0:1:错误(#132)语法错误:“ <”分析错误错误:错误(#273)1编译错误。 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: 这是具有所有load / compile / link方法的Shader类:

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. 我做了很多实验来找出问题所在,然后将其缩小为两件事之一。(A)以某种方式从loadShader()方法的文件中加载文本将返回一堆随机符号(注意在infoLog顶部,解析错误指示字符为“ <”,这显然在两个着色器中均不存在,或者(B)错误与addProgram()方法中的glCompileShader(shader)调用有关。选项A更有可能,但是正如我所说,fileLoader从来没有做过。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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