OpenGL获得统一的块偏移

时间:2016-07-07 04:36:05

标签: c++ opengl glsl

我试图将结构数组上传到统一块。为此,我在片段着色器中编写了下一个代码:

struct MaterialStruct {
    int ambientTexutre;
    int diffuseTexture;
    int specularTexture;
    int bumpTexture;

    vec4 ambientColor;
    vec4 diffuseColor;
    vec4 specularColor;

    float specularComponent;
    float alpha;
    int illuminationModel;
};
...
layout (std140) uniform MaterialsBlock {
    MaterialStruct materials[8];
} u_materials;

要获得materials[i]补偿,我尝试下一步执行:

m_materialsLocation = glGetUniformBlockIndex(m_program.id(), "MaterialsBlock");
GLint blockSize;
std::vector<const GLchar *> names({
        "MaterialsBlock.MaterialsStruct",
        "u_materials.MaterialsStruct",
        "u_materials",
        "u_materials.materials",
        "u_materials.materials[0]",
        "u_materials.materials.ambientTexture",
        "u_materials.materials[0].ambientTexture",
        "MaterialsBlock.materials",
        "MaterialsBlock.materials[0]",
        "MaterialsBlock.materials.ambientTexture",
        "MaterialsBlock.materials[0].ambientTexture",
        "materials",
        "materials[0]",
        "materials.ambientTexture",
        "materials[0].ambientTexture",
});
std::vector<GLuint> indices(names.size());
std::vector<GLint> offsets(names.size());

glGetActiveUniformBlockiv(m_program.id(), m_materialsLocation, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
glGetUniformIndices(m_program.id(), names.size(), names.data(), indices.data());
glGetActiveUniformsiv(m_program.id(), indices.size(), indices.data(), GL_UNIFORM_OFFSET, offsets.data());
for (int i = 0; i < names.size(); ++i) {
    std::cout << names[i] << ": " << indices[i] << " offset " << offsets[i] << std::endl;
}

std::cout << "Materials block size " << blockSize << std::endl;

但是没有一次尝试返回有效值:

MaterialsBlock.MaterialsStruct: 4294967295 offset 0
u_materials.MaterialsStruct: 4294967295 offset 0
MaterialsStruct: 4294967295 offset 0
u_materials: 4294967295 offset 0
u_materials.materials: 4294967295 offset 0
u_materials.materials[0]: 4294967295 offset 0
u_materials.materials.ambientTexture: 4294967295 offset 0
u_materials.materials[0].ambientTexture: 4294967295 offset 0
MaterialsBlock.materials: 4294967295 offset 0
MaterialsBlock.materials[0]: 4294967295 offset 0
MaterialsBlock.materials.ambientTexture: 4294967295 offset 0
MaterialsBlock.materials[0].ambientTexture: 4294967295 offset 0
materials: 4294967295 offset 0
materials[0]: 4294967295 offset 0
materials.ambientTexture: 4294967295 offset 0
materials[0].ambientTexture: 4294967295 offset 0
Materials block size 636

据我所知,std140布局会创建下一个偏移:

item          size  offset
materials[0]  76    0
materials[1]  76    80
...
materials[7]  76    560

但有两个问题: 我该如何正确读取数组中每个元素的偏移量? 为什么统一块的大小是636字节而不是640?

0 个答案:

没有答案