GlGetAttribLocation returns -1 when retrieving an existing shader attribute

I'm trying to pass attributes to my vertex shader, but for some reason it keeps giving me -1 in the third place of the attribute, I ask openGl to get it through glGetAttribLocation (). Currently, he continues to give me -1 for the texCoord attribute, and if I switch texAttrib and colAttrib around (by switching lines in the code), he gives me -1 on the color property instead of the texture, and I have no idea why? Since -1 is passed to glVertexAttribPointer, I get OpenGL error 1281: GL_INVALID_VALUE.

My vertex shader:

#version 150 in vec3 position; in vec3 color; in vec2 texcoord; out vec3 Color; out vec2 Texcoord; void main() { Color = color; Texcoord = texcoord; gl_Position = vec4(position.x, position.y, position.z, 1.0); } 

OpenGL Code:

 basicShader.Use(); // Buffers and shaders glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vbo); // Make it active vbo glBindBuffer(GL_ARRAY_BUFFER, vbo); // Build basic triangle float vertices[] = { // position // color // textures -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f// Bottom-left }; GLuint elements[] = { 0, 1, 2, 2, 3, 0 }; glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); GLint posAttrib = glGetAttribLocation(basicShader.shaderProgram, "position"); glEnableVertexAttribArray(posAttrib); // Enable attribute first before specifiying attributes glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0); // 6 float sizes is every new vertex GLint colAttrib = glGetAttribLocation(basicShader.shaderProgram, "color"); glEnableVertexAttribArray(colAttrib); glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); // same for color + offset GLint texAttrib = glGetAttribLocation(basicShader.shaderProgram, "texcoord"); // Returns -1 glEnableVertexAttribArray(texAttrib); glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); // 6 offset from beginning [..] 
+8
c ++ shader opengl vertex-shader
source share
1 answer

You must stop using glGetAttribLocation . Assign a location to each attribute , either using glBindAttribLocation before associating the program or with an explicit attribute location, if available to you.

That way, if the compiler disables the attribute (what happens here, your fragment shader probably doesn't use one of the interpolated values), you don't care. You will customize your arrays as usual using the standard convention for attribute indexes. In addition, you will not need to ask what attribute location will be each time you want to display using another program; you know where it is because you assigned it.

If you cannot / do not want, then you cannot do anything. The compiler will optimize the attributes if you are not actually using them. The only thing you can do is to detect that it returns -1 and has not configured the array for this attribute.

+22
source share

All Articles