Getting error with GLSL with very little context to why

49 Views Asked by At

the error code is:

Error compiling Shader code: ERROR: 0:1: '' : syntax error: #version My shader compiler code is:

public class Shader {

    private int shaderProgramID;
    private boolean beingUsed = false;

    private String vertexSource;

    public Shader (int type, String path) {
        shaderProgramID = glCreateProgram();

        StringBuilder builder = new StringBuilder();

        try (InputStream in = new FileInputStream(path);
             BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line).append("\n");
            }

        } catch (IOException ex) {
            throw new RuntimeException("Failed to load a shader file!"
                    + System.lineSeparator() + ex.getMessage());
        }
        vertexSource = builder.toString();

    }


    public void compile() {
        // ============================================================
        // Compile and link shaders
        // ============================================================
        int vertexID, fragmentID;

        // First load and compile the vertex shader
        int shaderId = glCreateShader(GL_VERTEX_SHADER);


        glShaderSource(shaderId, vertexSource);
        System.out.println(vertexSource);
        glCompileShader(shaderId);

        if (glGetShaderi(shaderId, GL_COMPILE_STATUS) == 0) {
            System.out.println("Error compiling Shader code: " + glGetShaderInfoLog(shaderId, 1024));
        }

        glAttachShader(shaderProgramID, shaderId);
        glLinkProgram(shaderProgramID);
        if (glGetProgrami(shaderProgramID, GL_LINK_STATUS) == 0) {
            System.out.println("Error linking shader program: " + glGetProgramInfoLog(shaderProgramID));
        }
    }
    
    public void use() {
        if (!beingUsed) {
            // Bind shader program
            glUseProgram(shaderProgramID);
            beingUsed = true;
        }
    }

    public int getAttributeLocation(CharSequence name) {
        return glGetAttribLocation(shaderProgramID, name);
    }


    public void detach() {
        glUseProgram(0);
        beingUsed = false;
    }

shader code is:

#version 120 core

void main(){
    realpos.x = (1920 / 2) + 40 * aPosition.x;
    realpos.y = (1000 / 2) + 40 * aPosition.y;
   

}

Im using java 11 with the lwjgl library the opengl version is 2.1 Metal - 83.1. Also might be worth noting my system is an m2 mac.

I was having this error earlier then it disappeared strangely and now its back, Ive tried changing the shader version but opengl 2.1 does use 120 and the other versions give the expected error of wrong version etc. Ive been at it for hours trying to get this working so any help would be hugely appreciated! (first time posting here so please let me know if i could've formatted this better :>)

1

There are 1 best solutions below

0
Nifil On

For what I can tell version 120 doesn't not have a core profile https://www.khronos.org/opengl/wiki/OpenGL_Context#Context_types

so you should use

#version 120