颜色不适用于大型FloatBuffer,格式为X,Y,Z,R,G,B,A,X,Y,Z,R,

时间:2014-04-08 19:27:21

标签: android opengl-es

在我使用OpenGL的Android应用程序中,我想使用大型FloatBuffers绘制颜色的线条,其中我将所有东西都包含在内,与颜色一起坐标,以获得更好的性能。我成功地在正确的坐标上绘制线条,但颜色仍然是黑色。我从http://www.learnopengles.com/android-lesson-one-getting-started的例子开始。唯一的区别(我可以看到)是,他们使用三角形,我使用线条。所以我的FloatBuffers有格式化(X,Y,Z,R,G,B,A,X,Y,Z,R,G,B,A,X,Y,Z,R,G,B,A ,. ..等等。每一行只有一种颜色,所以颜色重复自身的开始和终点(14中的4个浮点数是多余的,我不在乎)。 这是我的Renderer类的相关代码:

private void drawStuff()
{
    int mPositionDataSize = 3;
    int mColorDataSize = 4;
    int mColorOffset = 3 ;

    mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
    mColorHandle = GLES20.glGetUniformLocation(mProgramHandle, "a_Color");
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");

    //1. vertex positions:
    thisBuffer.getVertexFloatBuffers().position(0);
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, 
                                    GLES20.GL_FLOAT, false, 
                                    28, thisBuffer.getVertexFloatBuffers() );
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    //2. colors:
    thisBuffer.getVertexFloatBuffers().position(mColorOffset);
    GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, 
                                    GLES20.GL_FLOAT, false, 
                                    28, thisBuffer.getVertexFloatBuffers()) ;
    GLES20.glEnableVertexAttribArray(mColorHandle);

    // 3. Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, thisMVPmatrix, 0);

    // 4. Draw the lines
    GLES20.glDrawArrays(GLES20.GL_LINES, 0, thisBuffer.getNrOfVerteces());
}

想法,有人吗?

编辑:我的着色器是在我的onSurfaceCreated方法中定义和编译的:

    final String vertexShaderPerLayer =
    "uniform mat4 u_MVPMatrix;      \n"     // A constant representing the combined model/view/projection matrix.

  + "attribute vec4 a_Position;     \n"     // Per-vertex position information we will pass in.
  + "attribute vec4 a_Color;        \n"     // Per-vertex color information we will pass in.

  + "varying vec4 v_Color;          \n"     // This will be passed into the fragment shader.

  + "void main()                    \n"     // The entry point for our vertex shader.
  + "{                              \n"
  + "   v_Color = a_Color;          \n"     // Pass the color through to the fragment shader.
                                            // It will be interpolated across the triangle.
  + "   gl_Position = u_MVPMatrix   \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
  + "}                              \n";    // normalized screen coordinates.

int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
// Pass in the shader source.
GLES20.glShaderSource(vertexShaderHandle, vertexShaderPerLayer);


// Compile the shader.
GLES20.glCompileShader(vertexShaderHandle);

// Get the compilation status.
final int[] compileStatus = new int[1];
GLES20.glGetShaderiv(vertexShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

// FRAGMENTSHADER
final String fragmentShaderPerLayer =
        "precision mediump float;       \n"     // Set the default precision to medium. We don't need as high of a
                                                // precision in the fragment shader.
      + "varying vec4 v_Color;          \n"     // This is the color from the vertex shader interpolated across the
                                                // triangle per fragment.
      + "void main()                    \n"     // The entry point for our fragment shader.
      + "{                              \n"
      + "   gl_FragColor = v_Color;     \n"     // Pass the color directly through the pipeline.
      + "}                              \n";
int fragmentShaderHandlePerLayer = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);

// Pass in the shader source.
GLES20.glShaderSource(fragmentShaderHandlePerLayer, fragmentShaderPerLayer);

// Compile the shader.
GLES20.glCompileShader(fragmentShaderHandlePerLayer);

// PROGRAM STUFF
mProgramHandle = GLES20.glCreateProgram();

// Bind the vertex shader to the program.
GLES20.glAttachShader(mProgramHandle, vertexShaderHandle);

// Bind the fragment shader to the program.
GLES20.glAttachShader(mProgramHandle, fragmentShaderHandlePerLayer);

// Bind attributes
GLES20.glBindAttribLocation(mProgramHandle, 0, "a_Position");
GLES20.glBindAttribLocation(mProgramHandle, 1, "a_Color");

// Link the two shaders together into a program.
GLES20.glLinkProgram(mProgramHandle);

1 个答案:

答案 0 :(得分:0)

代码中出现错误。

mColorHandle = GLES20.glGetUniformLocation(mProgramHandle, "a_Color");

应该是:

mColorHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Color");