Android使用float[]作为顶点会导致OpenGL错误0x502

Android使用float[]作为顶点会导致OpenGL错误0x502,android,opengl-es-2.0,floatbuffer,Android,Opengl Es 2.0,Floatbuffer,我试图在Android上的OpenGLES 2.0中使用一个浮动数组来代替浮动缓冲区,但当我这样做时,glDrawArrays会给出错误0x502或GLU INVALID\U操作 当我使用FloatBuffers时,我没有得到这个错误,并且一切正常 我读到这个错误通常是由于没有设置程序引起的。我只使用一个着色器程序,在初始化所有内容时设置它 这是我的密码 public class LineEngine { private static final float[] IDENTIY = ne

我试图在Android上的OpenGLES 2.0中使用一个浮动数组来代替浮动缓冲区,但当我这样做时,glDrawArrays会给出错误0x502或GLU INVALID\U操作

当我使用FloatBuffers时,我没有得到这个错误,并且一切正常

我读到这个错误通常是由于没有设置程序引起的。我只使用一个着色器程序,在初始化所有内容时设置它

这是我的密码

public class LineEngine {
    private static final float[] IDENTIY = new float[16];
    private float[] mLinePoints;
    private float[] mLineColors;
    private int mCount;

    public LineEngine(int maxLines) {
        Matrix.setIdentityM(IDENTIY, 0);

        mLinePoints = new float[maxLines * 2 * 4];
        mLineColors = new float[maxLines * 2 * 4];

        reset();
    }

    public void addLine(float[] position, float[] color) {
        int offset = mCount * 2 * 4;
        System.arraycopy(position, 0, mLinePoints, offset, 8);
        System.arraycopy(color, 0, mLineColors, offset, 4);
        System.arraycopy(color, 0, mLineColors, offset + 4, 4);

        mCount++;
    }

    public void reset() {
        mCount = 0;
    }

    public void draw() {
        if (mCount > 0) {
            GraphicsEngine.setMMatrix(IDENTIY);
            GraphicsEngine.setColors(mLineColors);
            GraphicsEngine.setVertices4d(mLinePoints);
            GraphicsEngine.disableTexture();
            GLES20.glDrawArrays(GLES20.GL_LINES, 0, mCount * 2);

            int error = GLES20.glGetError();
            if (error != 0)
                Log.e("OpenGL", "Draw " + error); // Prints error 1282

            GraphicsEngine.disableColors();
            reset();
        }
    }
}
这段代码工作正常,没有错误

public class LineEngine {
    private static final float[] IDENTIY = new float[16];
    private FloatBuffer mLinePoints;
    private FloatBuffer mLineColors;
    private int mCount;

    public LineEngine(int maxLines) {
        Matrix.setIdentityM(IDENTIY, 0);

        ByteBuffer byteBuf = ByteBuffer.allocateDirect(maxLines * 2 * 4 * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        mLinePoints = byteBuf.asFloatBuffer();

        byteBuf = ByteBuffer.allocateDirect(maxLines * 2 * 4 * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        mLineColors = byteBuf.asFloatBuffer();

        reset();
    }

    public void addLine(float[] position, float[] color) {
        mLinePoints.put(position, 0, 8);
        mLineColors.put(color, 0, 4);
        mLineColors.put(color, 0, 4);
        mCount++;
    }

    public void reset() {
        mLinePoints.position(0);
        mLineColors.position(0);
        mCount = 0;
    }

    public void draw() {
        if (mCount > 0) {
            mLinePoints.position(0);
            mLineColors.position(0);
            GraphicsEngine.setMMatrix(IDENTIY);
            GraphicsEngine.setColors(mLineColors);
            GraphicsEngine.setVertices4d(mLinePoints);
            GraphicsEngine.disableTexture();
            GLES20.glDrawArrays(GLES20.GL_LINES, 0, mCount * 2);

            int error = GLES20.glGetError();
            if (error != 0)
                Log.e("OpenGL", "Draw " + error); // no errors

            GraphicsEngine.disableColors();
            reset();
        }
    }
}
相关的图形引擎代码在这里

public static void setVertices4d(FloatBuffer vertices) {
    GLES20.glVertexAttribPointer(aVertexHandle, 4, GLES20.GL_FLOAT, false,
            16, vertices);
    GLES20.glEnableVertexAttribArray(aVertexHandle);
    mState.shape = -1;
}

public static void setVertices4d(float[] vertices) {
    GLES20.glVertexAttrib4fv(aVertexHandle, vertices, 0);
    GLES20.glEnableVertexAttribArray(aVertexHandle);
    mState.shape = -1;
}

public static void setColors(FloatBuffer colors){
    GLES20.glVertexAttribPointer(aColor, 4, GLES20.GL_FLOAT, false,
            16, colors);
    GLES20.glEnableVertexAttribArray(aColor);
    GLES20.glUniform1i(GraphicsEngine.uEnableColors, 1);
}

public static void setColors(float[] colors){
    GLES20.glVertexAttrib4fv(aColor, colors, 0);
    GLES20.glEnableVertexAttribArray(aColor);
    GLES20.glUniform1i(GraphicsEngine.uEnableColors, 1);
}

我不想使用FloatBuffers,因为它们的更改速度比一个float数组慢。

您别无选择,只能对这段代码使用FloatBuffers


您的
setVertices4d
方法采用
float[]
已损坏,您不能以这种方式使用glVertexAttrib4fv。glVertexAttrib4仅指定单个顶点,使用v版本仅将属性值作为数组传递给该单个顶点,它不会设置类似于指针函数的顶点数组

您别无选择,只能对这段代码使用FloatBuffers


您的
setVertices4d
方法采用
float[]
已损坏,您不能以这种方式使用glVertexAttrib4fv。glVertexAttrib4仅指定单个顶点,使用v版本仅将属性值作为数组传递给该单个顶点,它不会设置类似于指针函数的顶点数组

你可以使用glUniform4fv通过一个长度超过1的花车[],对吗?我在代码的另一个部分中做了这件事,它正在工作。是不是顶点属性的某些独特之处使它无法实现?查看它时,会指定一个指向要用于常规顶点属性的值数组的指针。这听起来好像你可以传递多个值。制服和属性是完全不同的。制服只是一个始终不变的单一值。当您指定属性时,您正在创建一个大的属性列表,这样每个顶点都有一个属性。glVertexAttrib一次为单个顶点指定属性。如果你想定义一个顶点数组,那么你必须在FloatBuffers中使用glvertexattributepointer。你知道我在哪里可以找到关于所有这些的文档吗?我只知道你找到的sdk/文档是我知道的唯一来源,你花在上面的时间越多,你就会学到这些东西。我可以看到你是从哪里来的,你会认为你可以用它来指定一个顶点数组,但它就是不能这样工作。我想你会使用该函数的向量形式,例如,如果你正在进行多文本处理,并且你想将多个texcoords数组传递给每个顶点。
glVertexAttribX
只能设置单个顶点和单个属性的属性值。因此,这种多文本的想法也不会以这种方式起作用。任何大于4个元素的数组对该函数都没有任何意义,如果顶点属性是4个值的向量,该函数就存在。但当然,总体答案是正确的,并且您(OP)以错误的方式使用此函数。由于您(OP)无论如何都启用了属性数组,因此使用
glVertexAttrib
设置的值无论如何都不会使用,因为它们仅在禁用相应数组时才起作用。您可以使用glUniform4fv传入长度大于1的浮点[],对吗?我在代码的另一个部分中做了这件事,它正在工作。是不是顶点属性的某些独特之处使它无法实现?查看它时,会指定一个指向要用于常规顶点属性的值数组的指针。这听起来好像你可以传递多个值。制服和属性是完全不同的。制服只是一个始终不变的单一值。当您指定属性时,您正在创建一个大的属性列表,这样每个顶点都有一个属性。glVertexAttrib一次为单个顶点指定属性。如果你想定义一个顶点数组,那么你必须在FloatBuffers中使用glvertexattributepointer。你知道我在哪里可以找到关于所有这些的文档吗?我只知道你找到的sdk/文档是我知道的唯一来源,你花在上面的时间越多,你就会学到这些东西。我可以看到你是从哪里来的,你会认为你可以用它来指定一个顶点数组,但它就是不能这样工作。我想你会使用该函数的向量形式,例如,如果你正在进行多文本处理,并且你想将多个texcoords数组传递给每个顶点。
glVertexAttribX
只能设置单个顶点和单个属性的属性值。因此,这种多文本的想法也不会以这种方式起作用。任何大于4个元素的数组对该函数都没有任何意义,如果顶点属性是4个值的向量,该函数就存在。但当然,总体答案是正确的,并且您(OP)以错误的方式使用此函数。由于您(OP)无论如何都启用了属性数组,因此使用
glVertexAttrib
设置的值无论如何都不会使用,因为它们仅在禁用相应数组时才起作用。