-3

I started learning shaders and I ran into a problem where the function glGetAttribLocation returns -1.

programId >= 0

vertex & fragment Shader >= 0

position & color Attribute == -1

Here is my code:

Shaders:

int[] shaderData = createShaderProgram(gl, "in vec3 color;\n" + 
                "\n" + 
                "out vec4 outColor;\n" + 
                "\n" + 
                "void main()\n" + 
                "{\n" + 
                "    outColor = vec4(color.x, color.y, color.z, 1.0);\n" + 
                "}", 

                "in vec3 inColor;\n" + 
                "in vec3 inPosition;\n" + 
                "\n" + 
                "out vec3 color;\n" + 
                "\n" + 
                "void main()\n" + 
                "{\n" + 
                "    gl_Position = vec4(inPosition, 1.0);\n" + 
                "   color = inColor;\n" + 
                "}");
        programId = shaderData[0];
        vertexShader = shaderData[1];
        fragmentShader = shaderData[2];
        gl.glUseProgram(programId);

        int positionAttribute = gl.glGetAttribLocation(programId, "inPosition");
        int colorAttribute = gl.glGetAttribLocation(programId, "inColor");
        System.out.println(positionAttribute + "; " + colorAttribute); // -1; -1

And createShaderProgram function:

private int[] createShaderProgram(GL2 gl, String fragment, String vertex) {
        int v = gl.glCreateShader(GL2.GL_VERTEX_SHADER);
        int f = gl.glCreateShader(GL2.GL_FRAGMENT_SHADER);

        gl.glShaderSource(v, 1, new String[] {vertex}, null);
        gl.glCompileShader(v);

        gl.glShaderSource(f, 1, new String[] {fragment}, null);
        gl.glCompileShader(f);

        int shaderprogram = gl.glCreateProgram();
        gl.glAttachShader(shaderprogram, v);
        gl.glAttachShader(shaderprogram, f);
        gl.glLinkProgram(shaderprogram);
        gl.glValidateProgram(shaderprogram);

        return new int[] {shaderprogram, v, f};
    }
congard
  • 945
  • 2
  • 10
  • 28
  • Start by checking if compilation and linking succeeds. I guess they don't because you are missing a #version in the first line of your shaders. Also: glValidateShader does not do what you expect from it. And even if, you would have to check GL_VALIDATE_STATUS to see the result. – BDL Mar 06 '18 at 08:56
  • I added #version but nothing changed – congard Mar 06 '18 at 09:00
  • 2
    Did you check the GL_COMPILE_STATUS and glGetShaderInfoLog? – BDL Mar 06 '18 at 09:03
  • 1
    if your shader contains error it will not compile or link hence not found locations. The same goes for uniforms but with them they could be also optimized out by the driver if not used ... you should check `glGetShaderInfoLog` there is all the stuff you need to know to solve this. Also see [simple complete GL+VAO/VBO+GLSL+shaders example in C++](https://stackoverflow.com/a/31913542/2521214) may be that helps – Spektre Mar 06 '18 at 09:07
  • @Spektre, Im sorry, but ... Could you tell me how to use this method (glGetShaderInfoLog)? – congard Mar 06 '18 at 09:11
  • @DBCongard see the link in my comment there it is used. It returns string to your preallocated buffer. – Spektre Mar 06 '18 at 09:11
  • @Spektre, **vertex log:** `0:1(1): error: \`in' qualifier in declaration of \`inColor' only valid for function parameters in GLSL 1.10 0:2(1): error: \`in' qualifier in declaration of \`inPosition' only valid for function parameters in GLSL 1.10 0:4(1): error: \`out' qualifier in declaration of \`color' only valid for function parameters in GLSL 1.10` **and fragment:** `0:1(1): error: \`in' qualifier in declaration of \`color' only valid for function parameters in GLSL 1.10 0:3(1): error: \`out' qualifier in declaration of \`outColor' only valid for function parameters in GLSL 1.10` – congard Mar 06 '18 at 09:31
  • 1
    @DBCongard yes you are in the old GLSL 1.0 as you did not use `#version`. so it is not valid code for it as you use the newer stuff. See the link again and look at first line of my shaders there ... you do not need the core but you need to set which version of GLSL you want **use at least** `#version 110` or `#version 110 core` or `#version 110 compatibility` – Spektre Mar 06 '18 at 09:34
  • @Spektre, Thank you very much! And the question is, if I use the shader version 1.3 (`#version 130`), can I use OpenGL 2.0? – congard Mar 06 '18 at 09:39
  • 1
    @DBCongard they not related the shaders are compiled in driver your opengl code is not. The only thing that can be a problem is core profile against compatibility as in the newer versions of GL the old `glBegin/glEnd` is depreceated (but still can be used on nVidia) and you should use VBO/VAO instead. Try and you will see ... also GLSL 1.3 does not mean GL 1.3 !!! IIRC there where somewhere tables of versions of GL vs. GLSL in the GL docs ... – Spektre Mar 06 '18 at 09:43
  • 1
    Just FYI... regarding [OpenGL version versus GLSL version](https://en.wikipedia.org/wiki/OpenGL_Shading_Language#Versions). – G.M. Mar 06 '18 at 10:13

1 Answers1

0

Solution: add #version to the beginning of the shaders. In this case, this is #version 130

congard
  • 945
  • 2
  • 10
  • 28