LWJGL 中 glGetActiveAttrib 结果的字节顺序切换

Switched byte order in result of glGetActiveAttrib in LWJGL

我尝试将 glGetActiveAttrib(值类型和大小)的输出与 OpenGL 常量进行比较,但没有得到合理的结果。经过一些实验,我注意到字节顺序被切换了,所以这段代码:

    ByteBuffer auxType = ByteBuffer.allocateDirect(4);
    IntBuffer type = auxType.asIntBuffer();
    ByteBuffer auxSize = ByteBuffer.allocateDirect(4);
    IntBuffer size = auxSize.asIntBuffer();
    String name = glGetActiveAttrib(shader.program, 1, size, type);
    System.out.println(type.get(0));

输出 1384841216。重新排序字节后,我得到 35666 (GL_FLOAT_VEC4) - 这是正确的。尺寸值也有同样的问题。 LWJGL 中是否有任何 little-big endian 开关,或者它可能是显卡驱动程序的问题?我用的是LWJGL 3.1.1,显卡是NVIDIA GTX 1060

Is there any little-big endian switch in LWJGL or could it be issue with graphic card driver?

其实都是关于 ByteBuffer 本身的。它有一个字节顺序。所以不要像这样创建 type ByteBuffer

IntBuffer type = ByteBuffer.allocateDirect(4)
                           .asIntBuffer();

您创建它并告诉它字节顺序:

IntBuffer type = ByteBuffer.allocateDirect(Integer.BYTES)
                           .order(ByteOrder.nativeOrder())
                           .asIntBuffer();

此外,由于您使用的是 LWJGL,您还可以使用静态助手 class org.lwjgl.BufferUtils 而不是只执行:

IntBuffer type = BufferUtils.createIntBuffer(1);

现在 type.get(0) 应该正确地生成 35666 而不是 1384841216。如果将相同的方法应用于 size,那么应该同样产生假定值。

由于您没有提供着色器,我用以下方法对其进行了测试:

...
layout(location = 1) in vec3 position;
...

然后在调用 glGetActiveAttrib 时它正确地产生了:

String name = glGetActiveAttrib(shader.program, 1, size, type);
System.out.println(name); // prints "position"
System.out.println(type.get(0)); // prints "35665"
System.out.println(size.get(0)); // prints "1"

请记住 size 只是数组的元素数,因此由于我们不是在处理数组,因此它会产生 1.