Opengles 2.0 multiple line colors

In summary, the individual has been having trouble receiving answers on Stackoverflow and opening a new thread on the Khronos forum for their question about OpenGL ES 2.0. They have searched the internet for a week and have not found a solution, so they are seeking feedback on their own code. They are using Eclipse ADT for Android target and are able to display multiple lines on the screen, but are struggling to make each line a different color. They have also been able to post to the Khronos forum, but still need assistance. Their best lead is to make changes to the vertex and fragment shader code, specifically the draw code.
  • #1
MikeGomez
344
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
  • #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
 

Related to Opengles 2.0 multiple line colors

What is Opengles 2.0 multiple line colors?

Opengles 2.0 multiple line colors refers to the ability to have more than one color on a single line when using the Opengles 2.0 graphics library. This allows for more complex and visually appealing graphics.

Why is Opengles 2.0 multiple line colors important?

Opengles 2.0 multiple line colors allows for more flexibility and creativity in designing graphics. It also helps to create a more realistic and dynamic visual experience for the user.

How do you implement Opengles 2.0 multiple line colors?

To implement Opengles 2.0 multiple line colors, you will need to use functions such as glLineWidth() and glColor3f() to set the line width and color, respectively. You will also need to enable the GL_BLEND and GL_LINE_SMOOTH parameters for smooth blending of colors.

Can Opengles 2.0 multiple line colors be used in 3D graphics?

Yes, Opengles 2.0 multiple line colors can be used in 3D graphics. The same functions and parameters can be used to set the line width and color for 3D objects and lines.

Are there any limitations to Opengles 2.0 multiple line colors?

There are some limitations to Opengles 2.0 multiple line colors, such as the number of colors that can be used on a single line may be limited depending on the hardware and resources available. Additionally, some older devices may not support Opengles 2.0 multiple line colors.

Back
Top