0xCursor
0xCursor

Reputation: 2268

How to use a color buffer in OpenGL ES 2

I am a bit confused on how to draw color using a color buffer. I found a similar question here and made my shader the same as shown in the post's accepted answer. I then used the code:

mColorHandle = GLES20.glGetAttribLocation(Shader, "vColor");
GLES20.glEnableVertexAttribArray(mColorHandle);

ByteBuffer cb = ByteBuffer.allocateDirect(color.length * BYTES_PER_FLOAT);
cb.order(ByteOrder.nativeOrder());
colorBuffer = cb.asFloatBuffer();
colorBuffer.put(color);
colorBuffer.position(0);

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, cbo);
GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, colorBuffer.capacity(), colorBuffer);
GLES20.glVertexAttribPointer(mColorHandle, 4,
                GLES20.GL_FLOAT, false,
                0, 0);

in attempt to draw the color.

The shape displayed the color I was trying to draw but it faded out the color across the shape like this:

enter image description here

If someone could tell me what's going wrong and how I could get the shape to be all the same color, I would appreciate it.

Upvotes: 0

Views: 485

Answers (1)

0xCursor
0xCursor

Reputation: 2268

Thanks to Rabbid76 for helping me find the mistake.

Instead of 4 elements total in the color array, there needs to be 16, an RGBA value for each vertex. (4 elements of the array are used to make one RGBA value.)

Upvotes: 1

Related Questions