glGetActiveAttribute() Reading The Wrong Attribute Index?

Minimal reproducible example:

#include <iostream>
#include "glad.h"
#include <glfw3.h>

int main(int argc, char* args[])
{
    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    auto window = glfwCreateWindow(800, 600, "Example", NULL, NULL);
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        throw new std::runtime_error("Failed to initialize GLAD");

    const char* vertexSrc = "#version 330 coren"
        "in vec3 APos;n"
        "in vec3 VertCol_In;n"
        "out vec3 VertCol;n"
        "in vec2 TexCoord_In;n"
        "out vec2 TexCoord;n"
        "void main()n"
        "{n"
        "   gl_Position = vec4(APos.x, APos.y, APos.z, 1.0);n"
        "   VertCol = VertCol_In;n"
        "   TexCoord = TexCoord_In;n"
        "}";

    auto vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexSrc, NULL);
    glCompileShader(vertexShader);

    const char* fragSrc = "#version 330 coren"
        "out vec4 FragColor;n"
        "in vec3 VertCol;n"
        "in vec2 TexCoord;n"
        "void main()n"
        "{n"
        "   FragColor = vec4(VertCol.x,TexCoord.xy, 1.0f);n"
        "}";

    auto fragShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragShader, 1, &fragSrc, NULL);
    glCompileShader(fragShader);

    auto prog = glCreateProgram();
    glAttachShader(prog, vertexShader);
    glAttachShader(prog, fragShader);
    glLinkProgram(prog);

    auto aPos_layout = glGetAttribLocation(prog, "APos");
    auto vertCol_in_layout = glGetAttribLocation(prog, "VertCol_In");
    auto texCoord_in_layout = glGetAttribLocation(prog, "TexCoord_In");

    GLenum type;
    GLsizei length;
    GLint size;
    const GLuint bufSize = 16;
    GLchar bufA[bufSize];
    GLchar bufB[bufSize];
    GLchar bufC[bufSize];

    glGetActiveAttrib(prog, aPos_layout, bufSize, &length, &size, &type, bufA);
    std::cout << "Name at aPos_layout ("<<aPos_layout<<"): " << bufA << "n";

    glGetActiveAttrib(prog, vertCol_in_layout, bufSize, &length, &size, &type, bufB);
    std::cout << "Name at vertCol_in_layout ("<<vertCol_in_layout<<"): "<<bufB << "n";

    glGetActiveAttrib(prog, texCoord_in_layout, bufSize, &length, &size, &type, bufC);
    std::cout << "Name at texCoord_in_layout ("<<texCoord_in_layout<<"): " << bufC << "n";

    return 0;
}

As far as I understand how glGetActiveAttrib() and glGetAttribLocation() work, the output for executing this SHOULD be:

Name at aPos_layout (0): APos
Name at vertCol_in_layout (1): VertCol_In
Name at texCoord_in_layout (2): TexCoord_In

However, it is not. The output is instead:

Name at aPos_layout (0): APos
Name at vertCol_in_layout (1): **TexCoord_In**
Name at texCoord_in_layout (2): **VertCol_In**

Note how the names at vertCol_in_layout and texCoord_in_layout have swapped. Whilst obviously I already know the attribute names I’m getting an index from in this MRE, the point I’m trying to illustrate here is that either glGetAttribLocation()‘s return value or glGetActiveAttribute()‘s 2nd parameter aren’t acting according to documentation.

From glGetAttribLocation()‘s documentation:

Parameters

program
Specifies the program object to be queried.

index
Specifies the index of the attribute variable to be queried.

From glGetActiveAttribute()‘s documentation:

glGetAttribLocation queries the previously linked program object specified by program for the attribute variable specified by name and returns the index of the generic vertex attribute that is bound to that attribute variable

Is this just a bug in GLFW/OpenGL? I can’t figure how this would be happening otherwise since the intent in the documentation is pretty clear. That or something obvious is going wrong that I’m somehow blind-sighting and as someone pretty new to OpenGL/GLFW I’d be willing to believe that.

There is no conflict in the output, as the index (as used in glGetActiveAttrib) and the location of an active attribute are not the same thing. The OpenGL documentation calls the value of a location an “index” as well, which can make this confusing.

In the code provided, first the location of the attribute retrieved, which is then used as the index for glGetActiveAttribute. Please also see this answer, quoting Nicol Bolas: “Under the old introspection API, there is no way to retrieve an attribute index by name.” In other words, what the code is trying to do, is impossible.


Index, as related to vertex shader introspection

From the documentation:

The index​ is a number on the half-open range [0, GL_ACTIVE_ATTRIBUTES).

For typical vertex shader introspection, first glGetProgramiv is called with GL_ACTIVE_ATTRIBUTES to retrieve the total number of active attributes, and then glGetActiveAttrib is called in a loop where index ranges over the half-open range above.

Location

The location of an attribute is related to how the data is laid out, and is related to functions as glBindAttribLocation, glGetAttribLocation, glVertexAttribPointer, etc. One attribute (with a unique index as above) can take up multiple locations (e.g. for a mat4 attribute). The location of an attribute can also be specified in the shader code itself.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật