在我簡單的GLES20應用程序中,我試圖在運行時添加一些對象來繪製。 在我的渲染器類我有正方形的ArrayList中:如何使用GLES20添加對象以在運行時繪製?
private ArrayList<Square> mSquares = new ArrayList<Square>();
這是我Square類。
Square
如果在onSurfaceCreated
中創建它,它將很好地繪製。
但是我實際上想要做的。我需要在運行時通過單擊按鈕並在中繪製正方形來將新的Square
添加到mSquares
中。
在我的渲染器類我創建AddFigure()
方法:
public void AddFigure(float x, float y, float z)
{ // color coords angle
mSquares.add(new Square("Red square", new float[] {1, 0, 0, 1}, new float[] {x, y, z}, 100f));
}
我通過點擊按鈕,在我的活動調用此方法:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mGLRenderer.AddFigure(1f, 1f, 1f);
}
});
但它總是拋出此異常:
E/AndroidRuntime: FATAL EXCEPTION: GLThread 220
java.util.ConcurrentModificationException
and
E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
E/emuglGLESv2_enc: device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glEnableVertexAttribArray:741 GL error 0x501
Info: Invalid vertex attribute index. Wanted index: 4294967295. Max index: 16
我試過用某種方法修復它使用glBufferData
和glBufferSubData
,但沒有成功。 也許有一個例子或wiki如何做到這一點的權利?
'ArrayList'不是線程安全的,所以你應該讓同步嘗試Collections.synchronizedList() – Mohamed