試圖修改Android Developer OpenGL Tutorial中的三角形頂點顏色值。三角形呈現,但顯得黑暗。顏色緩衝區有什麼問題?OpenGL ES 2.0中片段着色器的非均勻顏色值
public class Triangle {
...
添加了以下幾行來建立顏色緩衝區。這是必要的嗎?
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;
用下面的着色器代碼,取代了原來的「均勻vec4 vColor」與屬性,而不是改變,因爲沒有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;"+
"}";
在構造函數中:
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);
}
}
太棒了,這清除了它。感謝徹底的迴應! –