2014-10-04 43 views
0

我正在屏幕上繪製一個簡單的三角形。這是我的對象類:android opengl skip faces

public class GLObj 
{ 
private FloatBuffer  vertBuff; 
private ShortBuffer  pBuff; 

private float   vertices[] = 
{ 
    0f, 1f, 0, // point 0 
    1f, -1f, 0, // point 1 
    -1f, -1f, 0 // point 3 
}; 

private short[]   pIndex = { 0, 1, 2 }; 

public GLObj() 
{ 
    ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4); // each point uses 4 bytes 
    bBuff.order(ByteOrder.nativeOrder()); 
    vertBuff = bBuff.asFloatBuffer(); 
    vertBuff.put(vertices); 
    vertBuff.position(0); 

    // #### WHY IS THIS NEEDED?! I JUST WANT TO DRAW VERTEXES/DOTS #### 
    ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2); // 2 bytes per short 
    pbBuff.order(ByteOrder.nativeOrder()); 
    pBuff = pbBuff.asShortBuffer(); 
    pBuff.put(pIndex); 
    pBuff.position(0); 
    // ################################################################ 
} 

public void Draw(GL10 gl) 
{ 
    gl.glFrontFace(GL10.GL_CW); 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff); 

    // ### HOW TO PASS VERTEXT ARRAY DIRECTLY WITHOUT FACES?? ### 
    gl.glDrawElements(GL10.GL_POINTS, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff); 

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
} 

}

我的問題是這樣的:瞭解OpenGL的我想跳過的面孔,只是在屏幕上顯示的頂點。問題是我不明白如何僅將頂點傳遞給glDrawElements函數。

爲了顯示頂點,即使它們是點,我是否必須定義「面」(pIndex變量)?

回答

0

不,你不需要索引數組來繪製點。使用glDrawArrays()而不是glDrawElements()glDrawElements()用於索引幾何,而glDrawArrays()只是繪製一系列頂點。

gl.glDrawArrays(GL10.GL_POINTS, 0, vertices.length/3); 

注意,最後一個參數是頂點計數:

在你的榜樣,通過更換glDrawElements()電話。由於vertices數組包含每個頂點的3個座標,因此需要將數組長度除以3以獲得頂點數。

+0

非常感謝!正是我需要的! :) – user3578847 2014-10-04 16:04:02