2011-10-02 128 views
1

如何將紋理應用於Android中的頂點緩衝區對象?將紋理應用於頂點緩衝區對象基元

答:


的代碼工作正常,但它缺少對

glEnable(GL_TEXTURE_2D); 

這一個電話和

glEnableClientState(GL_TEXTURE_COORD_ARRAY); 

通話都需要以便頂點緩衝區對象繪製紋理。


問題:

據我所知,首先你必須創建一個NIO緩衝區:

ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4); 
tbb.order(ByteOrder.nativeOrder()); 
FloatBuffer textureBuffer = tbb.asFloatBuffer(); 
textureBuffer.put(texCoords); 
textureBuffer.position(0); 

在此代碼示例,該陣列texCoords包含雙組分(s,t)紋理數據。

創建NIO緩衝區後,你需要將它傳遞給OpenGL和創建頂點緩衝區對象:

int[] id = new int[1];//stores the generated ID. 
gl11.glGenBuffers(1, id, 0); 
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, id[0]); 
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, texCoords.length * 4, textureBuffer, GL11.GL_STATIC_DRAW); 

這樣通吃initalization的照顧。接下來,我們需要藉助它,我們這樣做是這樣的:

gl11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);//enable for textures 
gl11.glActiveTexture(GL11.GL_TEXTURE0); 
//lets pretend we created our texture elsewheres and we have an ID to represent it. 
gl11.glBindTexture(GL11.GL_TEXTURE_2D, textureId); 

//Now we bind the VBO and point to the buffer. 
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, id[0])//the id generated earlier. 
gl11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);//this points to the bound buffer 

//Lets also pretend we have our Vertex and Index buffers specified. 
//and they are bound/drawn correctly. 

因此,即使這是什麼,我會覺得這是需要的,以便對OpenGL繪製紋理,我有一個錯誤,只有紅色三角形(沒有我調製過的石頭紋理)呈現。

+0

我很抱歉。我可以確認我提供的所有代碼都有效。我只錯過了一件事'gl11.glEnable(GL11.GL_TEXTURE_2D)'。我以爲我通過使用'gl11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY)'啓用,但似乎我需要調用兩者。 –

回答

1

需要調用VBO的兩個函數來啓用紋理。

gl.glEnable(GL11.GL_TEXTURE_2D); gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY); gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

相關問題