当我尝试在该onDrawFrame()
方法中初始化我的模型时,一切正常,创建缓冲区,然后以正确的方式渲染模型。问题是,当我尝试使用方法在另一点执行此初始化时queueEvent
,代码被执行但未创建缓冲区,奇怪的是我正在 OpenGL 线程中执行代码。
这是代码:
public synchronized void init() {
int[] tempVbo = new int[1];
int[] tempIbo = new int[1];
vertexBuffer = ByteBuffer.allocateDirect(vertexArray.length * FLOAT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertexBuffer.put(vertexArray);
vertexBuffer.position(0);
indicesBuffer = ByteBuffer.allocateDirect(indicesArray.length * SHORT_SIZE).order(ByteOrder.nativeOrder()).asShortBuffer();
indicesBuffer.put(indicesArray);
indicesBuffer.position(0);
GLES20.glGenBuffers(1, tempVbo, 0);
GLES20.glGenBuffers(1, tempIbo, 0);
if(tempVbo[0] > 0 && tempIbo[0] > 0) {
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, tempVbo[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexBuffer.capacity()*FLOAT_SIZE, vertexBuffer, GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, tempIbo[0]);
GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer.capacity()*SHORT_SIZE, indicesBuffer, GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
} else {
Log.e("ENG_MODEL","Errore creazione buffer modello: VBO=" + tempVbo[0] + " IBO=" + tempIbo[0]);
}
Log.d("ENG_MODEL","VBO=" + tempVbo[0] + " IBO=" + tempIbo[0]);
vbo = tempVbo;
ibo = tempIbo;
}
我认为这是关于线程同步的事情,不是吗?还是我用queueEvent
错了方式?