2017-12-27 1394 views
1

我有行,我想用數組的顏色繪製它們。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)。

什麼問題?

回答

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);