Multiple lines with more than one color

I’m currently using Eclipse ADT for Android target versions 8-17.

I’m able to get several thousand lines on the the screen and I am happy with that. The problem is I can’t figure how how to make each line a different color.

I guess you have to tell the vertex shader and fragment shader to have color attributes for each vertex, but I don’t know how to make that change reflect in the drawing code, and it seems like then the glUniform4fv() would then not be used?

Here is the current working code.

static final int COORDS_PER_VERTEX = 3;
static float LineCoords[] = new float [3 * 2 * MAX_LINE_LIST];
static float color[][] = new float [MAX_LINE_LIST][4];
static ByteBuffer bb;

public void draw2(float[] mvpMatrix) {
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader’s vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, “vPosition”);

// Enable a handle to the line vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);

// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
    
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
GLES20Renderer.checkGlError("glGetUniformLocation");

// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
GLES20Renderer.checkGlError("glUniformMatrix4fv");
    
if(g_NumLineList > 0)
{
    Bufferptr.put(LineCoords);
    Bufferptr.position(0);
    // Prepare the prim coordinate data
    GLES20.glVertexAttribPointer(mPositionHandle, (COORDS_PER_VERTEX),  GLES20.GL_FLOAT, false, 0, Bufferptr); 		
    // Set color for drawing the line
    GLES20.glUniform4fv(mColorHandle, 1, color[0], 0);         
    // Draw with 2 verts per line
    GLES20.glDrawArrays(GLES20.GL_LINES, 0, 2 * g_NumLineList);    }    

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

}


private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
“uniform mat4 uMVPMatrix;” +
“attribute vec4 vPosition;” +
“void main() {” +
// The matrix must be included as a modifier of gl_Position.
// Note that the uMVPMatrix factor must be first in order
// for the matrix multiplication product to be correct.
" gl_Position = uMVPMatrix * vPosition;" +
“}”;

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

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.