Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Opengles 2.0 multiple line colors

  1. Dec 15, 2013 #1
    I've posted to Stackoverflow and no one answers, and I've signed up for Khronos but it won't let me open a new thread for a question, so I was hoping maybe someone here knows something about opengl es 2.0.

    I've searched the internet for about a week now and ran into a lot of promising leads but nothing works out so I need some feedback on my own code instead. Thanks.

    I'm currently using Eclipse ADT for Android target.

    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. Here is the current working code.

    Code (Text):
    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
            // get handle to vertex shader's vPosition member
            mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
            // Enable a handle to the line vertices
            // 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");
            // Apply the projection and view transformation
            GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
            if(g_NumLineList > 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


        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;" +

    Last edited by a moderator: Dec 15, 2013
  2. jcsd
  3. Dec 15, 2013 #2
    Ok, I have now been able to post to the OpenGL Khronos forum, but in the mean time I still need help.

    The best lead I think I have is that there is some way to tell the vertex shader and the fragment shader to use colors per vertex, but I don't know how to make the changes to the rest of the code such as the draw code...

    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);
    GLES20.glDrawArrays(GLES20.GL_LINES, 0, 2 * g_NumLineList); // 2 verts per line
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook