Trying to modify the triangle vertex color values from the Android Developer OpenGL Tutorial. The triangle renders but appears dark. What is wrong with the color buffer?
public class Triangle {
...
Added the following lines to establish a color buffer. Is this necessary?
private FloatBuffer colorBuffer;
static final int COLORS_PER_VERTEX = 4;
static float triangleColors[] = {
0.6f, 0.2f, 0.2f, 1.0f,
0.2f, 0.6f, 0.2f, 1.0f,
0.9f, 0.9f, 0.2f, 1.0f
};
private final int colorStride = COLORS_PER_VERTEX * 4;
With the following shader codes, replaced the original "uniform vec4 vColor" with attribute instead of varying because there is no GLES20.getVaryingLocation.
private final String vertexShaderCode =
"attribute vec4 vPosition;void main(){gl_Position = vPosition;}";
private final String fragmentShaderCode =
"precision mediump float;" +
//originally uniform, use varying?
"attribute vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;"+
"}";
In the constructor:
public Triangle()
{
...
ByteBuffer cb = ByteBuffer.allocateDirect(triangleColors.length * 4);
cb.order(ByteOrder.nativeOrder());
colorBuffer = cb.asFloatBuffer();
colorBuffer.put(triangleColors);
colorBuffer.position(0);
... //compile and link shaders code is unchanged
}
public void draw()
{
GLES20.glUseProgram(mProgram);
...
/*
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
*/
mColorHandle = GLES20.glGetAttribLocation(mProgram, "vColor");
GLES20.glEnableVertexAttribArray(mColorHandle);
GLES20.glVertexAttribPointer(mColorHandle, COLORS_PER_VERTEX,
GLES20.GL_FLOAT, false, colorStride, colorBuffer);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
GLES20.glDisableVertexAttribArray(mPositionHandle);
GLES20.glDisableVertexAttribArray(mColorHandle);
}
}