OpenGL ES 3.0 Java使用颜色Array绘制顶点(每个顶点颜色不同)
我有行,我想用数组的颜色绘制它们。OpenGL ES 3.0 Java使用颜色Array绘制顶点(每个顶点颜色不同)
颜色数组是一个FloatBuffer,每个顶点都有RGBA数据。
我尝试了这种方式,但它不工作:
着色器代码:
private final String vertexShaderCode = "uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
的绘图方法:
public void draw(GL10 gl) {
GLES30.glUseProgram(mProgram);
mPositionHandle = GLES30.glGetAttribLocation(mProgram, "vPosition");
GLES30.glEnableVertexAttribArray(mPositionHandle);
GLES30.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES30.GL_FLOAT, false, vertexStride, vertexBuffer);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(vertexBufferSize/4/4, GL10.GL_FLOAT, 4*4, colorBuffer);
GLES30.glDrawArrays(GLES30.GL_LINES, 0, vertexBufferSize/4/COORDS_PER_VERTEX);
GLES30.glDisableVertexAttribArray(mPositionHandle);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
我用2个坐标设置顶点(COORDS_PER_VERTEX = 2)。
什么问题?
回答:
glEnableClientState
和glColorPointer
不是OpenGL ES的一部分。它们是Legacy OpenGL的一部分,并且已弃用Fixed Function Pipeline。
如果您使用统一的变量的颜色,那么整条生产线成为被设置为统一的可变颜色的彩色:
int color_loc = GLES30.glGetUniformLocation(mProgram, "vColor") GLES30.glUseProgram(mProgram);
GLES30.glUniform4f(color_loc, 1.0, 0.0, 0.0, 1.0); // red
mPositionHandle = GLES30.glGetAttribLocation(mProgram, "vPosition");
GLES30.glEnableVertexAttribArray(mPositionHandle);
GLES30.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES30.GL_FLOAT, false, vertexStride, vertexBuffer);
GLES30.glDrawArrays(GLES30.GL_LINES, 0, vertexBufferSize/4/COORDS_PER_VERTEX);
GLES30.glDisableVertexAttribArray(mPositionHandle);
但是,如果你要定义不同的颜色属性为每个顶点属性,那么你必须声明一个颜色属性,就像你为顶点坐标所做的那样。你必须从顶点着色器的属性传递给片段着色器:
private final String vertexShaderCode = "uniform mat4 uMVPMatrix;" +
"attribute vec4 aPosition;" +
"attribute vec4 aColor;" +
"varying vec4 vColor;" +
"void main() {" +
" vColor = aColor;" +
" gl_Position = aPosition;" +
"}";
private final String fragmentShaderCode = "precision mediump float;" +
"varying vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
定义颜色属性,你与顶点做到这一点坐标属性:
mPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition"); mColorHandle = GLES30.glGetAttribLocation(mProgram, "aColor");
GLES30.glEnableVertexAttribArray(mPositionHandle);
GLES30.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES30.GL_FLOAT, false, vertexStride, vertexBuffer);
GLES30.glEnableVertexAttribArray(mColorHandle);
GLES30.glVertexAttribPointer(mColorHandle, 4, GLES30.GL_FLOAT, false, 0, colorBuffer);
GLES30.glDrawArrays(GLES30.GL_LINES, 0, vertexBufferSize/4/COORDS_PER_VERTEX);
GLES30.glDisableVertexAttribArray(mPositionHandle);
GLES30.glDisableVertexAttribArray(mColorHandle);
以上是 OpenGL ES 3.0 Java使用颜色Array绘制顶点(每个顶点颜色不同) 的全部内容, 来源链接: utcz.com/qa/267354.html