简体   繁体   English

Android OpenGL ES 2.0色彩缓冲无法正常工作

[英]Android OpenGL ES 2.0 colour buffer not working

I have been following this tutorial: http://developer.android.com/training/graphics/opengl/index.html , and got it all working fine. 我一直在关注这个教程: http//developer.android.com/training/graphics/opengl/index.html ,并且一切正常。 Currently I have one buffer for the vertices position, one for the order that the vertices are drawn. 目前我有一个缓冲区用于顶点位置,一个缓冲区用于绘制顶点的顺序。 I want to add one for the colour at each vertex, but when I do I get error 1282 in GLES20.glUseProgram(). 我想在每个顶点添加一个颜色,但是当我这样做时,我在GLES20.glUseProgram()中得到错误1282。

Any help would be amazing, as I am a bit of a GL noob. 任何帮助都会很棒,因为我有点像GL菜鸟。

Here is my shader code: 这是我的着色器代码:

    static public final String vertexShaderCode =
        "uniform mat4 uMVPMatrix;" +
                "attribute vec4 aPosition;" +
                "attribute vec4 aColor;" + 
                "varying vec4 vColor;" + 
                "void main() {" +
                "  gl_Position = uMVPMatrix * aPosition;" +
                "  vColor = aColor;" +
                "}";

static public final String fragmentShaderCode =
        "varying vec4 vColor;" +
                "void main() {" +
                "  gl_FragColor = vColor;" +
                "}";

Which I load with: 我加载的是:

    int shaderV = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(shaderV, vertexShaderCode);
    GLES20.glCompileShader(shaderV);

    int shaderF = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(shaderF, fragmentShaderCode);
    GLES20.glCompileShader(shaderF);

I set up the program like this: 我设置了这样的程序:

    int vertexShader = ShaderInfo.GetVertexShader();
    int fragmentShader = ShaderInfo.GetFragmentShader();

    mProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program
    MyGLRenderer.checkGlError("glCreateProgram");
    GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
    MyGLRenderer.checkGlError("glAttachShader");
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    MyGLRenderer.checkGlError("glAttachShader");
    GLES20.glLinkProgram(mProgram);                  // creates OpenGL ES program executables
    MyGLRenderer.checkGlError("glLinkProgram");

At each timestep/loop/iteration/whatever I call this: 在每个时间步/循环/迭代/无论我称之为:

    GLES20.glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
    MyGLRenderer.checkGlError("glClearColor");

    // Redraw background color
    //GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    MyGLRenderer.checkGlError("glClear");

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
    MyGLRenderer.checkGlError("setLookAtM");

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
    MyGLRenderer.checkGlError("multiplyMM");

    // Draw shape
    rect.Draw(mMVPMatrix);

Where rect.Draw(); 其中rect.Draw(); is given by: 是(谁)给的:

    MyGLRenderer.checkGlError("BEFOREglUseProgram");
    GLES20.glUseProgram(mProgram);
    MyGLRenderer.checkGlError("glUseProgram");

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
    MyGLRenderer.checkGlError("glGetAttribLocationPosition");
    // Enable a handle to the triangle vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

    // get handle to fragment shader's aColor member
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "aColor");
    // Set color for drawing the triangle
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    // get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    MyGLRenderer.checkGlError("glGetUniformLocation");

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    MyGLRenderer.checkGlError("glUniformMatrix4fv");

    GLES20.glDrawElements(
            GLES20.GL_TRIANGLES, drawOrder.length,
            GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);

Finally, I create the buffers like so: 最后,我像这样创建缓冲区:

private short drawOrder[] = { };
private void SetDrawOrder()
{
    if(drawOrder.length != elements.size()*3)
        drawOrder = new short[elements.size()*3];
    for(int i = 0; i< elements.size(); i++)
    {
        drawOrder[i*3 + 0] = (short)elements.get(i).nodes.get(0).indexInMesh;
        drawOrder[i*3 + 1] = (short)elements.get(i).nodes.get(1).indexInMesh;
        drawOrder[i*3 + 2] = (short)elements.get(i).nodes.get(2).indexInMesh;
    }

    // initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);
}

private void AddNodesToArray()
{

    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
            // (number of coordinate values * 4 bytes per float)
            vertexCount * COORDS_PER_VERTEX * 4);
    // use the device hardware's native byte order
    bb.order(ByteOrder.nativeOrder());

    // create a floating point buffer from the ByteBuffer
    vertexBuffer = bb.asFloatBuffer();
    // add the coordinates to the FloatBuffer
    for(int i = 0; i< nodes.size(); i++)
        vertexBuffer.put(nodes.get(i).position);
    // set the buffer to read the first coordinate
    vertexBuffer.position(0);
}

private void AddColoursToArray()
{

    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
            // (number of coordinate values * 4 bytes per float)
            vertexCount * 4 * 4);
    // use the device hardware's native byte order
    bb.order(ByteOrder.nativeOrder());

    // create a floating point buffer from the ByteBuffer
    colourBuffer = bb.asFloatBuffer();
    // add the coordinates to the FloatBuffer
    for(int i = 0; i< nodes.size(); i++)
        colourBuffer.put(nodes.get(i).colour);
    // set the buffer to read the first coordinate
    colourBuffer.position(0);
}

This code 这段代码

// get handle to fragment shader's aColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "aColor");
// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);

is not going to work. 不会起作用。 aColor is not a uniform in your shaders, but a vertex attribute. aColor在着色器中不是一个统一的,而是一个顶点属性。 You never set up a vertex attrib pointer for your attribute. 您永远不会为属性设置顶点属性指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM