2014-01-15 67 views
0

嗨,我是新的android open gl。我嘗試在open gles 2.0中創建少量2d對象。現在我試圖在android open gles 2.0中畫一條線。我的代碼看起來像在android中打開一條線打開GLES 2.0

public class UserLine 
{ 
    private final String vertexShaderCode = 
      "uniform mat4 uMVPMatrix;" + 
      "attribute vec4 vPosition;" + 
      "void main() {" + 
      " gl_Position = uMVPMatrix * vPosition;" + 
      "}"; 

     private final String fragmentShaderCode = 
      "precision mediump float;" + 
      "uniform vec4 vColor;" + 
      "void main() {" + 
      " gl_FragColor = vColor;" + 
      "}"; 

     private final FloatBuffer vertexBuffer; 
     private final ShortBuffer drawListBuffer; 
     private final int mProgram; 
     private int mPositionHandle; 
     private int mColorHandle; 
     private int mMVPMatrixHandle; 

     static final int COORDS_PER_VERTEX = 3; 
     private final int vertexStride = COORDS_PER_VERTEX * 4; 

     private float[] pathCords = 
      { 
      0.00f, 0.0f, 0.0f, 

      0.5f, 0.3f, 0.0f 
      }; 
     private short[] pathDrawOrder = {0,1} 
     private float[] color = {1.0f, 0.0f, 0.0f, 1.0f}; 
    public UserLine() 
    { 
     ByteBuffer bb = ByteBuffer.allocateDirect(pathCords.length * 4); 
     bb.order(ByteOrder.nativeOrder()); 
     vertexBuffer = bb.asFloatBuffer(); 
     vertexBuffer.put(pathCords); 
     vertexBuffer.position(0); 

     ByteBuffer dlb = ByteBuffer.allocateDirect(pathDrawOrder.length * 2); 
     dlb.order(ByteOrder.nativeOrder()); 
     drawListBuffer = dlb.asShortBuffer(); 
     drawListBuffer.put(pathDrawOrder); 
     drawListBuffer.position(0); 

     int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,vertexShaderCode); 
     int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentShaderCode); 

     mProgram = GLES20.glCreateProgram();    
     GLES20.glAttachShader(mProgram, vertexShader); 
     GLES20.glAttachShader(mProgram, fragmentShader); 
     GLES20.glLinkProgram(mProgram); 
    } 

    public void draw(float[] mvpMatrix) { 

    GLES20.glUseProgram(mProgram); 
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); 
    GLES20.glEnableVertexAttribArray(mPositionHandle); 
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,GLES20.GL_FLOAT, false, 
      vertexStride, vertexBuffer); 

    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); 
    GLES20.glUniform4fv(mColorHandle, 1, color, 0); 
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); 
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); 

    GLES20.glDrawElements(GLES20.GL_LINES, color.length, 
      GLES20.GL_UNSIGNED_SHORT, drawListBuffer); 

    GLES20.glDisableVertexAttribArray(mPositionHandle); 
    GLES20.glDisable(mColorHandle); 
    } 
} 

所以這裏是我的問題,根據這個代碼,它必須制定一個線,但沿這條線也得出未預期的行爲多出一行。 enter image description here

我是在做一些錯誤的事情還是它的默認行爲。需要幫忙。謝謝。

回答

3

我認爲這個問題是在這裏:

GLES20.glDrawElements(GLES20.GL_LINES, color.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer); 

第二個參數是計數,我不明白你爲什麼把它設置爲顏色的長度。這是你進入着色器的線正好顏色,以便長度總是4:

private float[] color = {1.0f, 0.0f, 0.0f, 1.0f}; 

您正在繪製連得2分一條線,在pathDrawOrder指定的,所以我認爲使用的是長度,而不是爲你需要什麼:

GLES20.glDrawElements(GLES20.GL_LINES, pathDrawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer); 
+0

感謝點擊我的錯誤。謝謝。 – nilkash