无法处理 openGL ES 变量

Unable to get handle on openGL ES variables

我正在使用在教程中找到的以下顶点着色器:

                "uniform mat4 matriceDessin;" +
                "uniform mat4 matriceVueCamera;" +
                "uniform vec3 positionLumiere;" +
                "attribute vec4 positionsSommets;" +
                "attribute vec3 vecteursNormaux;" +
                "attribute vec4 couleursSommets;" +

                "varying vec4 v_Color;" +

                "void main() {" +
                    "vec3 sommet,vecteurNormal,directionLumiere;"+
                    "float distance,diffuse;" +

                    "sommet = vec3(matriceVueCamera * positionsSommets);" +
                    "vecteurNormal = vec3(matriceVueCamera * vec4(vecteursNormaux, 0.0));" +
                    "distance = length(positionLumiere - sommet);" +
                    "directionLumiere = normalize(positionLumiere - sommet);" +
                    "diffuse = max(dot(vecteurNormal, directionLumiere), 0.1);" +
                    "diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));" +
                    "v_Color = couleursSommets;" +

                    "gl_Position = matriceDessin * positionsSommets;" +
                "}";

为了获取变量的位置,我在成功创建并链接程序后使用:

            GLES31.glUseProgram(programme);
            pointeurSurPosition = GLES31.glGetAttribLocation(programme, "positionsSommets");
            pointeurSurVecteurNormal = GLES31.glGetAttribLocation(programme, "vecteursNormaux");
            pointeurSurPositionLumière = GLES31.glGetUniformLocation(programme, "positionLumiere");
            pointeurSurMatriceDessin = GLES31.glGetUniformLocation(programme, "matriceDessin");
            pointeurSurMatriceVueCaméra = GLES31.glGetUniformLocation(programme, "matriceVueCamera");

我面临的问题是检索 matriceVueCamerapositionLumierevecteursNormaux 位置的调用总是 return -1(正确检索其他位置).

根据文档,如果变量不是活动的,可能会发生这种情况,但可以注意到它们在着色器中使用,因此它们应该被视为活动的,至少据我了解这个概念。

我试图在链接程序之前用 glBindAttribLocation 将位置绑定到这个变量,但它没有帮助。

有人知道我的问题出在哪里吗?

我使用 Open GL ES 3.1。

局部变量diffuse被程序计算但从未在程序中使用。由于shader代码是驱动优化过的,所以导致所有的代码都被丢弃了,用来计算diffuse。 由于统一变量 matriceVueCamerapositionLumierevecteursNormaux 只是计算 diffuse 所必需的,它们不会成为活动的程序资源。他们不需要处理程序。

可能你错过了使用 diffuse,当计算 v_Color 时:

v_Color = couleursSommets * diffuse ;

参见OpenGL ES Shading Language 3.00 Specification - 2.12.6 Uniform Variables; page 58

7.6 Uniform Variables

Shaders can declare named uniform variables, as described in the OpenGL ES Shading Language Specification. Values for these uniforms are constant over a primitive, and typically they are constant across many primitives. A uniform is considered active if it is determined by the compiler and linker that the uniform will actually be accessed when the executable code is executed. In cases where the compiler and linker cannot make a conclusive determination, the uniform will be considered active.