java - glEnableVertexAttribArray 上的 OpenGL 2.0 ES 1281 错误

标签 java android opengl-es

我正在使用 https://github.com/d3kod/Texample2 中的代码在 OpenGL 2.0 中渲染文本。该项目的解释可以在 http://primalpond.wordpress.com/2013/02/26/rendering-text-in-opengl-2-0-es-on-android/ 找到。

该代码在运行 android 4.1.2 的 Samsung Galaxy S3 上运行良好。但是,它在我运行 android 2.3.4 的 Droid X 上不起作用,并且其他人在 android 4.0.4 Onepad 940 平板电脑上也遇到了此问题(请参阅 http://primalpond.wordpress.com/2013/02/26/rendering-text-in-opengl-2-0-es-on-android/#comment-89 )。

我收到 1281 错误: GLES20.glEnableVertexAttribArray(mColorHandle)

我确定这是因为我的 Droid X 上的 GL_MAX_VERTEX_ATTRIBS 只有 8,而 mColorHandle 设置为 26。当我在三星上运行应用程序时GS3,GL_MAX_VERTEX_ATTRIBS 为 16,但 mColorHandle 设置为 0。

因此,从 26>8 开始,就会抛出 1281 错误。我不明白为什么 Droid 的句柄值为 26,而 GS3 的句柄值为 0。

mProgram 程序在对象构造函数的这一行中设置:

mColorHandle = GLES20.glGetUniformLocation(mProgram.getHandle(), "u_Color");

这是产生错误的行:

void initDraw(float red, float green, float blue, float alpha) {
GLES20.glUseProgram(mProgram.getHandle()); // specify the program to use

// set color TODO: only alpha component works, text is always black #BUG
float[] color = {red, green, blue, alpha}; 
GLES20.glUniform4fv(mColorHandle, 1, color , 0); 
GLES20.glEnableVertexAttribArray(mColorHandle);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);  // Set the active texture unit to texture unit 0

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId); // Bind the texture to this unit

// Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0
GLES20.glUniform1i(mTextureUniformHandle, 0); 
//Log.i("error", "glerror3: " + GLES20.glGetError());
}

编辑

这是设置程序的类:

public class BatchTextProgram extends Program {

private static final AttribVariable[] programVariables = {
    AttribVariable.A_Position, AttribVariable.A_TexCoordinate, AttribVariable.A_MVPMatrixIndex
};

private static final String vertexShaderCode =
        "uniform mat4 u_MVPMatrix[24];      \n"     // An array representing the combined 
                                                    // model/view/projection matrices for each sprite

      + "attribute float a_MVPMatrixIndex; \n"  // The index of the MVPMatrix of the particular sprite
      + "attribute vec4 a_Position;     \n"     // Per-vertex position information we will pass in.
      + "attribute vec2 a_TexCoordinate;\n"     // Per-vertex texture coordinate information we will pass in
      + "varying vec2 v_TexCoordinate;  \n"   // This will be passed into the fragment shader.
      + "void main()                    \n"     // The entry point for our vertex shader.
      + "{                              \n"
      + "   int mvpMatrixIndex = int(a_MVPMatrixIndex); \n"
      + "   v_TexCoordinate = a_TexCoordinate; \n"
      + "   gl_Position = u_MVPMatrix[mvpMatrixIndex]   \n"     // gl_Position is a special variable used to store the final position.
      + "               * a_Position;   \n"     // Multiply the vertex by the matrix to get the final point in
                                                // normalized screen coordinates.
      + "}                              \n";    


private static final String fragmentShaderCode =
        "uniform sampler2D u_Texture;       \n"    // The input texture.
        +   "precision mediump float;       \n"     // Set the default precision to medium. We don't need as high of a
        // precision in the fragment shader.
        + "uniform vec4 u_Color;          \n"
        + "varying vec2 v_TexCoordinate;  \n" // Interpolated texture coordinate per fragment.

        + "void main()                    \n"     // The entry point for our fragment shader.
        + "{                              \n"
        + "   gl_FragColor = texture2D(u_Texture, v_TexCoordinate).w * u_Color;\n" // texture is grayscale so take only grayscale value from  
                                                                                   // it when computing color output (otherwise font is always black)
        + "}                             \n";

@Override
public void init() {
    super.init(vertexShaderCode, fragmentShaderCode, programVariables);
}

}

最佳答案

你的逻辑有问题。如果 u_Color 是统一的,则它不能是顶点属性。解决这个问题。

关于java - glEnableVertexAttribArray 上的 OpenGL 2.0 ES 1281 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18495612/

相关文章:

java - 从 mongo 更新内部文档

java - Octave 的 DDE 接口(interface)

java - JFrame 中的动画序列

java - Android异步操作实现

javascript - 我如何从 PhoneGap camera.getPicture 获取文件对象?

android - 我们可以在不连接无人机(实际设备)的情况下使用应用程序测试Dji Sdk吗?通过虚拟设备或使用 PC 模拟器的任何其他方式

ios - 为 Retina 和非 Retina 显示器开发 OpenGL ES

java - 如何向 Vaadin 项目添加新 UI?

ios - GLES2.0 上的 VBO glDrawElements 和 glVertexAttribPointer 什么都不显示

opengl-es - 了解 OpenGL 状态关联