2012-01-22 100 views
0

UPDATE2:嘗試渲染一個四邊形。 FAIL!繪製圓圈的奇怪問題

更新:完整的代碼在這裏。有人可以請確認我的代碼有問題嗎? http://dl.dropbox.com/u/8489109/HelloAndroid.7z

我一直在試圖用Opengl ES 1.0繪製一個圓。我在Windows平臺上使用了很多SDL和OpenGL,並且由於我的遊戲使用的多邊形數量較少,所以主要使用glBegin和glEnd。

粘貼是我創建對象時調用的代碼。

float ini[]=new float[360*3]; 
    ByteBuffer temp=ByteBuffer.allocateDirect(ini.length*4); 
    temp.order(ByteOrder.nativeOrder()); 
    vertex=temp.asFloatBuffer(); 
    int i; 
    float D2R=(float) (3.14159265/180); 
    for (i=0;i<360;i++){ 
     float XX=(float)(Math.sin(i*D2R)*size); 
     float YY=(float)(Math.cos(i*D2R)*size); 
     ini[i*2]=XX; 
     ini[i*2+1]=YY; 
     ini[i*2+2]=0; 
    } 
    vertex.put(ini); 
    Log.d("GAME","SPAWNED NEW OBJECT"); 
    length=ini.length; 
    //vertex=ByteBuffer.allocateDirect(temp.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer(); 
    //vertex.put(temp); 
    vertex.position(0); 

現在這裏是抽獎代碼

Log.d("OBJECT","DUH WRITE"); 
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
gl.glPushMatrix(); 
gl.glTranslatef((float)x,(float)y,0); 
gl.glVertexPointer(3, GL10.GL_FLOAT,0, vertex); 
gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, length); 
gl.glPopMatrix(); 
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 

它繪製一個圓(當它實際上是決定運行),並增加了一些奇怪的線條。 舉例: enter image description here

這是錯的嗎?

gl.glMatrixMode(gl.GL_PROJECTION); 
gl.glLoadIdentity(); 
gl.glViewport(0, 0, arg1, arg2); 
gl.glOrthof(0,(float)arg1,(float)arg2,0,-1,1); 
gl.glMatrixMode(gl.GL_MODELVIEW); 
gl.glLoadIdentity(); 

回答

1

這是沒有意義的:

float ini[]=new float[360*3]; 
/* ... */ 
for (i=0;i<360;i++){ 
    float XX=(float)(Math.sin(i*D2R)*size); 
    float YY=(float)(Math.cos(i*D2R)*size); 
    ini[i*2]=XX; 
    ini[i*2+1]=YY; 
    ini[i*2+2]=0; 
} 

您分配3元的倍數,但與2任的步幅乘以做

float ini[]=new float[360*2]; 
/* ... */ 
for (i=0;i<360;i++){ 
    float XX=(float)(Math.sin(i*D2R)*size); 
    float YY=(float)(Math.cos(i*D2R)*size); 
    ini[i*2]=XX; 
    ini[i*2+1]=YY; 
} 
/* ... */ 
gl.glVertexPointer(2, GL10.GL_FLOAT,0, vertex); 

float ini[]=new float[360*3]; 
/* ... */ 
for (i=0;i<360;i++){ 
    float XX=(float)(Math.sin(i*D2R)*size); 
    float YY=(float)(Math.cos(i*D2R)*size); 
    ini[i*3]=XX; 
    ini[i*3+1]=YY; 
    ini[i*3+2]=0; 
/*  ^  */ 
/*  ^  */ 
} 
/* ... */ 
gl.glVertexPointer(3, GL10.GL_FLOAT,0, vertex); 

另外,您正在使用glDrawArrays w榮。您不使用字節數組的長度,而是使用您繪製的頂點數量 - 360。

gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 360); 
+0

是的,我發現它就像我開始閱讀每一行10分鐘後。這是我第一次使用glDrawArrays,glVertexPointer,非常感謝您的幫助!它現在正在工作! – Xiofurry