为什么GLES20.glGetAttribLocation为不同的设备返回不同的值?

时间:2016-09-22 15:04:36

标签: android opengl-es opengl-es-2.0 vertex-shader

我尝试使用OpenGL2.0并在3D空间中为Android设备创建多个立方体。

它在某些设备中运行的代码非常完美,但在其他设备中却没有,我不知道为什么......(所有设备都支持OpenGL 2.0,最近的Android版本为[5.0或6.0])

我只知道问题是-1返回值(见下)

int vertexShader = loadGLShader(GLES20.GL_VERTEX_SHADER, R.raw.vertex);
int gridShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, R.raw.grid_fragment);
int passthroughShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, R.raw.fragment_color);

     cubeProgram1 = GLES20.glCreateProgram();
     GLES20.glAttachShader(cubeProgram1, vertexShader);
     GLES20.glAttachShader(cubeProgram1, passthroughShader);
     GLES20.glLinkProgram(cubeProgram1);
     GLES20.glUseProgram(cubeProgram1);

    cubePositionParam1 = GLES20.glGetAttribLocation(cubeProgram1, "a_Position");
    cubeNormalParam1 = GLES20.glGetAttribLocation(cubeProgram1, "a_Normal"); ----> Returns -1 Value
    cubeLightPosParam1 = GLES20.glGetUniformLocation(cubeProgram1, "u_LightPos");
    cubeModelParam1 = GLES20.glGetUniformLocation(cubeProgram1, "u_Model") cubeModelViewParam1 = GLES20.glGetUniformLocation(cubeProgram1, "u_MVMatrix");
    cubeModelViewProjectionParam1 = GLES20.glGetUniformLocation(cubeProgram1, "u_MVP");

...

 GLES20.glEnableVertexAttribArray(cubeNormalParam1); ---> Returns -1 ... 

cubeNormalParam1 = GLES20.glGetAttribLocation(cubeProgram1, "a_Normal");在某些设备中返回值2,在其他设备中返回-1。那些有-1值,给出错误并且没有运行的人......

我正在尝试查看vertex_shader是否有错误,但我看不到任何问题...

uniform mat4 u_Model;
uniform mat4 u_MVP;
uniform mat4 u_MVMatrix;
uniform vec3 u_LightPos;

attribute vec4 a_Position;
attribute vec4 a_Color;
attribute vec3 a_Normal;
attribute vec2 a_TexCoordinate;

varying vec4 v_Color;
varying vec3 v_Grid;
varying vec2 v_TexCoordinate;

void main() {
   v_Grid = vec3(u_Model * a_Position);


   vec3 modelViewVertex = vec3(u_MVMatrix * a_Position);
   vec3 modelViewNormal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));

   float distance = length(u_LightPos - modelViewVertex);
   vec3 lightVector = normalize(u_LightPos - modelViewVertex);
   float diffuse = max(dot(modelViewNormal, lightVector), 0.5);

   diffuse = diffuse * (1.0 / (1.0 + (0.00001 * distance * distance)));
   v_Color = a_Color * diffuse;

   v_TexCoordinate = a_TexCoordinate;

   gl_Position = u_MVP * a_Position;
}

为什么会这样?是硬件吗?

解决此问题的任何解决方案?

要绘制立方体我是这样的:

public void drawCube1() {
        cubeNumber = 1;

        GLES20.glUseProgram(cubeProgram1);
        GLES20.glUniform3fv(cubeLightPosParam1, 1, lightPosInEyeSpace, 0);
        // Set the Model in the shader, used to calculate lighting
        GLES20.glUniformMatrix4fv(cubeModelParam1, 1, false, modelCube, 0);
        // Set the ModelView in the shader, used to calculate lighting
        GLES20.glUniformMatrix4fv(cubeModelViewParam1, 1, false, modelView, 0);
        // Set the ModelViewProjection matrix in the shader.
        GLES20.glUniformMatrix4fv(cubeModelViewProjectionParam1, 1, false, modelViewProjection1, 0);
        // Set the position of the cube
        GLES20.glVertexAttribPointer(
                cubePositionParam1, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, cubeVertices);
        // Set the normal positions of the cube, again for shading
        GLES20.glVertexAttribPointer(cubeNormalParam1, 3, GLES20.GL_FLOAT, false, 0, cubeNormals);
        // Set the active texture unit to texture unit 0.
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        // Bind the texture to this unit.
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextura1);

        isLookingAtObject_Number(cubeNumber);

        // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
        GLES20.glUniform1i(mTextureUniform, 0);

        GLES20.glVertexAttribPointer(mTextureCoordinate, 2, GLES20.GL_FLOAT, false, 0, cubeTexture);
        GLES20.glEnableVertexAttribArray(mTextureCoordinate);

        GLES20.glEnableVertexAttribArray(cubePositionParam1);
        GLES20.glEnableVertexAttribArray(cubeNormalParam1);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 36);
        checkGLError("Drawing cube");
    }

2 个答案:

答案 0 :(得分:5)

Why GLES20.glGetAttribLocation returns different values for different devices?

...因为不同的设备将有不同的硬件来设置绑定表,并且在某些情况下可能会完全优化冗余计算并重新打包使用的属性以节省空间。这完全为什么API有一个函数调用,用于在运行时获取设备上的绑定位置;如果它们是确定性常数,那么根本不需要运行时调用......

它返回-1的事实不是错误;它只是表明属性不是必需的(例如已定义,但可以优化,因为它不会对片段像素颜色做出贡献),所以这不应该导致任何问题。

然而,这确实意味着您无法安全地假设(例如)a_Position始终属性为0,a_Color始终为1,a_Normal始终为2,等等。如果您在代码中对其进行了硬编码,则完全有可能将错误的数据上传到每个属性。您必须查询绑定位置以获取要使用的绑定号(或使用OpenGL ES 3.x中添加的着色器侧静态绑定)。

哪个实际GL API调用正在设置GL错误状态?

答案 1 :(得分:-1)

解决方案 - 乘以-1值

 ... = GLES20.glGetAttribLocation(cubeProgram1, "a_Normal") * -1;

int value;
if( GLES20.glGetAttribLocation(cubeProgram1, "a_Normal") < 0){
      value = -1;
}
else {
      value = 1;
}

... = GLES20.glGetAttribLocation(cubeProgram1, "a_Normal") * value;

不是最佳解决方案,但现在可行。