TekkunDashi
TekkunDashi

Reputation: 45

GLSL errors unexpected $end at token "<EOF>" and formatting issues

So I have an assigment to use opengl to draw a triangle and make it do various things. I have an issue though with my vertex Shader file For some reason, if I try to put in a new line in the shader file, it breaks and doesn't display a triangle. for example if i put in a new line.

#version 430
out vec4 varyingColor;
uniform float offsetV;
uniform int gradientChange;
uniform float size;
uniform float offsetCx; 
uniform float offsetCy;

this will break and it will no longer show the triangle. BUT when I keep them all in one line, I get an error BUT i get full functionality of the code, the triangle shows, it moves etc.

(0) : error C0000: syntax error, unexpected $end at token ""

this is my vertex shader

#version 430
out vec4 varyingColor;uniform float offsetV;uniform int gradientChange;uniform float size;uniform float offsetCx; uniform float offsetCy;
void main(void){    
    if(gl_VertexID==0){gl_Position= vec4(size*(0.25)+offsetCx,size*(-0.25+offsetV)+offsetCy,0.0,1.0);}
    if(gl_VertexID==1){gl_Position=vec4(size*(-0.25)+offsetCx,size*(-0.25+offsetV)+offsetCy,0.0,1.0);} 
    if(gl_VertexID==2){gl_Position=vec4(size*(0.0)+offsetCx,size*(0.25+offsetV)+offsetCy,0.0,1.0);}
    if(gradientChange == 0){varyingColor = vec4( 1.0,0.0,0.0,1.0);  }
}

This is my Fragment shader

#version 430
out vec4 color;
in vec4 varyingColor;
void main(void){
    color = varyingColor;
}




    private int createShaderProgram() {
        GL4 gl = (GL4) GLContext.getCurrentGL();
        int[] vertCompiled = new int[1];
        int[] fragCompiled = new int[1];
        int[] linked = new int[1];


        String vshaderSource [];
        String fshaderSource [];
        vshaderSource = readShaderSource("src/code/vertShader.glsl");
        fshaderSource = readShaderSource("src/code/fragShader.glsl");

        int vShader = gl.glCreateShader(GL_VERTEX_SHADER);
        gl.glShaderSource(vShader,  3,  vshaderSource, null, 0);
        gl.glCompileShader(vShader);

        //catch error code chunks
        checkOpenGLError();
        gl.glGetShaderiv(vShader,  GL_COMPILE_STATUS,  vertCompiled, 0);
        if(vertCompiled[0] !=1) {
            System.out.println("vertext compilation failed.");
            printShaderLog(vShader);
        }




        int fShader=gl.glCreateShader(GL_FRAGMENT_SHADER);
        gl.glShaderSource(fShader,  4,  fshaderSource, null, 0);
        gl.glCompileShader(fShader);


        //catch error code chunks
        checkOpenGLError();
        gl.glGetShaderiv(fShader,  GL_COMPILE_STATUS,  fragCompiled, 0);
        if(fragCompiled[0] !=1) {
            System.out.println("fragment compilation failed.");
            printShaderLog(fShader);
        }

        if((vertCompiled[0] !=1) ||((fragCompiled[0] !=1))) {
            System.out.println("\nCompilation Error; return-flags:");
            System.out.println("vertCompiled = " +vertCompiled[0] + "; fragCompiled " + fragCompiled[0]);
        }


        gl.glShaderSource(vShader, vshaderSource.length, vshaderSource, null, 0);
        gl.glShaderSource(fShader, vshaderSource.length, vshaderSource, null, 0);

        int vfProgram = gl.glCreateProgram();
        gl.glAttachShader(vfProgram, vShader);
        gl.glAttachShader(vfProgram, fShader);
        gl.glLinkProgram(vfProgram);

        gl.glDeleteShader(vShader);
        gl.glDeleteShader(fShader);
        return vfProgram;

    }

Upvotes: 1

Views: 369

Answers (1)

Rabbid76
Rabbid76

Reputation: 210928

The issue is the line

gl.glShaderSource(vShader,  3,  vshaderSource, null, 0);

respectively

gl.glShaderSource(fShader,  4,  fshaderSource, null, 0);

vshaderSource and fshaderSource are arrays of strings (String[]). glShaderSource can get an array of strings. But you have to set the length of the array to the 2nd argument:

gl.glShaderSource(vShader, vshaderSource.length, vshaderSource, null, 0);
gl.glShaderSource(fShader, fshaderSource.length, fshaderSource, null, 0);

Upvotes: 2

Related Questions