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};
}