2011-12-31 105 views
2

下面的代碼試圖繪製GLSurfaceView的框架和一個對角線但結果是一個大的黑色的屏幕來代替。我嘗試設置正交投影,將視口大小與視圖大小對齊,使用glDrawArrays()繪製框線,並使用glDrawElements()繪製對角線。我找不到問題的根源。畫線用OpenGL ES 1.0

public class MapView extends GLSurfaceView implements Renderer { 

private int w, h; 

private FloatBuffer frameVertices; 

private ByteBuffer diagIndices; 

public MapView(Context ctx) { this(ctx, null); } 

public MapView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setRenderer(this); 
} 

@Override 
public void onDrawFrame(GL10 gl) { 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, frameVertices); 
    gl.glColor4f(0f, 0f, 1f, 0.5f); 
    gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 4); 
    gl.glDrawElements(GL10.GL_LINES, 1, GL10.GL_UNSIGNED_BYTE, diagIndices); 
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
} 

@Override 
public void onSurfaceChanged(GL10 gl, int width, int height) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
    w = getWidth(); h = getHeight(); 
    TRC.debug("w = " + w + ", h = " + h); 
    gl.glClearColor(0, 0, 0, 1); 
    gl.glViewport(0, 0, w, h); 
    gl.glDepthRangex(1, -1); // TODO remove 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glLoadIdentity(); 
    gl.glOrthox(0, w, 0, h, 1, -1); 

    float[] frame = { 
     0, 0, 
     w-1, 0, 
     w-1, h-1, 
     0, h-1 }; 
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(frame.length * 4); 
    byteBuffer.order(ByteOrder.nativeOrder()); 
    frameVertices = byteBuffer.asFloatBuffer(); 
    frameVertices.put(frame); 
    frameVertices.flip(); 
    frameVertices.position(0); 
    gl.glLineWidthx(10); 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glLoadIdentity(); 
    gl.glDisable(GL10.GL_DEPTH_TEST); 
    diagIndices = ByteBuffer.allocateDirect(2); 
    diagIndices.order(ByteOrder.nativeOrder()); 
    diagIndices.put(new byte[] {0, 2}); 
    diagIndices.flip(); 
} 

} 

什麼可以回事?

回答

0

您不需要gl.glDepthRangex()方法調用,並且您的近和遠剪切平面在gl.glOrthox()之間切換。除此之外,我沒有看到任何錯誤。嘗試使用gl.glClearColor()設置不同的清晰顏色,但我認爲它沒有太大的區別。

關於第二個想法,近剪裁面可能會更好爲0,試試這個:gl.glOrthox(0, w, 0, h, 0, 1);

+0

嗨,謝謝你看看它。我嘗試了不同的清晰顏色,並有效果。我將裁剪平面改爲0,1,並沒有注意到任何區別。整個視角充滿了清晰的顏色(好,至少我知道部分GL正在工作)。 – Endre 2011-12-31 09:43:45

+0

首先嚐試將頂點更改爲朝向屏幕中間的隨機點。如果你沒有看到任何東西,請嘗試使用'int error = gl.glGetError(); if(error!= GL10.GL_NO_ERROR){Log.e(error); }'在繪製方法結束時查看是否存在OpenGL拋出的特定錯誤。你的錯誤將是一個整數,你可以查看[這裏](http://developer.android.com/reference/javax/microedition/khronos/opengles/GL10.html)的含義和錯誤代碼的解釋[here ](http://www.opengl.org/wiki/GL_Error_Codes)。評論這裏的錯誤,我會進一步研究它。 – 2011-12-31 09:53:16

+0

在onDrawFrame()方法中的每次GL調用之後,glGetError()返回0。現在我所嘗試的是在下載示例源文件中將基於透視的投影轉換爲正交。修改後的示例正在運行,雖然我的應用程序沒有,我仍然無法弄清楚這兩個應用程序之間有什麼棘手的區別。 – Endre 2012-01-01 17:07:27

2

我想通了這個問題。我用glOrthox(),我以爲x只是表示整數值。但這並不完全是這樣,中間是一個二叉點。 16bit.16bit。我的頂點超出了屏幕範圍......我的解決方案是將glOrthox()更改爲glOrthof()。謝謝您的幫助!