我目前正在實現一個3D查看器,它基本上呈現用戶在其SD卡上的所有圖像的子集。最匹配的產品,我覺得將是Cooliris的:Android OpenGL紋理:在飛行中創建和刪除它們
它只是在屏幕上顯示N個片區的滾動板,分別表示不同的圖像,用新的瓷磚進入屏幕,並顯示新的圖像。
現在爲我的問題:我有程序工作,並呈現很好的四邊形。當四分之一畫面離開屏幕時,它會被回收/釋放。新的四邊形在進入屏幕前繼續添加到瓷磚板上。
因爲可以有數百個圖像,所以需要動態地創建和刪除紋理(以便我們不會耗盡內存)。我遇到的問題是,在刪除紋理之後,新創建的紋理似乎會獲得當前正在使用的其他紋理的一些ID。
我的渲染循環是這樣的:
void render(GL10 gl) {
0. Move the camera
// Tile board maintenance
1. Remove tiles out of screen
2. Add new tiles which are about to enter screen
// Texture handling
3. glDeleteTextures on all unused textures followed by glFlush
4. For newly used images
- Create corresponding Bitmap
- Create the OpenGL texture, followed by glFlush
- Release the Bitmap
// Rendering
5. Render the tile (using a textured quad)
}
舉的數據是如何組織一個更好的主意,這裏是類的概述:
TileBoard {
Image[] allImages;
Tile[] board;
}
Tile {
Image image;
}
Image {
String path;
int textureId;
int referenceCount;
}
紋理創建代碼:
protected void loadSingleTexture(GL10 gl, long objectId, Bitmap bmp) {
int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
gl.glFlush();
if (bmp != null) bmp.recycle();
if (listener != null) listener.onTextureLoaded(gl, objectId, textures[0]);
}
紋理刪除代碼:
// pendingTextureUnloads is a Set<Integer>
if (pendingTextureUnloads.size() > 0) {
int[] textureIds = new int[pendingTextureUnloads.size()];
int i = 0;
Iterator<Integer> it = pendingTextureUnloads.iterator();
while (it.hasNext()) {
textureIds[i] = it.next();
}
gl.glDeleteTextures(textureIds.length, textureIds, 0);
gl.glFlush();
}
試圖在任何地方調用glGetError? – Tim 2012-03-13 16:26:57