代码之家  ›  专栏  ›  技术社区  ›  Zelig63

无法获取openGL ES变量的句柄

  •  1
  • Zelig63  · 技术社区  · 7 年前

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

                    "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");
    

    我面临的问题是,为检索 matriceVueCamera , positionLumiere vecteursNormaux 始终返回-1(正确检索其他位置)。

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

    我试图用 glBindAttribLocation 在连接程序之前,但它没有帮助。

    我使用OpenGL ES 3.1。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Rabbid76    7 年前

    局部变量 diffuse 由计算,但不在程序中使用。由于着色器代码由驱动程序优化,因此会导致丢弃所有用于计算的代码 . 由于统一变量 matriceVueCamera positionLumiere vecteursNormaux 只需要计算 弥漫的 ,它们不会成为活动的程序资源。他们不需要处理程序。

    什么时候 v_Color 计算如下:

    v_Color = couleursSommets * diffuse ;
    

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

    着色器可以声明命名的统一变量,如OpenGL ES着色语言规范中所述。这些制服的值在一段时间内保持不变 基本体,通常它们在许多基本体中都是常量。如果编译器和链接器确定统一是活动的,则认为统一是活动的 将在执行可执行代码时实际访问。如果编译器和链接器无法做出结论性决定,则统一将被删除 被认为是积极的。