Opengles 2.0 multiple line colors

Click For Summary
The discussion revolves around a user's challenge with OpenGL ES 2.0 while developing an Android application using Eclipse ADT. They have successfully rendered multiple lines on the screen but are struggling to assign different colors to each line. The user has shared their current implementation, which includes vertex and fragment shader code, and the OpenGL drawing commands. They are seeking feedback on how to modify their code to enable per-vertex coloring, as they suspect this approach may resolve their issue. The user has recently gained access to the OpenGL Khronos forum but is still looking for immediate assistance. Key points include the need for adjustments in the draw code and shader setup to facilitate multiple colors for the lines being rendered.
MikeGomez
Messages
343
Reaction score
16
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:
...
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;" +
            "}";

...
 
Last edited by a moderator:
Technology news on Phys.org
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
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...