OpenGL ES 3.0 Java绘制带有颜色数组的顶点(每个顶点的颜色不同)

问题描述 投票:1回答:1

我有线,我想用数组的颜色绘制它们。

颜色数组是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)。

问题是什么?

java android graphics opengl-es opengl-es-3.0
1个回答
1
投票

glEnableClientStateglColorPointer不是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);
© www.soinside.com 2019 - 2024. All rights reserved.