2

I have lines and I want to draw them with the colors of an array.

The color array is a FloatBuffer with RGBA data for each vertex.

I tried it this way, but it doesn't work:

The shader codes:

private final String vertexShaderCode =
    "uniform mat4 uMVPMatrix;" +
    "attribute vec4 vPosition;" +
    "void main() {" +
    "  gl_Position = vPosition;" + 
    "}";

private final String fragmentShaderCode =
    "precision mediump float;" +
    "uniform vec4 vColor;" + 
    "void main() {" +
    "  gl_FragColor = vColor;" + 
    "}";

The drawing method:

public void draw(GL10 gl)
{
    GLES30.glUseProgram(mProgram);

    mPositionHandle = GLES30.glGetAttribLocation(mProgram, "vPosition");

    GLES30.glEnableVertexAttribArray(mPositionHandle);

    GLES30.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES30.GL_FLOAT, false, vertexStride, vertexBuffer);

    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

    gl.glColorPointer(vertexBufferSize / 4 / 4, GL10.GL_FLOAT, 4*4, colorBuffer);

    GLES30.glDrawArrays(GLES30.GL_LINES, 0, vertexBufferSize / 4 / COORDS_PER_VERTEX);

    GLES30.glDisableVertexAttribArray(mPositionHandle);

    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}

I use 2 coordinates to set a vertex (COORDS_PER_VERTEX = 2).

What is the problem?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Gats János
  • 221
  • 1
  • 14

1 Answers1

5

glEnableClientState and glColorPointer are not part of OpenGL ES. They are part of Legacy OpenGL and deprecated Fixed Function Pipeline.

If you use a uniform variable for the color, then the entire line becomes colored in the color which is set to the uniform variable:

int color_loc = GLES30.glGetUniformLocation(mProgram, "vColor")
GLES30.glUseProgram(mProgram);
GLES30.glUniform4f(color_loc, 1.0, 0.0, 0.0, 1.0); // red  

mPositionHandle = GLES30.glGetAttribLocation(mProgram, "vPosition");
GLES30.glEnableVertexAttribArray(mPositionHandle);
GLES30.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES30.GL_FLOAT, false, vertexStride, vertexBuffer);

GLES30.glDrawArrays(GLES30.GL_LINES, 0, vertexBufferSize / 4 / COORDS_PER_VERTEX);
GLES30.glDisableVertexAttribArray(mPositionHandle);


But, If you want to define a different color attribute for each vertex attribute, then you have to declare a color attribute, similar like you do it for the vertex coordinates. And you have to pass the attribute from the vertex shader to the fragment shader:

private final String vertexShaderCode =
    "uniform mat4 uMVPMatrix;" +

    "attribute vec4 aPosition;" +
    "attribute vec4 aColor;" +

    "varying vec4 vColor;" +

    "void main() {" +
    "    vColor      = aColor;" +
    "    gl_Position = aPosition;" + 
    "}";

private final String fragmentShaderCode =
    "precision mediump float;" +

    "varying vec4 vColor;" +

    "void main() {" +
    "    gl_FragColor = vColor;" + 
    "}";

Define the color attributes as you do it with the vertex coordinate attributes:

mPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");
mColorHandle    = GLES30.glGetAttribLocation(mProgram, "aColor");

GLES30.glEnableVertexAttribArray(mPositionHandle);
GLES30.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES30.GL_FLOAT, false, vertexStride, vertexBuffer);

GLES30.glEnableVertexAttribArray(mColorHandle);
GLES30.glVertexAttribPointer(mColorHandle, 4, GLES30.GL_FLOAT, false, 0, colorBuffer);

GLES30.glDrawArrays(GLES30.GL_LINES, 0, vertexBufferSize / 4 / COORDS_PER_VERTEX);
GLES30.glDisableVertexAttribArray(mPositionHandle);
GLES30.glDisableVertexAttribArray(mColorHandle);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I manage to fix it I called the function glGetUniformLocation to get the color handler instead of glGetAttribLocation. Thanks for your help. – slaviboy May 14 '20 at 16:30