C++ 统一块和缓冲区

C++ 统一块和缓冲区,c++,opengl,glsl,C++,Opengl,Glsl,我一直在尝试实现一个类,该类使用OpenGL 3.3创建并允许修改统一缓冲区对象。不管统一块定义中的布局限定符是什么(std140、共享还是打包),该类都应该是可用的。在我重构phong着色器以使用统一块而不是统一块之前,它似乎工作得很好。从那时起,应用程序无法在phong着色器中找到统一块,但对于所有其他着色器都可以很好地工作。。 这让我相信这一定是我在重新格式化phong着色器时犯的错误,但我已经查看了无数次,什么也找不到。我能想到的唯一一件事是,编译器正在从着色器中删除统一块,因为它认为它

我一直在尝试实现一个类,该类使用OpenGL 3.3创建并允许修改统一缓冲区对象。不管统一块定义中的布局限定符是什么(std140、共享还是打包),该类都应该是可用的。在我重构phong着色器以使用统一块而不是统一块之前,它似乎工作得很好。从那时起,应用程序无法在phong着色器中找到统一块,但对于所有其他着色器都可以很好地工作。。 这让我相信这一定是我在重新格式化phong着色器时犯的错误,但我已经查看了无数次,什么也找不到。我能想到的唯一一件事是,编译器正在从着色器中删除统一块,因为它认为它们没有被使用,但它们肯定正在被使用,所以不可能是这样。 着色器肯定已成功编译,并且在调用glGetUniformBlockIndex()之前已链接

每次创建着色器时,将查找当前使用的两个统一块,如果找到,则将其绑定到绑定点

    programID = glCreateProgram();

    glAttachShader(programID, vertexID);
    glAttachShader(programID, fragmentID);

    glLinkProgram(programID);
    glUseProgram(programID);

    GLuint matricesIndex = glGetUniformBlockIndex(programID, "MatrixBlock");
    if (matricesIndex == GL_INVALID_INDEX)
        cout << "Could not get index for uniform block MatrixBlock in " << name << endl;
    glUniformBlockBinding(programID, matricesIndex, 0);

    GLuint lightsIndex = glGetUniformBlockIndex(programID, "LightBlock");
    if (lightsIndex == GL_INVALID_INDEX)
        cout << "Could not get index for uniform block LightBlock in " << name << endl;
    glUniformBlockBinding(programID, lightsIndex, 1);
然后使用GLGetUniformIndicates和GLGetActiveUniformOrmsiv正确填充缓冲区。但是这种命名元素的方法对我来说不起作用,使用实例名也不起作用,我也看到了一些地方。 对我有效的方法是使用元素名称,例如:

GLchar *names[] = {
"modelMat",
...
...
};
这些差异有什么特别的原因吗

//phong.frag
#version 330

// Some drivers require the following
precision highp float;

struct materialStruct
{
    vec4 ambient;
    vec4 diffuse;
    vec4 specular;
    float shininess;
};

const int maxLights = 30;

vec3 reflection;

layout (std140) uniform LightBlock
{
    int numLights;
    vec4 position[maxLights];
    vec4 direction[maxLights];
    vec4 ambient[maxLights];
    vec4 diffuse[maxLights];
    vec4 specular[maxLights];
    vec3 attenuation[maxLights];
    float cosCutoffAngle[maxLights];
};

uniform materialStruct material;
uniform sampler2D textureUnit0;
uniform bool textured = true;

in VS_OUT
{
    vec3 vs_normalVec;
    vec3 vs_vertexVec;
    vec2 vs_texCoord;
    vec3 vs_lightVec[maxLights];
    float vs_lightVertDistance[maxLights];
    float vs_attenuation[maxLights];
} fs_in;

layout (location = 0) out vec4 colour;


vec4 calculateLightColour(int i)
{
        //Calculate colour of light and object material.
        //Ambient intensity
    vec4 ambientI = ambient[i] * material.ambient;

        // Diffuse intensity
    vec4 diffuseI =  diffuse[i] * material.diffuse;
    diffuseI = diffuseI * max(dot(fs_in.vs_normalVec, fs_in.vs_lightVec[i]), 0.0) * fs_in.vs_attenuation[i];

        // Specular intensity
    vec4 specularI = specular[i] * material.specular;
        //Specular Intensity = Ks*(R.V)^(alpha)*Is
    specularI = specularI * pow( max( dot( reflection, fs_in.vs_vertexVec ), 0 ), material.shininess ) * fs_in.vs_attenuation[i];

    return ambientI + diffuseI + specularI;
}

vec4 calculateLight(int i)
{
        //Calculate reflection light makes to the surface. 
    reflection = reflect( -fs_in.vs_lightVec[i], fs_in.vs_normalVec );

    if(cosCutoffAngle[i] == 0 && direction[i] == vec4(0.0, 0.0, 0.0, 0.0))
    {
        return calculateLightColour(i);
    }
    else
    {
            //Calculate the direction vector of the light in view space. 
        vec3 spotDirection = normalize((position[i] - normalize(direction[i])).xyz);

            //Get dot product of spotlight direction and vertex vector. 
        float dotProduct = dot(fs_in.vs_lightVec[i], -normalize(direction[i].xyz));
        //float dotProduct = dot(fs_in.vs_lightVec[i], -spotDirection);

        if(dotProduct > cosCutoffAngle[i])
        {
            return calculateLightColour(i);
        }
        else
            return vec4(0.0, 0.0, 0.0, 1.0);
    }
}

void main(void)
{
    vec4 lightColour = vec4(0.0, 0.0, 0.0, 1.0);

    for(int i=0; i<numLights; ++i)
    {
        lightColour += vec4(calculateLight(i).rgb, 0.0);
    }

    if(textured)
        colour = lightColour * texture(textureUnit0, fs_in.vs_texCoord);
    else
        colour = lightColour;
}
GLchar *names[] = {
"MatrixBlock.modelMat",
...
...
};
GLchar *names[] = {
"modelMat",
...
...
};